Advertisement
TermSpar

Calculator And Split Method (Custom)

Sep 9th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. //C++ Console Calculator
  2. //By Ben Bollinger
  3.  
  4. #include <iostream>
  5. #include <vector>
  6. #include <string>
  7.  
  8. //Split Method Declaration:
  9. std::vector<std::string> split(std::string uString, char oper);
  10.  
  11. int main() {
  12.  
  13.     std::string statement;
  14.     std::cout << "Enter statement (x to stop): ";
  15.     std::cin >> statement;
  16.  
  17.     //Keep Program Running:
  18.     while (statement != "x") {
  19.         //Addition:
  20.         if (strstr(statement.c_str(), "+")) {
  21.             //Convert Split Method String Vec To Ints:
  22.             int num1 = atoi(split(statement, '+')[0].c_str());
  23.             int num2 = atoi(split(statement, '+')[1].c_str());
  24.  
  25.             //Calculate And Display Answer:
  26.             int total = num1 + num2;
  27.             std::cout << num1 << "+" << num2 << "=" << total << "\n";
  28.         }
  29.         //Subtraction:
  30.         else if (strstr(statement.c_str(), "-")) {
  31.             //Convert Split Method String Vec To Ints:
  32.             int num1 = atoi(split(statement, '-')[0].c_str());
  33.             int num2 = atoi(split(statement, '-')[1].c_str());
  34.  
  35.             //Calculate And Display Answer:
  36.             int total = num1 - num2;
  37.             std::cout << num1 << "-" << num2 << "=" << total << "\n";
  38.         }
  39.         //Multiplication:
  40.         else if (strstr(statement.c_str(), "*")) {
  41.             //Convert Split Method String Vec To Ints:
  42.             int num1 = atoi(split(statement, '*')[0].c_str());
  43.             int num2 = atoi(split(statement, '*')[1].c_str());
  44.  
  45.             //Calculate And Display Answer:
  46.             int total = num1 * num2;
  47.             std::cout << num1 << "*" << num2 << "=" << total << "\n";
  48.         }
  49.         //Division:
  50.         else if (strstr(statement.c_str(), "/")) {
  51.             //Convert Split Method String Vec To Ints:
  52.             int num1 = atoi(split(statement, '/')[0].c_str());
  53.             int num2 = atoi(split(statement, '/')[1].c_str());
  54.  
  55.             //Calculate And Display Answer:
  56.             int total = num1 / num2;
  57.             std::cout << num1 << "/" << num2 << "=" << total << "\n";
  58.         }
  59.         //Exception Handling:
  60.         else {
  61.             std::cout << "Invalid\n";
  62.         }
  63.         //Re-Ask To Continue:
  64.         std::cout << "Enter statement (x to stop): ";
  65.         std::cin >> statement;
  66.     }
  67.     //Pause Console:
  68.     system("PAUSE");
  69. }
  70.  
  71. //Split Method Definition:
  72. std::vector<std::string> split(std::string uString, char oper) {
  73.     //Variables:
  74.     std::vector<std::string> sVec;
  75.     bool hit = false;
  76.  
  77.     std::string statement1 = "";
  78.     std::string statement2 = "";
  79.  
  80.     //Loop Through All String Chars:
  81.     for (int i = 0; i < uString.size(); i++) {
  82.         if (uString.at(i) != oper && hit == false) {
  83.             statement1 += uString.at(i);
  84.         }
  85.         //After Operator Add 1st Num:
  86.         else if (uString.at(i) == oper) {
  87.             sVec.push_back(statement1);
  88.             hit = true;
  89.         }
  90.         //After Operator And First Num Create 2nd Num:
  91.         else if (uString.at(i) != oper && hit == true) {
  92.             statement2 += uString.at(i);
  93.         }
  94.     }
  95.     //Add 2nd Num:
  96.     sVec.push_back(statement2);
  97.     return sVec;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement