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
注意,该程序应该接受的名字包含多个单词。另外,程序将向下调整成绩,即向上调一个字母。假设用户请求A、B或C,所以不必担心D和F之间的空档。
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "What is your first name? " ;
string FirstName;
getline(cin,FirstName);
cout << "What is your last name? ";
string LastName;
getline(cin,LastName);
cout << "What letter grade do you deserve? ";
char Grade;
cin >> Grade;
cout << "what is your age? ";
int age;
cin >> age;
cout << "Name: " << LastName << "," << FirstName << endl;
cout << "Grade: " << char(Grade+1) << endl;
cout << "age: " << age << endl;
return 0;
}
2.修改程序清单4.4,使用C++ string类而不是char数组。
#include <iostream>
#include <string>
int main()
{
using namespace std;
// const int ArSize = 20;
// char name[ArSize];
// char dessert[ArSize];
string name , dessert;
cout << "Enter your name:\n";
// cin.getline(name,ArSize);
getline(cin,name);
cout << "Enter your favorite dessert:\n";
// cin.getline(dessert,ArSize);
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>
int main()
{
using namespace std;
const int ArSize = 20;
char name1[ArSize],name2[ArSize];
cout << "Enter your first name:" ;
cin.getline(name1,ArSize);
cout << "Enter your last name:";
cin.getline(name2,ArSize);
string name;
name = name2;
name = name + ", " + name1;
cout << "Here's the information in a single string:" << name << endl;
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
#include <iostream>
#include <cstring>
int main()
{
using namespace std;
string name1,name2;
cout << "Enter your first name:" ;
getline(cin,name1);
cout << "Enter your last name:";
getline(cin,name2);
name2 += ", " + name1;
cout << "Here's the information in a single string:" << name2 << endl;
return 0;
}
5.结构 CandyBar包含3个成员。第一个成员存储了糖块的品牌;第二个成员存储糖块的重量(可以有小数);第三个成员存储了糖块的卡路里含量(整数).请编写一个程序,声明这个结构,创建一个名为snack的 CandyBar变量,并将其成员分别初始化为“ Mocha munch”、2.3和350.初始化应在声明 snack时进行。最后,程序显示 snack变量的内容。
#include <iostream>
#include <string>
struct CandyBar
{
std::string PinPai;
double Weight;
int KaLuLi;
};
int main()
{
CandyBar snack = {"Mocha munch", 2.3, 350};
std::cout <<"品牌:"<< snack.PinPai <<std::endl;
std::cout <<"重量:"<< snack.Weight <<std::endl;
std::cout << "卡路里:"<< snack.KaLuLi << std::endl;
return 0;
}
6.结构 CandyBar包含3个成员,如编程练习5所示。请编写一个程序,创建一个包含3个元素的CandyBar数组,并将它们初始化为所选择的值,然后显示每个结构的内容。
#include <iostream>
#include <string>
struct CandyBar
{
std::string PinPai;
double Weight;
int KaLuLi;
};
int main()
{
CandyBar snack[3] = {{"Mocha munch", 2.3, 350},
{"ping Guo", 4.2, 120},
{"xiang jiao", 5.5, 130}};
for(int i = 0; i != 3; i++)
{
std::cout <<"品牌:"<< snack[i].PinPai <<std::endl;
std::cout <<"重量:"<< snack[i].Weight <<std::endl;
std::cout << "卡路里:"<< snack[i].KaLuLi << std::endl;
}
return 0;
}
- William Wingate从事比萨饼分析服务。对于每个披萨饼,他都需要记录下列信息:
●披萨饼公司的名称,可以有多个单词组成。
●披萨饼的直径
●披萨饼的重量。
请设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。程序将请求用户输入上述信息,然后显示这些信息。请使用cin(或它的方法)和cout。
#include <iostream>
#include <cstring>
struct PiSa
{
std::string Company;
double ZhiJing;
double ZhongLiang;
};
int main()
{
using namespace std;
PiSa PS;
cout << "请输入公司的名字:";
cin >> PS.Company;
cout << "请输入披萨饼的直径:";
cin >> PS.ZhiJing;
cout << "请输入披萨饼的重量:";
cin >> PS.ZhongLiang;
cout << "公司的名字:"<<PS.Company << endl;
cout << "披萨饼的直径:"<< PS.ZhiJing << endl;
cout << "披萨饼的重量:"<< PS.ZhongLiang << endl;
return 0;
}
8.完成编程练习7,但使用new来为结构分配内存,而不是声明一个结构变量。另外,让程序在请求输入比萨饼公司名称之前输入比萨饼的直径。
#include <iostream>
#include <cstring>
struct PiSa
{
std::string Company;
double ZhiJing;
double ZhongLiang;
};
int main()
{
using namespace std;
PiSa * PS = new PiSa;
cout << "请输入披萨饼的直径:";
cin >> PS->ZhiJing;
cout << "请输入公司的名字:";
cin >> PS->Company;
cout << "请输入披萨饼的重量:";
cin >> PS->ZhongLiang;
cout << "公司的名字:"<<PS->Company << endl;
cout << "披萨饼的直径:"<< PS->ZhiJing << endl;
cout << "披萨饼的重量:"<< PS->ZhongLiang << endl;
delete PS;
return 0;
}
9.完成编程练习6,但使用new来动态分配数组,而不是声明一个包含3个元素的 CandyBar数组。
#include <iostream>
#include <string>
struct CandyBar
{
std::string PinPai;
double Weight;
int KaLuLi;
};
int main()
{
CandyBar * pt = new CandyBar [3];
pt[0] = {"Mocha munch", 2.3, 350};
pt[1] = {"ping Guo", 4.2, 120};
pt[2] = {"xiang jiao", 5.5, 130};
for(int i = 0; i != 3; i++)
{
std::cout <<"品牌:"<< pt[i].PinPai <<std::endl;
std::cout <<"重量:"<< pt[i].Weight <<std::endl;
std::cout << "卡路里:"<< pt[i].KaLuLi << std::endl;
}
delete [] pt;
return 0;
}
10.编写一个程序,让用户输入三次40码跑的成绩(如果您愿意,也可让用户输入40米跑的成绩),并显示次数和平均成绩。请使用一个aray对象来存储数据(如果编译器不支持aray类,请使用数组)。
#include <iostream>
#include <array>
int main()
{
using namespace std;
cout << "请输入三次40米跑的成绩" << endl;
array<double, 3> array;
cout << "第一次:";
cin >> array[0];
cout << "第二次:";
cin >> array[1];
cout << "第三次:";
cin >> array[2];
double ave;
ave = (array[0] + array[1] + array[2])/3;
cout << "三次40米跑的平均成绩:"<< ave << endl;
return 0;
}