Fractional Calculator in C++

0
935
Structures in C++
Linked Lists with applications in C++

We can easily use structures to create a fractional calculator than can solve fractional numbers in C++. It is just an easy approach through structures, we can easily create a simple calculator in C++, but for a fractional calculator it is a little bit different approach.  The code of our fractional calculator is given below:

  1. // Fractional Calculator in C++
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. struct fraction
  6. {
  7.     int n,d;
  8.     int n1,d1;
  9. };
  10.  
  11. int main()
  12. {
  13.     char p;
  14.     fraction opr;
  15.  
  16.     cout<<“enter nomerator of first fraction : “;     cin>>opr.n;
  17.     cout<<endl;
  18.     cout<<“enter demonerator of first fraction”;     cin>>opr.d;
  19.     cout<<endl;
  20.     cout<<“enter operation : “;     cin>>p;
  21.     cout<<endl;
  22.  
  23.     cout<<“enter nomerator of second fraction : “;     cin>>opr.n1;
  24.     cout<<endl;
  25.     cout<<“enter denomerator of second fraction : “;     cin>>opr.d1;
  26.     cout<<endl;
  27.  
  28.     if(p==‘+’)
  29.     {
  30.         int z = (opr.* opr.d1 + opr.* opr.n1 );
  31.         int y = (opr.* opr.d1);
  32.         cout<<z<<“/”<<y<<endl;
  33.  
  34.     }
  35.  
  36.     if(p==‘-‘)
  37.     {
  38.         int a = (opr.* opr.d1  opr.* opr.n1 );
  39.         int b = (opr.* opr.d1);
  40.         cout<<a<<“/”<<b<<endl;
  41.  
  42.     }
  43.     return 0;
  44. }

Now lets explain the above code of Fractional Calculator. First of all We defined a structure named as “fraction” which contains the variables for numerators and denominators in it, then we used it in the main() function by defining a variable of type “ fraction ”. Then we asked the user to enter the numbers and an operator to be performed by the program, we simply used “ if “ statements for performing the arithmetic operations, and hence it is our fractional calculator. So i think creating a calculator in C++ in not a big deal, we just have to use simplest conditional operators.

Structures in C++
Linked Lists with applications in C++

Leave a Reply