#include <iostream>
#include <string>
#include <array>
using namespace std;
void p4t1()
{
char a0[20];
char a1[20];
char a2;
int age=0;
cout<<"What is your first name?"<<endl;
cin>>a0;
cout<<"What is your last name?"<<endl;
cin>>a1;
cout<<"What letter grade do you deserve?"<<endl;
cin>>a2;
a2+=1;
cout<<"What is your age?"<<endl;
cin>>age;
cout<<"Name: "<<a1<<" "<<a0<<endl;
cout<<"Grade: "<<a2;
cout<<"Age: "<<age;
}
void p4t2()
{
string name;
string dessert;
cout << "Enter your name:"<<endl;
cin>>name;
cout << "Enter your favourite dessert:"<<endl;
cin>>dessert;
cout << "I have some delicious " << dessert<< " for you, " << name << ".\n";;
}
void p4t3()
{
char first_name[256];
char last_name[256];
cout <<"Enter your first name: "<<endl;
cin.getline(first_name,256);
cout <<"Enter your last name: "<<endl;
cin.getline(last_name,256);
cout << "Here's your infomation is a single string: "<<last_name<<","<<first_name<<endl;
}
void p4t4()
{
string first_name;
string last_name;
cout <<"Enter your first name: "<<endl;
getline(cin,first_name);
cout <<"Enter your last name: "<<endl;
getline(cin,last_name);
cout << "Here's your infomation is a single string: "<<last_name<<","<<first_name<<endl;
}
struct CandyBar
{
char name[256];
float weight;
int calory;
};
void p4t5()
{
struct CandyBar snack={ "Mocha Munch", 2.3, 350 };
cout<<"Name: "<<snack.name<<" : "<<snack.weight<<" : "<<" : "<<snack.calory<<endl;
}
void p4t6()
{
struct CandyBar cs[3]={
{"hello",7,7},{"bye",7,7},{"zip",7,7}
};
for (int i = 0; i < 3; i++) {
cout << cs[i].calory <<" : "<< cs[i].weight <<" : "<< cs[i].name << endl;
}
}
typedef struct pizza7{
string name;
double d;
double weight;
}p7;
void p4t7()
{
p7 newpizza;
cout<<"INPUT PIZZA NAME"<<endl;
getline(cin,newpizza.name);
cout<<"INPUT PIZZA SIZE"<<endl;
cin>>newpizza.d;
cout<<"INPUT PIZZA WEIGHT"<<endl;
cin>>newpizza.weight;
cout<<newpizza.name<<newpizza.d<<newpizza.weight<<endl;
}
void p4t8()
{
auto * pointer=new pizza7;
cout<<"INPUT PIZZA SIZE"<<endl;
cin>>pointer->d;
cin.get();//这里是把缓冲区读走
cout<<"INPUT PIZZA NAME"<<endl;
getline(cin,pointer->name);
cout<<"INPUT PIZZA WEIGHT"<<endl;
cin>>pointer->weight;
cout<<pointer->name<<pointer->d<<pointer->weight<<endl;
}
void p4t9()
{
auto p=new struct CandyBar[3]{
{"hello",7,7},{"bye",7,7},{"zip",7,7}
};
for (int i = 0; i < 3; i++) {
cout << p[i].calory <<" : "<< p[i].weight <<" : "<< p[i].name << endl;
}
}
void p4t10()
{
array<float,3> a={0,0,0};
cout<<"INPUT THREE GRADES"<<endl;
cin>>a[0];
cin>>a[1];
cin>>a[2];
cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<" "<<"AVERAGE:"<<(a[0]+a[1]+a[2])/3<<endl;
}
int main()
{
while(1) {
int num;
cout << "\n输入要做的题号" << endl;
cin >> num;
if (num == 1)
p4t1();
else if(num==2)
p4t2();
else if(num==3)
p4t3();
else if(num==4)
p4t4();
else if(num==5)
p4t5();
else if(num==6)
p4t6();
else if(num==7)
p4t7();
else if(num==8)
p4t8();
else if(num==9)
p4t9();
else if(num==10)
p4t10();
else
break;
}
}
C++ Primer Plus 中文第六版 第四章编程练习答案
最新推荐文章于 2025-12-04 22:20:49 发布
538

被折叠的 条评论
为什么被折叠?



