内容仅供参考,如有错误,欢迎指正 !
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>
int main()
{
using namespace std;
const int size = 20;
char first_name[size];
char last_name[size];
char grade;
int age;
cout << "What is your first name?" << endl;
cin.get(first_name, size).get();
//cin.getline(first_name,size)也可以
cout << "What is your last name?" << endl;
cin.get(last_name, size).get();
cout << "What letter grade do you deserve?" << endl;
cin >> grade;
cout << "What is your age?" << endl;
cin >> age;
cout << "Name: " << last_name << ", " << first_name << endl;
cout << "Grade: " << ++grade << endl;
//C的ASCII码比B高一位
cout << "Age: " << age << endl;
return 0;
}
2.修改程序清单4.4,使用c++string类而不是char数组。
#include<iostream>
#include<string>
int main()
{
using namespace std;
string name;
string dessert;
cout << "Enter your name:\n";
cin >> name;
cout << "Enter your favorite dessert:\n";
cin >> dessert;
cout << "I have some delicious " << dessert << " for you, " << name <<"."<< endl;
return 0;
}
3.编写一个程序,他要求用户首先输入其名,然后输入其姓;然后程序使用一个逗号和一个空格将姓和名组合起来,并存储和显示结合效果,请使用char数组和头文件cstring中的函数。下面是该程序的运行时的情形:
Enter your first name: Flip
Enter your last name: Fleaming
Here's the information in a single string: Fleming, Flip
#include<iostream>
#include<cstring>
int main()
{
using namespace std;
char first_name[20];
char last_name[20];
char full_name[20];
cout << "Enter your first name: " ;
cin.getline(first_name,20);
cout << "Enter your last name: " ;
cin.getline(last_name, 20);
strcpy_s(full_name, last_name);
strcat_s(full_name, ", ");
strcat_s(full_name, first_name);
//错误C4996 'strcpy'与'strcat': This function or variable may be unsafe. Consider using strcpy_s instead.
cout << "Enter your last name: " << endl;
cout << "Here's the information in a single string: " << full_name << endl;
return 0;
}
4.编写一个程序,他要求用户首先输入其名,然后输入其姓;然后程序使用一个逗号和一个空格将姓和名组合起来,并存储和显示结合效果,请使用string对象和文件string中的函数。下面是该程序的运行时的情形:
Enter your first name: Flip
Enter your last name: Fleaming
Here's the information in a single string: Fleming, Flip
#include<iostream>
#include<string>
#include<cstring>
int main()
{
using namespace std;
string first_name;
string last_name;
string full_name;
cout << "Enter your first name: " << endl;
cin>>first_name;
cout << "Enter your last name: " << endl;
cin>>last_name;
full_name=last_name;
full_name+=", ";
full_name+=first_name;
cout << "Enter your last name: " << endl;
cout << "Here's the information in a single string: " << full_name << endl;
return 0;
}
5.结构CandyBar包含3个成员。第一个成员存储了糖块的品牌;第二个成员存储糖块的重量(可以有小数);第三个成员存储了糖的卡路里含量(整数)。请编写一个程序,声明这个结构,创建一个名为snack的CandBar变量,并将其成员分别初始化为“Mocha Munch”、2.3和350。初始化在声明snack时进行。最后,程序显示snack变量的内容。
#include<iostream>
#include<string>
int main()
{
using namespace std;
struct CandyBar
{
string brand;
double weight;
int cal;
};
CandyBar snack = { "Mocha Munch", 2.3, 350 };
cout <<snack.brand<<endl;
cout << snack.weight << endl;
cout << snack.cal<< endl;
return 0;
}
6.结构CandyBar包含3个成员,如编程练习5所示。请编写一个程序,创建一个包含3个元素的CandyBar数组,并将他们初始化为所选择的值,然后显示每个结构的内容。
#include<iostream>
#include<string>
using namespace std;
struct Pizza
{
string Inc;
double diameter;
double weight;
};
int main()
{
Pizza pizza;
cout << "Please enter the Inc of the pizza: " << endl;
cin >> pizza.Inc;
cout << "Please enter the diameter of the pizza: " << endl;
cin >> pizza.diameter;
cout << "Please enter the weight of the pizza: " << endl;
cin >> pizza.weight;
cout << "This pizza is form " << pizza.Inc << endl;
cout << "diameter: " << pizza.diameter << endl;
cout << "weight: " << pizza.weight << endl;
return 0;
}
7.William Wingate从事披萨分析服务。对于每个披萨饼,他都需要记录下列信息:
披萨饼公司的名称,可以有多个单词组成。
披萨饼的直径
披萨饼的重量
请设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。程序将请求用户输入上述信息,然后显示这些信息。请使用cin(或它的方法)和cout。
#include<iostream>
#include<string>
using namespace std;
struct Pizza
{
string Inc;
double diameter;
double weight;
};
int main()
{
Pizza pizza;
cout << "Please enter the Inc of the pizza: " << endl;
cin >> pizza.Inc;
cout << "Please enter the diameter of the pizza: " << endl;
cin >> pizza.diameter;
cout << "Please enter the weight of the pizza: " << endl;
cin >> pizza.weight;
cout << "This pizza is form " << pizza.Inc << endl;
cout << "diameter: " << pizza.diameter << endl;
cout << "weight: " << pizza.weight << endl;
return 0;
}
8.完成编程练习7,但使用new来为结构动态分配内存,而不是声明一个结构变量。另外,让程序在请求输入披萨名称之前输入披萨直径。
#include<iostream>
#include<string>
using namespace std;
struct Pizza
{
string Inc;
double diameter;
double weight;
};
int main()
{
Pizza* p = new Pizza;
cout << "Please enter the diameter of the pizza: " << endl;
cin >> p->diameter;
cout << "Please enter the Inc of the pizza: " << endl;
cin >>p->Inc;
cout << "Please enter the weight of the pizza: " << endl;
cin >>p->weight;
cout << "This pizza is form " << p->Inc << endl;
cout << "diameter: " << p->diameter << endl;
cout << "weight: " << p->weight << endl;
delete p;
return 0;
}
9.完成编程练习6,但使用new来动态分配数组,而不是声明一个包含3个元素的CandyBar的数组。
#include<iostream>
#include<string>
using namespace std;
struct CandyBar
{
string brand;
double weight;
int cal;
};
int main()
{
CandyBar* p = new CandyBar[3];
p[0]={ "Mocha Munch0", 2.30, 3500 };
p[1]={ "Mocha Munch1", 2.31, 3501 };
p[2]={ "Mocha Munch2", 2.32, 3502 };
for (int i = 0; i < 3; i++)
{
cout << "brand: " << p[i].brand << endl;
cout << "weight: " << p[i].weight << endl;
cout << "calorie: " << p[i].cal << endl;
cout << endl;
}
delete [] p;
return 0;
}
10.编写一个程序,让用户输入三次40码跑的成绩(如果你愿意,也可以让用户输入40米跑的成绩),并显示次数和平均成绩。请使用一个array对象来存储数据(如果编译器不支持array类,请使用数组)。
#include<iostream>
#include<array>
int main()
{
using namespace std;
array<double, 3>grade;
cout << "Please input your first grade:";
cin >> grade[0];
cout << "Please input your second grade:";
cin >> grade[1];
cout << "Please input your third grade:";
cin >> grade[2];
cout << "Average grade of three times:" << (grade[0] + grade[1] + grade[2]) / 3<< endl;
system("pause");
return 0;
}