C++PrimerPlus 第二章 开始学习C++(编程练习含答案)
#include<iostream>
using namespace std;
int main() {
cout << "name:" << "Hank" << endl;
cout << "Address:" << "Shanghai" << endl;
return 0;
}
#include<iostream>
using namespace std;
int main() {
cout << "请输入一个以long为单位的距离:" ;
double dis1;
cin >> dis1;
double dis2 = dis1 * 220;
cout << dis1 << "long单位距离为" << dis2 << "码距离" << endl;
return 0;
}
#include<iostream>
using namespace std;
void Print_1();
void Print_2();
int main() {
Print_1();
Print_1();
Print_2();
Print_2();
return 0;
}
void Print_1() {
cout << "Three blind mice" << endl;
}
void Print_2() {
cout << "See how they run" << endl;
}
#include<iostream>
using namespace std;
int main() {
cout << "请输入您的年龄:";
int age;
cin >> age;
cout << "您的年龄包含:" << age * 12 << "个月" << endl;
return 0;
}
#include<iostream>
using namespace std;
double CelsiusToFahreheit(double celsius);
int main() {
cout << "Please enter a Celsius value: ";
double celsius;
cin >> celsius;
double fahrenheit;
fahrenheit = CelsiusToFahreheit(celsius);
cout << celsius << " degrees Celsius is " << fahrenheit << " degrees Fahrenheit." << endl;
return 0;
}
double CelsiusToFahreheit(double celsius) {
return celsius * 1.8 + 32.0;
}
#include <iostream>
using namespace std;
double LightToUnit(double lightYears);
int main()
{
cout << "Enter the number of light years:";
double lightYears;
cin >> lightYears;
double lightUnit;
lightUnit = LightToUnit(lightYears);
cout << lightYears << " light years = " << lightUnit << " astronomical units." << endl;
return 0;
}
double LightToUnit(double lightYears) {
return 63240 * lightYears;
}
#include <iostream>
using namespace std;
void display(int hours, int minutes);
int main()
{
cout << "Enter the number of hours: ";
int hours;
cin >> hours;
cout << "Enter the number ouf minutes: ";
int minutes;
cin >> minutes;
display(hours, minutes);
return 0;
}
void display(int hours, int minutes)
{
cout << "Time: " << hours << ":" << minutes << endl;
}