0
Switch statement
write a c++ program that uses switch statement to the user to firstly enter two integer numbers. then your program should ask the user to enter one of the following symbols: '+' , '-' , '*' , '/' . your program should print to the user the result of performing the mathematical operation between the two numbers according to the entered symbol.
5 odpowiedzi
+ 1
see the simple calculator code (you need at least c++11 supported compiler to compile it):
#include<iostream>
#include<string> //You need at least c++11 to use some functions in this header
using namespace std;
const char* MathSigns = "+-*/";
enum {
PLUS = '+',
MINUS = '-',
MULTIPLY = '*',
DIVIDE = '/'
};
int main()
{
cout << "Please enter the mathematical expression to evaluate: ";
string input;
getline(cin, input); //Don't use cin >> input here, it won't capture all the input if it contains spaces
int index = input.find_first_of(MathSigns); //Gets the index of the mathematical sign
string firstNumber = input.substr(0, index); //Gets the string of the first number; you should take care of the preceding and trailing whitespaces
string secondNumber = input.substr(index + 1, input.length()); //Gets the string of the second number
char sign = input[index]; //Gets the mathematical sign
double num1 = stod(firstNumber); //converts the first number string to a number
double num2 = stod(secondNumber); //converts the second number string to a number
double result;
bool success = true; //to indicate if the operation succeeded and print the result
switch(sign){
case PLUS:
result = num1 + num2;
break;
case MINUS:
result = num1 - num2;
break;
case MULTIPLY:
result = num1 * num2;
break;
case DIVIDE:
result = num1 / num2;
break;
default:
success = false;
cout << "Unsupported mathematical operation " << sign << endl;
break;
}
if(success){
cout << firstNumber << " " << sign << " " << secondNumber << " = " << result <<endl;
}
return 0;
}
+ 1
well just in case you don't have a c++11 enabled compiler, i replaced the function stod with strtod from cstdlib header file (you cannot test this on sololearns c++ compiler - it doesn't support c++11, and does not ask for user input from 'getline(cin, string)' as it should). you can however test the program on sololearns by manually entering the expression into the code, for example, declare input as string input = "345 * 8.5”; and comment out getline(cin, input);.
It's not permitting me to post the modified code (too long I guess). so, I'll post it separately
+ 1
Here's the modified code (part 1):
#include<iostream>
#include<string> //You need at least c++11 to use some functions in this header
#include<cstdlib> //the sololearns compiler doesn't seem to support c++11, so for function strtod, we need this header file
using namespace std;
const char* MathSigns = "+-*/";
enum {
PLUS = '+',
MINUS = '-',
MULTIPLY = '*',
DIVIDE = '/'
};
+ 1
Modified code (part 2):
int main()
{
cout << "Please enter the mathematical expression to evaluate: ";
string input;
getline(cin, input); //Don't use cin >> input here, it won't capture all the input if it contains spaces
int index = input.find_first_of(MathSigns); //Gets the index of the mathematical sign
string firstNumber = input.substr(0, index); //Gets the string of the first number; you should take care of the preceding and trailing whitespaces
string secondNumber = input.substr(index + 1, input.length()); //Gets the string of the second number
char sign = input[index]; //Gets the mathematical sign
//double num1 = stod(firstNumber);
double num1 = strtod(firstNumber.c_str(), NULL); //converts the first number string to a number
//double num2 = stod(secondNumber);
double num2 = strtod(secondNumber.c_str(), NULL) ; //converts the second number string to a number
double result;
bool success = true; //to indicate if the operation succeeded and print the result
switch(sign){
case PLUS:
result = num1 + num2;
break;
case MINUS:
result = num1 - num2;
break;
case MULTIPLY:
result = num1 * num2;
break;
case DIVIDE:
result = num1 / num2;
break;
default:
success = false;
cout << "Unsupported mathematical operation " << sign << endl;
break;
}
if(success){
cout << firstNumber << " " << sign << " " << secondNumber << " = " << result <<endl;
}
return 0;
}
0
why not just write a simple mathematical calculator (parser) that allows the user to write the simple expression and then calculate it, for example, user can input "4 * 5" and your program calculates it and gives 20. I don't really know if you are just trying to learn how to use switch (you can also use it here too). I will write the code when I get to my pc, it ain't so convenient typing code on the phone.