performance/perf-cpp/scripts/ch4.cc

38 lines
902 B
C++

/*
Write the following program: The user is asked to enter 2 floating point numbers (use doubles). The user is then asked to enter one of the following mathematical symbols: +, -, *, or /. The program computes the answer on the two numbers the user entered and prints the results. If the user enters an invalid symbol, the program should print nothing.
*/
#include <iostream>
int main() {
double x, y;
std::cout << "enter x: "<<'\n';
std::cin >> x;
std::cout << "enter x: "<<'\n';
std::cin >> y;
std::cout << "enter symbol " << '\n';;
char symbol;
std::cin >> symbol;
double ans;
switch (symbol) {
case '+':
ans = x + y;
break;
case '-':
ans = x - y;
break;
case '*':
ans = x * y;
break;
case '/':
ans = x / y;
break;
default:
return 0;
}
std::cout << "answer: " << ans << '\n';
return 0;
}