記錄一下自己看書做題的代碼,也記錄一下學習遇到的問題。代碼都是自己寫的,有錯誤請指出。
1.
// 4_13_1.cpp -- exercise 4.13.1
#include <iostream>
int main()
{
using namespace std;
cout << "What's your first name? ";
char fstname[20];
cin.getline(fstname, 20);
cout << "What's your last name? ";
string lstname;
cin >> lstname;
cout << "What letter grade do you deserve? ";
char grade;
cin >> grade;
cout << "What's your age? ";
int age;
cin >> age;
cout << "Name: " << lstname << "," << fstname << endl;
cout << "Grade: " << char(grade + 1) << endl;
cout << "Age: " << age << endl;
return 0;
}
2.
//4_13_2.cpp --4.13.2 exercise
#include<iostream>
int main()
{
using namespace std;
string name;
string desert;
cout << "Enter your name:\n";
getline(cin, name);
cout << "Enter your favorite dessert:\n";
getline(cin, desert);
cout << "I have some delicious " << desert;
cout << " for you, " << name << endl;
return 0;
}
以上兩道題一個考點是ASCII碼與對應的字符串的轉換,還有就是cin.getline及getline函數的應用。
3.
// 4_13_3.cpp -- 4.13.3 exercise
#include<iostream>
int main()
{
using namespace std;
cout << "Enter your first name: ";
char fstname[20];
cin >> fstname;
cout << "Enter your last name: ";
char lstname[20];
cin >> lstname;
cout << "Here's the information in a single string: " << lstname << ", " << fstname;
return 0;
}
這道題考了字符串的拼接。
4.
// 4_13_4.cpp -- exercise 4.13.4
#include <iostream>
#include <string>
int main()
{
using namespace std;
cout << "Enter your first name: ";
string fstname;
getline(cin, fstname);
cout << "Enter your last name: ";
string lstname;
getline(cin, lstname);
cout << "Here's the information in a single string: " << lstname + ", " + fstname << endl;
return 0;
}
考察了string類型的拼接,相比於char來說方便很多。
5.
// 4_13_5.cpp -- exercise 4.13.5
#include <iostream>
int main()
{
using namespace std;
struct CandyBar
{
string name;
float weight;
int Calorie;
};
CandyBar snacks =
{
"Mocha Munch",
2.3,
350
};
cout << "The name of this snack is " << snacks.name << "." << endl;
cout << "The weight of it is " << snacks.weight << endl;
cout << "The Calorie of it is " << snacks.Calorie << endl;
return 0;
}
考察瞭如何創建struct。
6.
// 4_13_6.cpp -- exercise 4.13.6
#include <iostream>
int main()
{
using namespace std;
struct CandyBar
{
string name;
float weights;
int calorie;
};
CandyBar snacks[3] =
{
{"Barone", 2.0, 350},
{"Bartwo", 2.1, 360},
{"Barthree", 2.2, 370}
};
cout << "Names of snacks are:" << snacks[0].name << " " << snacks[1].name << " "
<< snacks[2].name;
}
考點:創建結構數組。
7.
// 4_13_7.cpp -- exercise 4.13.7
#include <iostream>
int main()
{
using namespace std;
struct Pizza_info
{
string name;
int diameter;
float weights;
};
Pizza_info cus_pizza;
cout << "Please tell me the brand of your pizza: ";
getline(cin, cus_pizza.name);
cout << "Please chose the diameter of your pizza: ";
cin >> cus_pizza.diameter;
cout << "Please chose the weight of your pizza: ";
cin >> cus_pizza.weights;
cout << "Okay,your pizza is " << cus_pizza.name
<< " and the diameter of it is " << cus_pizza.diameter
<< ".The pizza's weight is " << cus_pizza.weights << "." << endl;
return 0;
}
考點,創建一個結構變量,然後再通過訪問結構的成員來賦值。
8.
// 4_13_8.cpp -- exercise 4.13.8
#include <iostream>
#include <string>
struct pizza_info
{
std::string name;
int diameter;
float weights;
};
int main()
{
using namespace std;
pizza_info *ps = new pizza_info;
cout << "How big of the pizza do you want?" << endl;
cin >> (*ps).diameter;
cin.get(); // very important! C++ Primer Plus page 81!!!
cout << "Okay, which brand are you prefer?"<< endl;
getline(cin, ps->name);
cout << "And tell me the weight of the pizza." << endl;
cin >> ps->weights;
cout << "Your pizza is " << (*ps).name << " and the diameter of it is "
<< ps->diameter << " and it weights " << ps->weights << endl;
return 0;
}
這道題之所以標紅,是因爲我在先輸入披薩的尺寸後,沒等我輸入名稱就直接跳到輸入重量那裏了。最後認真讀書之後發現再C++PP第六版81頁有寫,cin在讀取用戶輸入的數字時,將回車生成的換行符留在輸入隊列中。因此加入一句cin.get(),跳過這個換行符就好了。
9.
//4_13_9.cpp -- exercise 4.13.9
#include<iostream>
struct CandyBar
{
std::string name;
float weights;
int calorie;
};
int main()
{
using namespace std;
CandyBar *ps = new CandyBar[3]
{
{"One", 3.5, 20},
{"Two", 3.6, 21},
{"Three", 3.7, 22},
};
cout << "Names of snacks are:" << ps[0].name << " " << ps[1].name << " "
<< ps[2].name << endl;
cout << "Weights of snacks are:" << ps[0].weights << " " << ps[1].weights << " "
<< ps[2].weights << endl;
cout << "Calorie of snacks are:" << ps[0].calorie << " " << ps[1].calorie << " "
<< ps[2].calorie << endl;
}
考察用new來動態分配結構數組。(自己做的很不熟練)
10.
// 4_13_10.cpp -- exercise 4.13.10
#include <iostream>
#include <array>
int main()
{
using namespace std;
array<double, 3> run_grade;
cout << "Please enter your 1st time of 100m running:" << endl;
double fst_time;
cin >> fst_time;
run_grade[0] = fst_time;
cout << "Please enter your 2ed time of 100m running:" << endl;
double snd_time;
cin >> snd_time;
run_grade[1] = snd_time;
cout << "Please enter your 3rd time of 100m running:" << endl;
double trd_time;
cin >> trd_time;
run_grade[2] = trd_time;
double avg_time;
avg_time = (fst_time + snd_time + trd_time) / 3;
cout << "After 3 times running,your average time is:" << avg_time << endl;
return 0;
}
考察array的創建,賦值以及訪問。