第四章 复合类型
第一题:按示例请求显示信息。
#include<iostream>
using namespace std;
int main()
{
cout << "What's your first name? ";
char first_name[20];
cin.getline(first_name,19);
cout << "What's your last name? ";
char last_name[20];
cin.getline(last_name, 19);
cout << "What letter grade do you deserve ? ";
char grade;
cin >> grade;
cout << "What is your age ? ";
int age;
cin >> age;
cout << "Name: " << last_name << ',' << first_name << endl;
cout << "Grade: " <<char (grade+1) << endl;
cout << "Age: " << age<<endl;
cin.get();
return 0;
}
第二题:修改程序4.4,使用C++string类
#include<iostream>
#include<string>
using namespace std;
int main()
{
string dessert;
string name;
cout << "Enter your last name: ";
getline(cin,name);
cout << "Enter your favourite dessert: ";
getline(cin, dessert);
cout << "I have some delicious " << dessert;
cout << " for you," << name << ".\n";
cin.get();
return 0;
}
第三题:使用char数组,和cstring中的函数,按示例格式显示输出
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char f_name[20];
char l_name[20];
char name[40] = {0};//初始化为0
cout << "Enter your first name: ";
cin >> f_name;
cout << "Enter your last name: ";
cin >> l_name;
strcat_s(name,l_name);
strcat_s(name, ", ");
strcat_s(name, f_name);
cout << "Here's the imformation in a string: " << name<<endl;
cin.get();
return 0;
}
第四题:使用string对象,和string中的函数,按示例格式显示输出
#include<iostream>
#include<string>
int main(void)
{
using namespace std;
cout << "Enter your first name: ";
string f_name;
cin >> f_name;
cout << "Enter your last name: ";
string l_name;
cin >> l_name;
string name;
name = f_name + ", " + l_name;
cout << "Here's the imformation in a string: "<<name<<endl;
cin.get();
return 0;
}
第五题:编写一个包含三个成员的结构并声明,再创建一个该结构的变量并显示
#include<iostream>
#include<string>
using namespace std;
struct CandyBar { string name; double weight; int calorie; };
int main(void)
{
CandyBar snack{ "Mocha Munch" ,2.3,350};
cout<<snack.name<<" is "<<snack.weight<<"g with " <<snack.calorie<<" calories."<<endl;
cin.get();
return 0;
}
第六题:编写一个包含三个成员的结构并声明,再创建包含3个该结构的数组并初始化,最后显示每个结构的内容
#include<iostream>
#include<string>
using namespace std;
struct CandyBar { string name; double weight; int calorie; };
int main()
{
CandyBar candy[3]={{"One_mocha",2.5,30},{"Two_mocha",3.5,40 },{"Three_mocha",4.5,50}};
cout << candy[0].name << " is " << candy[0].weight;
cout<< "g with " << candy[0].calorie << endl;
cout << candy[1].name << " is " << candy[1].weight;
cout<< "g with " << candy[1].calorie << endl;
cout << candy[2].name << " is " << candy[2].weight;
cout<< "g with " << candy[2].calorie << endl;
cin.get();
return 0;
}
第七题:编写一个包含三个成员的结构并声明,根据用户输入初始化并显示
#include<iostream>
#include<string>
using namespace std;
struct PizzaBar { char name[30]; double diameter; double weight; };
int main(void)
{
PizzaBar WW;
cout << "Enter the name of the company: ";
cin.getline(WW.name, 29);
cout << "Enter the pizza diameter: ";
cin >> WW.diameter;
cout << "Enter the pizza weight: ";
cin >> WW.weight;
cout << WW.name << "'pizza is " << WW.weight << "g with " << WW.diameter << " cm." << endl;
cin.get();
cin.get();
return 0;
}
第八题:使用new完成上一题,根据用户输入初始化并显示
#include<iostream>
#include<string>
using namespace std;
struct PizzaBar { char name[30]; double diameter; double weight; };
int main(void)
{
PizzaBar *p= new PizzaBar;
cout << "Enter the pizza diameter: ";
cin>>p->diameter;
cout << "Enter the name of the company: ";
cin.get(); //取消换行符;
cin.getline(p->name,29);
cout << "Enter the pizza weight: ";
cin >> p->weight;
cout << (*p).name << "'pizza is " << p->weight << "g with " <<p->diameter << " cm." << endl;
cin.get();
cin.get();
return 0;
}
第九题:重新完成第六题,使用new来分配数组,然后初始化并显示
#include<iostream>
using namespace std;
struct CandyBar { char name[30]; double weight; int calorie; };
int main(void)
{
CandyBar *p=new CandyBar[3];
p[0] = {"One_mocha",2.5,30 };
p[1] = {"Two_mocha",3.5,40 };
p[2] = {"Three_mocha",4.5,50 };
cout << p[0].name << " is " << p[0].weight;
cout << "g with " << p[0].calorie << endl;
cout << p[1].name << " is " << p[1].weight;
cout << "g with " << p[1].calorie << endl;
cout << p[2].name << " is " << p[2].weight;
cout << "g with " << p[2].calorie << endl;
cin.get();
return 0;
}
第四章结束。_