/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/19
From : C++ Primer Plus第五版第四章编程练习 第1题
Problem : 编写一个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>
using namespace std;
int main()
{
char first_name[20];
char 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 grad do you deserve? ";
cin >> grade;
cout << "What is your age? ";
cin >> age;
cout << "Name: " << last_name << ", " << first_name <<endl;
cout << "Grade: " << char (grade+1) <<endl;
cout << "Age: " << age <<endl;
system("pause");
return 0;
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/19
From : C++ Primer Plus第五版第四章编程练习 第2题
Problem : 修改程序清单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";
system("pause");
return 0;
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/19
From : C++ Primer Plus第五版第四章编程练习 第3题
Problem : 编写一个程序,它要求用户首先输入其名,然后输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并存储
和显示组合结果。请使用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>
const int SIZE =20;
using namespace std;
int main()
{
char firstName[SIZE];
char lastName[SIZE];
char fullName[2*SIZE + 1];
cout << "Enter your first name: ";
cin >> firstName;
cout << firstName;
cout << "Enter your last name: ";
cin >> lastName;
strcpy(fullName,lastName);
strcat(fullName, ", ");
strcat(fullName, firstName);
fullName[SIZE - 1] = '\0';
cout << "Here's the information in a single string: "
<< fullName << endl;
system("pause");
return 0;
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/19
From : C++ Primer Plus第五版第四章编程练习 第4题
Problem : 编写一个程序,它要求用户首先输入其名,再输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并存储和
显示组合结果。请使用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>
using namespace std;
int main()
{
string firstName;
string lastName;
string fullName;
cout << "Enter your first name: ";
getline(cin,firstName);
cout << "Enter your last name: ";
getline(cin,lastName);
fullName = lastName + ", " + firstName ;
cout << "Here's the information in a single string: "
<< fullName << endl;
system("pause");
return 0;
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/19
From : C++ Primer Plus第五版第四章编程练习 第5题
Problem : 结构CandyBar包含3个成员。第一个成员存储了糖块的品牌;第二个成员存储糖块的重量(可以有小数);第三个成员
存储了糖块的卡路里含量(整数)。请编写一个程序,声明这个结构,创建一个名为snack的CandyBar变量,并将其成员分别初始化
为“Mocha Munch"、2.3和350。初始化应在声明snack时进行。最后,程序显示snack变量的内容。
*******************************************************************************************************************/
#include<iostream>
struct CandyBar
{
char brand[20];
float weight;
int cal;
};
using namespace std;
int main()
{
CandyBar snack ={"Mocha Munch",2.3,350};
cout << "Brand: " << snack.brand <<endl;
cout << "Weight: " << snack.weight <<endl;
cout << "Cal: " << snack.cal <<endl;
system("pause");
return 0;
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/19
From : C++ Primer Plus第五版第四章编程练习 第6题
Problem : 结构CandyBar包含3个成员,如编程练习5所示。请编写一个程序,创建一个包含3个元素的CandyBar数组,并将它们初始化
为所选择的值,然后显示每个结构的内容。
*******************************************************************************************************************/
#include<iostream>
struct CandyBar
{
char brand[20];
float weight;
int cal;
};
using namespace std;
int main()
{
CandyBar snack[3] ={{"Mocha Munch1",2.3,350},{"Mocha Munch2",2.4,360},{"Mocha Munch3",2.5,370}};
for (int i=0; i<3; i++)
{
cout << "Brand:\t" << snack[i].brand <<endl;
cout << "Weight:\t" << snack[i].weight <<endl;
cout << "Cal:\t" << snack[i].cal <<endl;
}
system("pause");
return 0;
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/19
From : C++ Primer Plus第五版第四章编程练习 第7题
Problem : William Wingate从事比萨饼分析服务。对于每个披萨饼,他都需要记录下列信息:
披萨饼公司的名称,可以有多个单词组成
披萨饼的直径
披萨饼的质量
请设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。程序将请求用户输入上述信息,然后显示这些信息。
请使用cin和cout
*******************************************************************************************************************/
#include<iostream>
struct pizza
{
char company[20];
float diameter;
float weight;
};
using namespace std;
int main()
{
pizza pizza1;
cout << "Enter the name of company: \n";
cin.getline(pizza1.company,20);
cout << "Enter the diameter of pizza: \n";
cin >> pizza1.diameter;
cout << "Enter the weight of pizza: \n";
cin >> pizza1.weight;
cout << "Company: " << pizza1.company << endl;
cout << "Diameter: " << pizza1.diameter <<endl;
cout << "Weight: " << pizza1.weight <<endl;
system("pause");
return 0;
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/19
From : C++ Primer Plus第五版第四章编程练习 第8题
Problem : 完成编程练习7,但使用new来为结构分配内存,而不是声明一个结构变量。另外,让程序在请求输入比萨饼公司名称
之前输入比萨饼的直径
*******************************************************************************************************************/
#include<iostream>
struct pizza
{
char company[20];
float diameter;
float weight;
};
using namespace std;
int main()
{
pizza *p = new pizza;
cout << "Enter the diameter of pizza: \n";
cin >> p->diameter;
cin.get();
cout << "Enter the name of company: \n";
cin.getline(p->company,20);
cout << "Enter the weight of pizza: \n";
cin >> p->weight;
cout << "Company:\t" << p->company << endl;
cout << "Diameter:\t" << (*p).diameter <<endl;
cout << "Weight:\t" << (*p).weight <<endl;
system("pause");
return 0;
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/19
From : C++ Primer Plus第五版第四章编程练习 第9题
Problem : 完成编程练习6,但是用new来动态分配数组,而不是声明一个包含3个元素的CandyBar数组。
*******************************************************************************************************************/
#include<iostream>
#include<string>
struct CandyBar
{
string brand;
double weight;
int cal;
};
using namespace std;
int main()
{
CandyBar *bar =new CandyBar[3];
for(int i = 0;i < 3;i ++)
{
bar[i].brand = "new";
bar[i].cal = 1.1;
bar[i].weight = 3;
}
delete []p;
system("pause");
return 0;
}