1.编写一个C++程序,它显示您的姓名和地址。
//myPractice201.cpp -- 编程练习,显示自己的姓名和地址
#include <iostream>
using namespace std;
int main()
{
cout << "姓名:Tom\n地址:漂亮国" << endl;
return 0;
}
2.编写一个C++程序,它要求用户输入一个以long为单位的距离,然后将它转换成码(一long等于220码)。
//myPractice202.cpp -- 输入long值,转换为码
#include <iostream>
using namespace std;
int LongToMa(int);
int main()
{
cout << "请输入long值:";
int long_num;
cin >> long_num;
//直接计算法
int ma_num = long_num * 220;
cout << long_num << " long = "
<< ma_num << " 码。" << endl;
//调用函数法
cout << long_num << " long = "
<< LongToMa(long_num) << " 码。" << endl;
return 0;
}
int LongToMa(int ln)
{
return 220 * ln;
}
3.编写一个C++程序,它使用3个用户定义的函数(包括main()),并生成下面的输出:
Three blind mice
Three blind mice
See how they run
See how they run
其中一个函数要被调用两次,该函数生成前两行;另一个函数也被调用两次,并生成取余的输出。
//myPractice203.cpp -- 自定义函数用于内容输出
#include <iostream>
using namespace std;
void MessageOutput1();
void MessageOutput2();
int main()
{
MessageOutput1();
MessageOutput1();
MessageOutput2();
MessageOutput2();
return 0;
}
void MessageOutput1()
{
cout << "Three blind mice" << endl;
}
void MessageOutput2()
{
cout << "See how they run" << endl;
}
4.编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月。
//myPractice204.cpp -- 输入年龄,显示月数
#include <iostream>
using namespace std;
int YearToMonth(int);
int main()
{
cout << "请输入您的年龄:";
int year_num;
cin >> year_num;
//直接计算法
cout << year_num << "岁对应的月数是" << year_num*12 << "个月。" << endl;
cout << year_num << "岁对应的月数是" << YearToMonth(year_num) << "个月。" << endl;
return 0;
}
int YearToMonth(int yn)
{
return 12 * yn;
}
5.编写一个程序,其中的main()调用一个用户定义的函数(以摄氏温度值为参数,并返回相应的华氏温度值)。该程序按下面的格式要求用户输入摄氏温度值,并显示结果:
Please enter a Celsius value:20
20 degrees Celsius is 68 degrees Fahrenheit.
转换公式:华氏温度 = 1.8 × 摄氏温度 + 32.0
//myPractice205.cpp -- 输入摄氏温度值,转换为华氏温度值输出
#include <iostream>
using namespace std;
double CelsiusToFahrenheit(double);
int main()
{
cout << "请输入摄氏温度值:";
double celsius_num;
cin >> celsius_num;
double fahrenheit_num = CelsiusToFahrenheit(celsius_num);
cout << celsius_num << "摄氏度等于" << fahrenheit_num << "华氏度。" << endl;
return 0;
}
double CelsiusToFahrenheit(double cn)
{
return 1.8*cn + 32.0;
}
6.编写一个程序,其main()调用一个用户定义的函数(以光年值为参数,并返回对应天文单位的值)。该程序按下面的格式要求用户输入光年值,并显示结果:
Enter the number of light years:4.2
4.2 light years = 265608 astronomical units.
天文单位是从地球到太阳的平均距离(约150000000公里或93000000英里),光年是光一年走的距离(约10万亿公里或6万亿英里)(除太阳外,最近的恒星大约离地球4.2光年)。请使用double类型,转换公式:1 光年 = 63240 天文单位
//myPractice206.cpp -- 输入光年值,转换为光年单位并输出
#include <iostream>
using namespace std;
double convert(double);
int main()
{
cout << "请输入光年值:";
double light_year,astro_unit;
cin >> light_year;
astro_unit = convert(light_year);
cout << light_year << "光年等于" << astro_unit << "天文单位" << endl;
return 0;
}
double convert(double ly)
{
return 63240 * ly;
}
7.编写一个程序,要求用户输入小时数和分钟数。在main()函数中,将这两个值传递给一个void函数,后者以下面这样的格式显示这两个值:
Enter the number of hours:9
Enter the number of minutes:28
Time:9:28
//myPractice207.cpp -- 用户输入小时数和分钟数,系统显示时间
#include <iostream>
using namespace std;
void DisplayTime(int h, int m);
int main()
{
int hour,minute;
cout << "请输入小时数:";
cin >> hour;
cout << "请输入分钟数:";
cin >> minute;
DisplayTime(hour, minute);
return 0;
}
void DisplayTime(int h, int m)
{
cout << "时间:" << h << ":" << m << endl;
}