(1)编写 C++ 程序显示姓名和地址。
#include <iostream>
using namespace std;
int main()
{
cout << "My name is zhangts20." << endl;
cout << "I live in Sichuan." << endl;
return 0;
}
(2)编写 C++ 程序,它要求用户输入一个以 long 为单位的单位的距离,让后将它转换为码(一 long 等于 220 码)。
#include <iostream>
using namespace std;
int main()
{
cout << "Please enter the distance in long: ";
int dist;
cin >> dist;
cout << dist << " longs are " << dist * 220 << " yards." << endl;
return 0;
}
(3)编写 C++ 程序,它使用 3 个用户定义的函数(包括 main 函数)产生输出。
#include <iostream>
using namespace std;
void func1();
void func2();
int main()
{
func1();
func1();
func2();
func2();
return 0;
}
void func1()
{
cout << "Three blind mice" << endl;
}
void func2()
{
cout << "See how they run" << endl;
}
(4)编写 C++ 程序,让用户输入年龄,然后显示该年龄包含多少个月。
#include <iostream>
using namespace std;
int main()
{
cout << "Enter your age: ";
int age;
cin >> age;
cout << age << " years contain " << age * 12 << " months." << endl;
return 0;
}
(5)编写 C++ 程序,其中 main 函数中调用自定义函数,该函数以摄氏度值为参数并返回相应的华氏摄氏度。
#include <iostream>
using namespace std;
double convert(double);
int main()
{
cout << "Please enter a Celsius value: ";
double c;
cin >> c;
cout << c << " degrees Celsius is " << convert(c) << " degrees Fahrenheit." << endl;
return 0;
}
double convert(double v)
{
return 1.8 * v + 32.0;
}
(6)编写 C++ 程序,其中 main 函数中调用自定义函数,该函数以光年值为参数,并返回相应的天文单位值。
#include <iostream>
using namespace std;
double convert(double);
int main()
{
cout << "Please enter the number of light years: ";
double c;
cin >> c;
cout << c << " light years = " << convert(c) << " astronomical units." << endl;
return 0;
}
double convert(double v)
{
return 63240 * v;
}
(7)编写 C++ 程序,要求用户输入小时数和分钟数,在 main 函数中,将这个两个值传递给一个 void 函数并完成打印。
#include <iostream>
using namespace std;
void show(int, int);
int main()
{
cout << "Enter the number of hours: ";
int hours;
cin >> hours;
cout << "Enter the number of minutes: ";
int minutes;
cin >> minutes;
show(hours, minutes);
return 0;
}
void show(int hs, int ms)
{
cout << "Time: " << hs << ":" << ms << endl;
}
|