C++ Primer Pluse_7_课后题
#include <iostream> using namespace std; double Sum2(double x, double y)
{
double sum = 0; if (x + y < 0.0000000001)
{
cout << "x, y 的调和数为无穷大;\n";
system("pause");
exit(0);
}
sum = 2.0*x*y / (x + y);
return sum;
} void test7_1()
{
double x;
double y;
double sum = 0; while ((cin>>x>>y) && x != 0 && y!=0)
{
sum = Sum2(x, y);
cout << "x,y的调和为: " << sum << endl;
}
} /************************************************************************/
/* 2 */
/************************************************************************/
const int SIZE = 10;
int GetScores(double scores [],int *size)
{
cout << "Enter the scores( press q to quit).\n";
int i = 0;
for (i = 0; i < 10 && cin >> scores[i]; i++)
{
if (scores[i]<0)
{
cout << "score below zero, bad input.\n";
cout << "Input terminated.\n";
return -1;
}
} *size = i;
return 0;
} void Show(const double * scores, int size)
{
int i = 0;
for (i = 0; i < size; i++)
{
cout << scores[i] << " ";
}
cout << endl;
} double Average(const double *scores, int size)
{
double sum = 0; if (size == 0)
{
return 0;
}
for (int i = 0; i < size; i ++)
{
sum += scores[i];
} return sum/size;
}
void test7_2()
{
int size = 0;
double scores[SIZE] = { 0 }; int ret = 0;
ret = GetScores(scores, &size);
while (ret != 0)
{
cout << "Please input again.\n";
ret = GetScores(scores, &size);
} Show(scores, size);
double avg = Average(scores, size);
cout << "平均成绩为:" << avg << endl; } /************************************************************************/
/* 3 */
/************************************************************************/
typedef struct Box
{
char maker[40];
float height;
float width;
float length;
float volume;
}box; void ShowBox(box b1)
{
cout << "Showing the box......\n";
cout << b1.maker << endl;
cout << b1.height << endl;
cout << b1.width << endl;
cout << b1.length << endl;
cout << b1.volume << endl;
} void GetVol(box * b1)
{
b1->volume = b1->height *b1->length*b1->width;
}
void test7_3()
{
box b1 = { "Cat", 10, 2, 3, 0 };
ShowBox(b1);
GetVol(&b1);
ShowBox(b1);
} /************************************************************************/
/* 4 */
/************************************************************************/ void test7_4()
{ }
/************************************************************************/
/* 5 */
/************************************************************************/
long GetFactorial(int n)
{
if (n == 0 || n == 1)
{
return 1;
}
else
return n*GetFactorial(n - 1); }
void test7_5()
{
int n = 0;
cin >> n;
if (n < 0)
{
cout << "bad input.\nprogram terminated.\n";
return;
} cout << n << "! = " << GetFactorial(n)<<endl; } void test7_6()
{ } /************************************************************************/
/* 7 */
/************************************************************************/
const int Max = 5;
double * full_array(double ar[], int limit)
{
double *ar_end = ar;
double tmp = 0;
for (int i = 0; i < limit; i++, ar_end++)
{
cout << "Enter value #" << (i + 1) << ": ";
cin >> tmp; if (!cin)
{
cin.clear();
while (cin.get()!='\n')
{
continue;
} cout << "Bad input;input process terminated.\n";
break;
}
else if (tmp < 0)
{
break;
}
*ar_end = tmp;
} return --ar_end;
} void show_array(double ar[], const double *ar_end)
{
double *pt = ar;
for (int i = 0; pt <= ar_end; pt++, i++)
{
cout << "Property #" << (i + 1) << ": $";
cout << *pt << endl;
}
} void revalue(double r, double *ar, double *ar_end)
{
double *pt = ar;
for (; pt <= ar_end ; pt++)
{
*pt *= r; }
} void test7_7()
{
double properties[Max];
double *ar_end = NULL;
ar_end = full_array(properties, Max);
show_array(properties,ar_end);
if (ar_end - properties > 0)
{
cout << "Enter revaluation factor: ";
double factor;
while (!(cin >> factor))
{
cin.clear();
while (cin.get()!= '\n')
{
continue;
}
cout << "Bad input. input process terminated.";
}
revalue(factor, properties, ar_end);
show_array(properties, ar_end);
}
cout << "Done.\n";
cin.get();
cin.get();
} /************************************************************************/
/* 8 */
/************************************************************************/
const int Seasons = 4;
const char * Snames[Seasons] = { "Spring", "Summer", "Fall", "Winter" };
void fill(double expenses[], int Seasons)
{
for (int i = 0; i < Seasons; i++)
{
cout << "Enter " << Snames[i] << " expense: ";
cin >> expenses[i];
}
} void show(double expense[], int Seasons)
{
double total = 0;
cout << "EXPENSE\n";
for (int i = 0; i < Seasons; i++)
{
cout << Snames[i] << ": $" << expense[i] << endl;
total += expense[i];
} cout << "Total Expense: $" << total << endl; }
void test7_8()
{
double expense[Seasons] = { 0 };
fill(expense, Seasons);
show(expense, Seasons);
} /************************************************************************/
/* 9 */
/************************************************************************/
const int Size = 30;
typedef struct Student{ char fullname[Size];
char hobby[Size];
int ooplevel; }student; int getInfo(student pa[], int n)
{
int i = 0;
for (i = 0; i < n; i++)
{
cout << "Student #" << i + 1 << ":\n";
cout << "Enter name: ";
cin >> pa[i].fullname;
if (!cin)
{
cin.clear();
while (cin.get()!= '\n')
{
continue;
}
cout << "bad input. input process terminated.\n";
} cout << "Enter hobby:";
cin >> pa[i].hobby;
cout << "Enter ooplevel:";
cin >> pa[i].ooplevel;
} return i;
} void display1(student st)
{
cout << "Student info:\n";
cout << "name: " << st.fullname << endl;
cout << "hobby: " << st.hobby << endl;
cout << "ooplevel" << st.ooplevel << endl;
} void display2(student *ps)
{
cout << "Student info:\n";
cout << "name: " << ps->fullname << endl;
cout << "hobby: " << ps->hobby << endl;
cout << "ooplevel" << ps->ooplevel << endl;
} void display3(const student pa[], int n)
{
int i = 0;
for (; i < n; i++)
{
cout << "Student #" << i + 1<< "info:"<< endl;
cout << "name: " << pa[i].fullname << endl;
cout << "hobby: " << pa[i].hobby << endl;
cout << "ooplevel" << (pa+i)->ooplevel << endl;
}
} void test7_9()
{
cout << "Enter class size: ";
int class_size;
cin >> class_size; while (cin.get()!= '\n')
{
continue;
} student *ptr_stu = new student[class_size];
int entered = getInfo(ptr_stu, class_size);
for (int i = 0; i < entered; i++)
{
display1(ptr_stu[i]);
display2(ptr_stu + i);
} display3(ptr_stu, entered);
delete [] ptr_stu;
cout << "Done.\n";
}
double add(double x, double y)
{
return x + y;
} double sub(double x, double y)
{
return x - y;
} double mul(double x, double y)
{
return x*y;
} void test7_10()
{
double(*calculate[3])(double, double); calculate[0] = add;
calculate[1] = sub;
calculate[2] = mul;
for (int i = 0; i < 3; i++)
{
cout << calculate[i] << ": "<<calculate[i](5, 2) << endl;
}
}
int main()
{
test7_10();
system("pause");
return 0;
}
C++ Primer Pluse_7_课后题的更多相关文章
- C++ Primer Pluse_8_课后题
#include <iostream> #include <string> #include<cstring> using namespace std; void ...
- C++ Primer Pluse_6_课后题
#include <iostream> #include <cctype> #include <array> #include <string> #in ...
- 玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)
前文 的最后给出了玉伯的一道课后题,今天我们来讲讲这题的思路. 题目是这样的: Number.MAX_VALUE + 1 == Number.MAX_VALUE; Number.MAX_VALUE + ...
- 算法(JAVA)----两道小小课后题
LZ最近翻了翻JAVA版的数据结构与算法,无聊之下将书中的课后题一一给做了一遍,在此给出书中课后题的答案(非标准答案,是LZ的答案,猿友们可以贡献出自己更快的算法). 1.编写一个程序解决选择问题.令 ...
- 课后题2.87&2.86
课后题2.86&2.87 单纯就是想加点分第十章的题目都被做过了就做下第二章的,正好复习一下前面学的知识,第二章给我剩下的题目也不多了,我就挑了这个题目. 2.86 考虑一个基于IEEE浮点格 ...
- c++面向对象程序设计 课后题 答案 谭浩强 第四章
c++面向对象程序设计课后题答案 谭浩强 第四章 1: #include <iostream> using namespace std; class Complex {public: Co ...
- 学习参考《零基础入门学习Python》电子书PDF+笔记+课后题及答案
国内编写的关于python入门的书,初学者可以看看. 参考: <零基础入门学习Python>电子书PDF+笔记+课后题及答案 Python3入门必备; 小甲鱼手把手教授Python; 包含 ...
- 学习《零基础入门学习Python》电子书PDF+笔记+课后题及答案
初学python入门建议学习<零基础入门学习Python>.适合新手入门,很简单很易懂.前一半将语法,后一半讲了实际的应用. Python3入门必备,小甲鱼手把手教授Python,包含电子 ...
- Java程序设计(2021春)——第一章课后题(选择题+编程题)答案与详解
Java程序设计(2021春)--第一章课后题(选择题+编程题)答案与详解 目录 Java程序设计(2021春)--第一章课后题(选择题+编程题)答案与详解 第一章选择题 1.1 Java与面向对象程 ...
随机推荐
- WPF学习笔记(3):Path绘制命令zz
WPF的XAML提供了一系列功能强大.用法复杂的 mini-language 来描述可扩展应用程序标记语言 (XAML) 中的几何路径.如下所示: XAML <Canvas> < ...
- 一个简单的C语言语法检查器的实现
我自己的实现方法的核心过程:首先用一个非终结符代表所有要检查的程序代码,然后根据文法将这个整体的符号不断展开,以拼凑成按检查的程序的顺序排列的终结符序列,能成功说明语法正确,否则有错误. 关键词:分词 ...
- BZOJ4112 : [Wf2015]Pipe Stream
枚举答案,考虑将速度区间等长地划分成若干个小区间. 设$n_i$表示$i$次敲击能得到的区间数,$v_i$表示$i$次敲击之后答案落在$[v1,v_i]$之间,则$n_0=1,v_0=v2$. 因为对 ...
- ACM 荷兰国旗问题
荷兰国旗问题 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 荷兰国旗有三横条块构成,自上到下的三条块颜色依次为红.白.蓝.现有若干由红.白.蓝三种颜色的条块序列,要 ...
- [知识点]网络流之Edmond-Karp算法
// 此博文为迁移而来,写于2015年2月2日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vr12.html ...
- 【BZOJ】1104: [POI2007]洪水pow
题意 给一个\(n * m(1 \le n, m \le 1000)\)的矩阵,如果\(a_{i, j}\)为正表示城市.\(|a_{i, j}|(|a_{i, j}| \le 1000)\)是格子\ ...
- C程序演示产生僵死进程的过程
先抄录网上一段对僵死进程的描述: 僵尸进程:一个进程使用fork创建子进程,如果子进程退出,而父进程并没有调用wait或waitpid获取子进程的状态信息,那么子进程的进程描述符仍然保存在系统中.这种 ...
- BZOJ4184: shallot
Description 小苗去市场上买了一捆小葱苗,她突然一时兴起,于是她在每颗小葱苗上写上一个数字,然后把小葱叫过来玩游戏. 每个时刻她会给小葱一颗小葱苗或者是从小葱手里拿走一颗小葱苗,并且 让小葱 ...
- CentOS VirtualBox启动虚拟及报错:VirtualBox error: Kernel driver not installed (rc=1908)
VirtualBox error: Kernel driver not installed (rc=1908) Hi all, Let me first say that this is my fin ...
- session/SessionFactory线程非安全和线程安全
SessionFactory负责创建session,SessionFactory是线程安全的,多个并发线程可以同时访问一个 SessionFactory 并从中获取Session实例. (Sessio ...