Advertisement
Nisheeth

Calculator

Jul 10th, 2011
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <sstream>
  5. #include <string>
  6. #include <cmath>
  7. #include <vector>
  8. #include <exception>
  9. #include <ctime>
  10. #include <ctype.h>
  11.  
  12. using std::cout;
  13. using std::cin;
  14. using std::endl;
  15. using std::string;
  16. using std::vector;
  17. using std::stringstream;
  18.  
  19. void DrawLine (string Str, int Len = 80)
  20. {
  21.     int x = 0;
  22.     cout << endl;
  23.     for (int i = 0; i < Len;i++)
  24.     {
  25.         cout << Str[x];
  26.         if (x == Str.size())
  27.             x = 0;
  28.     }
  29.     if (Len != 80)
  30.         cout << endl;
  31. }
  32.  
  33. class Calculator
  34. {
  35. private:
  36.     long double Num1;
  37.     long double Num2;
  38. public:
  39.     void SetVal(int, int);
  40.  
  41.     long double Add ();
  42.     long double Subtract();
  43.     long double Multiply();
  44.     long double Divide();
  45.     int Mod();
  46.     long double Expon();
  47. }
  48. calculator,
  49. *Calc;
  50.  
  51. void Calculator::SetVal(int a, int b)
  52. {
  53.     Num1 = a;
  54.     Num2 = b;
  55. }
  56.  
  57. long double Calculator::Add()
  58. {
  59.     return Num1 + Num2;
  60. }
  61.  
  62. long double Calculator::Subtract()
  63. {
  64.     return Num1-Num2;
  65. }
  66.  
  67. long double Calculator::Multiply()
  68. {
  69.     return Num1*Num2;
  70. }
  71.  
  72. long double Calculator::Divide ()
  73. {
  74.     return Num1/Num2;
  75. }
  76.  
  77. int Calculator::Mod()
  78. {
  79.     return ((int(Num1))%(int(Num2)));
  80. }
  81.  
  82. long double Calculator::Expon()
  83. {
  84.     long double X = 1;
  85.     if (Num2 > 0)  
  86.     {
  87.         for (int i = 0; i < int(Num2); i++)
  88.         {
  89.             X *= Num1;
  90.         }
  91.     return X;
  92.     }
  93.  
  94.     else if (Num2 == 0)
  95.         return 1;
  96.     else if (Num2 < 0)
  97.     {
  98.         Num2 *= (-1);
  99.         for (int i = 0; i < int(Num2); i++)
  100.         {
  101.             X *= Num1;
  102.         }
  103.     return (1/X);
  104.     }
  105. }
  106.  
  107. int main ()
  108. {
  109.     stringstream Ent1, Ent2, Oper, Sign1, Sign2;
  110.     bool Check1 = true;
  111.     bool Check2 = false;
  112.     string Action;
  113.     int x, y;
  114.     string a , b;
  115.  
  116.     Calc =  &calculator;
  117.     cout << "This calculator can calclulate a couple of thing that listed are in the Table shown below.";
  118.     cout << "To tell the computer wha to calculate simply write the Expression.\n";
  119.     cout << "NOTE: 1. Only one operator & two Numbers are allowed in the expression.\n";
  120.     cout << "2. No variables are allowed.\n\n";
  121.     cout << "3.The Sign of a number should only appear once.[i.e. No 1+ (---+1)]";
  122.  
  123.     cout << "\t--------------------------------------------------------------\n";
  124.     cout << "\t|   Symbol       |       Operator             |" << endl;
  125.     cout << "\t|-----------------------|------------------------------------|\n";
  126.     cout << "\t|   +            |       Addition             |" << endl;
  127.     cout << "\t|-----------------------|------------------------------------|\n";
  128.     cout << "\t|   -            |       Subtraction          |" << endl;
  129.     cout << "\t|-----------------------|------------------------------------|\n";
  130.     cout << "\t|   *            |       Multiplication       |" << endl;
  131.     cout << "\t|-----------------------|------------------------------------|\n";
  132.     cout << "\t|   /            |       Divide               |" << endl;
  133.     cout << "\t|-----------------------|------------------------------------|\n";
  134.     cout << "\t|   %            |       Modulus*             |" << endl;
  135.     cout << "\t|-----------------------|------------------------------------|\n";
  136.     cout << "\t|   ^            |       Exponent             |" << endl;
  137.     cout << "\t--------------------------------------------------------------\n";
  138.  
  139.     cout << endl << "* Modulus gives the remainder of a number divided by another.\n";
  140.     bool LOOP = true;
  141.  
  142.     while (LOOP)
  143.     {
  144.         Check1 = true;
  145.         Check2 = false;
  146.  
  147.         cout << endl << "What do you want to calculate:  ";
  148.         getline (cin, Action);
  149.        
  150.         for (int i = 0; i < Action.size(); i++)
  151.         {
  152.             int Ax;
  153.  
  154.             if (Action[0] == '-')
  155.                 {
  156.                     Sign1 << '-';
  157.                 }
  158.             if (isdigit(Action[i]) && Check1)
  159.             {
  160.                 Ent1 << Action[i];
  161.             }
  162.  
  163.             if (!isalnum(Action[i]) && Check1 && i != 0)
  164.             {
  165.                 Oper << Action[i];
  166.                 Check1 = false;
  167.                 Check2 = true;
  168.                 Ax = i;
  169.                 if (Action[Ax+1] == '-')
  170.                 {
  171.                     Sign2 << '-';
  172.                 }
  173.             }
  174.  
  175.                        
  176.             if (isdigit(Action[i]) && Check2)
  177.             {
  178.                 Ent2 << Action[i];
  179.             }
  180.         }
  181.        
  182.         a = Ent1.str();
  183.         b = Ent2.str();
  184.  
  185.         x = atoi(a.c_str());
  186.         y = atoi(b.c_str());
  187.  
  188.         if (Sign1.str() == "-")
  189.         {
  190.             x = 0-x;
  191.         }
  192.  
  193.         if (Sign2.str() == "-")
  194.         {
  195.             y = 0-y;
  196.         }
  197.    
  198.         Calc -> SetVal(x,y);
  199.  
  200.         if (Oper.str() == "+")
  201.         {
  202.             cout << Calc -> Add();
  203.         }
  204.    
  205.         else if (Oper.str() == "-")
  206.         {
  207.             cout << Calc -> Subtract();
  208.         }
  209.  
  210.         else if (Oper.str() == "*")
  211.         {
  212.             cout << Calc -> Multiply();
  213.         }
  214.    
  215.         else if (Oper.str() == "/")
  216.         {
  217.             cout << Calc -> Divide();
  218.         }
  219.  
  220.         else if (Oper.str() == "%")
  221.         {
  222.             cout << Calc -> Mod();
  223.         }
  224.  
  225.         else if (Oper.str() == "^")
  226.         {
  227.             cout << Calc -> Expon();
  228.         }
  229.  
  230.         char x;
  231.         cout << "\nDo you want to perform another calculation? [N -> Exit, Anything Else -> Restart]:  ";
  232.         cin >> x;
  233.         cin.sync();
  234.  
  235.         if (x == 'n' || x == 'N')
  236.             LOOP = false;
  237.  
  238.         DrawLine ("_");
  239.         Ent1.str("");
  240.         Ent2.str("");
  241.         Oper.str("");
  242.         Sign1.str("");
  243.         Sign2.str("");
  244.     }
  245.  
  246.         ;cout << "\n\nPress Enter to exit . . .";
  247.         cin.sync();
  248.         cin.get();
  249.         return 0;
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement