c++ primer plus(第6版)中文版 第二章编程练习答案

第二章编程联练习答案:

2.1编写一个c++程序,它显示您的姓名和住址

// 2.1编写一个c++程序,它显示您的姓名和住址
#include <iostream>
using namespace std;

int main()
{
	cout << "My name is dongge" << endl;
	cout << "My address is China"<< endl;
}

2.2编写一个c++程序,它要求用户输入一个以long为单位的距离,然后把它转换为码(1 long=220码)

// 2.2编写一个c++程序,它要求用户输入一个以long为单位的距离,然后把它转换为码(1 long=220码)
#include <iostream>
using namespace std;

int main()
{
	int n;
	cout<<"输入一个以long为单位的距离:";
	cin>> n;
	cout<< "转换之后为:" << n*220 << "码" << endl; 
	return 0;      
}


2.3编写一个c++程序,它使用3个用户定义的函数,并生成输出

//2.3编写一个c++程序,它使用3个用户定义的函数,并生成输出
#include <iostream>
using namespace std;
void myFuction1()
{
	cout<<"Three blind mice"<<endl;
}
void myFuction2()
{
	cout<<"see how they run"<<endl; 
}

int main()
{
    myFuction1(); 
    myFuction1(); 
    myFuction2(); 
    myFuction2();
    return 0;   
}

2.4 编写一个程序,让用户输入其年龄,然后显示包含多少个月

//2.4 编写一个程序,让用户输入其年龄,然后显示包含多少个月
#include <iostream>
using namespace std;

int main()
{
	 int age;
     cout << "请输入你的年龄:";
     cin >> age;
     cout << "包含" << age*12 << "个月" << endl;
}


2.5输入摄氏温度值,显示华氏温度=1.8*摄氏温度+32.0


//2.5输入摄氏温度值,显示华氏温度=1.8*摄氏温度+32.0
#include <iostream>
using namespace std;

double change(double n)
{ 
     return n*1.8+32.0;       
}
int main()
{
     int n;
     cout << "please enter a Celsius valus:";
     cin >> n;
     cout << "20 degrees Celsius is " << change(n) <<" degrees Fashrenheit" << endl;
}


2.6输入摄光年,显示天文单位(1光年=63240天文单位)

//2.6输入摄光年,显示天文单位(1光年=63240天文单位)
#include <iostream>
using namespace std;

double change(double n)
{ 
	return n*63240;    
}
int main()
{
     double n;
     cout << "Enter the number of light years:";
     cin >> n;
     cout << n << " light years = " << change(n) << " astronmical units" << endl;
}

2.7编写一个程序,输入小时和分钟,显示时间

//2.7编写一个程序,输入小时和分钟,显示时间
#include <iostream>
using namespace std;

void show(int h,int m)
{
	cout << "Time: " << h << ":" << m << endl;
}

int main()
{
	int h,m;
	cout << "Enter the number of hours:";
	cin >> h;
	cout << "Enter the number of minutes:";
	cin >> m;
	show(h,m);
}


  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值