c++ primer plus 第七章 课后题答案
- #include <iostream>
- using namespace std;
- double HAR_AVG(double, double);
- void TEST(bool);
- int main()
- {
- double x, y;
- cout << "Please enter two values(encountered zero end):\n";
- cin >> x;
- TEST(cin.fail());
- cin >> y;
- TEST(cin.fail());
- while (x*y != )
- {
- cout << "The harmonic mean of the two values is " << HAR_AVG(x, y) << ".\n";
- cout << "Please enter two values(encountered zero end):\n";
- cin >> x;
- TEST(cin.fail());
- cin >> y;
- TEST(cin.fail());
- }
- system("pause");
- }
- double HAR_AVG(double x, double y)
- {
- return 2.0*x*y / (x + y);
- }
- void TEST(bool a)
- {
- if (a == true)
- cout << "Not value!\n";
- }
- #include <iostream>
- using namespace std;
- const int Max_Num = ;
- int In_put(double *, int);
- double Pro_data(double *, int);
- void Display(const double *, int, double);
- int main()
- {
- double grade[Max_Num];
- int count;
- double avg_grade;
- count=In_put(grade, Max_Num);
- avg_grade=Pro_data(grade, count);
- Display(grade, count, avg_grade);
- system("pause");
- }
- int In_put(double *grade,int a)
- {
- cout << "Please enter ten golf scores (press any non-numeric button to end): \n";
- int count=;
- for (int i = ; i < a; i++)
- {
- cin >> grade[i];
- if (cin.fail())
- {
- cout << "End!\n";
- return count;
- }
- count++;
- }
- return count;
- }
- double Pro_data(double *grade, int count)
- {
- double sum=;
- for (int i = ; i < count; i++)
- {
- sum += grade[i];
- }
- return sum / count;
- }
- void Display(const double *grade, int count, double a)
- {
- cout << "The grades you entered are:";
- for (int i = ; i < count; i++)
- {
- cout << grade[i] << " ";
- }
- cout << "\nThe average score is " << a << ".\n";
- }
- #include<iostream>
- using namespace std;
- const int Max_num = ;
- struct box
- {
- char maker[];
- float height;
- float width;
- float length;
- float volume;
- };
- bool In_put(box *,int);
- void Display(box,int);
- void Setting(box *,int);
- int main()
- {
- box *abc = new box[Max_num];
- int count=;
- for (int i = ; i < Max_num; i++)
- {
- if (In_put(abc, i))
- break;
- count++;
- }
- cout << "You have entered " << count << " boxes.\n";
- for (int j = ; j < count; j++)
- {
- Display(abc[j], j);
- }
- system("pause");
- }
- bool In_put(box *abc, int i)
- {
- cout << "Please enter the maker of the box, as well as the length, width, and height:\n";
- cin >> abc[i].maker;
- if (cin.fail())
- {
- cout << "The maker of the input box has an error.\n";
- return cin.fail();
- }
- cin >> abc[i].length;
- if (cin.fail())
- {
- cout << "The maker of the input length has an error.\n";
- return cin.fail();
- }
- cin >> abc[i].width;
- if (cin.fail())
- {
- cout << "The maker of the input width has an error.\n";
- return cin.fail();
- }
- cin >> abc[i].height;
- if (cin.fail())
- {
- cout << "The maker of the input height has an error.\n";
- return cin.fail();
- }
- Setting(abc,i);
- return false;
- }
- void Setting(box *abc, int i)
- {
- abc[i].volume = abc[i].length*abc[i].width*abc[i].height;
- }
- void Display(box abc,int j)
- {
- cout << "The " << j+ << "th box has the following information:\n";
- cout << "maker:" << abc.maker << endl;
- cout << "length:" << abc.length << endl;
- cout << "width:" << abc.width << endl;
- cout << "height:" << abc.height << endl;
- cout << "volume:" << abc.volume << endl;
- }
- #include <iostream>
- long double probability(unsigned, unsigned, unsigned, unsigned);
- int main()
- {
- using namespace std;
- double total1, choices1;
- double total2, choices2;
- cout << "Enter the total number of Domain choices on the game card and\n"
- "the number of picks allowed:\n";
- cin >> total1 >> choices1;
- cout << "Enter the total number of Special choices on the game card and\n"
- "the number of picks allowed:\n";
- cin >> total2 >> choices2;
- while (!(cin.fail()) && choices1 <= total1 && choices2 <= total2)
- {
- cout << "You have one chance in ";
- cout << probability(total1, choices1, total2, choices2);
- cout << " of winning.\n";
- cout << "Next four numbers (Non-numeric to quit): ";
- cin >> total1 >> choices1;
- cin >> total2 >> choices2;
- }
- cout << "bye\n";
- system("pause");
- return ;
- }
- long double probability(unsigned numbers1, unsigned picks1, unsigned numbers2, unsigned picks2)
- {
- long double result = 1.0; // here come some local variables
- long double n;
- unsigned p;
- for (n = numbers1, p = picks1; p > ; n--, p--)
- result = result * n / p;
- for (n = numbers2, p = picks2; p > ; n--, p--)
- result = result * n / p;
- return result;
- }
- #include <iostream>
- using namespace std;
- unsigned long factorial(unsigned int);
- int main()
- {
- unsigned int pp;
- unsigned long qq;
- cout << "Please enter a non-negative number:";
- if (cin >> pp)
- {
- qq = factorial(pp);
- cout << "The factorial of this value is:" << qq << endl;
- }
- system("pause");
- }
- unsigned long factorial(unsigned int pp)
- {
- unsigned long qq;
- if (pp > )
- qq = pp * factorial(pp - );
- else
- return ;
- return qq;
- }
- #include <iostream>
- using namespace std;
- const int Max_num = ;
- int Fill_array(double *, int);
- void Show_array(const double *, int);
- void Reverse_array(double *, int);
- int main()
- {
- double abc[Max_num];
- int num_rel;
- num_rel=Fill_array(abc, Max_num);
- Show_array(abc, num_rel);
- Reverse_array(abc, num_rel);
- Show_array(abc, num_rel);
- Reverse_array(abc+, num_rel-);
- Show_array(abc, num_rel);
- system("pause");
- }
- int Fill_array(double *abc, int t)
- {
- int count_num = ;
- cout << "Please enter no more than" << t << "digits (experiencing non-numeric exits):\n";
- for (int i = ; i < t; i++)
- {
- if (!(cin >> abc[i]))
- break;
- count_num++;
- }
- cout << "Input completed.\n";
- return count_num;
- }
- void Show_array(const double *abc, int t)
- {
- for (int i = ; i < t; i++)
- {
- cout << abc[i] << endl;
- }
- }
- void Reverse_array(double *abc, int t)
- {
- cout << "Reverse!" << endl;
- int i = , j = t - ;
- double qqq;
- while (j > i)
- {
- qqq = abc[i];
- abc[i] = abc[j];
- abc[j] = qqq;
- i++;
- j--;
- }
- }
- #include <iostream>
- const int Max = ;
- double * fill_array(double *, double *);
- void show_array(const double *, const double *);
- void revalue(double, double *, double *);
- int main()
- {
- using namespace std;
- double properties[Max];
- double *end = fill_array(properties, properties+Max);
- show_array(properties, end);
- if (end > properties)
- {
- cout << "Enter revaluation factor: ";
- double factor;
- while (!(cin >> factor))
- {
- cin.clear();
- while (cin.get() != '\n')
- continue;
- cout << "Bad input; Please enter a number: ";
- }
- revalue(factor,properties, end);
- show_array(properties, end);
- }
- cout << "Done.\n";
- system("pause");
- }
- double * fill_array(double * ar, double * br)
- {
- using namespace std;
- double temp;
- double *i;
- int j = ;
- for (i = ar; i<br; i++,j++)
- {
- cout << "Enter value #" << (j + ) << ": ";
- cin >> temp;
- if (!cin)
- {
- cin.clear();
- while (cin.get() != '\n')
- continue;
- cout << "Bad input; input process terminated.\n";
- break;
- }
- else if (temp < )
- break;
- *i = temp;
- }
- return i;
- }
- void show_array(const double *ar, const double *br)
- {
- using namespace std;
- const double *i;
- int j = ;
- for (i = ar; i < br; i++,j++)
- {
- cout << "Property #" << (j + ) << ": $";
- cout << *i << endl;
- }
- }
- void revalue(double r,double *ar,double *br)
- {
- double *i;
- for (i = ar; i < br; i++)
- (*i)*= r;
- }
- #include <iostream>
- #include <string>
- const int Seasons = ;
- const char * Snames[Seasons] = { "Spring", "Summer", "Fall", "Winter" };
- int main()
- {
- double expenses[Seasons];
- fill(expenses);
- show(expenses);
- system("pause");
- }
- void fill(double * pa)
- {
- for (int i = ; i < Seasons; i++)
- {
- std::cout << "Enter " << Snames[i] << " expenses: ";
- std::cin >> pa[i];
- }
- }
- void show(double * da)
- {
- double total = 0.0;
- std::cout << "\nEXPENSES\n";
- for (int i = ; i < Seasons; i++)
- {
- std::cout << Snames[i] << ": $" << da[i] << '\n';
- total += da[i];
- }
- std::cout << "Total: $" << total << '\n';
- }
- #include <iostream>
- #include <string>
- const int Seasons = ;
- const char * Snames[Seasons] = { "Spring", "Summer", "Fall", "Winter" };
- struct expenses
- {
- double expenses;
- };
- void fill(expenses *);
- void show(expenses *);
- int main()
- {
- expenses *cost = new expenses[Seasons];
- fill(cost);
- show(cost);
- system("pause");
- }
- void fill(expenses * pa)
- {
- for (int i = ; i < Seasons; i++)
- {
- std::cout << "Enter " << Snames[i] << " expenses: ";
- std::cin >> pa[i].expenses;
- }
- }
- void show(expenses * da)
- {
- double total = 0.0;
- std::cout << "\nEXPENSES\n";
- for (int i = ; i < Seasons; i++)
- {
- std::cout << Snames[i] << ": $" << da[i].expenses << '\n';
- total += da[i].expenses;
- }
- std::cout << "Total: $" << total << '\n';
- }
- #include <iostream>
- using namespace std;
- const int SLEN = ;
- struct student {
- char fullname[SLEN];
- char hobby[SLEN];
- int ooplevel;
- };
- int getinfo(student [], int);
- void display1(student);
- void display2(const student *);
- void display3(const student *, int);
- int main()
- {
- cout << "Enter class size:";
- int class_size;
- while (!(cin >> class_size))
- {
- cin.clear();
- cin.ignore();
- cout << "Please enter class size:";
- };
- student * ptr_stu = new student[class_size];
- int entered = getinfo(ptr_stu, class_size);
- for (int i = ; i < entered; i++)
- {
- display1(ptr_stu[i]);
- display2(&ptr_stu[i]);
- }
- display3(ptr_stu, entered);
- delete[] ptr_stu;
- cout << "Done\n";
- system("pause");
- }
- int getinfo(student pa[], int n)
- {
- int count = ;
- for (int i = ; i < n; i++)
- {
- cin.ignore();
- cout << "Please enter the fullname:";
- cin.getline(pa[i].fullname, SLEN);
- cout << "Please enter the hobby:";
- cin.getline(pa[i].hobby, SLEN);
- cout << "Please enter the ooplevel:";
- cin >> pa[i].ooplevel;
- count++;
- }
- cout << "\nEnter end!";
- return count;
- }
- void display1(student pa)
- {
- cout << "\ndisplay1:\nFullName:" << pa.fullname << "\nhobby:" << pa.hobby
- << "\nooplevel:" << pa.ooplevel << endl;
- }
- void display2(const student * pa)
- {
- cout << "\ndisplay2:\nFullName:" << pa->fullname << "\nhobby:" << pa->hobby
- << "\nooplevel:" << pa->ooplevel << endl;
- }
- void display3(const student * pa, int n)
- {
- cout << "\ndispaly3:\n" << endl;
- for (int i = ; i < n; i++)
- cout << i+ << "::FullName:" << pa[i].fullname << "\nhobby:" << pa[i].hobby
- << "\nooplevel:" << pa[i].ooplevel << endl;
- }
- #include <iostream>
- using namespace std;
- double add(double, double);
- double sub(double, double);
- double calculate(double, double, double (double, double));
- int main()
- {
- double a, b, c, d;
- cout << "Please enter two values:\n";
- while ((cin >> a) && (cin >> b))
- {
- c=calculate(a, b, add);
- d=calculate(a, b, sub);
- cout << "The sum and subtraction of the values are:" << c << "," << d << endl;
- cout << "Please continue to enter:\n";
- }
- system("pause");
- }
- double add(double a, double b)
- {
- double c;
- c = a + b;
- return c;
- }
- double sub(double a, double b)
- {
- double d;
- d = a - b;
- return d;
- }
- double calculate(double a, double b, double cal(double, double))
- {
- double e;
- e = cal(a, b);
- return e;
- }
c++ primer plus 第七章 课后题答案的更多相关文章
- c++ primer plus 第六章 课后题答案
#include <iostream> #include <cctype> using namespace std; int main() { char in_put; do ...
- c++ primer plus 第五章 课后题答案
#include <iostream> using namespace std; int main() { ; cout << "Please enter two n ...
- c++ primer plus 第四章 课后题答案
#include<iostream> #include<string> using namespace std; int main() { string first_name; ...
- c++ primer plus 第三章 课后题答案
#include<iostream> using namespace std; int main() { ; int shen_gao; cout <<"Please ...
- c++ primer plus 第二章 课后题答案
#include<iostream> using namespace std; int main() { cout << "My name is Jiantong C ...
- python核心编程第5章课后题答案
5-8Geometry import math def sqcube(): s = float(raw_input('enter length of one side: ')) print 'the ...
- python核心编程第4章课后题答案(第二版75页)
4-1Python objects All Python objects have three attributes:type,ID,and value. All are readonly with ...
- python核心编程第3章课后题答案(第二版55页)
3-4Statements Ues ; 3-5Statements Use\(unless part of a comma-separated sequence in which case \ is ...
- 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 ...
随机推荐
- iOS 第三方框架-MJRefresh
MJRefresh是一款非常好用的上拉下拉第三方库,使用也很简单.github地址: https://github.com/CoderMJLee/MJRefresh . 下拉刷新 官方给过来的例子很简 ...
- iOS UI基础-3.0图片浏览器及plist使用
需求: 1.显示当前图片序号/总图片数 2.显示图片 3.上一张图片.下一张图片转换 4.显示图片描述 下面用代码来实现 // // UYViewController.m // 3.0图片查看器 // ...
- html select 和dropdownlist小结收集
//html select var x = $("#selectSort").val(); //获取选中的value值 获取select选中的索引: $("#selec ...
- C语言头文件、库文件的查找路径
在 程序设计中,文件包含是很有用的.一个大的程序可以分为多个模块,由多个程序员分别编程.有些公用的符号常量或宏定义等可单独组成一个文件,在其它文件的开头用包含命令包含该文件即可使用.这样,可避免在每个 ...
- win10安装后耳机有声音而外放无声音
安装win10后耳机声音正常,而外放没声音.检查了小喇叭.播放器和设备管理器一切正常,驱动也重装了好些次.喇叭音量设置.视频音量设置均正常.花费很多时间,结果发现是键盘上有个小喇叭+的键的问题.按fn ...
- SLF4J和log4j的使用
概念 SLF4J:即简单日志门面(Simple Logging Facade for Java),不是具体的日志解决方案,它只服务于各种各样的日志系统.按照官方的说法,SLF4J是一个用于日志系统的简 ...
- OVS中的key解析
OVS在处理每条流的时候,先根据每条流生产相应的key,然后根据key匹配相应的流表,根据流表中的action操作来处理每条流,本文对key的结构体进行分析,看看对于一条流会提出那些特征信息.对于ke ...
- OAuth : open Authorization 开发授权
OAuth : open Authorization 开发授权 用户访问慕课网,慕课网请求OAuth登陆页面,用户输入QQ号码和密码,这个页面的域名不属于慕课网是属于QQ的,随后把结果给慕课网,这个结 ...
- Python入门之python可变对象与不可变对象
本文分为如下几个部分 概念 地址问题 作为函数参数 可变参数在类中使用 函数默认参数 类的实现上的差异 概念 可变对象与不可变对象的区别在于对象本身是否可变. python内置的一些类型中 可变对象: ...
- RAM,ROM,NAND Flash,NOR Flash(A)
他们四者相互独立 RAM掉电易失数据: RAM又分两种,一种是静态RAM,SRAM:一种是动态RAM,DRAM.前者的存储速度要比后者快得多,我们现在使用的内存一般都是动态RAM. DDR是Doubl ...