1. 编写一个C++程序,它显示您的姓名和地址。
#includeusing namespace std; int main(void) { cout << "My name is YoRHa,l live in Shanghai" << endl; return 0; }
输出结果:
2.编写一个CH程序,它要求用户输入一个以long为单位的距离,然后将它转换为码(一long等于220码)。
#includeusing namespace std; double long_to_yard(double long1); int main(void) { double long1; cout << "Gvie long: "; cin >> long1; double yard1; yard1 = long_to_yard(long1); cout << long1 << " long= " << yard1 << " yard." << endl; return 0; } double long_to_yard(double long1) { return 200 * long1; }
输出结果:
3.编写一个C++程序,它使用3个用户定义的函数(包括main( )),并生成下面的输出:
Three blind mice
Three blind mice
See how they run
See how they run
includeusing namespace std; void print_mice(void); void print_run(void); int main(void) { print_mice(); print_mice(); print_run(); print_run(); return 0; } void print_mice(void) { cout << "Three blind mice." << endl; } void print_run(void) { cout << "See how the run." << endl; }
输出结果:
4.编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月,如下所示:Enter your age:29
#includeusing namespace std; int age_to_month(int age); int main(void) { int age; cout << "Enter your age: "; cin >> age; int months; months = age_to_month(age); cout << "This age include " << months << " month" << endl; return 0; } int age_to_month(int age) { return age*12; }
输出结果:
5.编写一个程序,其中的main()调用一个用户定义的函数(以摄氏温度值为参数,并返回相应的华氏温度值)。该程序按下面的格式要求用户输入摄氏温度值,并显示结果:
Please enter a celsius value: 20
20 degrees celsius is 68 degrees Fahrenheit.
下面是转换公式:华氏温度=1.8×摄氏温度+32.0
#includeusing namespace std; double convert(double c); int main(void) { double c_degree,f_degree; cout << "Please enter a Celsius value: "; cin >> c_degree; f_degree = convert(c_degree); cout << c_degree << " degrees Celsius is " << f_degree << " degrees Fahrenheit." << endl; return 0; } double convert(double c) { return 1.8 * c + 32; }
输出结果:
7.编写一个程序,要求用户输入小时数和分钟数。在main()函数中,将这两个值传递给一个void函数,后者以下面这样的格式显示这两个值:
Enter the number of hours: 9
Enter the number of minutes: 28
Time:9:28
#includeusing namespace std; void show(int h,int m); int main(void) { int hour,minute; cout << "Enter the number of hours: "; cin >> hour; cout << "Enter the number of minutes: "; cin >> minute; show(hour,minute); return 0; } void show(int h,int m) { cout << "Time: " << h <<":" << m << endl; }
输出结果:



