Advertisement
TermSpar

Split Method Calculator Tut

Sep 9th, 2016
939
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. std::vector<std::string> split(std::string uString, char breakOn);
  6.  
  7. int main() {
  8.  
  9.     std::string statement = "";
  10.     int num1 = 0;
  11.     int num2 = 0;
  12.     int total = 0;
  13.  
  14.     std::cout << "Enter a calculation (x to stop): ";
  15.     std::cin >> statement;
  16.  
  17.     while (statement != "x") {
  18.         if (strstr(statement.c_str(), "+")) {
  19.             num1 = atoi(split(statement, '+')[0].c_str());
  20.             num2 = atoi(split(statement, '+')[1].c_str());
  21.  
  22.             total = num1 + num2;
  23.             std::cout << num1 << "+" << num2 << "=" << total << std::endl;
  24.         }
  25.         else if (strstr(statement.c_str(), "-")) {
  26.             num1 = atoi(split(statement, '-')[0].c_str());
  27.             num2 = atoi(split(statement, '-')[1].c_str());
  28.  
  29.             total = num1 - num2;
  30.             std::cout << num1 << "-" << num2 << "=" << total << std::endl;
  31.         }
  32.         else if (strstr(statement.c_str(), "*")) {
  33.             num1 = atoi(split(statement, '*')[0].c_str());
  34.             num2 = atoi(split(statement, '*')[1].c_str());
  35.  
  36.             total = num1 * num2;
  37.             std::cout << num1 << "*" << num2 << "=" << total << std::endl;
  38.         }
  39.         else if (strstr(statement.c_str(), "/")) {
  40.             num1 = atoi(split(statement, '/')[0].c_str());
  41.             num2 = atoi(split(statement, '/')[1].c_str());
  42.  
  43.             total = num1 / num2;
  44.             std::cout << num1 << "/" << num2 << "=" << total << std::endl;
  45.         }
  46.         else {
  47.             std::cout << "Invalid\n";
  48.         }
  49.         std::cout << "Enter a calculation (x to stop): ";
  50.         std::cin >> statement;
  51.     }
  52.  
  53.     system("PAUSE");
  54. }
  55.  
  56. std::vector<std::string> split(std::string uString, char breakOn) {
  57.     std::vector<std::string> sVec;
  58.     bool hit = false;
  59.  
  60.     std::string statement1 = "";
  61.     std::string statement2 = "";
  62.  
  63.     for (unsigned int i = 0; i < uString.size(); i++) {
  64.         if (uString.at(i) != breakOn && hit == false) {
  65.             statement1 += uString.at(i);
  66.         }
  67.         else if (uString.at(i) == breakOn) {
  68.             sVec.push_back(statement1);
  69.             hit = true;
  70.         }
  71.         else if (uString.at(i) != breakOn && hit == true) {
  72.             statement2 += uString.at(i);
  73.         }
  74.     }
  75.     sVec.push_back(statement2);
  76.     return sVec;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement