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

来源:互联网 发布:sony 摄像机 网络直播 编辑:程序博客网 时间:2024/04/30 02:02

初学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;
}

0 0
原创粉丝点击