Original C ++ Primer Plus Chapter Four After-Class Programming Exercises 1-10

1题。。。
#include
#include
int main()
{
using namespace std;

char fname[40];
char lname[40];
char achievement;
int age;
cout <<"What is your first name? ";
cin.getline(fname, 40);
cout <<"what is your last name? ";
cin.getline(lname,40);
cout <<"What letter grade do you deserve? ";
cin >> achievement;
cout <<"What is your age? ";
cin >> age;

cout <<"Name: " <<lname <<", " <<fname << endl;

//+1后会变成int整型输出,所以需要强制转换char类型
cout <<"Grade: "<<(char)(achievement+1) << endl;
cout <<"Age: " << age << endl;

return 0;

}

2题。。。
#include
#include
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";

return 0;

}

3题。。。。
#include
#include
int main()
{
using namespace std;
char fname[20];
char lname[20];

cout <<"Enter your first name: ";
cin >>fname;
cout <<"Enter your last name: ";
cin >>lname;
strcat(lname, ", ");
strcat(lname, fname);
cout <<"Here's the information in a single string: " <<lname <<endl;

return 0;

}

4题。。。。
#include
#include
int main()
{
using namespace std;
string fname;
string lname;

cout <<"Enter your first name: ";
cin >> fname;
cout <<"Enter your last name: ";
cin >>lname;
fname = fname + ", ";
fname = fname + lname;
cout <<"Here's the information in a single string: " <<fname << endl;

return 0;

}

5题。。。。。
#include
struct CandyBar {
char brand[20];
float weight;
int calorie;
};
int main()
{
using namespace std;
struct CandyBar snack = {
“Mocha Munch”,
2.3,
350
};

cout <<"snack brand: " <<snack.brand << endl;
cout <<"snack weight: "<<snack.weight << endl;
cout <<"snack calorie: "<<snack.calorie << endl;

return 0;

}

6题。。。。
#include
struct CandyBar {
char brand[20];
float weight;
int calorie;
};
int main()
{
using namespace std;
struct CandyBar snack[3] = {
{“Mocha Munch”, 2.3, 350},
{“Kisld Cisdj”, 3.5, 400},
{“Ljise Jsdiw”, 5.2, 450}
};
cout<<“snack1 brand: " <<snack[0].brand;
cout<<” snack1 weight: " <<snack[0].weight;
cout<<" snack1 calorie: " <<snack[0].calorie << endl;
cout<<“snack2 brand: " <<snack[1].brand;
cout<<” snack2 weight: " <<snack[1].weight;
cout<<" snack2 calorie: " <<snack[1].calorie << endl;
cout<<“snack3 brand: " <<snack[2].brand;
cout<<” snack3 weight: " <<snack[2].weight;
cout<<" snack3 calorie: " <<snack[2].calorie << endl;

return 0;

}

7 questions. . . .
#include
struct WilliamWingate_pizza {
char p_c_name [40];
int p_diameter; // inch
int p_weight; // g
};
int main ()
{
the using namespace STD;
struct WilliamWingate_pizza PS;
COUT << "Please enter the name of the pizza company. : ";
cin.getline (ps.p_c_name, 40);
cout <<" please enter the diameter of the pizza (inches): ";
cin >> ps.p_diameter;
cout <<" please enter the weight of the pizza (grams): " ;
cin >> ps.p_weight;

cout <<"公司名称: " << ps.p_c_name << endl;
cout <<"披萨的直径(寸): " <<ps.p_diameter << endl;
cout <<"披萨的重量(克): " <<ps.p_weight << endl;

return 0;

}

8 questions. . . .
#include
struct WilliamWingate_pizza {
char p_c_name [40];
int p_diameter; // inch
int p_weight; // gram
};
int main ()
{
using namespace std;
// allocate dynamic memory for the structure, ps refers to the address in the structure memory Pointer
WilliamWingate_pizza * ps = new WilliamWingate_pizza;
cout << "Please enter the diameter of the pizza (inches):";
// The line breaks left in the buffer after the input is discarded
(cin >> ps-> p_diameter) .get ();
cout << "Please enter the name of this pizza company:";
cin.getline (ps-> p_c_name, 40);
cout << "please enter the weight of the pizza (g):";
(cin >> ps-> p_weight ) .get ();

cout <<"公司名称: " << ps->p_c_name << endl;
cout <<"披萨的直径(寸): " <<ps->p_diameter << endl;
cout <<"披萨的重量(克): " <<ps->p_weight << endl;
delete ps;    //不要忘记释放掉分配的动态内存
return 0;

}

9题。。。。
#include
struct CandyBar {
char brand[20];
float weight;
int calorie;
};
int main()
{
using namespace std;
CandyBar *snack1 = new CandyBar;
CandyBar *snack2 = new CandyBar;
CandyBar *snack3 = new CandyBar;
*snack1 = {“Mocha Munch”,2.3, 350};
*snack2 = {“Kisld Cisdj”, 3.5, 400};
*snack3 = {“Ljise Jsdiw”, 5.2, 450};

cout<<"snack1 brand: " <<snack1->brand;
cout<<"  snack1 weight: " <<snack1->weight;
cout<<"  snack1 calorie: " <<snack1->calorie << endl;
cout<<"snack2 brand: " <<snack2->brand;
cout<<"  snack2 weight: " <<snack2->weight;
cout<<"  snack2 calorie: " <<snack2->calorie << endl;
cout<<"snack3 brand: " <<snack3->brand;
cout<<"  snack3 weight: " <<snack3->weight;
cout<<"  snack3 calorie: " <<snack3->calorie << endl;
//不要忘记释放分配的动态内存
delete snack1;
delete snack2;
delete snack3;

return 0;

}

10 questions. . . .
#include
#include
int main ()
{
using namespace std;
array <double, 3> achievement;
cout << "Please enter the result of the first 40-meter run:";
cin >> achievement [0];
cout << " Please enter the result of running the 40m for the second time: ";
cin >> achievement [1];
cout <<" Please enter the result of running the 40m for the third time: ";
cin >> achievement [2];

cout <<"三次跑40米的平均成绩是: ";
cout << (achievement[0] + achievement[1] + achievement[2])/3 << endl;

return 0;

}

Published 85 original articles · Like1 · Visits1889

Guess you like

Origin blog.csdn.net/Tekkenwxp/article/details/103692951