1.编写一个程序,它显示您的姓名和年龄 自己写的
#include <iostream>
using namespace std;
#include <string>
int main() {
char str[]="";
cout << "请输入您的姓名:" << endl;
cin >> str;
cout << "您的姓名是 " << str << endl;
int age;
cout << "请输入您的年龄" << endl;
cin >> age;
cout << "您的年龄是 " << age << endl;
system("pause");
}
网上的参考
#include <iostream>
using namespace std;
int main()
{
cout << "My name is C++ Primer Plus." << endl;
cout << "My address is in library." << endl;
return 0;
}
2.编写一个c++程序,它要求用户输入一个以long为单位的距离,然后将他转换为码(一long等于220码。) 自己写的
#include <iostream>
using namespace std;
int main()
{
double Distance;
cout << "请输入距离:(单位long)" << endl;
cin >> Distance;
cout << "等于:" << Distance * 220 << "码" << endl;
system("pause");
}
网上的参考1
#include <iostream>
int main(void)
{
using namespace std;
double distance;
cout << "Enter the distance (in long):";
cin >> distance;
cout << "The distance" << distance << "long" << "equals"
<< 220 * distance << "yard." << endl;
return 0;
}
网上的参考2
#include <iostream>
using namespace std;
int main()
{
double long_distance;
cout << "Enter a distance for long unit: ";
cin >> long_distance;
cout << long_distance << " long distance ";
cout << "is equal to ";
long_distance *= 220.0;
cout << long_distance << " yard distance." << endl;
system("pause");
return 0;
}
3.编写一个c++程序,它使用3个用户定义的函数(包括main()),并生成下面的输出: Three blind mice Three blind mice See how they run See how they run 其中一个函数要调用两次,该函数生成前两行:另一个函数也被调用两次,并生成其余的输出。
自己写的
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 2; i++) {
cout << "Three blind mice" << endl;
}
for (int j = 0; j < 2; j++) {
cout << "See how they run" << endl;
}
system("pause");
return 0;
}
网上的参考1
#include <iostream>
void print_mice(void);
void print_run(void);
using namespace std;
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 they run." << endl;
}
网上的参考2
#include<iostream>
void A(int);
void B(int);
using namespace std;
void A(int n)
{
int i;
for (i = 0; i < n; i++)
cout << "Three blind mice" << endl;
}
void B(int n)
{
int i;
for (i = 0; i < n; i++)
cout << "See how they run" << endl;
}
int main()
{
A(2);
B(2);
system("pause");
}
4.编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月,如下所示: Enter you age:29 自己写的
#include <iostream>
using namespace std;
int main()
{
int Age;
cout << "请输入年龄:(岁)" << endl;
cin >> Age;
cout << "该年龄包含:" << Age * 12 << "个月" << endl;
system("pause");
}
网上的参考1
#include <iostream>
using namespace std;
int main(void)
{
int years;
cout << "Enter your age:";
cin >> years;
cout << "You are " << years << " years old,equals = "
<< years * 12 << " months old." << endl;
}
网上的参考2
#include <iostream>
using namespace std;
int main()
{
int age_total_months;
cout << "Enter your age: ";
cin >> age_total_months;
cout << "Your age includes " << age_total_months * 12 << " months." << endl;
system("pause");
return 0;
}
5.编写一个程序,其中的mian()调用一个用户定义的函数(以摄氏度值为参数,并返回相应的华氏温度值)。该程序按下面的格式要求用户输入摄氏温度值,并显示结果: Please enter a Celsius value :20 20 degree Celsiue is 68 degrees Fahrenheit. 下面是转换公式: 华氏温度 = 1.8 摄氏温度 + 32.0* 自己写的
#include<iostream>
using namespace std;
int main() {
double Celsius;
cout << "Please enter a Celsius value:";
cin >> Celsius;
cout << Celsius << " degree Celsius is " << Celsius * 1.8 + 32.0
<< " degree Fahrenheit" << endl;
system("pause");
return 0;
}
网上的参考1
#include <iostream>
using namespace std;
double convert(double c);
double convert(double c)
{
return 1.8 * c + 32.0;
}
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;
}
网上的参考2
#include <iostream>
using namespace std;
double temperature(double temp);
int main()
{
double celsius;
cout << "Please enter a Celsius value: ";
cin >> celsius;
cout << celsius << " degrees Celsius is ";
cout << temperature(celsius);
cout << " degrees Fahrenheit." << endl;
return 0;
}
double temperature(double temp)
{
return 1.8 * temp + 32.0;
}
网上的参考3
#include <iostream>
using namespace std;
double celsiustofahrenheit(double);
int main()
{
double Celsius;
cout << "please enter a Celsius value:";
cin >> Celsius;
double Fahrenheit = celsiustofahrenheit(Celsius);
cout << Celsius << " degrees Celsius is "<<Fahrenheit<<" degrees Fahrenheit" << endl;
}
double celsiustofahrenheit(double Celsius)
{
return 1.8 * Celsius + 32.0;
}
6.编写一个程序,其main()调用一个用户定义的函数(以光年值为参数,并返回对应天文单位的值。)该程序按下面的格式要求用户输入光年值,并显示结果: Enter the number of light years:4.2 4.2 light years = 265608 astronomical inits. 使用double类型,转换公式为: 1光年 = 63240天文单位 自己写的
#include<iostream>
using namespace std;
int main() {
double LightYears;
cout << "Enter the number of light years:";
cin >> LightYears;
cout << LightYears << " light years = " << LightYears * 63240
<< " astronomical units" << endl;
system("pause");
return 0;
}
网上的参考1
#include <iostream>
using namespace std;
double converet(double light);
double converet(double light)
{
return 63240 * light;
}
int main(void)
{
double light_years, astro_years;
cout << "Enter the number of light years:";
cin >> light_years;
astro_years = converet(light_years);
cout << light_years << " light years = "
<< astro_years << " astronomical units." << endl;
return 0;
}
网上的参考2
#include <iostream>
using namespace std;
double transform(double temp);
int main()
{
double light_years;
cout << "Enter the number of light years: ";
cin >> light_years;
cout << light_years << " light years = ";
cout << transform(light_years);
cout << " astronomical units." << endl;
return 0;
}
double transform(double temp)
{
return temp * 63240.0;
}
7.编写一个程序,要求用户输入小时数和分钟数。在main()函数中,将这两个值传递给一个void函数,后者以下面这样的格式显示这两个值: Enter the number of hours:9 Enter the number of minutes:28 Time:9:28 自己写的
#include<iostream>
using namespace std;
int main() {
int Hours;
cout << "Enter the number of light hours:";
cin >> Hours;
int Minutes;
cout << "Enter the number of light minutes:";
cin >> Minutes;
cout << "Time:" << Hours << ":"<<Minutes << endl;
system("pause");
return 0;
}
网上的参考1
#include <iostream>
using namespace std;
void display_time(int h, int m);
void display_time(int h, int m)
{
cout << "Time: " << h << ":" << m << endl;
}
int main(void)
{
int hours, minutes;
cout << "Enter the number of hour:";
cin >> hours;
cout << "Enter the number of minutes:";
cin >> minutes;
display_time(hours, minutes);
return 0;
}
网上的参考2
#include <iostream>
using namespace std;
void show_time(int hour, int minute);
int main()
{
int hour, minute;
cout << "Enter the number of hours: ";
cin >> hour;
cout << "Enter the number of minutes: ";
cin >> minute;
show_time(hour, minute);
return 0;
}
void show_time(int hour, int minute)
{
cout << "Time: " << hour << ":" << minute << endl;
return;
}
|