C++Primer Plus的第二章课后习题
还算简单,仅仅做个记录
/*
1、c++程序的模块:
书上没找到这个回答,网络搜索C++20有模块,找书上的练习题附录答案要连环加微信(异步图书恶心/大便)。
2、 #include<iostream>的作用
#include是预处理指令,程序会使用预处理器将iostream文件的内容添加到程序中。具体来说,在源代码被编译前,替换或添加文本。
3、using namespace std;的作用
using编译指令,使iostream中的定义对程序可用。namespace将产品封装在一个namespace单元中,可以用名称空间的名称来指出使用的产品。
4、使用cout<<"Hello world"<<endl;打印短语"Hello world"并开启新的一行。
5、int cheeses;创建名为cheeses的整数变量
6、cheeses=32;赋值32给变量cheeses
7、cin>>cheeses;从键盘输入值给cheeses
8、cout<<"We have "<<cheeses<<" varieties of cheese,"为对应打印
9、函数的返回值类型、函数名、参数类型、参数名
10、在定义函数时,若返回值类型设为void时,不必使用关键字return
11、因为未正确引用<iostream>库,(1)#include <iostream>(2)using namespace std;(3)std::cout<<"";
*/
#include<iostream>
//#include<string>
using namespace std;
void func_1() {
cout << "以下为练习题一的内容。"<<endl;
string myName = "XXX";
string myAdress = "";
cout << "我的名字是: " << myName << endl;
cout << "我的地址是: " << myAdress << endl;
}
void func_2() {
cout << "以下为练习题2的内容。" << endl;
double longNum;
cout << "输入一个以long为单位的距离:" << endl;
cin >> longNum;
cout << endl;
cout << longNum << "long对应 " << longNum / 220 << "码。";
}
void func_3_1() {
cout << "Three blind mice" << endl;
}
void func_3_2() {
cout << "See how they run" << endl;
}
void func_4() {
cout << "以下为练习题4的内容。" << endl;
int age;
cout << "Enter yourage: ";
cin >> age;
cout << "Your age in months is " << 12 * age << endl;;
}
void func_5() {
cout << "以下为练习题5的内容。" << endl;
int cel;
cout << "Please enter a Celsius value:";
cin >> cel;
cout << endl;
cout << cel << " degrees Celsius is "<<1.8*cel+32<< "degrees Fahrenheit." << endl;
}
void func_6() {
cout << "以下为练习题6的内容。" << endl;
double lightYear;
cout << "Enter the number of light years:";
cin >> lightYear;
cout << endl;
cout << lightYear << " light years " <<63240*lightYear<< " astronomical units." << endl;
}
void func_7(int h,int m) {
cout << "以下为练习题7的内容。" << endl;
cout << "Time: "<<h<<":"<<m << endl;
}
void main() {
//func_1();
//func_2();
////3:
//func_3_1(); func_3_1(); func_3_2(); func_3_2();
func_4();
func_5();
func_6();
//7:
int h;
int m;
cout << "Enter the number of hours :";
cin >> h;
cout << endl;
cout << "Enter the number of minutes :";
cin >> m;
cout << endl;
func_7(h, m);
}
浙公网安备 33010602011771号