C++ Primer Plus(第6版)Chapter 4 编程题答案

第1题:

//  task 1
#include <iostream>
#include <string>
#include <cstring>
#include <array>int main()
{using namespace std;cout << "What is your first name? ";   char fname[20];cin.getline(fname, 20);       //getline(), 因为Betty Sue之间有空格cout << "What is your last name? ";char lname[20];cin.getline(lname, 20);   //Yewecout << "What letter grade do you deserve? "; char score;cin >> score;   //Bcout << "What is your age? ";int age;cin >> age;  //22cout << "Name: " << lname << ", " << fname << endl;cout << "Grade: " << char(score + 1) << endl;cin.get();cin.get();return 0;
}

第2题:

//  task 2
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 << "T have some delicious " << dessert;cout << " for you, " << name << ".\n";cin.get();cin.get();return 0;
}

第3题:

//  task 3
int main()
{using namespace std;cout << "Enter your first name: ";char fname[20];cin.getline(fname, 20);cout << "Enter your last name: ";char lname[20];cin.getline(lname, 20);char name[40];strcpy_s(name, lname);    //加上  _s  for safestrcat_s(name, ", ");strcat_s(name, fname);cout << "Here's the information in a single string: " << name;cin.get();cin.get();return 0;
}

第4题:

//  task 4
int main()
{using namespace std;string fname, lname, name;cout << "Enter your first name: ";getline(cin, fname);cout << "Enter your last name: ";getline(cin, lname);name = lname + ", " + fname;cout << "Here's the information in a single string: " << name;cin.get();cin.get();return 0;
}

第5题:

//  task 5
int main()
{using namespace std;struct CandyBar{   char band[20];float weight;int colory;};CandyBar snack = { "Mocha Munch", 2.3, 350 };cout << "The band is " << snack.band << endl;cout << "The weight is " << snack.weight << endl;cout << "The kaluli is " << snack.colory << endl;cin.get();cin.get();return 0;
}

第6题:

//  task 6
int main()
{using namespace std;struct CandyBar{char band[20];float weight;int colr;};// 创建一个结构数组并初始化CandyBar snacks[3] = { { "Mocha Munch", 2.3, 350 }, { "star bucks", 1.8, 200 }, { "hagendasi", 4.9, 638 } };cout << "The 1st band is " << snacks->band << endl;cout << "The 1st weight is " << snacks->weight << endl;cout << "The 1st kaluli is " << snacks->colr << endl;cout << "The 2st band is " << (snacks + 1)->band << endl;cout << "The 2st weight is " << (snacks + 1)->weight << endl;cout << "The 2st kaluli is " << (snacks + 1)->colr << endl;cout << "The 3st band is " << (snacks + 2)->band << endl;cout << "The 3st weight is " << (snacks + 2)->weight << endl;cout << "The 3st kaluli is " << (snacks + 2)->colr << endl;cin.get();cin.get();return 0;
}

第7题:

//  task 7
int main()
{using namespace std;struct pizza{string name;float d;float weight;};pizza bishengke;cout << "Enter the name: ";getline(cin, bishengke.name);cout << "Enter the zhijing: ";cin >> bishengke.d;cout << "Enter the weight: ";cin >> bishengke.weight;cout << "The band is: " << bishengke.name << endl;cout << "The zhijing is: " << bishengke.d << endl;cout << "The weight is: " << bishengke.weight << endl;cin.get();cin.get();return 0;
}

第8题:

//  task 8
int main()
{using namespace std;struct pizza{string name;float d;float weight;};pizza* pt = new pizza;cout << "Enter the zhijing: ";cin >> pt->d;cout << "Enter the name: ";cin.get();   //用来吃掉回车,不能少getline(cin, pt->name);cout << "Enter the weight: ";cin >> pt->weight;cout << "The band is: " << pt->name << endl;cout << "The zhijing is: " << pt->d << endl;cout << "The weight is: " << pt->weight << endl;cin.get();cin.get();return 0;
}

第9题:

//  task 9
int main()
{using namespace std;struct CandyBar{char band[20];float weight;int colr;};// 动态创建一个结构数组CandyBar* snacks = new CandyBar[3];snacks[0] = { "Mocha Munch", 2.3, 350 };snacks[1] = { "Star Bucks", 1.8, 200 };snacks[2] = { "HaGenDaSi", 4.9, 645 };cout << "The 1st band is " << snacks->band << endl;cout << "The 1st weight is " << snacks->weight << endl;cout << "The 1st kaluli is " << snacks->colr << endl;cout << "The 2st band is " << (snacks + 1)->band << endl;cout << "The 2st weight is " << (snacks + 1)->weight << endl;cout << "The 2st kaluli is " << (snacks + 1)->colr << endl;cout << "The 3st band is " << (snacks + 2)->band << endl;cout << "The 3st weight is " << (snacks + 2)->weight << endl;cout << "The 3st kaluli is " << (snacks + 2)->colr << endl;delete[] snacks;cin.get();cin.get();return 0;
}

第10题:

//  task 10
int main()
{using namespace std;cout << "Enter the three score: ";array<double, 3> scores;cin >> scores[0];cin >> scores[1];cin >> scores[2];double av = (scores[0] + scores[1] + scores[2]) / 3;cout << "The average score of 3 is: " << av << endl;cin.get();cin.get();return 0;
}

C++ Primer Plus(第6版)Chapter 4 编程题答案相关推荐

  1. **PTA:浙大版《C语言程序设计(第3版)》编程题答案*

    PTA:浙大版<C语言程序设计(第3版)>编程题答案 练习2-3 输出倒三角图案 练习2-4 温度转换 练习2-6 计算物体自由下落的距离 练习2-8 计算摄氏温度 练习2-9 整数四则运 ...

  2. C ++ Primer Plus 第六版 第九章编程练习答案

    2.修改程序清单9.9,用string对象代替字符数组.这样,该程序将不再需要检查输入的字符串是否过长,同时可以将输入字符串同字符串""进行比较,以判断是否为空行. #includ ...

  3. C++ Primer Plus 第六版第二章编程练习答案

    1.编写一个c++程序,它显示您的姓名和地址. #include<iostream> int main() {using namespace std;cout << " ...

  4. C Primer Plus 第六版 章节课后编程练习答案(下)(缘更)

    接上次写的前五章的答案,这次更新后面章节的 PS:目录在左边袄┗|`O′|┛ ~~ 第六章 6.1 #include <stdio.h> int main(void) {char lett ...

  5. C Primer Plus 第7章之菜鸟儿的编程题答案

    本文仅为记录自己手打编程题答案所用,不能保证正确性,欢迎大家学习交流,有问题请随时指出~ 1.原版: #include<stdio.h>int main(void) {int i = 0; ...

  6. 《精通labview教程——由浅入深的范例学习(第二版)》课后题答案

    <精通labview教程--由浅入深的范例学习(第二版)>课后题答案,[美]John Essick著,[译]邓科等 课后题答案偶数部分是教程自带答案,奇数题目是自制答案 下载链接: 第一章 ...

  7. 《C++ Primer Plus(第六版)》(17)(第十章 对象和类 编程题答案)

    10.10编程题 1. Test.h #ifndef _Test_H_ #define _Test_H_ #include <iostream> #include <string&g ...

  8. C++primer plus第六版课后编程题答案8.3(正解)

    在百度知道里面得到了正确的答案 http://zhidao.baidu.com/question/198940026560129285.html?quesup2&oldq=1 #include ...

  9. C++primer plus第六版课后编程题答案8.6

    8.6 #include <iostream> #include <string> using namespace std;template <typename AnyT ...

最新文章

  1. 关于机器翻译的三个话题的讨论
  2. spring MVC - Inteceptors(拦截器)
  3. 《Java程序员,上班那点事儿》书名的由来
  4. 30个流行的jQuery Plugins
  5. junit测试@注解
  6. 【Java线程】简单实现带界面的一对一聊天
  7. Android中handler的使用及原理---学习笔记
  8. mac adb 找不到设备_win/Mac办公软件下载找不到资源?试试这三个强大的神器
  9. __stdcall函数调用约定
  10. Springboot启动报错Error handling failed
  11. day049--jQuery文档操作示例
  12. (附源码)spring boot西安市中小学生护眼平台开发 毕业设计 080855
  13. 深入浅出Yolo系列之Yolov3Yolov4Yolov5Yolox核心基础知识完整讲解
  14. 彻底清除Zencart的cache文件夹myDEBUG.log文件
  15. 关于STM8CAN产生bus-off如何自动恢复
  16. 联发科毫米波雷达解决方案芯片MT2706(Autus R10)
  17. 毕业生的档案都有什么意义,为什么说它那么重要|智测优聘总结
  18. Python学习笔录(四)--- 数据结构
  19. Spring 的 init-method 和 destory-method
  20. MFC 对话框打印和打印预览知识总结

热门文章

  1. 苹果App Store申请和管理相关知识
  2. [事务] 编程式事务和声明式事务的优缺点
  3. MySQL运行退出命令
  4. SpringBoot整合过滤器、拦截器
  5. html video 修改封面,html5自定义video标签的海报与播放按钮功能
  6. Spring Cloud Gateway 结合 OAuth2 提供 UAA 服务,来袭。
  7. Spring Cloud Gateway 结合OAuth2提供UAA服务
  8. 理解adapterview
  9. GPU与CPU、显卡、显存关系
  10. qt中configure参数配置说明