总结
数组,例如:int card[4] = {3, 6, 8, 10};
字符串,例如:string str = “Hello world!”;
C风格字符串是一个特殊的字符数组,以空字符(’\0’)表示字符串的结束。
结构体定义
指针,例如:
int months =12;
int* p = &months;
int* pt = new int[12];
解决cin忽略空格和换行符的问题用getline()和get(),例如
char FirstName[20];
string LastName;
cin.getline(FirstName, 20);
getline(cin, LastName);
cin.get(FirstName, 20);
cin.get();
一、第一题
题目:编写一个C++程序,如下述输出示例所示的那样请求并显示信息:
What is your first name? Beety 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之间的空档。
#include <iostream>
int main() {
using std::cout;
using std::cin;
const int ArSize = 15;
char FirstName[ArSize];
char LastName[ArSize];
char Grade;
int Age;
cout << "what is your first name? ";
cin.getline(FirstName, 15);
cout << "what is your last name? ";
cin.getline(LastName, 15);
cout << "what letter grade do you deserve? ";
cin >> Grade;
cout << "what is your age? ";
cin >> Age;
std::cout << "name: " << LastName << ", " << FirstName << std::endl;
cout << "Grade: " << char(Grade + 1) << std::endl;
cout << "Age: " << Age << std::endl;
return 0;
}
二、第二题
题目:修改程序清单4.4,使用C++ string类而不是char数组。
#include <iostream>
#include <string>
int main() {
using namespace std;
std::string name;
std::string dessert;
cout << "Enter your name:\n";
getline(cin, name);
cout << "Enter your favority dessert:\n";
getline(cin, dessert);
cout << "I have some delicious " << dessert;
cout << " for you, " << name << ".\n";
return 0;
}
三、第三题
题目:编写一个程序,它要求用户首先输入其名,然后输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用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 std::cout;
using std::cin;
const int ArSize = 30;
char FirstName[ArSize];
char LastName[ArSize];
char TotalName[ArSize*2];
cout << "Enter your first name: ";
cin.getline(FirstName, ArSize);
cout << "Enter your last name: ";
cin.getline(LastName, ArSize);
strcpy_s(TotalName, LastName);
strcat_s(TotalName, ", ");
strcat_s(TotalName, FirstName);
cout << "Here's the information: " << TotalName;
return 0;
}
四、第四题
题目:编写一个程序,它要求用户首先输入其名,再输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用string对象和头文件string中的函数。下面是该程序运行时的情形:
Enter your first name: Flip
Enter your last name: Fleming
Here’s the information in a single string: Fleming, Flip
#include <iostream>
#include <string>
int main() {
using std::cout;
using std::cin;
using std::string;
string FirstName;
string LastName;
string TotalName;
cout << "Enter your first name: ";
getline(cin, FirstName);
cout << "Enter your last name: ";
getline(cin, LastName);
TotalName = LastName + ", " + FirstName;
cout << "Here's the information: " << TotalName;
return 0;
}
五、第五题
题目:结构CandyBar包含3个成员。第一个成员存储了糖块的品牌;第二个成员存储糖块的重量(可以有小数);第三个成员存储了糖块的卡路里含量(整数)。请编写一个程序,声明这个结构,创建一个名为snack的CandyBar变量,并将其成员分别初始化为“Mocha Munch”、2.3和350。初始化应在声明snack时进行。最后,程序显示snack变量的内容。
#include <iostream>
struct CandyBar
{
std::string Brand;
float Weight;
int Calorie;
};
int main() {
CandyBar snack = {"Mocha Munch", 2.3, 350};
std::cout << snack.Brand << std::endl;
std::cout << snack.Weight << std::endl;
std::cout << snack.Calorie << std::endl;
return 0;
}
六、第六题
题目:结构CandyBar包含3个成员,如编程练习5所示。请编写一个程序,创建一个包含3个元素的CandyBar数组,并将它们初始化为所选择的值,然后显示每个结构的内容。
#include <iostream>
struct CandyBar
{
std::string Brand;
float Weight;
int Calorie;
};
int main() {
CandyBar snack[3] = {
{"Aocha Aunch", 2.3, 350},
{"Bocha Bunch", 4.56, 123},
{"Cocha Cunch", 7.8910, 6778}
};
std::cout << snack[0].Brand << " " << snack[0].Weight << " " << snack[0].Calorie << std::endl;
std::cout << snack[1].Brand << " " << snack[1].Weight << " " << snack[1].Calorie << std::endl;
std::cout << snack[2].Brand << " " << snack[2].Weight << " " << snack[2].Calorie << std::endl;
return 0;
}
七、第七题
题目:William Wingate从事比萨饼分析服务。对于每个披萨饼,他都需要记录下列信息:
(1)披萨饼公司的名称,可以有多个单词组成
(2)披萨饼的直径
(3)披萨饼的质量
请设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。程序将请求用户输入上述信息,然后显示这些信息。请使用cin和cout。
#include <iostream>
#include <string>
struct pizza
{
char company[40];
float height;
float weight;
};
int main() {
using std::cout;
using std::cin;
pizza pizza;
cout << "please input company: ";
cin.getline(pizza.company, 40);
cout << "please input height: ";
cin >> pizza.height;
cout << "please input weight: ";
cin >> pizza.weight;
cout << pizza.company << " " << pizza.height << " " << pizza.weight;
return 0;
}
八、第八题
题目:完成编程练习7,但使用new来为结构分配内存,而不是声明一个结构变量。另外,让程序在请求输入比萨饼公司名称之前输入比萨饼的直径。
#include <iostream>
#include <string>
struct Pizza
{
char company[40];
float height;
float weight;
};
int main() {
using std::cout;
using std::cin;
Pizza* pizza = new Pizza;
cout << "please input company: ";
cin.getline(pizza->company, 40);
cout << "please input height: ";
cin >> pizza->height;
cout << "please input weight: ";
cin >> pizza->weight;
cout << pizza->company << " " << pizza->height << " " << pizza->weight;
return 0;
}
九、第九题
题目:完成编程练习6,但是用new来动态分配数组,而不是声明一个包含3个元素的CandyBar数组。
#include <iostream>
#include <string>
struct Pizza
{
std::string company;
double height;
double weight;
};
int main() {
using std::cout;
using std::cin;
Pizza* pizza = new Pizza[3];
for (int i = 0; i < 3; i++) {
cout << "please input company: ";
getline(cin, pizza[i].company);
cout << "please input height: ";
cin >> pizza[i].height;
cout << "please input weight: ";
cin >> pizza[i].weight;
cin.get();
}
for (int i = 0; i < 3; i++) {
cout << (pizza + i)->company << " " << (pizza + i)->height << " " << (pizza + i)->weight << std::endl;
cout << pizza[i].company << " " << pizza[i].height << " " << pizza[i].weight << std::endl;
cout << std::endl;
}
return 0;
}
十、第十题
题目:编写一个程序,让用户输入三次40码跑的成绩(如果您愿意,也可让用户输入40米跑的成绩)。并显示次数和平均成绩。请使用一个array对象来存储数据(如果编译器不支持array类,请使用数组)。
#include <iostream>
#include <array>
int main() {
using std::cin;
std::array<float, 3>runtime;
float average;
cin >> runtime[0]>>runtime[1]>>runtime[2];
std::cout << runtime[0] << std::endl;
std::cout << runtime[1] << std::endl;
std::cout << runtime[2] << std::endl;
average = (runtime[0] + runtime[1] + runtime[2]) / 3;
std::cout << average;
return 0;
}
谢谢观看,祝顺利。