Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- std::vector<std::string> split(std::string uString, char breakOn);
- int main() {
- std::string statement = "";
- int num1 = 0;
- int num2 = 0;
- int total = 0;
- std::cout << "Enter a calculation (x to stop): ";
- std::cin >> statement;
- while (statement != "x") {
- if (strstr(statement.c_str(), "+")) {
- num1 = atoi(split(statement, '+')[0].c_str());
- num2 = atoi(split(statement, '+')[1].c_str());
- total = num1 + num2;
- std::cout << num1 << "+" << num2 << "=" << total << std::endl;
- }
- else if (strstr(statement.c_str(), "-")) {
- num1 = atoi(split(statement, '-')[0].c_str());
- num2 = atoi(split(statement, '-')[1].c_str());
- total = num1 - num2;
- std::cout << num1 << "-" << num2 << "=" << total << std::endl;
- }
- else if (strstr(statement.c_str(), "*")) {
- num1 = atoi(split(statement, '*')[0].c_str());
- num2 = atoi(split(statement, '*')[1].c_str());
- total = num1 * num2;
- std::cout << num1 << "*" << num2 << "=" << total << std::endl;
- }
- else if (strstr(statement.c_str(), "/")) {
- num1 = atoi(split(statement, '/')[0].c_str());
- num2 = atoi(split(statement, '/')[1].c_str());
- total = num1 / num2;
- std::cout << num1 << "/" << num2 << "=" << total << std::endl;
- }
- else {
- std::cout << "Invalid\n";
- }
- std::cout << "Enter a calculation (x to stop): ";
- std::cin >> statement;
- }
- system("PAUSE");
- }
- std::vector<std::string> split(std::string uString, char breakOn) {
- std::vector<std::string> sVec;
- bool hit = false;
- std::string statement1 = "";
- std::string statement2 = "";
- for (unsigned int i = 0; i < uString.size(); i++) {
- if (uString.at(i) != breakOn && hit == false) {
- statement1 += uString.at(i);
- }
- else if (uString.at(i) == breakOn) {
- sVec.push_back(statement1);
- hit = true;
- }
- else if (uString.at(i) != breakOn && hit == true) {
- statement2 += uString.at(i);
- }
- }
- sVec.push_back(statement2);
- return sVec;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement