注意:每个C++项目中只能有一个main()函数
1、编写一个C++程序,它显示您的姓名和地址。
#include<iostream>
using namespace std;
void main()
{
cout << "你的名字是:李华,地址是:地球。" << endl;
}
2、编写一个C++程序,它要求用户输入一个以long为单位的距离,然后将它转化为码(一long等于220码)。
#include<iostream>
using namespace std;
//编写一个C++程序,它要求用户输入一个以long为单位的距离,然后将它转化为码(一long等于220码)。
void main()
{
int Long=0;
cout << "请输入一个以long为单位的距离:" << endl;
cin >> Long;
cout << "您输入的这个距离等于" << Long * 220 << "码" << endl;
}
3、编写一个C++程序,它使用三个用户定义的函数包括main(),并生成下面的输出:
Three blind mice
Three blind mice
See how they run
See how they run
其中一个函数要调用两次,该函数生成前两行;另外一个函数也被调用两次,并生成其余的输出。
#include<iostream>
using namespace std;
void Tbm() {
cout << "Three blind mice" << endl;
}
void Shtr() {
cout << "See how they run" << endl;
}
void main()
{
Tbm();
Tbm();
Shtr();
Shtr();
}
4、编写一个程序,让用户输入其年龄,然后显示该年龄包含多少月。
#include<iostream>
using namespace std;
void main() {
int age;
cout << "请输入您的年龄:" << endl;
cin >> age;
cout << "您的年龄包含" << age * 12 << "个月" << endl;
}
5、编写一个程序,其中的main()调用用户定义的函数(以摄氏度值为参数,并返回相应的华氏摄氏度)。
转换公式:华氏温度=1.8*摄氏温度+32.0
#include<iostream>
using namespace std;
double Fah(double C) {
return C * 1.8 + 32.0;
}
void main() {
double c;
cout << "请输入摄氏度温度:" << endl;
cin >> c;
cout << "您输入的摄氏度温度转化为华氏摄氏度为:" << Fah(c) << endl;
}
6、 编写一个程序,其main()调用一个用户自定义的函数(以光年为参数,并返回对应天文单位的值)
转换公式为:1光年=63240天文单位
#include<iostream>
using namespace std;
//将光年转化为天文单位
double Light(double ly) {
return ly * 63240;
}
void main() {
double ly;
cout << "请输入光年数:" << endl;
cin >> ly;
cout << "光年转化为天文单位为:" << Light(ly) << endl;
}
7、编写一个程序,要求用户输入小时数和分钟数。在main()函数中,将这两个值传递给一个void函数,后者以下面这样的格式显示这两个值:
输入的小时数是:9
输入的分钟数是:18
时间:9:18
#include<iostream>
using namespace std;
void Cheage(int a, int b) {
cout << "时间:" << a << ":" << b << endl;
}
void main() {
int hours, minutes;
cout << "您输入的小时数是:" << endl;
cin >> hours;
cout << "您输入的分钟数是:" << endl;
cin >> minutes;
Cheage(hours, minutes);
}