Advertisement
Nisheeth

Calculator

Jul 10th, 2011
1,111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 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.     int 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.         X /= X*X;
  104.     return X;
  105.     }
  106. }
  107.  
  108. int main ()
  109. {
  110.     stringstream Ent1, Ent2, Oper;
  111.     bool Check1 = true;
  112.     bool Check2 = false;
  113.     string Action;
  114.     int x, y;
  115.     string a , b;
  116.  
  117.     Calc =  &calculator;
  118.     cout << "This calculator can calclulate a couple of thing that listed are in the Table shown below.";
  119.     cout << "To tell the computer wha to calculate simply write the Expression.\n";
  120.     cout << "NOTE: 1. Only one operator & two Numbers are allowed in the expression.\n";
  121.     cout << "2. No variables are allowed";
  122.  
  123.     cout << "What do you want to calculate:  ";
  124.     getline (cin, Action);
  125.  
  126.    
  127.     for (int i = 0; i < Action.size(); i++)
  128.     {
  129.         if (isdigit(Action[i]) && Check1)
  130.         {
  131.             Ent1 << Action[i];
  132.         }
  133.  
  134.         else if (!isalnum(Action[i]))
  135.         {
  136.             Oper << Action[i];
  137.             Check1 = false;
  138.             Check2 = true;
  139.         }
  140.  
  141.         else if (isdigit(Action[i]) && Check2)
  142.         {
  143.             Ent2 << Action[i];
  144.         }
  145.     }
  146.    
  147.     a = Ent1.str();
  148.     b = Ent2.str();
  149.  
  150.     x = atoi(a.c_str());
  151.     y = atoi(b.c_str());
  152.  
  153.     Calc -> SetVal(x,y);
  154.  
  155.    
  156.  
  157.     ;cout << "\n\nPress Enter to exit . . .";
  158.     cin.sync();
  159.     cin.get();
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement