c++ primer plus第四章习题答案

这章的内容不错,字符串结构体共用体枚举指针数组,编程的最基本类型基本都有涉及

字符串操作比较易错且知识易忘,各位要注意了

结构体共用体会与以后的内存对齐有关,这个就是寻址和存储上的事了

指针就是地址,老师多次强调,各位不妨一记

给代码!!


第一题

#include <iostream>
#include <string>
using namespace std;

int main()
{
string f_name, l_name;
char grade;
int age;

cout << "What is your first name? ";
getline(cin, f_name);

cout << "What is your last name? ";
getline(cin, l_name);

cout << "What letter grade do you deserve? ";
cin >> grade;

cout << "What is your age? ";
cin >> age;

grade = grade + 1;

cout << "Name: " << l_name << ", " << f_name << endl;
cout << "Grade: " << grade << endl;
cout << "Age: " << age << endl;

return 0;
}


第二题

#include <iostream>
#include <string>

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;
}


第三题

#include <iostream>
//#include <string>
//#include <cstring>
using namespace std;

int main()
{
const int ArSize = 30;
char f_name[ArSize], l_name[ArSize];

cout << "Enter your first name: ";
cin.getline(f_name, ArSize);
cout << "Enter your last name: ";
cin.getline(l_name, ArSize);

strcat(l_name, ", ");
strcat(l_name, f_name);

cout << "Here's the information in a singe string: " << l_name << endl;

return 0;
}

第四题

#include <iostream>
#include <string>
using namespace std;

int main()
{
string f_name, l_name;

cout << "Enter your first name: ";
getline(cin, f_name);
cout << "Enter your last name: ";
getline(cin, l_name);

l_name = l_name + ", " + f_name;

cout << "Here's the information in a single string: " << l_name << "\n";

return 0;
}

第五题

#include <iostream>
using namespace std;

const int ArSize = 20;

typedef struct
{
char brand[ArSize];
double mass;
int calorie;
}CandyBar;

int main()
{
CandyBar snack = {"Mocha Munch", 2.3, 350};

cout << "Brand name: " << snack.brand << endl
<< "Weight is: " << snack.mass << endl
<< "Calorie is: " << snack.calorie << endl;

return 0;
}

第六题

#include <iostream>
using namespace std;

const int ArSize = 20;

typedef struct
{
char brand[ArSize];
double mass;
int calorie;
}CandyBar;

int main()
{
CandyBar snack[3] = {{"Mocha Munch", 2.3, 350},{"nothing wrong", 5.6, 2500},{"unexpectation", 0.6, 15}};

for(int i = 0; i < 3; i ++ )
{
cout << "No." << i+1 << ": " << endl;
cout << "Brand name: " << snack[i].brand << endl
<< "Weight is: " << snack[i].mass << endl
<< "Calorie is: " << snack[i].calorie << endl;
cout << "\n";
}
return 0;
}

第七题

#include <iostream>
using namespace std;

const int ArSize = 20;

typedef struct
{
char CompanyName[ArSize];
double dia;
double weight;
}Pizza;


int main()
{
Pizza a = {};
Pizza * data1 = &a;

cout << "Enter Company's name: ";
cin.get(data1 -> CompanyName, ArSize).get();
cout << "Enter diameter of pizza: ";
cin >> data1 -> dia;
cout << "Enter weight of pizza: ";
cin >> data1 -> weight;

cout << "Company name is " << data1->CompanyName << endl
<< "Pizza's diameter is " << data1->dia << endl
<< "Pizza's weight is " << data1->weight << endl;

return 0;
}

第八题

#include <iostream>
using namespace std;

const int ArSize = 20;

typedef struct
{
char CompanyName[ArSize];
double dia;
double weight;
}Pizza;

int main()
{
Pizza * data1 = new Pizza;

cout << "Enter diameter of pizza: ";
cin >> data1 -> dia;
cout << "Enter Company's name: ";
cin.get();
cin.get(data1 -> CompanyName, ArSize).get();
cout << "Enter weight of pizza: ";
cin >> data1 -> weight;

cout << "Company name is " << data1->CompanyName << endl
<< "Pizza's diameter is " << data1->dia << endl
<< "Pizza's weight is " << data1->weight << endl;

delete data1;

return 0;
}

第九题

#include <iostream>
using namespace std;

const int ArSize = 20;

typedef struct
{
char brand[ArSize];
double mass;
int calorie;
}CandyBar;

int main()
{
CandyBar * data = new CandyBar [3];

for(int i = 0; i < 3; i++ )
{
cout << "No." << i+1 << ":" << endl;
cout << "Enter brand: ";
cin.get((data+i) -> brand, ArSize).get();
cout << "Enter weight: ";
cin >> (data+i) -> mass;
cout << "Enter calorie: ";
cin >> (data+i) -> calorie;
cin.get();
}
cout << endl;

for(int i = 0; i < 3; i++ )
{
cout <<  "No." << i+1 << ":" << endl;
cout << "Brand is " << data[i].brand << endl
<< "Weight is " << data[i].mass << endl
<< "Calorie is " << data[i].calorie << endl;
}

delete [] data;

return 0;
}


第十题

#include <iostream>
#include <array>
using namespace std;

int main()
{
array<double, 3> grade;

cout << "Enter your grades: " << endl;
for(int i = 0; i < 3; i++ )
{
switch (i)
{
case 0:
cout << "First time: ";
break;
case 1:
cout << "Second time: ";
break;
case 2:
cout << "Third time: ";
break;
default:
cout << "How can it happen?";
}
cin >> grade[i];
}
cout << endl;

for(int i = 0; i < 3; i++ )
{
cout << "No." << i+1 << ":" 
<< grade[i] << endl;
}

return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值