c++ primer plus 第六版第二章编程练习答案

初学c++,希望大家在看的时候有问题来告诉我,谢谢~

我用的IDE是vs2013。

2.7.1

#include <cstdlib>
#include <iostream>
int main()
{
using namespace std;
cout << "My name is Liu Wei "
<< endl
<< "And my address is Daqing."
<< endl;
system("pause");
return 0;
}


2.7.2

#include <cstdlib>
#include <iostream>
//码的英文是yard
int main()
{
using namespace std;
int distance, yard;
cout << "Input a long and I'll "
<< "tell you how yard is :";
cin >> distance;
yard = 220 * distance;
cout << "The " << distance << " long is "
<< yard << " yard " << endl;
system("pause");
return 0;
}


2.7.3

#include <iostream>
#include <cstdlib>
using namespace std;
void mice();
void run();
int main()
{
mice();
mice();
run();
run();
system("pause");
return 0;
}
void mice()
{
cout << "Three blind mice." << endl;
}
void run()
{
cout << "See how they run." << endl;
}


2.7.4

#include <iostream>
#include <cstdlib>
int mon(int);
int main()
{
using namespace std;
int year, month;
cout << "Enter your age :";
cin >> year;
month = mon(year);
cout << "The month is: " << month << endl;
system("pause");
return 0;
}
int mon(int year)
{
int month;
month = 12 * year;
return month;
}


2.7.5

//摄氏温度(Celsius [ˈselsiəs] temperature)
//华氏度(Fahrenheit ['færənhaɪt])
//华氏温度 = 摄氏温度 × 1.8 + 32
#include <iostream>
#include <cstdlib>

using namespace std;
int change(int Celsius);

int main()
{
int Celsius, Fahrenheit;
cout << "Please enter a Celsius value: ";
cin >> Celsius;
Fahrenheit = change(Celsius);
cout << Celsius << " degrees Celsius is " << Fahrenheit
<< " degrees Fahrenheit" << endl;
system("pause");
return 0;
}

int change(int Celsius)
{
return Celsius * 1.8 + 32;
}



2.7.6

#include <iostream>
#include <cstdlib>
using namespace std;

double  change(double light_year);
int main()
{
double light_year, astronomical_units;
cout << "Enter the number of light years: ";
cin >> light_year;
astronomical_units = change(light_year);
cout << light_year << " light years = " << astronomical_units
<< " astronomical units. " << endl;
system("pause");
return 0;
}
double change(double light_year)
{
return 63240 * light_year;
}


2.7.7

#include <iostream>
#include <cstdlib>
using namespace std;

void time(int hours, int minutes);
int main()
{
int hours, minutes;
cout << "Enter the number of hours: ";
cin >> hours;
cout << "Enter the number of minutes: ";
cin >> minutes;
time(hours, minutes);
system("pause");
return 0;
}
void time(int hours, int minutes)
{
cout << "Time: " << hours << ":" << minutes << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值