2.7 编程联系
1 .编写一个c++程序,显示您的姓名何地址。(略)
2.编写一个c++程序,要求用户输入一个以浪为单位的距离,然后将它转换为吗(一浪=220码).
#include <iostream>
int demox(int);
using namespace std;
int main ()
{
int demo;
cout<<"请输入距离浪:";
cin>>demo;
int demo0=demox(demo);
cout<<demo<<" 浪等于"<<demo0<<"码"<<endl;
cin.get();
cin.get();
return 0;
}
int demox(int sta)
{
return 220*sta;
}
3.编写一个c++程序,它使用3个用户自定义的函授(包括main()),并生成线面的输出:
three blind mice
three blind mice
see how they run
see how they run
其中一个函数要调用两次,该函数生成前两行;另一个函数也被调用两次,并生成其余的输出。
#include <iostream>
using namespace std;
void demo()
{
cout<<"three blind mice"<<endl;
}
void demo2()
{
cout<<"see how they run"<<endl;
}
int main()
{
demo();
demo();
demo2();
demo2();
cin.get();
cin.get();
return 0;
}
4.编写一个程序,其中的main()调用一个用户定义的函数(一摄氏度值为参数,并返回相应的华氏
1 .编写一个c++程序,显示您的姓名何地址。(略)
2.编写一个c++程序,要求用户输入一个以浪为单位的距离,然后将它转换为吗(一浪=220码).
#include <iostream>
int demox(int);
using namespace std;
int main ()
{
int demo;
cout<<"请输入距离浪:";
cin>>demo;
int demo0=demox(demo);
cout<<demo<<" 浪等于"<<demo0<<"码"<<endl;
cin.get();
cin.get();
return 0;
}
int demox(int sta)
{
return 220*sta;
}
3.编写一个c++程序,它使用3个用户自定义的函授(包括main()),并生成线面的输出:
three blind mice
three blind mice
see how they run
see how they run
其中一个函数要调用两次,该函数生成前两行;另一个函数也被调用两次,并生成其余的输出。
#include <iostream>
using namespace std;
void demo()
{
cout<<"three blind mice"<<endl;
}
void demo2()
{
cout<<"see how they run"<<endl;
}
int main()
{
demo();
demo();
demo2();
demo2();
cin.get();
cin.get();
return 0;
}
4.编写一个程序,其中的main()调用一个用户定义的函数(一摄氏度值为参数,并返回相应的华氏
温度
值)。改程序按下面的格式要求用户输入摄氏温度值,并显示结果;
Please enter a Celsius value ;20
20 degrees Celeius is 68 degrees fahrenheit.
下面是转换公式;
华氏温度=1.8*摄氏度+32.0
#include <iostream>
double stonetolb(double);
using namespace std;
int main ()
{
double stone;
cout <<"Please enter a Celsius value:";
cin>>stone;
double pounds=stonetolb(stone);
cout << stone <<" degrees Celeius is "<<pounds<<" degrees fahrenheit."<<endl;
cin.get();
cin.get();
return 0;
}
double stonetolb(double sta)
{
return 1.8*sta +32.0;
}
5.编写一个程序,其main()调用一个用户自定义函数(以光年值为参数,并返回对应天文单位的值。
改程序按下面的格式要求用户输入光年值,并显示结果:
Enter the number of light years;4.2
4.2 light years=265608 asteonomical units.
天文单位是从地球到太阳的平均距离(约150 000 000公里或93 000 000英里),光年是光一年做的距离
(约10万亿公里或6万亿英里。除太阳外,最近的恒星大约离地球4.2光年)。请使用double类型(参见程
值)。改程序按下面的格式要求用户输入摄氏温度值,并显示结果;
Please enter a Celsius value ;20
20 degrees Celeius is 68 degrees fahrenheit.
下面是转换公式;
华氏温度=1.8*摄氏度+32.0
#include <iostream>
double stonetolb(double);
using namespace std;
int main ()
{
double stone;
cout <<"Please enter a Celsius value:";
cin>>stone;
double pounds=stonetolb(stone);
cout << stone <<" degrees Celeius is "<<pounds<<" degrees fahrenheit."<<endl;
cin.get();
cin.get();
return 0;
}
double stonetolb(double sta)
{
return 1.8*sta +32.0;
}
5.编写一个程序,其main()调用一个用户自定义函数(以光年值为参数,并返回对应天文单位的值。
改程序按下面的格式要求用户输入光年值,并显示结果:
Enter the number of light years;4.2
4.2 light years=265608 asteonomical units.
天文单位是从地球到太阳的平均距离(约150 000 000公里或93 000 000英里),光年是光一年做的距离
(约10万亿公里或6万亿英里。除太阳外,最近的恒星大约离地球4.2光年)。请使用double类型(参见程
序清单2.4),转换公式为;
一光年=63240天文单位
#include <iostream>
double stonetolb(double);
using namespace std;
int main ()
{
double stone;
cout <<" Enter the number of light years:";
cin>>stone;
double pounds=stonetolb(stone);
cout << stone <<" light years= "<<pounds<<" asteonomical units."<<endl;
cin.get();
cin.get();
return 0;
}
double stonetolb(double sta)
{
return 63240*sta;
}
6.编写一个程序,要求用户输入小时数和分钟数。在main()函数中,将这两个值传递给一个void函数
一光年=63240天文单位
#include <iostream>
double stonetolb(double);
using namespace std;
int main ()
{
double stone;
cout <<" Enter the number of light years:";
cin>>stone;
double pounds=stonetolb(stone);
cout << stone <<" light years= "<<pounds<<" asteonomical units."<<endl;
cin.get();
cin.get();
return 0;
}
double stonetolb(double sta)
{
return 63240*sta;
}
6.编写一个程序,要求用户输入小时数和分钟数。在main()函数中,将这两个值传递给一个void函数
,然后下面这样的格式显示这两个值;
Enter the number of hours;9
Enter the number of minutes;20
TIME 9:28
Enter the number of hours;9
Enter the number of minutes;20
TIME 9:28
#include <iostream>
using namespace std;
void time (int,int);
int main()
{
int h,m;
cout <<"ENTER the number of hours: ";
while(cin>>h)
{
if(h>=0&&h<=24)
break;
else
cout<<"请输入正确的时间(hour); ";
}
cout <<"Enter the unmber of minuters; ";
while(cin >>m)
{
if(m>=0&&m<=60)
break;
else
cout<<"请输入正确的时间(miunter) ;";
{
int h,m;
cout <<"ENTER the number of hours: ";
while(cin>>h)
{
if(h>=0&&h<=24)
break;
else
cout<<"请输入正确的时间(hour); ";
}
cout <<"Enter the unmber of minuters; ";
while(cin >>m)
{
if(m>=0&&m<=60)
break;
else
cout<<"请输入正确的时间(miunter) ;";
}
time (h,m);
cin.get();
cin.get();
return 0;
}
void time (int h,int m)
{
cout <<"TIME; "<<h<<":"<<m<<endl;
}
time (h,m);
cin.get();
cin.get();
return 0;
}
void time (int h,int m)
{
cout <<"TIME; "<<h<<":"<<m<<endl;
}
以上的每个程序都通过Microsoft Visual C++ 2008 速成版 SP1编译
转载于:https://blog.51cto.com/kongzi330/287948