C++ Primer Plus Chapter4
//4.1
#include<iostream>
int main()
{
using namespace std;
const int size = 20;
char fname[size],lname[size];
char grade;
unsigned int age;
cout << "What is your first name?";
cin.getline(fname, 20);
cout << "What is your last name?";
cin.getline(lname, 20);
cout << "What letter grade do you deserve?";
cin >> grade;
cout << "What is your age?";
cin >> age;
cout << "Name: " << lname << ", " << fname << endl;
cout << "Grade: " << char(grade+1) << endl;
cout << "Age: " << age << endl;
return 0;
}
//4.2
#include<iostream>
#include<string>
int main()
{
using namespace std;
string fname,lname;
char grade;
unsigned int age;
cout << "What is your first name?";
getline(cin,fname);
cout << "What is your last name?";
getline(cin, lname);
cout << "What letter grade do you deserve?";
cin >> grade;
cout << "What is your age?";
cin >> age;
cout << "Name: " << lname << ", " << fname << endl;
cout << "Grade: " << char(grade+1) << endl;
cout << "Age: " << age << endl;
return 0;
}
//4.3
#include<iostream>
#include<cstring>
int main()
{
using namespace std;
const int size = 20;
char fname[size];
char lname[size];
cout << "Enter your first name: ";
cin.getline(fname, 20);
cout << "Enter your last name: ";
cin.getline(lname, 20);
char *const pn = new char[strlen(fname) + strlen(lname) + 1];
strcpy(pn, fname);
strcat(pn, " ");
strcat(pn, lname);
cout <<"Here's the information in a single string: "<< pn;
delete[]pn;
cout << endl;
return 0;
}
//4.4
#include<iostream>
#include<string>
int main()
{
using namespace std;
string fname;
string lname;
cout << "Enter your first name: ";
cin >> fname;
cout << "Enter your last name: ";
cin >> lname;
string name;
name = fname + ", " + lname;
cout << "Here's the information in a single string: " << name;
cout << endl;
return 0;
}
//4.5
#include<iostream>
struct CandyBar
{
char kind[20];
double weight;
unsigned int Calorie;
};
int main()
{
using namespace std;
CandyBar snack{
"Mocha Munch", 2.3, 350
};
cout << snack.kind << endl;
cout << snack.weight << endl;
cout << snack.Calorie << endl;
return 0;
}
//4.6
#include<iostream>
struct CandyBar
{
char kind[20];
double weight;
unsigned int Calorie;
};
int main()
{
using namespace std;
CandyBar snack[] = { { "Mocha Munch", 2.3, 350 }, { "阿尔卑斯", 6.3, 310 },{"优の良品", 2.1, 291} };
cout << snack[0].kind << "\t" << snack[0].weight << "\t" << snack[0].Calorie << endl;
cout << snack[1].kind << "\t" << snack[1].weight << "\t" << snack[1].Calorie << endl;
cout << snack[2].kind << "\t" << snack[2].weight << "\t" << snack[2].Calorie << endl;
return 0;
}
//4.7
#include<iostream>
struct Pizza
{
char name[20];
double diameter;
double weight;
};
int main()
{
using namespace std;
Pizza pa;
cout << "Enter the name of Pizza: ";
cin >> pa.name;
cout << "Enter the diameter of Pizza: ";
cin >> pa.diameter;
cout << "Enter the weight of Pizza:";
cin >> pa.weight;
cout << "Name:" << pa.name << endl;
cout << "Diameter:" << pa.diameter << endl;
cout << "Weight: " << pa.weight << endl;
return 0;
}
//4.8
#include<iostream>
struct Pizza
{
char name[20];
double diameter;
double weight;
};
int main()
{
using namespace std;
Pizza * pn = new Pizza;
cout << "Enter the diameter of Pizza:";
cin>>pn->diameter;
cin.get();
cout << "Enter the name of Pizza:";
cin.get( pn->name,20);
cout << "Enter the weight of Pizza: ";
cin >> (*pn).weight;
cout << "Name:" << pn->name << endl;
cout << "Diameter:" << pn->diameter << endl;
cout << "Weight: " << pn->weight << endl;
return 0;
}
//4.8
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
struct TPizza
{
string company_name;
double diameter;
double weight;
};
int main()
{
TPizza* p_pizza = new TPizza;
cout << "输入披萨直径(厘米):";
cin >> p_pizza->diameter; // cin忽略输入流中的空白符(回车符、空格符、制表符),回车符仍保留在输入流中
cout << "输入披萨名:";
cin.get(); // 提取先前留在输入流中的回车符,否则下面的getline()不会等待输入,而直接读取输入流中已有的回车符
getline(cin, p_pizza->company_name);
cout << "输入披萨重量(克):";
cin >> p_pizza->weight;
cout << p_pizza->company_name << "公司出品直径为" << p_pizza->diameter << "厘米重量为" << p_pizza->weight << "克的披萨" << endl;
delete p_pizza;
p_pizza = NULL;
cout << endl;
return (0);
}
//4.9
#include<iostream>
#include<string>
struct CandyBar
{
string name;
double weight;
unsigned int calories;
};
int main()
{
using namespace std;
const unsigned int size = 3;
CandyBar *pn = new CandyBar[size];
pn[0].name = "Mocha Munch";
pn[0].weight = 2.3;
pn[0].calories = 350;
pn[1].name = "阿尔卑斯";
pn[1].weight = 6.3;
pn[1].calories = 310;
pn[2].name = "优の良品";
pn[2].weight = 2.1;
pn[2].calories = 291;
cout << pn[0].name << "\t" << pn[0].weight << "\t" << pn[0].calories << endl;
cout << pn[1].name << "\t" << pn[1].weight << "\t" << pn[1].calories << endl;
cout << pn[2].name << "\t" << pn[2].weight << "\t" << pn[2].calories << endl;
delete[] pn;
return 0;
}
//4.10
#include<iostream>
#include<array>
int main()
{
using namespace std;
const unsigned int size = 3;
array <double, size> grade;
cout << "输入三次40码跑的成绩: ";
cin >> grade[0] >> grade[1] >> grade[2];
cout << "长跑次数: " << size << endl;
cout << "平均成绩: " << (grade[0] + grade[1] + grade[2]) / 3 << endl;
}