p221.8

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<cstring>
  4. using namespace std;
  5. const int SLEN = ;
  6. struct student{
  7. char fullname[SLEN];
  8. char hobby[SLEN];
  9. int ooplevel;
  10. };
  11. int getinfo(student pa[], int n);
  12. void display1(student st);
  13. void display2(const student *ps);
  14. void display3(const student pa[], int n);
  15.  
  16. int main(){
  17. cout << "enter class size:";
  18. int class_size;
  19. cin >> class_size;
  20. while (cin.get() != '\n')
  21. continue;
  22. student *ptr_stu = new student[class_size];
  23. int entered = getinfo(ptr_stu, class_size);
  24. for (int i = ; i < entered; i++){
  25. display1(ptr_stu[i]);
  26. display2(&ptr_stu[i]);
  27. }
  28. display3(ptr_stu, entered);
  29. delete[]ptr_stu;
  30. cout << "done\n";
  31. system("pause");
  32. return ;
  33. }
  34.  
  35. int getinfo(student pa[], int n){
  36. int i = , count = ;
  37. while (i < n && *pa[i].fullname != '\0'){
  38. cin.getline(pa[i].fullname, SLEN);
  39. cin.getline(pa[i].hobby, SLEN);
  40. cin >> pa[i].ooplevel;
  41. cin.get();
  42. count++;
  43. i++;
  44. }
  45. return count;
  46. }
  47.  
  48. void display1(student st){
  49. cout << "st.fullname" << st.fullname <<
  50. endl << "st.hobby" << st.hobby <<
  51. endl << "st.ooplevel" << st.ooplevel <<
  52. endl;
  53. }
  54.  
  55. void display2(const student *ps){
  56. cout << "ps->fullname" << ps->fullname <<
  57. endl << "ps->hobby" << ps->hobby <<
  58. endl << "ps->ooplevel" << ps->ooplevel <<
  59. endl;
  60. }
  61.  
  62. void display3(const student pa[], int n){
  63. for (int i = ; i < n; i++){
  64. cout << "pa[i].fullname" << pa[i].fullname
  65. << endl << "pa[i].hobby" << pa[i].hobby
  66. << endl << "pa[i].ooplevel" << pa[i].ooplevel
  67. << endl;
  68. }
  69. }

p259.2

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<cstring>
  4. using namespace std;
  5. struct candybar {
  6. char brand[];
  7. float weight;
  8. int calory;
  9. };
  10. void assign(candybar &, char br[]="millennium munch", float w=2.85, int c=);
  11. void show_menu(candybar&);
  12.  
  13. int main(){
  14. candybar tasty;
  15. assign(tasty);
  16. show_menu(tasty);
  17. system("pause");
  18. return ;
  19. }
  20.  
  21. void assign(candybar &delicious, char br[], float w , int c){
  22. strcpy(delicious.brand, br);
  23. delicious.weight = w;
  24. delicious.calory = c;
  25. }
  26.  
  27. void show_menu(candybar&delicious){
  28. cout << delicious.brand << endl;
  29. cout << delicious.weight << endl;
  30. cout << delicious.calory << endl;
  31. }

p259.4

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<cctype>
  4. using namespace std;
  5. struct stringy{
  6. char* str;
  7. int ct;
  8. };
  9. void set(stringy &, const char*);
  10. void show(const stringy &, int times = );
  11. void show(const char*, int times = );
  12.  
  13. int main(){
  14. stringy beany;
  15. char testing[] = "reality isn't what it used to be.";
  16. set(beany, testing);
  17. show(beany);
  18. show(beany, );
  19. delete beany.str;
  20. testing[] = 'D';
  21. testing[] = 'u';
  22. show(testing);
  23. show(testing, );
  24. show("Done!");
  25. system("pause");
  26. return ;
  27. }
  28.  
  29. void set(stringy &beany, const char* testing){
  30. int num = strlen(testing);
  31. beany.str = new char[num + ];
  32. strcpy(beany.str, testing);
  33. beany.ct = num;
  34. }
  35.  
  36. void show(const stringy &beany, int times){
  37. for (int i = ; i < times; i++)
  38. cout << beany.str << endl
  39. << beany.ct << endl;
  40. }
  41.  
  42. void show(const char* testing, int times){
  43. for (int i = ; i < times; i++)
  44. cout << testing << endl;
  45. }

p260.6

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<cstring>
  4. using namespace std;
  5. template<class T>
  6. T maxn(T ar[], int num);
  7. template<> char *maxn(char *pt[], int num);
  8.  
  9. int main(){
  10. int num1[];
  11. double num2[];
  12. cout << "enter the member of two array\n";
  13. for (int i = ; i < ; i++)
  14. cin >> num1[i];
  15. for (int i = ; i < ; i++)
  16. cin >> num2[i];
  17. int imax = maxn(num1, );
  18. double dmax = maxn(num2, );
  19. cout << "imax is" << imax << endl
  20. << "dmax is" << dmax << endl;
  21. char *str[];
  22. str[] = "his eyes came round";
  23. str[] = "it all seems very unchanged";
  24. str[] = "which contain of old the pipes";
  25. str[] = "yes, billy, i know";
  26. str[] = "will you be please to dine";
  27. char *address = maxn(str, );
  28. cout << (int*)address << " " <<
  29. address << endl;
  30. system("pause");
  31. return ;
  32. }
  33.  
  34. template<class T>
  35. T maxn(T ar[], int num){
  36. T max=ar[];
  37. for (int i = ; i < num; i++)
  38. if (max < ar[i])
  39. max = ar[i];
  40. return max;
  41. }
  42.  
  43. template<> char *maxn(char *pt[], int num){
  44. int length[], i;
  45. for (i = ; i < ; i++)
  46. length[i] = strlen(pt[i]);
  47. int max = length[];
  48. for (i = ; i < ; i++)
  49. if (max < length[i])
  50. max = length[i];
  51. for (i = ; i < ; i++)
  52. if (max == length[i])
  53. break;
  54. return pt[i];
  55. }

c++ primer plus 习题答案(2)的更多相关文章

  1. c++ primer plus 习题答案(1)

    c++ primer plus 习题答案用的是第五版,IDE仍然是vs2013.我只标注了题号,具体的题目找下书上对应内容吧. p110.8 #include<iostream> #inc ...

  2. c++ primer plus 习题答案(8)

    p475.2 //头文件: class Cd{ private: char *performers; char *label; int selections; double playtime; pub ...

  3. c++ primer plus 习题答案(7)

    p427.4 //头文件: #include<iostream> #ifndef STACK_H_ #define STACK_H_ typedef unsigned long Item; ...

  4. c++ primer plus 习题答案(6)

    p425.1 #include<iostream> #include<cstring> #include<cstdlib> using namespace std; ...

  5. c++ primer plus 习题答案(5)

    p333.7 #include<iostream> #include<cstring> #include<cstdlib> using namespace std; ...

  6. c++ primer plus 习题答案(4)

    p333.3 #include<iostream> #include<cstdlib> #include<cstring> #include<string&g ...

  7. c++ primer plus 习题答案(3)

    p296.3 #include<iostream> #include<cstdlib> #include<string> #include<cstring&g ...

  8. C++Primer第五版——习题答案目录

    目前正在刷<C++Primer>这本书,会在博客上记录课后习题答案,答案仅供参考. 因为水平有限,如有有误之处,希望大家不吝指教,谢谢! 目录地址 使用的系统为:win 10,编译器:VS ...

  9. 《C++Primer》第五版习题答案--第五章【学习笔记】

    <C++Primer>第五版习题答案--第五章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/15 第五章:语句 ...

随机推荐

  1. [LeetCode][Python]Container With Most Water

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/container-with-most-water/ Given n non-neg ...

  2. ubuntu14.04 Markdown编辑器推荐之Remarkable

    如今已经习惯了用Markdown编辑器写博文的习惯,那么ubuntu以下有什么好用的呢?搜索中发现了这个叫Remarkable的免费Markdown编辑器.为什么推荐这个呢?说说它的特点: 实时预览 ...

  3. 深入理解Linux网络技术内幕——中断与网络驱动程序

    接收到帧时通知驱动程序     在网络环境中.设备(网卡)接收到一个数据帧时,须要通知驱动程序进行处理. 有一下几种通知机制: 轮询:     内核不断检查设备是否有话要说.(比較耗资源,但在一些情况 ...

  4. ASPxGridView-单元格合并

    <dx:ASPxGridView ID="gridView" runat="server" ClientInstanceName="gvResu ...

  5. 保护眼睛,绿豆沙颜色的RGB值和HSL值

    现在的人尤其是职场中人,每天都得花很长时间对着电脑,对眼睛的伤害很大,其实我们可以对电脑进行一个简单的设置,把窗口背景设置成绿豆沙颜色的,对眼睛的保护很有帮助的. 下面是绿豆沙颜色的RGB值和HSL值 ...

  6. ubuntu10.04 安装NVIDIA GT 420M驱动

    安装ubuntu已经好几天了,由于显卡驱动没装,屏幕在600X800下的效果很难看,于是就想办法,查阅资料终于安装成功了,下面将我的安装方法记录下来以供大家参考. 借鉴:ubuntu12.04下安装N ...

  7. spring schema自定义

    今天看了一下分布式服务框架的那本书,于是里面提到了spring schema的自定义,于是去简单的了解了一下 参考资源:spring schema扩展: http://www.yihaomen.com ...

  8. HelloWorld——Cocos2d-x学习历程(二)

    HelloWorld分析: 1."resource"文件夹 该文件夹主要用于存放游戏中需要的图片.音频和配置等资源文件. 2."include"和"s ...

  9. nginx install lua module

    #install luajit #http://luajit.org/download.html .tar.gz cd LuaJIT- make install PREFIX=/home/allen. ...

  10. Spring学习之常用注解(转)

    使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base-package ...