C++语言程序设计

程序：
根据上面的分析，我们可以编写出完整的程序。

| 例4-18

#include <iostream.h>
int main()
{
double displayed_value;
double new_entry;
char command_character;
displayed_value = 0.0;
cout << "计算器演示程序" << endl
<< "----" << endl;
cout << "在'>' 提示后输入一个命令字符"
<< endl;
cout << "Value : " << displayed_value << endl;
cout << "command>";
cin >> command_character;
while (command_character != 'Q') {
switch(command_character) {
case 'C':
displayed_value = 0.0;
break;
case '+':
cout << "number>";
cin >> new_entry;
displayed_value += new_entry;
break;
case '-':
cout << "number>";
cin >> new_entry;
displayed_value -= new_entry;
break;
case '*':
cout << "number>";
cin >> new_entry;
displayed_value *= new_entry;
break;
case '/':
cout << "number>";
cin >> new_entry;
displayed_value /= new_entry;
break;
default:
cout << "不可理解的输入!";
cin.ignore(100,'\n');
} //End of switch
cout << "Value : " << displayed_value << endl;
cout << "command>";
cin >> command_character;
} //End of while
return 0;
}

下面给出本程序的一个运行实例：
计算器演示程序
----
在'>' 提示后输入一个命令字符
Value : 0
command>+↙
number>1.23↙
Value : 1.23
command>*↙
number>6.4↙
Value : 7.872
command>C↙
Value : 0
command> Q↙
键入'Q'后，程序运行终止。