輸入 兩個數字和一個符號
經過計算並輸出答案- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
-
- int num1 = 0;
- int num2 = 0;
-
- char st; //符號用char
- cout<<"請輸入數字1: ";
- cin>>num1;
- cout<<"請輸入數字2: ";
- cin>>num2;
- cout<<"請輸入+-*/ ";
- cin>>st;
- switch(st)
- {
- case '+': //字串 用 ' 包起來
- cout << num1 + num2 << endl;
- break;
-
- case '-':
- cout<<num1 - num2<<endl;
- break;
-
- case '*':
- cout<<num1 * num2<<endl;
- break;
-
- case '/':
- cout<<num1 / num2<<endl;
- break;
-
- default: // 預設值
- cout<<"輸入錯誤!"<<endl;
- }
- system("pause");
- return 0;
- }
複製代碼 |