大致效果如下
/**
*
* ch02 exercise
*
*/
#include <iostream>
using namespace std;
#define pi 3.14159
void menu()
{
cout << "1\t算术运算" << endl;
cout << "2\t输出" << endl;
cout << "3\t整数比较" << endl;
cout << "4\t算术运算,求最大和最小数" << endl;
cout << "5\t圆的直径、周长和面积" << endl;
cout << "6\t用星号显示图形" << endl;
cout << "7\t最大和最小整数" << endl;
cout << "8\t奇数和偶数" << endl;
cout << "9\t倍数" << endl;
cout << "10\t棋盘图案" << endl;
cout << "11\t字符的等价整数值" << endl;
cout << "12\t整数的各位数字" << endl;
cout << "13\t表格" << endl;
}
/**
*(算术运算)编写一个程序,要求用户输入两个数,获取用户输入的数,并输出这两个数的和、乘积、差和商。 */
void display01()
{
double a,b;
cout << "Please enter two integers: ";
//提醒用户输入两个数
cin >> a >> b;
cout << a << " + " << b << " = " << a + b << endl;
cout << a << " - " << b << " = " << a - b << endl;
cout << a << " * " << b << " = " << a * b << endl;
cout << a << " / " << b << " = " << a / b << endl;
}
/**
(输出)
编写一个程序,在同一行上输出数 1∼4 ,并且用一个空格分隔每一对相邻的数。请用下述多种方式实现:
a)使用1条语句,包含1个流插入运算符;
b)使用1条语句,包含4个流插入运算符;
c)使用4条语句。 */
void display02()
{
//01
cout << "1 2 3 4" << endl;
//02
cout << "1" << " 2" << " 3" << " 4" << endl;
//03
cout << "1 ";
cout << "2 ";
cout << "3 ";
cout << "4 " << endl;
}
/**
* (整数比较)
* 编写一个程序。要求用户输入两个整数,获取用户输入的数,
* 然后输出较大数,后面跟随“is large.”。
* 如果这两个数相等,则输出信息“These numbers are equal.”。
*/
//(os:精度不够)
void display03()
{
double a,b;
cout << "Please enter two integers: ";
//提醒用户输入两个数
cin >> a >> b;
if(a > b)
{
cout << a << " is large." << endl;
}
else if(a < b)
cout << b << " is large." << endl;
else
cout << "These numbers are equal." << endl;
}
/**
* (算术运算、求最大和最小数)编写一个程序。
* 要求从键盘输入3个整数,并输出它们的和、平均值、乘积、最小值和最大值。
*/
void display04()
{
int a,b,c;
cout << "Input three different integers: " ;
cin >> a >> b >> c;
cout << a << " + " << b << " + " << c << " = " << a + b + c << endl;
cout << "The average is " << (a + b + c) / 3 << endl;
cout << a << " * " << b << " * " << c << " = " << a * b * c << endl;
if(a > b && b > c)//notice: 不能写成 a > b > c
{
cout << "The smallest is " << c << endl;
cout << "The biggest is " << a << endl;
}
else if(a > c && c > b)
{
cout << "The smallest is " << b << endl;
cout << "The biggest is " << a << endl;
}
else if(b > a && a > c)
{
cout << "The smallest is " << c << endl;
cout << "The biggest is " << b << endl;
}
else if(b > c && c > a)
{
cout << "The smallest is " << a << endl;
cout << "The biggest is " << b << endl;
}
else if(c > a && a > b)
{
cout << "The smallest is " << b << endl;
cout << "The biggest is " << c << endl;
}
else if(c > b && b > a)
{
cout << "The smallest is " << a << endl;
cout << "The biggest is " << c << endl;
}
}
/**
* (圆的直径、周长和面积)编写一个程序。
* 要求读入圆的半径(一个整数),并输出圆的直径、周长和面积(Diameter, Perimeter, and Area)。
*
* π的值取常量3.14159 #define pi 3.14159
*/
void display05()
{
int r;
cout << "Please enter a radius: ";
cin >> r;
cout << "The diameter is " << 2 * r << endl;
cout << "The perimeter is " << 2 * pi * r << endl;
cout << "The area is " << pi * r * r << endl;
}
/**
* (用星号显示图形)编写一个程序,输出如下所示的矩形、椭圆、箭头和菱形。
*/
void display06()
{
cout << "*********" << "\t" << " *** " << "\t" << " * " << "\t" << " * " << endl;
cout << "* *" << "\t" << " * * " << "\t" << " *** " << "\t" << " * * " << endl;
cout << "* *" << "\t" << "* *" << "\t" << " ***** " << "\t" << " * * " << endl;
cout << "* *" << "\t" << "* *" << "\t" << " * " << "\t" << " * * " << endl;
cout << "* *" << "\t" << "* *" << "\t" << " * " << "\t" << "* *" << endl;
cout << "* *" << "\t" << "* *" << "\t" << " * " << "\t" << " * * " << endl;
cout << "* *" << "\t" << "* *" << "\t" << " * " << "\t" << " * * " << endl;
cout << "* *" << "\t" << " * * " << "\t" << " * " << "\t" << " * * " << endl;
cout << "*********" << "\t" << " *** " << "\t" << " * " << "\t" << " * " << endl;
}
/**
* (最大和最小整数)编写一个程序,读入5个整数,然后确定并输出它们中的最大值和最小值
*/
void display07()
{
int a,b,c,d,e;
cout << "Please enter five integers: ";
cin >> a >> b >> c >> d >> e;
// if(a > b && b > c && c > d && d > e)//a b c d e
// {
// cout << "The smallest is " << e << endl;
// cout << "The biggest is " << a << endl;
// }
//
// else if(a > b && b > c && c > e && e > d)//a b c e d
// {
// cout << "The smallest is " << d << endl;
// cout << "The biggest is " << a << endl;
// }
//
// else if(a > b && b > d && d > c && c > e)//a b d c e
// {
// cout << "The smallest is " << e << endl;
// cout << "The biggest is " << a << endl;
// }
// else if(a > b && b > d && d > e && e > c)//a b d e c
// {
// cout << "The smallest is " << c << endl;
// cout << "The biggest is " << a << endl;
// }
//
// else if(a > c && c > b && b > e && e > d)//a c b e d
// {
// cout << "The smallest is " << d << endl;
// cout << "The biggest is " << a << endl;
// }
// else if(a > c && c > d && d > b && b > e)//a c d b e
// {
// cout << "The smallest is " << e << endl;
// cout << "The biggest is " << a << endl;
// }
// else if(a > c && c > d && d > e && e > b)//a c d e b
// {
// cout << "The smallest is " << b << endl;
// cout << "The biggest is " << a << endl;
// }
// else if(a > d && d > b && b > c && c > e)//a d b c e
// {
// cout << "The smallest is " << e << endl;
// cout << "The biggest is " << a << endl;
// }
// else if(a > d && d > b && b > e && e > c)//a d b e c
// {
// cout << "The smallest is " << c << endl;
// cout << "The biggest is " << a << endl;
// }
// else if(a > d && d > c && c > b && b > e)//a d c b e
// {
// cout << "The smallest is " << e << endl;
// cout << "The biggest is " << a << endl;
// }
// else if(a > d && d > c && c > e && e > b)//a d c e b
// {
// cout << "The smallest is " << b << endl;
// cout << "The biggest is " << a << endl;
// }
// else if(a > d && d > e && e > b && b > c)//a d e b c
// {
// cout << "The smallest is " << c << endl;
// cout << "The biggest is " << a << endl;
// }
// else if(a > e && e > b && b > c && c > d)//a e b c d
// {
// cout << "The smallest is " << d << endl;
// cout << "The biggest is " << a << endl;
// }
// else if(a > e && e > b && b > d && d > c)//a e b d c
// {
// cout << "The smallest is " << c << endl;
// cout << "The biggest is " << a << endl;
// }
// else if(a > e && e > c && c > b && b > d)//a e c b d
// {
// cout << "The smallest is " << d << endl;
// cout << "The biggest is " << a << endl;
// }
// else if(a > e && e > c && c > d && d > b)//a e c d b
// {
// cout << "The smallest is " << b << endl;
// cout << "The biggest is " << a << endl;
// }
// else if(a > e && e > d && d > b && b > c)//a e d b c
// {
// cout << "The smallest is " << c << endl;
// cout << "The biggest is " << a << endl;
// }
int min_num = a;
int max_num = a;
if (b > max_num)
{
max_num = b;
}
if (b < min_num)
{
min_num = b;
}
if (c > max_num)
{
max_num = c;
}
if (c < min_num)
{
min_num = c;
}
if (d > max_num)
{
max_num = d;
}
if (d < min_num)
{
min_num = d;
}
if (e > max_num)
{
max_num = e;
}
if (e < min_num)
{
min_num = e;
}
cout << "The smallest is " << min_num << endl;
cout << "The biggest is " << max_num << endl;
}
/**
*(奇数和偶数)编写一个程序。要求读入一个整数,然后确定并输出它是奇数还是偶数
*/
void display08()
{
int a;
cout << "Please enter an integer: ";
cin >> a;
if(a % 2 == 0)
{
cout << a << " is an even number" << endl;//偶数
}
else
{
cout << a << " is an odd number" << endl;//奇数
}
}
/**
* (倍数)编写一个程序。
* 要求读入两个整数,确定并输出第一个数是否是第二个数的倍数
*
*/
void display09()
{
int a,b;
cout << "Please enter two integers: ";
cin >> a >> b;
if(a % b == 0)
{
cout << a << " is " << a / b << " times " << b << endl;
}
else
{
cout << a << " is not a multiple of " << b << endl;
}
}
/**
*(棋盘图案)显示下面的棋盘图案
*/
void display10()
{
for(int i = 0;i < 4;i++)
{
cout << "* * * * * * * * " << endl;
cout << " * * * * * * *" << endl;
}
}
/**
* (字符的等价整数值)
* 编写一个程序,输出键盘上键入的字符所对应的整数。
* 使用大写字母、小写字母、数字和特殊符号(例如$)反复测试你的程序
*/
void display11()
{
char ch;
cout << "Please enter a character: ";
cin >> ch;
cout << static_cast<int>(ch);
}
/**
* (整数的各位数字)编写一个程序。
* 要求输入一个5位整数,然后分解出它的每位数字,并将这些数字按间隔3个空格的形式输出出来。
*/
void display12()
{
int a;
cout << "Please enter a five-digit number: ";
cin >> a;
/*倒序输出*/
// while(a != 0)
// {
// int b = a % 10;
// a /= 10;
// cout << b << " " ;
// }
int b[5];
for(int i = 0; i < 5; i++)
{
b[5 - i - 1] = a % 10;
a /= 10;
}
for(int i = 0; i < 5; i++)
{
cout << b[i] << " ";
}
cout << endl;
}
/**
* (表格)计算整数0~10的平方和立方,然后使用制表符输出具有整齐格式的数值表
*/
void display13()
{
cout << "integer\tsquare\tcube" << endl;
for(int i = 0;i <= 10;i++)
{
cout << i << "\t" << i * i << "\t" << i * i * i << endl;
}
}
///choice 仅限1 - 13 (其他不考虑) 若为零则退出程序
void display(int choice)
{
switch(choice)
{
case 1:display01();break;
case 2:display02();break;
case 3:display03();break;
case 4:display04();break;
case 5:display05();break;
case 6:display06();break;
case 7:display07();break;
case 8:display08();break;
case 9:display09();break;
case 10:display10();break;
case 11:display11();break;
case 12:display12();break;
case 13:display13();break;
case 0: cout << "exit" << endl;break;
}
}
int main()
{
cout << "****************************************" << endl;
cout << "Welcome to the program!" << endl;
menu();
cout << "Welcome there again!" << endl;
cout << "****************************************" << endl;
int choice;
cin >> choice;
display(choice);
while(choice >= 1 && choice <= 13)
{
cout << endl;
cout << "****************************************" << endl;
cout << "Welcome to the program!" << endl;
menu();
cout << "Welcome there again!" << endl;
cout << "****************************************" << endl;
cout << endl;
cout << "Please choose again: ";
cin >> choice;
display(choice);
}
return 0;
}