Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cstdlib>
- #include <sstream>
- #include <string>
- #include <cmath>
- #include <vector>
- #include <exception>
- #include <ctime>
- #include <ctype.h>
- using std::cout;
- using std::cin;
- using std::endl;
- using std::string;
- using std::vector;
- using std::stringstream;
- void DrawLine (string Str, int Len = 80)
- {
- int x = 0;
- cout << endl;
- for (int i = 0; i < Len;i++)
- {
- cout << Str[x];
- if (x == Str.size())
- x = 0;
- }
- if (Len != 80)
- cout << endl;
- }
- class Calculator
- {
- private:
- long double Num1;
- long double Num2;
- public:
- void SetVal(int, int);
- long double Add ();
- long double Subtract();
- long double Multiply();
- long double Divide();
- int Mod();
- long double Expon();
- }
- calculator,
- *Calc;
- void Calculator::SetVal(int a, int b)
- {
- Num1 = a;
- Num2 = b;
- }
- long double Calculator::Add()
- {
- return Num1 + Num2;
- }
- long double Calculator::Subtract()
- {
- return Num1-Num2;
- }
- long double Calculator::Multiply()
- {
- return Num1*Num2;
- }
- long double Calculator::Divide ()
- {
- return Num1/Num2;
- }
- int Calculator::Mod()
- {
- return ((int(Num1))%(int(Num2)));
- }
- long double Calculator::Expon()
- {
- long double X = 1;
- if (Num2 > 0)
- {
- for (int i = 0; i < int(Num2); i++)
- {
- X *= Num1;
- }
- return X;
- }
- else if (Num2 == 0)
- return 1;
- else if (Num2 < 0)
- {
- Num2 *= (-1);
- for (int i = 0; i < int(Num2); i++)
- {
- X *= Num1;
- }
- return (1/X);
- }
- }
- int main ()
- {
- stringstream Ent1, Ent2, Oper, Sign1, Sign2;
- bool Check1 = true;
- bool Check2 = false;
- string Action;
- int x, y;
- string a , b;
- Calc = &calculator;
- cout << "This calculator can calclulate a couple of thing that listed are in the Table shown below.";
- cout << "To tell the computer wha to calculate simply write the Expression.\n";
- cout << "NOTE: 1. Only one operator & two Numbers are allowed in the expression.\n";
- cout << "2. No variables are allowed.\n\n";
- cout << "3.The Sign of a number should only appear once.[i.e. No 1+ (---+1)]";
- cout << "\t--------------------------------------------------------------\n";
- cout << "\t| Symbol | Operator |" << endl;
- cout << "\t|-----------------------|------------------------------------|\n";
- cout << "\t| + | Addition |" << endl;
- cout << "\t|-----------------------|------------------------------------|\n";
- cout << "\t| - | Subtraction |" << endl;
- cout << "\t|-----------------------|------------------------------------|\n";
- cout << "\t| * | Multiplication |" << endl;
- cout << "\t|-----------------------|------------------------------------|\n";
- cout << "\t| / | Divide |" << endl;
- cout << "\t|-----------------------|------------------------------------|\n";
- cout << "\t| % | Modulus* |" << endl;
- cout << "\t|-----------------------|------------------------------------|\n";
- cout << "\t| ^ | Exponent |" << endl;
- cout << "\t--------------------------------------------------------------\n";
- cout << endl << "* Modulus gives the remainder of a number divided by another.\n";
- bool LOOP = true;
- while (LOOP)
- {
- Check1 = true;
- Check2 = false;
- cout << endl << "What do you want to calculate: ";
- getline (cin, Action);
- for (int i = 0; i < Action.size(); i++)
- {
- int Ax;
- if (Action[0] == '-')
- {
- Sign1 << '-';
- }
- if (isdigit(Action[i]) && Check1)
- {
- Ent1 << Action[i];
- }
- if (!isalnum(Action[i]) && Check1 && i != 0)
- {
- Oper << Action[i];
- Check1 = false;
- Check2 = true;
- Ax = i;
- if (Action[Ax+1] == '-')
- {
- Sign2 << '-';
- }
- }
- if (isdigit(Action[i]) && Check2)
- {
- Ent2 << Action[i];
- }
- }
- a = Ent1.str();
- b = Ent2.str();
- x = atoi(a.c_str());
- y = atoi(b.c_str());
- if (Sign1.str() == "-")
- {
- x = 0-x;
- }
- if (Sign2.str() == "-")
- {
- y = 0-y;
- }
- Calc -> SetVal(x,y);
- if (Oper.str() == "+")
- {
- cout << Calc -> Add();
- }
- else if (Oper.str() == "-")
- {
- cout << Calc -> Subtract();
- }
- else if (Oper.str() == "*")
- {
- cout << Calc -> Multiply();
- }
- else if (Oper.str() == "/")
- {
- cout << Calc -> Divide();
- }
- else if (Oper.str() == "%")
- {
- cout << Calc -> Mod();
- }
- else if (Oper.str() == "^")
- {
- cout << Calc -> Expon();
- }
- char x;
- cout << "\nDo you want to perform another calculation? [N -> Exit, Anything Else -> Restart]: ";
- cin >> x;
- cin.sync();
- if (x == 'n' || x == 'N')
- LOOP = false;
- DrawLine ("_");
- Ent1.str("");
- Ent2.str("");
- Oper.str("");
- Sign1.str("");
- Sign2.str("");
- }
- ;cout << "\n\nPress Enter to exit . . .";
- cin.sync();
- cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement