第四章 编程练习
注:以下程序中,函数和结构里的内容一般情况尽量缩进。
1.编写一个小程序,如下述输出示例的那样请求并显示信息:
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
注意:该程序应该接受的名字包含多个单词。另外,程序将向下调整成绩,即向上调一个字母,假设用户请求A、B或C,所以不必担心D和F之间的空档。
//编写一个C++程序,如下述输出示例所示的那样请求并显示信息。
#include<iostream>
int main()
{
using namespace std;
const int ARsize=10;
char first_name[ARsize];
char last_name[ARsize];
char grade;
int age;
cout<<"What is your first name? ";
cin.getline(first_name,ARsize);
cout<<"What is your last name? ";
cin.getline(last_name,ARsize);
cout<<"What letter grade do you deserve? ";
cin>>grade;
cout<<"What is your age? ";
cin>>age;
cout<<"Name: "<<last_name<<", "<<first_name<<endl;
cout<<"Grade: "<<char(grade+1)<<"\n";
cout<<"Age: "<<age<<endl;
cin.get();
cin.get();
return 0;
}
2.修改程序清单4.4, 使用C++ string类而不是char数组。
// 修改程序清单4.4,使用C++string类而不是char数组。
#include <iostream>
#include<string>
int main()
{
using namespace std;
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";
cin.get();
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>
int main()
{
using namespace std;
char firstName[20];
char lastName[20];
char fullName[40];
cout<< "Enter your first name: ";
cin.getline(firstName,20);
cout<< "Enter your last name: ";
cin>> lastName;
strcpy(fullName,lastName);
strcat(fullName, ", ");
strcat(fullName, firstName);
//fullName[39] = '\0';
cout<< "Here's the information in a single string: "<< fullName << endl;
cin.get();
cin.get();
return 0;
}
4.编写一个程序,它要求用户首先输入其名,再输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用string对象和头文件string中的函数。下面是该程序运行的情形:
Enter your first name: Flip
Enter your last name: Fleming
Here’s the information in a single string: Fleming, Flip
//和第3题实现的内容是一样的,这次是通过string对象和头文件string中的函数来实现。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string first_name;
string last_name;
string name;
cout<<"Enter your first name: ";
//getline(cin,first_name);
cin>>first_name;
cout<<"Enter your last name: ";
//getline(cin,last_name);
cin>>last_name;
name = last_name + ", " + first_name;
cout<<"Here's the information in a single string: "<<name<<endl;
cin.get();
cin.get();
return 0;
}
5.结构CandyBar包含3个成员。第一个成员存储了糖块的品牌;第二个成员存储糖块的重量(可以有小数);第三个成员存储了糖块的卡路里含量(整数)。请编写一个程序,声明这个结构,创建一个名为snack的CandyBar变量,并将其成员分别初始化为"Mocha Munch"、2.3 和350。初始化应在声明snack时进行。最后,程序显示snack变量的内容。
//结构:存储三个成员(品牌、重量、卡路里),然后结构初始化,最后显示出来.
#include<iostream>
struct CandyBar
{
char brand[20];
float weight;
int caloie;
};
int main()
{
using namespace std;
CandyBar snack =
{
"Mocha Munch",
2.3,
350
};
cout<<"The snack is following: \n";
cout<<"Brand: "<<snack.brand<<endl;
cout<<"Weight: "<<snack.weight<<endl;
cout<<"Caloie: "<<snack.caloie<<endl;
cin.get();
return 0;
}
6.结构CandyBar包含3个成员,如编程练习5所示。请编写一个程序,创建一个包含3个元素的CandyBar数组,并将它们初始化为所选择的值,然后显示每个结构的内容。
//结构CandBar包含三个成员,如习题5所示。请编写一个程序,创建一个包含3个元素的CandyBar数组,
//并将它们初始化为所选择的值,然后显示每个结构的内容。
#include<iostream>
struct CandyBar
{
char brand[20];
float weight;
int caloie;
};
int main()
{
using namespace std;
CandyBar snack[3]=
{
{"Mocha Munch", 2.3, 350},
{"Fruit Salad", 1.6, 140},
{"Fried Chicken", 2.4, 880}
};
cout<<"The snack is following: "<<endl;
cout<<"Brand: "<<snack[0].brand<<", "<<"Weight: "<<snack[0].weight<<", "<<"Caloie: "<<snack[0].caloie<<" ;\n";
cout<<"Brand: "<<snack[1].brand<<", "<<"Weight: "<<snack[1].weight<<", "<<"Caloie: "<<snack[1].caloie<<" ;\n";
cout<<"Brand: "<<snack[2].brand<<", "<<"Weight: "<<snack[2].weight<<", "<<"Caloie: "<<snack[2].caloie<<" ;\n";
cin.get();
return 0;
}
7.William Wingate 从事披萨饼分析服务。对于每个披萨饼,他都需要记录下列信息:
(1)披萨饼公司的名称,可以有多个单词组成。
(2)披萨饼的直径。
(3)披萨饼的重量。
请设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。程序将请求用户输入上述信息,然后显示这些信息。请使用cin(或它的方法)和cout。
//设计一个存储比萨饼信息(公司名称、直径、重量)的结构,并编写一个使用这种结构变量的程序。
//程序将请求用户输入上述信息,然后显示这些信息。
//请使用cin或它的方法和cout。
#include<iostream>
#include<string>
using namespace std;
struct William
{
string brand;
float diameter;
float weight;
};
int main()
{
William recording;
cout<<"Please enter your pizza's information: "<<endl;
cout<<"Brand: ";
getline(cin,recording.brand);
cout<<"Diameter: ";
cin>>recording.diameter;
cout<<"Weight: ";
cin>>recording.weight;
cout<<"So the following is your pizza's information: "<<endl;
cout<<"Brand: "<<recording.brand<<endl;
cout<<"Diameter: "<<recording.diameter<<" cm"<<endl;
cout<<"Weight: "<<recording.weight<<" kg"<<endl;
cin.get();
cin.get();
return 0;
}
8.完成编程练习7,但使用new来为结构分配内存,而不是声明一个结构变量。另外,让程序在请求输入比萨饼公司名称之前输入披萨饼的直径。
//完成习题7,但使用new来为结构分配内存,而不是声明一个结构变量的程序。
//让程序在请求输入比萨公司名称之前输入比萨的直径。
#include<iostream>
#include<string>
using namespace std;
struct William
{
string brand;
float diameter;
float weight;
};
int main()
{
William *recording = new William;
cout<<"Please enter your pizza's information: "<<endl;
cout<<"Diameter: ";
cin>>recording->diameter;
cin.get();
cout<<"Brand: ";
getline(cin,(*recording).brand);
cout<<"Weight: ";
cin>>recording->weight;
cout<<"So the following is your pizza's information: "<<endl;
cout<<"Brand: "<<(*recording).brand<<endl;
cout<<"Diameter: "<<recording->diameter<<" cm"<<endl;
cout<<"Weight: "<<recording->weight<<" kg"<<endl;
delete recording;
cin.get();
cin.get();
return 0;
}
9.完成编程练习6,但使用new来动态分配数组,而不是声明一个包含3个元素的CandyBar数组。
//完成编程练习6,但使用new来动态分配数组,而不是声明一个包含3个元素的CandyBar数组。
#include<iostream>
#include<string>
using namespace std;
struct CandyBar
{
string brand;
float weight;
int calorie;
};
int main()
{
CandyBar* sugar = new CandyBar[3];
sugar[0].brand ="Mocha Munch";
sugar[0].weight=2.3;
sugar[0].calorie = 350;
sugar[1].brand ="Fruit Salad";
sugar[1].weight=5.0;
sugar[1].calorie =400;
sugar[2].brand ="Fried Chicken";
sugar[2].weight=3.5;
sugar[2].calorie =200;
//sugar[0]={"Mocha Munch",2.3,350};
//sugar[1]={"Fruit Salad",5.0,400}; // 不能这么初始化,编译会报错!!!
cout<<"The first brand is "<<sugar[0].brand<<endl;
cout<<"The first weight is "<<sugar[0].weight<<endl;
cout<<"The first calorie is "<<sugar[0].calorie<<endl;
cout<<"The second brand is "<<sugar[1].brand<<endl;
cout<<"The second weight is "<<sugar[1].weight<<endl;
cout<<"The second calorie is "<<sugar[1].calorie<<endl;
cout<<"The third brand is "<<sugar[2].brand<<endl;
cout<<"The third weight is "<<sugar[2].weight<<endl;
cout<<"The third calorie is "<<sugar[2].calorie<<endl;
delete [] sugar;
cin.get();
return 0;
}
10.编写一个程序,让用户输入三次40码跑的成绩(如果您愿意,也可以让用户输入40米跑的成绩),并显示次数和平均成绩,请使用一个array对象来存储数据。
//用户输入三次40码跑的成绩,并显示次数和平均成绩。
//使用array对象来存储数据。
#include<iostream>
#include<array>
int main()
{
using namespace std;
const int num = 3;
array<double,num>time;
double average;
cout<<"请输入第一次40米跑成绩: ";
cin>>time[0];
cout<<"请输入第二次40米跑成绩: ";
cin>>time[1];
cout<<"请输入第三次40米跑成绩: ";
cin>>time[2];
average = (time[0]+time[1]+time[2])/num;
cout<<"第一次成绩为: "<<time[0]<<endl;
cout<<"第二次成绩为: "<<time[1]<<endl;
cout<<"第三次成绩为: "<<time[2]<<endl;
cout<<num<<"次的平均成绩为: "<<average<<endl;
cin.get();
cin.get();
return 0;
}