C++ Primer Plus 第四章编程练习
第一题:
#include<string>
using namespace std;
int main()
{
string firstName, lastName;
char grade;
int age;
cout << "What is your first name? ";
getline(cin,firstName);
cout << "What is your last name? ";
getline(cin,lastName);
cout << "What letter grade do you deserve? ";
cin >> grade;
cout << "What is your age? ";
cin >> age;
grade += 1;
cout << "Name: " << lastName << ", " << firstName
<< endl << "Grade: " << grade
<< endl << "Age: " << age;
return 0;
}
第二题:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string name, dessert;
cout << "Enter your name:" << endl;
getline(cin,name);
cout << "Enter your favorite dessert:" << endl;
getline(cin, dessert);
cout << "I have some delicious " << dessert
<< " for you, " << name << endl;
return 0;
}
第三题:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char firstName[10];
char lastName[10];
char name[20];
cout << "Enter your first Name :" ;
cin.getline(firstName, 20);
cout << "Enter your last Name :" ;
cin.getline(lastName, 20);
strcat(lastName,", ");
strcat(lastName,firstName);
cout << "Here's the infomation in a single string: " << lastName;
}
第四题:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string firstName, lastName, name;
cout << "Enter your first Name :" ;
getline(cin, firstName);
cout << "Enter your last Name :" ;
getline(cin, lastName);
name = lastName + ", " +firstName;
cout << "Here's the infomation in a single string: " << name;
}
第五题:
#include<iostream>
#include<string>
using namespace std;
struct CandyBar
{
string brand;
double weight;
int calorie;
};
int main()
{
CandyBar snack = {"Mocha Munch", 2.3, 350};
cout << "[CandyBar]" << endl
<< "Brand:" << snack.brand << endl
<< "Weight:" << snack.weight << endl
<< "Calorie:" << snack.calorie << endl;
return 0;
}
第六题:
#include<iostream>
using namespace std;
struct CandyBar
{
string brand;
double weight;
int calorie;
};
int main()
{
CandyBar snack[3] =
{
{"Mocha Munch", 2.3, 350},
{"Mocha Munch", 2.4, 360},
{"Mocha Munch", 2.5, 370}
};
cout << "[CandyBar]" << endl
<< "Brand:" << snack[0].brand << endl
<< "Weight:" << snack[1].weight << endl
<< "Calorie:" << snack[2].calorie << endl;
return 0;
}
第七题:
#include<iostream>
#include<string>
using namespace std;
struct William
{
string name;
union diameter
{
int i_diameter;
double d_diameter;
}d;
double weight;
};
int main()
{
William w;
cout << "Please enter the name of the pizza company: " ;
getline(cin,w.name);
cout << "Please enter the diameter of the pizza: ";
cin >> w.d.d_diameter;
cout << "Please enter the weight of the pizza: ";
cin >> w.weight;
cout << "Company:[" << w.name << "]" << endl
<< "Diameter: " << w.d.d_diameter << endl
<< "Weight: " << w.weight << endl;
return 0;
}
第八题:
#include<iostream>
#include<string>
using namespace std;
struct William
{
string name;
union diameter
{
int i_diameter;
double d_diameter;
}d;
double weight;
};
int main()
{
William* w = new William;
cout << "Please enter the diameter of the pizza: ";
cin >> w->d.d_diameter;
cout << "Please enter the name of the pizza company: " ;
cin.get();
getline(cin,w->name);
cout << "Please enter the weight of the pizza: ";
cin >> w->weight;
cout << "Company:[" << w->name << "]" << endl
<< "Diameter: " << w->d.d_diameter << endl
<< "Weight: " << w->weight << endl;
delete w;
return 0;
}
第九题:
#include<iostream>
#include<cstring>
#include<vector>
#include<array>
using namespace std;
struct CandyBar
{
string brand;
double weight;
int calorie;
};
void method01()
{
CandyBar* snack = new CandyBar[3];
snack[0] = {"Mocha Munch", 2.3, 350};
snack[1] = {"Mocha Munch", 2.4, 360};
snack[2] = {"Mocha Munch", 2.5, 370};
cout << "[CandyBar]" << endl
<< "Brand:" << snack[0].brand << endl
<< "Weight:" << snack[1].weight << endl
<< "Calorie:" << snack[2].calorie << endl;
}
void method02()
{
vector<CandyBar> snack(3);
snack[0] = {"Mocha Munch", 2.3, 350};
snack[1] = {"Mocha Munch", 2.4, 360};
snack[2] = {"Mocha Munch", 2.5, 370};
cout << "[CandyBar]" << endl
<< "Brand:" << snack[0].brand << endl
<< "Weight:" << snack[1].weight << endl
<< "Calorie:" << snack[2].calorie << endl;
}
void method03()
{
array<CandyBar, 3> snack =
{
// 这种形式是比较好的,显示构造对象后拷贝/移动
// 也不用在意CandyBar是个结构体,还是提供了(string,double,int)构造函数的类
CandyBar{"Mocha Munch", 2.3, 350},
CandyBar{"Mocha Munch", 2.4, 360},
CandyBar{"Mocha Munch", 2.5, 370}
//使用非ISO C++会报warning
// (CandyBar){"Mocha Munch", 2.3, 350},
// (CandyBar){"Mocha Munch", 2.4, 360},
// (CandyBar){"Mocha Munch", 2.5, 370}
// 或者:
// "Mocha Munch", 2.3, 350,
// "Mocha Munch", 2.4, 360,
// "Mocha Munch", 2.5, 370
};
cout << "[CandyBar]" << endl
<< "Brand:" << snack[0].brand << endl
<< "Weight:" << snack[1].weight << endl
<< "Calorie:" << snack[2].calorie << endl;
}
int main()
{
method01();
method02();
method03();
return 0;
}
这里用了三种方法,第三种方法我一开始出现了初始化问题,后面经过大佬们的解答解决了,具体方法如图以及图中注释!(我觉得这个get到的知识点不是随随便便一道题目那么简单了已经!开心!)
第十题:
#include<iostream>
#include<array>
using namespace std;
int main()
{
double avarage=0.0, sum =0.0;
array<double, 3> grade;
for(int i=0; i<3; i++)
{
cin >> grade[i];
cout << "The " << (i+1) << " time is " << grade[i] << endl;
sum += grade[i];
}
avarage = sum/3;
cout << "Avarage:" << avarage << endl;
return 0;
}
以上算是记录吧……萌新,如果有什么写的不对或者不够好的地方,欢迎前辈们来指正QWQ