第四章 复合类型
1,编写一个C++程序,输出如下:
What is your first name? Betty Sue
What is your last name? Yewe
What letter grade do you deserve? B
What is your age? 22
Name: Yewe, Betty Sue
Grade: C
Age: 22
程序将向下调整程序
#include<iostream>
uisng namespace std;
int main()
{
char first_name[20],last_name[20];
char grade;
int age;
cout<<"What is your first name?";
cin>>getline(first_name,20);
cout<<"What is your last name?";
cin>>getline(last_name,20);
cout<<"What letter grade do you deserve?";
cin>>grade;
cout<<"What is your age?";
cin>>age;
cout<<"Name: "<<first_name<<" , "<<last_name<<endl;
cout<<"Grade: "<<char(grade+1)<<endl;
cout<<"Age: "<<age<<endl;
return 0;
}
2,修改程序清单4.4,使用C++string类而不是char数组。
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main()
{
string name;
string dessert;
cout<<"Enter your name:\n";
getline(cin,name);
cout<<"Enter your favorite dessert:\n";
getline(cin,dessert);
cout<<" I have some delicious "<<dessert;
cout<<" for you, "<<name<<".\n";
return 0;
}
3,编写一个程序,它要求用户输入其名,然后输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示结果。请使用char数组和头文件cstring中的函数。下面是该函数运行时的情形:
Enter your first name: Flip
Enter your last name:Fleming
Here‘s the information in a single string :Fleming,Flip
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char first_name[20],last_name[20];
char name[40]; //声明一个变量储存合并在一起的姓名
cout<<"Enter your first name:";
cin.getline(first_name,20);
cout<<"Enter your last name:";
cin.getline(last_name,20);
strcpy(name,first_name);
strcat(name,", ");
strcat(name,last_name); //把名和姓结合在一起
cout<<"Here's the infoormation in a single string:"<<name<<endl;
return 0;
}
4,编写一个程序,它要求用户输入其名,然后输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示结果。请使用string对象和头文件cstring中的函数。下面是该函数运行时的情形:
Enter your first name: Flip
Enter your last name:Fleming
Here‘s the information in a single string :Fleming,Flip
#include<iostream>
#include<string>
using namespace std;
int main()
{
string first_name,last_name;
string name;
cout<<"Enter your first name:";
getline(cin,first_name);
cout<<"Enter your last name:";
getline(cin,last_name);
name = first_name+", "+last_name;
cout<<"Here's the infoormation in a single string:"<<name<<endl;
return 0;
}
5,结构CandyBar包含3个成员。第一个成员存储来糖块的品牌;第二个成员存储糖块的重量(可以有小数);第三个成员存储了糖块的卡路里含量(整数)。请编写一个程序,声明这个结构,构建一个名为snack的CandyBar变量,并将其成员分别初始化为“Mocha Munch”,2.3和350.初始化应在声明snack时进行。最后,程序显示snack变量的内容。
#include<iostream>
using namespace std;
struct CandyBar{
char name[20];
float weight;
int calorie;
}; //声明结构
int main()
{
CandyBar snack = {"Mocha Munch", 2.3, 350};//初始化
cout<<"Name:"<<snack.name<<endl;
cout<<"Weight:"<<snack.weight<<endl;
cout<<"Calorie:"<<snack.calorie<<endl;
return 0;
}
6,结构CandyBar包含3个成员,如练习五所示。请编写一个程序,创建一个包含3个元素的CandyBar数组,并将它们初始化为所选择的值,然后显示每个结构的内容。
#include<iostream>
using namespace std;
struct CandyBar{
char name[20];
float weight;
int calorie;
};
int main()
{
CandyBar snack[3] = {{"Mocha Munch", 2.3, 350},{"Sweet Candy", 3.4 ,400},{"Sunday Sweet", 2.4, 250}};
//初始化数组
cout<<"The first shop:"<<endl;
cout<<"Name:"<<snack[0].name<<endl;
cout<<"Weight:"<<snack[0].weight<<endl;
cout<<"Calorie:"<<snack[0].calorie<<endl; //输出第一家店
cout<<"The second shop:"<<endl;
cout<<"Name:"<<snack[1].name<<endl;
cout<<"Weight:"<<snack[1].weight<<endl;
cout<<"Calorie:"<<snack[1].calorie<<endl; //输出第二家店
cout<<"The third shop:"<<endl;
cout<<"Name:"<<snack[2].name<<endl;
cout<<"Weight:"<<snack[2].weight<<endl;
cout<<"Calorie:"<<snack[2].calorie<<endl; //输出第三家店
return 0;
}
7,William Wingate 从事比萨饼分析服务。对于每个披萨饼,他都需要记录下列信息:
披萨饼公司的名称,可以有多个单词组成。
披萨饼的直径。
披萨饼的重量。
请设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。程序将请求用户输入上述信息,然后显示这些信息。请使用cin(或它的方法)和cout。
#include<iostream>
#include<string>
using namespace std;
struct Sell_pizza{
char name[40];
float diameter;
float weight;
};
int main()
{
Sell_pizza take_out;
cout<<"Please place your order"<<endl;
cout<<"The name of the restaurant:";
cin.getline(take_out.name,40);
cout<<"The size of pizza:";
cin>>take_out.diameter;
cout<<"The weight of pizza:";
cin>>take_out.weight; //输入订单
cout<<"Here is your order:"<<endl;
cout<<"Name:"<<take_out.name<<endl;
cout<<"Diameter"<<take_out.diameter<<endl;
cout<<"Weight:"<<take_out.weight<<endl;//输出订单
return 0;
}