fork download
  1. #include <cstdlib>
  2. #include <iostream>
  3. using namespace std;
  4. class Fraction {
  5. private:
  6. int num,den; //числитель и знаменатель
  7. public:
  8. void set (int n,int d) {num=n; den=d;
  9. normalize();}
  10. int get_num() {return num;}
  11. int get_den(){return den;}
  12. Fraction add(Fraction other);
  13. Fraction mult(Fraction other);
  14. private:
  15. void normalize();//доб. дроби в стандартн форм
  16. int gcf(int a,int b);//наибольший общий делитель
  17. int lcm(int a,int b);//наименьшее общее кратное
  18. };
  19. int main(){
  20. Fraction fract1,fract2,fract3;
  21. fract1.set(num,den);
  22. fract2.set(num1,den1);
  23. fract3=fract1.add(fract2);
  24. int num;
  25. int num1;
  26. int den;
  27. int den1 ;
  28. cout<<"введем num";
  29. cin>>num;
  30. cout<<"num1";
  31. cin>>num1;
  32. cout<<den;
  33. cin>>den;
  34. cout<<"den1";
  35. cin>>den1;
  36. cout<<fract3.get_num()<<"/"<<fract3.get_den();
  37. }
  38. //ФУНКЦИИ КЛАССА FRACTION
  39. //нормализация : преобразовать дробь к стандартному
  40. //виду ,уникальному для каждого математически отличающегося
  41. //значения
  42. void Fraction::normalize(){
  43. //обработать случаи со значением 0
  44. if(den==0 || num==0){
  45. num=0;
  46. den=1;
  47. }
  48. //оставить отрицательный знак только в числителе.
  49. if(den<0){
  50. num*=-1;
  51. den*=-1;
  52. }
  53. //извлечение наибольшего общего делителя из числителя и
  54. //знаменателя.
  55. int n=gcf(num,den);
  56. num=num/n;
  57. den=den/n;
  58. }
  59. //наибольший общий делитель
  60. int Fraction::gcf(int a,int b){
  61. if(a%b==0)
  62. return abs(b);
  63. else
  64. return gcf(b,a%b);
  65. }
  66. //наименьшее общее кратное
  67. int Fraction::lcm(int a,int b){
  68. return (a/gcf(a,b))*b;
  69. }
  70. Fraction Fraction::add(Fraction other){
  71. Fraction fract;
  72. int lcd=lcm(den,other.den);
  73. int quot1=lcd/den;
  74. int quot2 =lcd/other.den;
  75. fract.set(num*quot1+other.num*quot2,lcd);
  76. fract.normalize();
  77. return fract;
  78. }
  79. Fraction Fraction::mult(Fraction other){
  80. Fraction fract;
  81. fract.set(num*other.num,den*other.den);
  82. fract.normalize();
  83. return fract;
  84. }
  85.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:21:12: error: ‘num’ was not declared in this scope
 fract1.set(num,den);
            ^
prog.cpp:21:16: error: ‘den’ was not declared in this scope
 fract1.set(num,den);
                ^
prog.cpp:22:12: error: ‘num1’ was not declared in this scope
 fract2.set(num1,den1);
            ^
prog.cpp:22:17: error: ‘den1’ was not declared in this scope
 fract2.set(num1,den1);
                 ^
stdout
Standard output is empty