1. #include <iostream>
  2. using namespace std;
  3. double HAR_AVG(double, double);
  4. void TEST(bool);
  5.  
  6. int main()
  7. {
  8. double x, y;
  9. cout << "Please enter two values(encountered zero end):\n";
  10. cin >> x;
  11. TEST(cin.fail());
  12. cin >> y;
  13. TEST(cin.fail());
  14. while (x*y != )
  15. {
  16. cout << "The harmonic mean of the two values is " << HAR_AVG(x, y) << ".\n";
  17. cout << "Please enter two values(encountered zero end):\n";
  18. cin >> x;
  19. TEST(cin.fail());
  20. cin >> y;
  21. TEST(cin.fail());
  22. }
  23.  
  24. system("pause");
  25.  
  26. }
  27.  
  28. double HAR_AVG(double x, double y)
  29. {
  30. return 2.0*x*y / (x + y);
  31. }
  32.  
  33. void TEST(bool a)
  34. {
  35. if (a == true)
  36. cout << "Not value!\n";
  37. }

  1. #include <iostream>
  2. using namespace std;
  3. const int Max_Num = ;
  4. int In_put(double *, int);
  5. double Pro_data(double *, int);
  6. void Display(const double *, int, double);
  7.  
  8. int main()
  9. {
  10. double grade[Max_Num];
  11. int count;
  12. double avg_grade;
  13.  
  14. count=In_put(grade, Max_Num);
  15. avg_grade=Pro_data(grade, count);
  16. Display(grade, count, avg_grade);
  17.  
  18. system("pause");
  19. }
  20.  
  21. int In_put(double *grade,int a)
  22. {
  23. cout << "Please enter ten golf scores (press any non-numeric button to end): \n";
  24. int count=;
  25. for (int i = ; i < a; i++)
  26. {
  27. cin >> grade[i];
  28. if (cin.fail())
  29. {
  30. cout << "End!\n";
  31. return count;
  32. }
  33. count++;
  34. }
  35. return count;
  36. }
  37.  
  38. double Pro_data(double *grade, int count)
  39. {
  40. double sum=;
  41. for (int i = ; i < count; i++)
  42. {
  43. sum += grade[i];
  44. }
  45.  
  46. return sum / count;
  47. }
  48.  
  49. void Display(const double *grade, int count, double a)
  50. {
  51. cout << "The grades you entered are:";
  52. for (int i = ; i < count; i++)
  53. {
  54. cout << grade[i] << " ";
  55. }
  56.  
  57. cout << "\nThe average score is " << a << ".\n";
  58. }

  1. #include<iostream>
  2. using namespace std;
  3. const int Max_num = ;
  4.  
  5. struct box
  6. {
  7. char maker[];
  8. float height;
  9. float width;
  10. float length;
  11. float volume;
  12. };
  13.  
  14. bool In_put(box *,int);
  15. void Display(box,int);
  16. void Setting(box *,int);
  17.  
  18. int main()
  19. {
  20. box *abc = new box[Max_num];
  21. int count=;
  22.  
  23. for (int i = ; i < Max_num; i++)
  24. {
  25. if (In_put(abc, i))
  26. break;
  27. count++;
  28. }
  29.  
  30. cout << "You have entered " << count << " boxes.\n";
  31. for (int j = ; j < count; j++)
  32. {
  33. Display(abc[j], j);
  34. }
  35.  
  36. system("pause");
  37. }
  38.  
  39. bool In_put(box *abc, int i)
  40. {
  41. cout << "Please enter the maker of the box, as well as the length, width, and height:\n";
  42. cin >> abc[i].maker;
  43. if (cin.fail())
  44. {
  45. cout << "The maker of the input box has an error.\n";
  46. return cin.fail();
  47. }
  48. cin >> abc[i].length;
  49. if (cin.fail())
  50. {
  51. cout << "The maker of the input length has an error.\n";
  52. return cin.fail();
  53. }
  54. cin >> abc[i].width;
  55. if (cin.fail())
  56. {
  57. cout << "The maker of the input width has an error.\n";
  58. return cin.fail();
  59. }
  60. cin >> abc[i].height;
  61. if (cin.fail())
  62. {
  63. cout << "The maker of the input height has an error.\n";
  64. return cin.fail();
  65. }
  66. Setting(abc,i);
  67. return false;
  68. }
  69.  
  70. void Setting(box *abc, int i)
  71. {
  72. abc[i].volume = abc[i].length*abc[i].width*abc[i].height;
  73. }
  74.  
  75. void Display(box abc,int j)
  76. {
  77. cout << "The " << j+ << "th box has the following information:\n";
  78. cout << "maker:" << abc.maker << endl;
  79. cout << "length:" << abc.length << endl;
  80. cout << "width:" << abc.width << endl;
  81. cout << "height:" << abc.height << endl;
  82. cout << "volume:" << abc.volume << endl;
  83. }

  1. #include <iostream>
  2. long double probability(unsigned, unsigned, unsigned, unsigned);
  3. int main()
  4. {
  5. using namespace std;
  6. double total1, choices1;
  7. double total2, choices2;
  8. cout << "Enter the total number of Domain choices on the game card and\n"
  9. "the number of picks allowed:\n";
  10. cin >> total1 >> choices1;
  11. cout << "Enter the total number of Special choices on the game card and\n"
  12. "the number of picks allowed:\n";
  13. cin >> total2 >> choices2;
  14. while (!(cin.fail()) && choices1 <= total1 && choices2 <= total2)
  15. {
  16. cout << "You have one chance in ";
  17. cout << probability(total1, choices1, total2, choices2);
  18. cout << " of winning.\n";
  19. cout << "Next four numbers (Non-numeric to quit): ";
  20. cin >> total1 >> choices1;
  21. cin >> total2 >> choices2;
  22. }
  23. cout << "bye\n";
  24. system("pause");
  25. return ;
  26. }
  27.  
  28. long double probability(unsigned numbers1, unsigned picks1, unsigned numbers2, unsigned picks2)
  29. {
  30. long double result = 1.0; // here come some local variables
  31. long double n;
  32. unsigned p;
  33.  
  34. for (n = numbers1, p = picks1; p > ; n--, p--)
  35. result = result * n / p;
  36. for (n = numbers2, p = picks2; p > ; n--, p--)
  37. result = result * n / p;
  38. return result;
  39. }

  1. #include <iostream>
  2. using namespace std;
  3. unsigned long factorial(unsigned int);
  4.  
  5. int main()
  6. {
  7. unsigned int pp;
  8. unsigned long qq;
  9. cout << "Please enter a non-negative number:";
  10. if (cin >> pp)
  11. {
  12. qq = factorial(pp);
  13. cout << "The factorial of this value is:" << qq << endl;
  14. }
  15. system("pause");
  16. }
  17.  
  18. unsigned long factorial(unsigned int pp)
  19. {
  20. unsigned long qq;
  21. if (pp > )
  22. qq = pp * factorial(pp - );
  23. else
  24. return ;
  25. return qq;
  26. }

  1. #include <iostream>
  2. using namespace std;
  3. const int Max_num = ;
  4. int Fill_array(double *, int);
  5. void Show_array(const double *, int);
  6. void Reverse_array(double *, int);
  7.  
  8. int main()
  9. {
  10. double abc[Max_num];
  11. int num_rel;
  12. num_rel=Fill_array(abc, Max_num);
  13. Show_array(abc, num_rel);
  14. Reverse_array(abc, num_rel);
  15. Show_array(abc, num_rel);
  16. Reverse_array(abc+, num_rel-);
  17. Show_array(abc, num_rel);
  18.  
  19. system("pause");
  20. }
  21.  
  22. int Fill_array(double *abc, int t)
  23. {
  24. int count_num = ;
  25. cout << "Please enter no more than" << t << "digits (experiencing non-numeric exits):\n";
  26. for (int i = ; i < t; i++)
  27. {
  28. if (!(cin >> abc[i]))
  29. break;
  30. count_num++;
  31. }
  32. cout << "Input completed.\n";
  33. return count_num;
  34. }
  35.  
  36. void Show_array(const double *abc, int t)
  37. {
  38. for (int i = ; i < t; i++)
  39. {
  40. cout << abc[i] << endl;
  41. }
  42. }
  43.  
  44. void Reverse_array(double *abc, int t)
  45. {
  46. cout << "Reverse!" << endl;
  47. int i = , j = t - ;
  48. double qqq;
  49. while (j > i)
  50. {
  51. qqq = abc[i];
  52. abc[i] = abc[j];
  53. abc[j] = qqq;
  54. i++;
  55. j--;
  56. }
  57. }

  1. #include <iostream>
  2. const int Max = ;
  3.  
  4. double * fill_array(double *, double *);
  5. void show_array(const double *, const double *);
  6. void revalue(double, double *, double *);
  7.  
  8. int main()
  9. {
  10. using namespace std;
  11. double properties[Max];
  12.  
  13. double *end = fill_array(properties, properties+Max);
  14. show_array(properties, end);
  15. if (end > properties)
  16. {
  17. cout << "Enter revaluation factor: ";
  18. double factor;
  19. while (!(cin >> factor))
  20. {
  21. cin.clear();
  22. while (cin.get() != '\n')
  23. continue;
  24. cout << "Bad input; Please enter a number: ";
  25. }
  26. revalue(factor,properties, end);
  27. show_array(properties, end);
  28. }
  29. cout << "Done.\n";
  30.  
  31. system("pause");
  32. }
  33.  
  34. double * fill_array(double * ar, double * br)
  35. {
  36. using namespace std;
  37. double temp;
  38. double *i;
  39. int j = ;
  40. for (i = ar; i<br; i++,j++)
  41. {
  42. cout << "Enter value #" << (j + ) << ": ";
  43. cin >> temp;
  44. if (!cin)
  45. {
  46. cin.clear();
  47. while (cin.get() != '\n')
  48. continue;
  49. cout << "Bad input; input process terminated.\n";
  50. break;
  51. }
  52. else if (temp < )
  53. break;
  54. *i = temp;
  55. }
  56. return i;
  57. }
  58.  
  59. void show_array(const double *ar, const double *br)
  60. {
  61. using namespace std;
  62. const double *i;
  63. int j = ;
  64. for (i = ar; i < br; i++,j++)
  65. {
  66. cout << "Property #" << (j + ) << ": $";
  67. cout << *i << endl;
  68. }
  69. }
  70.  
  71. void revalue(double r,double *ar,double *br)
  72. {
  73. double *i;
  74. for (i = ar; i < br; i++)
  75. (*i)*= r;
  76. }

  1. #include <iostream>
  2. #include <string>
  3. const int Seasons = ;
  4. const char * Snames[Seasons] = { "Spring", "Summer", "Fall", "Winter" };
  5.  
  6. int main()
  7. {
  8. double expenses[Seasons];
  9. fill(expenses);
  10. show(expenses);
  11. system("pause");
  12. }
  13.  
  14. void fill(double * pa)
  15. {
  16. for (int i = ; i < Seasons; i++)
  17. {
  18. std::cout << "Enter " << Snames[i] << " expenses: ";
  19. std::cin >> pa[i];
  20. }
  21. }
  22.  
  23. void show(double * da)
  24. {
  25. double total = 0.0;
  26. std::cout << "\nEXPENSES\n";
  27. for (int i = ; i < Seasons; i++)
  28. {
  29. std::cout << Snames[i] << ": $" << da[i] << '\n';
  30. total += da[i];
  31. }
  32. std::cout << "Total: $" << total << '\n';
  33. }
  1. #include <iostream>
  2. #include <string>
  3. const int Seasons = ;
  4. const char * Snames[Seasons] = { "Spring", "Summer", "Fall", "Winter" };
  5. struct expenses
  6. {
  7. double expenses;
  8. };
  9. void fill(expenses *);
  10. void show(expenses *);
  11.  
  12. int main()
  13. {
  14. expenses *cost = new expenses[Seasons];
  15. fill(cost);
  16. show(cost);
  17. system("pause");
  18. }
  19.  
  20. void fill(expenses * pa)
  21. {
  22. for (int i = ; i < Seasons; i++)
  23. {
  24. std::cout << "Enter " << Snames[i] << " expenses: ";
  25. std::cin >> pa[i].expenses;
  26. }
  27. }
  28.  
  29. void show(expenses * da)
  30. {
  31. double total = 0.0;
  32. std::cout << "\nEXPENSES\n";
  33. for (int i = ; i < Seasons; i++)
  34. {
  35. std::cout << Snames[i] << ": $" << da[i].expenses << '\n';
  36. total += da[i].expenses;
  37. }
  38. std::cout << "Total: $" << total << '\n';
  39. }

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

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. double add(double, double);
  5. double sub(double, double);
  6. double calculate(double, double, double (double, double));
  7.  
  8. int main()
  9. {
  10. double a, b, c, d;
  11. cout << "Please enter two values:\n";
  12. while ((cin >> a) && (cin >> b))
  13. {
  14. c=calculate(a, b, add);
  15. d=calculate(a, b, sub);
  16. cout << "The sum and subtraction of the values are:" << c << "," << d << endl;
  17. cout << "Please continue to enter:\n";
  18. }
  19. system("pause");
  20. }
  21.  
  22. double add(double a, double b)
  23. {
  24. double c;
  25. c = a + b;
  26. return c;
  27. }
  28.  
  29. double sub(double a, double b)
  30. {
  31. double d;
  32. d = a - b;
  33. return d;
  34. }
  35.  
  36. double calculate(double a, double b, double cal(double, double))
  37. {
  38. double e;
  39. e = cal(a, b);
  40. return e;
  41. }

c++ primer plus 第七章 课后题答案的更多相关文章

  1. c++ primer plus 第六章 课后题答案

    #include <iostream> #include <cctype> using namespace std; int main() { char in_put; do ...

  2. c++ primer plus 第五章 课后题答案

    #include <iostream> using namespace std; int main() { ; cout << "Please enter two n ...

  3. c++ primer plus 第四章 课后题答案

    #include<iostream> #include<string> using namespace std; int main() { string first_name; ...

  4. c++ primer plus 第三章 课后题答案

    #include<iostream> using namespace std; int main() { ; int shen_gao; cout <<"Please ...

  5. c++ primer plus 第二章 课后题答案

    #include<iostream> using namespace std; int main() { cout << "My name is Jiantong C ...

  6. python核心编程第5章课后题答案

    5-8Geometry import math def sqcube(): s = float(raw_input('enter length of one side: ')) print 'the ...

  7. python核心编程第4章课后题答案(第二版75页)

    4-1Python objects All Python objects have three attributes:type,ID,and value. All are readonly with ...

  8. python核心编程第3章课后题答案(第二版55页)

    3-4Statements Ues ; 3-5Statements Use\(unless part of a comma-separated sequence in which case \ is ...

  9. python核心编程第2章课后题答案(第二版36页)

    2-5 Loops and Numbers a) i = 0    while i <11:     print i    i += 1 b) for i in range(0,11): pri ...

随机推荐

  1. iOS 第三方框架-MJRefresh

    MJRefresh是一款非常好用的上拉下拉第三方库,使用也很简单.github地址: https://github.com/CoderMJLee/MJRefresh . 下拉刷新 官方给过来的例子很简 ...

  2. iOS UI基础-3.0图片浏览器及plist使用

    需求: 1.显示当前图片序号/总图片数 2.显示图片 3.上一张图片.下一张图片转换 4.显示图片描述 下面用代码来实现 // // UYViewController.m // 3.0图片查看器 // ...

  3. html select 和dropdownlist小结收集

    //html select var x = $("#selectSort").val();  //获取选中的value值 获取select选中的索引: $("#selec ...

  4. C语言头文件、库文件的查找路径

    在 程序设计中,文件包含是很有用的.一个大的程序可以分为多个模块,由多个程序员分别编程.有些公用的符号常量或宏定义等可单独组成一个文件,在其它文件的开头用包含命令包含该文件即可使用.这样,可避免在每个 ...

  5. win10安装后耳机有声音而外放无声音

    安装win10后耳机声音正常,而外放没声音.检查了小喇叭.播放器和设备管理器一切正常,驱动也重装了好些次.喇叭音量设置.视频音量设置均正常.花费很多时间,结果发现是键盘上有个小喇叭+的键的问题.按fn ...

  6. SLF4J和log4j的使用

    概念 SLF4J:即简单日志门面(Simple Logging Facade for Java),不是具体的日志解决方案,它只服务于各种各样的日志系统.按照官方的说法,SLF4J是一个用于日志系统的简 ...

  7. OVS中的key解析

    OVS在处理每条流的时候,先根据每条流生产相应的key,然后根据key匹配相应的流表,根据流表中的action操作来处理每条流,本文对key的结构体进行分析,看看对于一条流会提出那些特征信息.对于ke ...

  8. OAuth : open Authorization 开发授权

    OAuth : open Authorization 开发授权 用户访问慕课网,慕课网请求OAuth登陆页面,用户输入QQ号码和密码,这个页面的域名不属于慕课网是属于QQ的,随后把结果给慕课网,这个结 ...

  9. Python入门之python可变对象与不可变对象

    本文分为如下几个部分 概念 地址问题 作为函数参数 可变参数在类中使用 函数默认参数 类的实现上的差异 概念 可变对象与不可变对象的区别在于对象本身是否可变. python内置的一些类型中 可变对象: ...

  10. RAM,ROM,NAND Flash,NOR Flash(A)

    他们四者相互独立 RAM掉电易失数据: RAM又分两种,一种是静态RAM,SRAM:一种是动态RAM,DRAM.前者的存储速度要比后者快得多,我们现在使用的内存一般都是动态RAM. DDR是Doubl ...