Lab 4: 簡易科學計算機
Lab 4-1: 計算機功能選擇 (40%)
- 輸入:要執行的計算功能,分別如下:
- 加 (
+
) - 減 (
-
) - 乘 (
*
) - 除 (
/
)
- 加 (
- 輸出:所選擇的運算子字元 (i.e.
+
,-
,*
,/
) - 檔名:lab4_1_<學號>.cpp (e.g. lab4_1_106062802.cpp)
程式需提示使用者輸入需要的運算功能,輸出該運算功能的運算子字元。
Simple scientific calculator
1) plus (+)
2) minus (-)
3) multiplication (*)
4) division (/)
Please select the operator: <1-4>
You selected: <+, -, *, />
Example:
$ ./a.out
Simple scientific calculator
1) plus (+)
2) minus (-)
3) multiplication (*)
4) division (/)
Please select the operator: 3
You selected: *
Reference Code:
if ... else ...
: Credit: 許景煬 (107071017)
#include <iostream>
#include <iomanip>
int main(void)
{
int a = 0;
std::cout << "Simple scientific calculator\n"
<< "1) plus (+)\n"
<< "2) minus (-)\n"
<< "3) multiplication (*)\n"
<< "4) division (/)\n"
<< "Please select the operator: "
<< std::endl;
std::cin >> a;
if (a == 1)
{
std::cout << "You selected: "
<< "+"
<< std::endl;
}
else if (a == 2)
{
std::cout << "You selected: "
<< "-"
<< std::endl;
}
else if (a == 3)
{
std::cout << "You selected: "
<< "*"
<< std::endl;
}
else if (a == 4)
{
std::cout << "You selected: "
<< "/"
<< std::endl;
}
else
{
std::cout << "Your operator is not in here!!!!!!!"
<< std::endl;
}
}
switch ... case ...
: Credit: 陳欣妤 (110021108)
#include <iostream>
using namespace std;
int main()
{
int a;
cout << "Simple scientific calculator" << '\n'
<< "1) plus (+)" << '\n'
<< "2) minus (-)" << '\n'
<< "3) multiplication (*)" << '\n'
<< "4) division (/)" << '\n'
<< "Please select the operator: ";
cin >> a;
switch (a)
{
case 1:
cout << "You selected: +" << endl;
break;
case 2:
cout << "You selected: -" << endl;
break;
case 3:
cout << "You selected: *" << endl;
break;
case 4:
cout << "You selected: /" << endl;
break;
default:
cout << "You selected: ?" << endl;
}
return 0;
}