C++ primer plus 第四章课后习题

#include <iostream>
#include <string>
#include <cstring>
#include <array>
using namespace std;
#pragma warning(disable:4996)//用VS编译代码是遇到如上问题,vs准备弃用strcpy的,安全性较低,
                            //所以微软提供了strcpy_s来代替,如果想继续使用strcpy的,使用这句话
void grade_information()//exercise 4.1
{
const int size = 10;
char first_name[size] = { 0 }, last_name[size] = { 0 },grade;
int age;//这里如果写为char的话,则输入的时候只会当成一个字符;22就会输入成2;
cout << "What is your first name?";
cin.getline(first_name,size);
cout << "What is your last name?";
cin.getline(last_name, size);
cout << "What letter grade do you deserve?";
cin >> grade;
grade = grade + 1;
cout << "What is your age?";
cin >> age;
cout << "Name: " << last_name << "," << first_name<<endl;
cout << "Grade: " << grade << endl;
cout << "Age: " << age<<endl;
}
void instr2()//4.2
{
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;
cout << " for you, " << name << ".\n";
}
void addstr1()//4.3
{
const int size = 10;
const int Asize = 20;
char first_name[size] = { 0 }, last_name[Asize] = { 0 },others[2] = { ',','\0' };
char name[Asize] = { 0 };
cout << "Enter your first name: ";
cin.getline(first_name,size);
cout << "Enter your last name: ";
cin.getline(last_name, size);
strcpy(name, last_name);
strcat(name, ", ");
strcat(name, first_name);
cout << "Here's the information in a single string: " << name << endl;
}
void addstr2()//4.4
{
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;
}
void Candy1()//4.5
{
struct CandyBar
{
string brand;
float weight;
int energy;
};
CandyBar snack = { "Mocha Munch",2.3,350 };
cout << "Candy_name: " << snack.brand << endl;
cout << "Candy_weight: " << snack.weight << endl;
cout << "Candy_energy: " << snack.energy << endl;
}
void Candy2()//4.6
{
struct CandyBar
{
string brand;
float weight;
int energy;
};
CandyBar snack[3] = 
{
{ "Mocha Munch",2.3,350 },
{"toffee",2.4,360},
{"Fruit candy",2.5,370},
};
cout << "Candy1 " << snack[0].brand << " and "<< snack[0].weight<<" and "<< snack[0].energy << endl;
cout << "Candy2 " << snack[1].brand << " and " << snack[1].weight << " and " << snack[1].energy << endl;
cout << "Candy3 " << snack[2].brand << " and " << snack[2].weight << " and " << snack[2].energy << endl;
}
void pizza1()//4.7
{
struct pizza {
string company_name;
float diameter;
float weight;
};
pizza pizza1 = { };
cout << "Welcome to the pizza anlayse!"<<endl;
cout << "Company_name: ";
getline(cin, pizza1.company_name);
cout << "diameter: ";
cin >> pizza1.diameter;
cout << "weight: ";
cin >> pizza1.weight;
cout << "Pizza1_company name: " << pizza1.company_name << " ,diameter is " << pizza1.diameter;
cout << " and weight is " << pizza1.weight;
}
void pizza2()//4.8
{
struct pizza {
string company_name;
float diameter;
float weight;
};
pizza *pizza2 = new pizza;
cout << "Welcome to the pizza anlayse!" << endl;
cout << "diameter: ";
cin >> pizza2->diameter;//确实会保留换行符;
cin.get(); //在此处添加一个去掉换行符;
cout << "Company_name: ";
getline(cin, pizza2->company_name);//遇到换行符直接停止输入;
cout << "weight: ";
cin >> pizza2->weight;
cout << "Pizza1_company name: " << pizza2->company_name << " ,diameter is " << pizza2->diameter;
cout << " and weight is " << pizza2->weight;
}
void Candy3()//4.9
{
struct CandyBar
{
string brand;
double weight;
int energy;
};
CandyBar *snack = new CandyBar[3];
*snack = { "Mocha Munch",2.22,350 };//这里的bug是weight的精度好像2.22被默认为double导致
*(snack+1) = { "toffee",2.4,360 };//如果是float形会被提示从double到float截断,编译不通过
*(snack+2) = { "Fruit candy",2.5,370 };//若要使用float 应该要加f;
cout << "Candy1 " << snack[0].brand << " and " << snack[0].weight << " and " << snack[0].energy << endl;
cout << "Candy2 " << snack[1].brand << " and " << snack[1].weight << " and " << snack[1].energy << endl;
cout << "Candy3 " << snack[2].brand << " and " << snack[2].weight << " and " << snack[2].energy << endl;
}
void Run()//4.10
{
array<int, 4> grade= { 0 };
cout << "Input the first grade: ";
cin >> grade[0];
cout<< "Input the second grade: ";
cin >> grade[1];
cout << "Input the third grade: ";
cin >> grade[2];
grade[3] = (grade[0] + grade[1] + grade[2]) / 3;
cout << "It's the average grade: " << grade[3];
}
void main()
{   
Run();
system("pause");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值