C++ Primer Plus 第六版 第四章习题
4.13.1
#include<iostream>
using namespace std;
struct info
{
char name_1[20];
char name_2[20];
char grade;
int age;
};
int main()
{
info student1;
cout<<"What is your first name?"<<endl;
cin.getline(student1.name_1,20);
cout<<"What is your last name?"<<endl;
cin.getline(student1.name_2,20);
cout<<"What letter grade do you deserve?"<<endl;
cin>>student1.grade;
cout<<"What is your age?"<<endl;
cin>>student1.age;
cout<<"Name: "<<student1.name_1<<", "<<student1.name_2<<"\n";
cout<<"Grade: "<<++student1.grade<<endl;
cout<<"Age: "<<student1.age<<endl;
return 0;
}
注意: 字符串的输入方式
4.13.2
#include<iostream>
#include<string>
using namespace std;
int main()
{
string Name, Dessert;
cout << "Enter your name: ";
getline(cin, Name);//string类的输入方式
cout << "Enter your favourite dessert: ";
getline(cin, Dessert);
cout << "I have some delicious " << Dessert;
cout << " for you, " << Name << endl;
return 0;
}
注意: string类的输入方式
4.13.3
#include<iostream>
#include <cstring>
using namespace std;
int main()
{
char Name_first[20], Name_last[20], name[40];
cout << "Enter your first name: ";
cin.getline(Name_first, 20);
cout << "Enter your last name: ";
cin.getline(Name_last, 20);
strcat(strcat(strcat(name,Name_first),", "),Name_last);
cout << "Here is the information in a single string: " << name;
return 0;
}
注意: 常用函数:
(1)memcpy:
(2)strcpy:
(3)strncpy:
(4)strcat:
(5)strcmp:
(6)memset:
(7)strlen:
4.13.4
#include<iostream>
using namespace std;
int main()
{
string Name_first, Name_last, name;
cout << "Enter your first name: ";
getline(cin,Name_first);
cout << "Enter your last name: ";
getline(cin,Name_last);
name=Name_first+", "+Name_last;
cout << "Here is the information in a single string: " << name;
return 0;
}
4.13.5
#include<iostream>
using namespace std;
struct CandyBar
{
string brand;
double weight;
int calorie;
};
int main()
{
CandyBar snack={"Mocha Munch",2.3,350};
cout<<"Brand: "<<snack.brand<<"\n"
<<"Weight: "<<snack.weight<<"\n"
<<"Calorie: "<<snack.calorie<<endl;
return 0;
}
4.13.6
#include<iostream>
using namespace std;
struct CandyBar
{
string brand;
double weight;
int calorie;
};
int main()
{
CandyBar snack[3]={{"Mocha Munch",2.3,350},{"Dabaitu",2.5,350},{"Wowo",2.6,350}};
cout<<"Brand: "<<snack[0].brand<<"\n"
<<"Weight: "<<snack[0].weight<<"\n"
<<"Calorie: "<<snack[0].calorie<<endl;
cout<<"Brand: "<<snack[1].brand<<"\n"
<<"Weight: "<<snack[1].weight<<"\n"
<<"Calorie: "<<snack[1].calorie<<endl;
cout<<"Brand: "<<snack[2].brand<<"\n"
<<"Weight: "<<snack[2].weight<<"\n"
<<"Calorie: "<<snack[2].calorie<<endl;
return 0;
}
4.13.7
#include<iostream>
using namespace std;
struct Pizza_info
{
string company;
double diameter;
double weight;
};
int main()
{
Pizza_info Pizza1;
cout<<"Enter the Pizza's company: "<<endl;
getline(cin,Pizza1.company);
cout<<"Enter the Pizza's diameter: "<<endl;
cin>>Pizza1.diameter;
cout<<"Enter the Pizza's weight: "<<endl;
cin>>Pizza1.weight;
cout<<"Company: "<<Pizza1.company<<"\n"
<<"Diameter: "<<Pizza1.diameter<<"\n"
<<"Weight: "<<Pizza1.weight<<endl;
return 0;
}
4.13.8
#include<iostream>
using namespace std;
struct Pizza_info
{
string company;
double diameter;
double weight;
};
int main()
{
Pizza_info* Pizza1 = new Pizza_info;
cout<<"Enter the Pizza's diameter: "<<endl;
cin>>Pizza1->diameter;
cin.get();
cout<<"Enter the Pizza's company: "<<endl;
getline(cin,Pizza1->company);
cout<<"Enter the Pizza's weight: "<<endl;
cin>>Pizza1->weight;
cout<<"Company: "<<Pizza1->company<<"\n"
<<"Diameter: "<<Pizza1->diameter<<"\n"
<<"Weight: "<<Pizza1->weight<<endl;
delete Pizza1;
return 0;
}
注意: cin输入后可以用cin.get()将输入序列中的回车清除。
4.13.9
#include<iostream>
using namespace std;
struct CandyBar
{
string brand;
double weight;
int calorie;
};
int main()
{
CandyBar* snack = new CandyBar[3];
*snack={"Mocha Munch",2.3,350};
*(snack+1)={"Dabaitu",2.5,350};
*(snack+2)={"Wowo",2.6,350};
cout<<"Brand: "<<snack[0].brand<<"\n"
<<"Weight: "<<snack[0].weight<<"\n"
<<"Calorie: "<<snack[0].calorie<<endl;
cout<<"Brand: "<<snack[1].brand<<"\n"
<<"Weight: "<<snack[1].weight<<"\n"
<<"Calorie: "<<snack[1].calorie<<endl;
cout<<"Brand: "<<snack[2].brand<<"\n"
<<"Weight: "<<snack[2].weight<<"\n"
<<"Calorie: "<<snack[2].calorie<<endl;
return 0;
}
注意: 动态数组的表示方法:1.*snack; 2.snack[0]。
4.13.10
#include<iostream>
#include <array>
using namespace std;
int main()
{
array<double,3> grade;
cout<<"PLease enter your first grade in 40m:"<<endl;
cin>>grade[0];
cout<<"PLease enter your second grade in 40m:"<<endl;
cin>>grade[1];
cout<<"PLease enter your third grade in 40m:"<<endl;
cin>>grade[2];
cout<<"Your average grade of 3 testings is "<<(grade[0]+grade[1]+grade[2])/3<<"\n"
<<"First: "<<grade[0]<<"\n"
<<"Second: "<<grade[1]<<"\n"
<<"Third: "<<grade[2]<<endl;
return 0;
}