В главе 3 из Программирование: принципы и практика с использованием C++ Bjarne Stroustrup просит вас «написать программу, которая выполняет операцию, за которой следуют два операнда, и выводит результат. Например:Программа умножает целые числа на 10
+ 100 3.14
* 4 5
Вот что я сделал.
#include<iostream>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
// This little program does addition, substraction, multiplication, and division as long as the number of operators doesn't exceed two.
int main()
{
cout << "Bonvole enigu du nombrojn post unu el tiuj ĉi matematikaj operaciloj: +,-, *, or/\n";
cout << "Please enter two numbers preceded by either of these mathematical operators: +,-, *, or/\n";
string s;
int a, b;
cin >> s >> a >> b;
if (s == "+") {
cout << a + b;
}
else if (s == "-") {
cout << a - b;
}
else if (s == "*") {
cout << a * b;
}
else (s == "/"); {
cout << a/b << "\n";
}
return 0;
}
Результаты все неправильные.
@localhost ~]$ ./a.out
Bonvole enigu du nombrojn post unu el tiuj ĉi matematikaj operaciloj: +,-, *, or/
Please enter two numbers preceded by either of these mathematical operators: +,-, *, or/
+ 2 3
50
[[email protected] ~]$ ./a.out
Bonvole enigu du nombrojn post unu el tiuj ĉi matematikaj operaciloj: +,-, *, or/
Please enter two numbers preceded by either of these mathematical operators: +,-, *, or/
- 3 2
11
[[email protected] ~]$ ./a.out
Bonvole enigu du nombrojn post unu el tiuj ĉi matematikaj operaciloj: +,-, *, or/
Please enter two numbers preceded by either of these mathematical operators: +,-, *, or/
* 2 3
60
[[email protected] ~]$ ./a.out
Bonvole enigu du nombrojn post unu el tiuj ĉi matematikaj operaciloj: +,-, *, or/
Please enter two numbers preceded by either of these mathematical operators: +,-, *, or/
/3 2
1
Это сообщение об ошибке, которое я получаю, если я удалю эту точку с запятой (;) после else (s == "/"). @localhost ~] $ g ++ -std = C++ 11 /home/chetan/Programs/Bjarne/bjarne_c3.cpp /home/chetan/Programs/Bjarne/bjarne_c3.cpp: В функции 'int main()': /home/chetan/Programs/Bjarne/bjarne_c3.cpp:29:21: error: expected ';' before '{' token else (s == "/") { ^ –