C++ Primer Plus(第六版)第四章课后习题

C++ Primer Plus(第六版)第四章课后习题

4.13.1

#include
int main()
{
using namespace std;
const int fixed=30;
char first_name[fixed],last_name[fixed];
char grade;
int age;
cout << "What is your first name? ";
cin.get( first_name,fixed).get();
cout << "What is your last name? ";
cin.get( last_name,fixed);
cout << "What letter grade do you deserve? ";
cin >> grade;
cout << "What is your age? ";
cin >> age;
cout << "Name: " << last_name << “,” << first_name << endl;
grade++;
cout << "Grade: "<< grade <<endl;
cout << "Age: " << age;
return 0;
}

4.13.2

#include
#include
int main()
{
using namespace std;
string name, dessert;
cout << “Enter your name:\n”;
getline(cin,name);
cout << “Enter your favorite dessert:\n”;
getline(cin,dessert);
cout << " I have some delicious " << dessert << " for you, "<< name << endl;
return 0;
}

4.13.3

#include
#include
int main()
{
using namespace std;
char first_name[20],last_name[20];
cout <<"Enter your first name: ";
cin.getline(first_name,20);
cout <<"Enter your last name: ";
cin.getline (last_name,20);
cout <<"Here’s the information in a single string: “<< last_name <<” " <<first_name<<endl;
return 0;
}

4.13.4

#include
#include
int main()
{
using namespace std;
string first_name,last_name,name;
cout << "Enter your first name: ";
getline(cin,first_name);
cout << "Enter your last name: “;
getline(cin, last_name);
name=last_name+”, "+first_name;
cout << "Here’s the information in a single string: "<<name <<endl;
return 0;
}

4.13.5

#include
#include

int main()
{
using namespace std;
struct CandyBar
{
string brand;
double weight;
int calorie;
};
CandyBar snack={“Mocha Munch”,2.3,350};
cout << " The brand of candy is "<< snack.brand << endl;
cout << " The weigt of candy is " << snack.weight << endl;
cout << " The calorie of candy is " << snack.calorie <<endl;
return 0;
}

4.13.6

#include
struct CandyBar
{
char brand[20];
double weight;
int calorie;
};
int main()
{
using namespace std;

CandyBar snack[3]={{"a",33.5,222},{"b",55.3,666},{"c",22.1,442}};
cout << " The brand of candy is "<< snack[0].brand<< endl;
cout << " The weigt of candy is " << snack[1].weight <<endl;
cout << " The calorie of candy is " << snack[2].calorie <<endl;
return 0;

}

4.13.7

#include
#include
int main()
{
using namespace std;
struct pizza
{
string name;
double diameter;
int weight;
};
pizza A;
cout <<"Enter the name of company: ";
getline(cin, A.name);
cout <<"Enter the diameter of pizza: ";
cin >>A.diameter;
cout << "Enter the weight of pizza: ";
cin >> A.weight ;
cout << " The name of company is “<< A.name<<”,the diameter of pizza is "<< A.diameter
<< ", the weight of pizza is "<< A.weight <<endl;
return 0;
}

4.13.8

#include
struct pizza
{
char brand[20];
double diameter;
int weight;
};
int main()
{
using namespace std;
pizza * point= new pizza;

cout << "Enter the diameter of pizza:";
cin >> point->diameter ;
cin.get();
cout << "Enter the name of pizza company:";
cin.getline(point->brand ,20);
cout << "Enter the weight of pizza:";
cin >> point->weight;
cout << "wo zhenshuai";
cout << "The brand of pizza company is " << (*point)brand <<", the diameter of pizza is "<<point->diameter<<", and the weight of pizza is " << point->weight<<endl;
delete point;
return 0;

}

4.13.9

#include
const int NUM=3;
struct CandyBar
{
char brand[20];
double weight;
int calorie;
};
int main()
{
using namespace std;

CandyBar *snack=new CandyBar[NUM];
for(int i=0;i<NUM;i++)
{
	cout << "Enter the brand of candy: ";
	cin.getline(snack[i].brand, 20);
	cout << "Enter the weight of candy: ";
	cin >> snack[i].weight;
	cout << "Enter the calorie of candy: ";
	cin >> snack[i].calorie;
	cin.get();
}
cout << " The brand of candy is "<< snack[0].brand<< endl;
cout << " The weigt of candy is " << snack[1].weight <<endl;
cout << " The calorie of candy is " << snack[2].calorie <<endl;
return 0;

}

4.13.10

#include
#include
int main()
{
using namespace std;
array<double,3> grade;
cout << “Enter the first grade:”;
cin >> grade[0];
cout <<“Enter the second grade:”;
cin >> grade[1];
cout << “Enter the third grade:”;
cin >>grade[2];
double even=(grade[0]+grade[1]+grade[2])/3.0;
cout <<"The even of grades is "<< even<< endl;
return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值