1.用户不断输入两个数,计算调和平均数,直到其中一个数为0。

#include<iostream>
using namespace std; double harm_mean(double x, double y)
{
double result;
result = 2 * x*y / (x + y);
return result;
} int main()
{
double num1, num2;
int flag = 1; while (flag)
{
cout << "Enter two numbers:\n";
cin >> num1;
cin >> num2;
if ((num1 == 0) || (num2 == 0))
flag = 0;
else
cout << "Harmonic mean:" << harm_mean(num1, num2) << endl; } cout << "done.\n";
system("pause");
}

2.要求用户输入最多10个高尔夫成绩,并将其存储在一个数组中。程序允许用户提早结束输入,并在一行上显示所有成绩,然后报告平均成绩。使用3个数组处理函数分别进行输入、显示和计算平均成绩。

#include<iostream>
using namespace std; void score_in(int si[]);
void score_out(int so[]);
void score_mean(int sm[]); static int counts = 0; int main()
{
int score[10]; cout << "(enter any letter to stop input)\nEnter the score:\n";
score_in(score);
score_out(score);
score_mean(score); cout << "done.\n";
system("pause");
} void score_in(int si[])
{
for (int i = 0; i < 10; i++)
{
if (cin >> si[i])
{
counts++;
}
else
{
cout << "Input stopped." << endl;
break;
}
}
} void score_out(int so[])
{
cout << "Score:";
for (int i = 0; i < counts; i++)
{
cout << so[i] << " ";
}
cout << "\n";
} void score_mean(int sm[])
{
double sum = 0;
for (int i = 0; i < counts; i++)
{
sum += sm[i];
}
cout << "Average:" << sum / counts << endl;
}

3.定义一个box结构,存储生产者,长高宽和体积的信息。编写两个函数,第一个按值传递box结构,显示所有成员的值。第二个传递box结构的地址,并将volume成员设置为其它三维长度的体积。将两个函数组成一个简单的程序。

#include<iostream>
using namespace std; struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
}; void set_box(box *sptr, float x, float y, float z);
void v_box(box *vptr); int main()
{
box box1;
float l, w, h; cout << "Enter the box's l,w and h:\n";
cin >> l >> w >> h; set_box(&box1, l, w, h);
v_box(&box1); cout << "done.\n";
system("pause");
} void set_box(box *sptr, float x, float y, float z)
{
sptr->height = z;
sptr->length = x;
sptr->width = y;
cout << "Length:" << sptr->length << endl;
cout << "Width:" << sptr->width << endl;
cout << "Height:" << sptr->height << endl;
} void v_box(box *vptr)
{
vptr->volume = vptr->height*vptr->length*vptr->width;
cout << "The volume of this box is:" << vptr->volume << endl;
}

4.修改书中7.4的程序,域号码1~47,中奖条件增加一项从27个数中抽中号码。

#include<iostream>
using namespace std; long double probability(unsigned numbers, unsigned picks)
{
long double result = 1.0;
unsigned n;
unsigned p; for (n = numbers, p = picks; p > 0; n--, p--)
{
result = result * n / p;
} return result;
} unsigned particular(unsigned pn)
{
return pn;
} int main()
{
double total, choices;
cout << "Enter the total number of choices on the game card and\n";
cout << "the number of picks allowed:\n"; while ((cin >> total >> choices) && (choices <= total ))
{
cout << "You have one chance in ";
cout << probability(total, choices)*particular(27);
cout << " of winning.\n";
cout << "Next two numbers(q to quit):";
} cout << "\ndone.";
system("pause");
}

*这题没太看懂什么意思,结果再乘个27不就行了?跟着敲了一边代码。

5.定义一个递归函数算阶乘。

#include<iostream>
using namespace std; long long fac(int n)
{
if (n > 0)
return n * fac(n - 1);
else if (n == 0)
return 1;
} int main()
{
int n; cout << "Enter a number:\n";
cin >> n;
cout << "Its factorial:" << fac(n) << endl; cout << "done.\n";
system("pause");
}

6.编写三个处理double数组的函数,分别用于提示输入、显示数组内容和翻转数组中值的顺序。程序先填充数组,显示数组,再翻转数组,再显示数组。

#include<iostream>
using namespace std; static int counts = 0; //输入数字计数 void fill_array(double fa[], int asize);
void show_array(double sa[], int ssize);
void reverse_array(double ra[], int rsize); int main()
{
double input[10]; fill_array(input, 10);
show_array(input, 10);
reverse_array(input, 10);
show_array(input, 10); cout << "done.\n";
system("pause");
} void fill_array(double fa[], int asize)
{
cout << "Enter some numbers:\n";
for (int i = 0; i < asize; i++)
{
if (cin >> fa[i])
{
counts++;
if (counts == asize)
{
cout << "Input stopped.\n";
break;
}
else {};
}
else
{
cout << "Input stopped.\n";
break;
}
}
cout << counts << " numbers entered.\n";
} void show_array(double sa[], int ssize)
{
cout << "Input numbers:";
for (int i = 0; i < counts; i++)
{
cout << sa[i] << " ";
}
cout << "\n";
} void reverse_array(double ra[], int rsize)
{
double t;
//如果要翻转除第一位和最后一位的数字,把i改成1
for (int i = 0; i <= counts / 2; i++)
{
t = ra[i];
ra[i] = ra[counts - i - 1];
ra[counts - i - 1] = t;
}
}

7.修改书中7.7的程序,使用两个指针参数来表示区间。fill_array()函数返回一个指向最后被填充的位置的指针,其它函数可以将它作为第二个参数标识结尾。

#include<iostream>
using namespace std; const int Max = 5; double* fill_array(double ar[], int limit);
void show_array(double *ptl, double *ptr);
double add_array(double *ptl, double *ptr); int main()
{ double pro[Max]; cout << "5 numbers to show\n";
show_array(pro, fill_array(pro, Max)); cout << "Next 5 numbers to sum\n";
cout << "Sum:" << add_array(pro, fill_array(pro, Max)); cout << "\ndone.";
system("pause");
} double* fill_array(double ar[], int limit)
{
double *ptr;
int i = 0;
for (i = 0; i < limit; i++)
{
cout << "Enter value #" << i + 1 << ":";
cin >> ar[i];
}
ptr = &ar[i];
return ptr;
} void show_array(double *ptl, double *ptr)
{
for (int i = 0; ptl[i] != *ptr; i++)
{
cout << "Property #" << i + 1 << ":" << ptl[i] << endl;
}
} double add_array(double *ptl, double *ptr)
{
double sum = 0;
for (int i = 0; ptl[i] != *ptr; i++)
{
sum += ptl[i];
}
return sum;
}

8.修改程序7.15,编写不使用array类两个版本。

#include<iostream>
//#include<array>
//#include<string> using namespace std;
/*原程序
const int seasons = 4;
const array<string, seasons>Snames =
{ "Spring","Summer","Fall","Winter" }; void fill(array<double, seasons>*pa);
void show(array<double, seasons>da); int main()
{
array<double, seasons>expenses; fill(&expenses);
show(expenses); cout << "\ndone.";
system("pause");
} void fill(array<double, seasons>*pa)
{
for (int i = 0; i < seasons; i++)
{
cout << "Enter " << Snames[i] << " expenses:";
cin >> (*pa)[i];
}
} void show(array<double, seasons>da)
{
double total = 0.0;
cout << "\nEXPENSES\n";
for (int i = 0; i < seasons; i++)
{
cout << Snames[i] << ":$" << da[i]<<"\t";
total += da[i];
}
cout << "\nTotal Expenses:$" << total << endl;
}
*/ const char Snames[4][10] = {"Spring","Summer","Fall","Winter"};
const int seasons = 4;
void fill_exp(double fe[]);
void show_exp(double se[]); struct Expenses
{
double expenses[seasons];
}; int main()
{
//double expenses[seasons]; 版本a Expenses exp;
fill_exp(exp.expenses);
show_exp(exp.expenses); //fill_exp(expenses); 版本a
//show_exp(expenses); 版本a cout << "done.\n";
system("pause");
} void fill_exp(double fe[])
{
for (int i = 0; i < seasons; i++)
{
cout << "Enter " << Snames[i] << " expenses:";
cin >> fe[i];
}
} void show_exp(double se[])
{
double total = 0;
for (int i = 0; i < seasons; i++)
{
cout << Snames[i] << ":$" << se[i] << "\t";
total += se[i];
}
cout << "\nTotal expenses:" << total << endl;
}

9.把程序补充完整。getinfo函数用于输入学生结构的信息,三种display函数显示学生的信息,但使用三种不同的参数传递方式。

#include<iostream>
using namespace std; const int SLEN = 30; struct student
{
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
}; void getinfo(student pa[], int n); void display1(student st); void display2(const student *ps); void display3(const student pa[], int n); int main()
{
student students[3]; getinfo(students, 3); display1(students[0]);
display2(&students[1]);
display3(students, 3); cout << "done.\n";
system("pause");
} void getinfo(student pa[], int n)
{
int i = 0;
for (i = 0; i < n; i++)
{
cout << "Input name of student " << i + 1 << ":\n";
cin.get(pa[i].fullname, SLEN);
cin.get();
cout << "Input hobby of student " << i + 1 << ":\n";
cin.get(pa[i].hobby, SLEN);
cout << "Input ooplevel of student " << i + 1 << ":\n";
cin >> pa->ooplevel;
cin.get();
}
} void display1(student st)
{
cout << "Student1's name:" << st.fullname << endl;
} void display2(const student *ps)
{
cout << "Student2's hobby:" << ps->hobby << endl;
} void display3(const student pa[], int n)
{
for (int i = 0; i < n; i++)
{
cout << pa[i].fullname << " ";
}
}

10.函数指针练习。编写一个calculate()函数,接受三项参数,两个double值和一个函数指针,指针指向计算两数之和(或其它功能)的函数。可尝试使用函数指针数组。

#include<iostream>
using namespace std; double add(double x, double y)
{
return x + y;
} double div(double x, double y)
{
return x / y;
} double calculate(double x, double y, double (*ptf)(double,double))
{
return ptf(x, y);
} int main()
{
double x1, x2;
double(*ptf[2])(double, double);
ptf[0] = add;
ptf[1] = div; cout << "Enter two number:";
while (cin >> x1 >> x2)
{
cout << "Add:" << calculate(x1, x2, ptf[0]) << endl;
cout << "Div:" << calculate(x1, x2, ptf[1]) << endl;
cout << "Enter two number:";
} cout << "done.\n";
system("pause");
}

《C++primerplus》第7章练习题的更多相关文章

  1. 《C++ primerplus》第13章练习题

    1.对CD类的派生练习.基类CD类存储作者和作品号等信息,派生类Classic额外增加一格"主要作品"的信息.主函数使用拷贝构造函数.按引用传递参数的函数和指针来测试基类和派生类的 ...

  2. 《C++primerplus》第12章练习题

    做一下倒数两题,都是在队列模拟的程序基础上做点修改测试. 5.找出平均等候时间为1分钟时,每小时到达的客户数为多少(试验时间不少于100小时). 指定队伍最大长度10人,模拟100小时.粗略估计答案在 ...

  3. 《C++primerplus》第11章练习题

    1.修改程序清单11.5(随机漫步),使之以特定的格式将结果写入文件中. //vector.h -- Vector Class #ifndef _VECTOR_H_ #define _VECTOR_H ...

  4. 《C++primerplus》第10章练习题

    1.定义一个类表示银行账户.数据成员包括姓名,账号和存款.成员函数可以执行初始化数据.显示数据和取款存款的功能. //Bank.cpp #include<iostream> #includ ...

  5. 《C++primerplus》第9章练习题

    1.(未使用原书例题)练习多文件组织.在一个头文件中定义一种学生的结构体,存储姓名和年龄,声明三个函数分别用于询问有多少个学生,输入学生的信息和展示学生的信息.在另一个源文件中给出所有函数的定义.在主 ...

  6. 《C++primerplus》第8章练习题

    1.(简单用一下引用变量,没有采用书中的题目)定义一个替身结构体,存储名字(char[])和力量值(int).使用结构体引用作为形参写两个函数,一个不加const,使得能对定义的结构体做修改,另一个加 ...

  7. 《C++primerplus》第6章练习题

    本来前面五题都做完了,写博客时没保存好草稿= =,写了个整合版的程序,实现前五题的关键部分. 1.定义一个叫jojo的结构,存储姓名.替身和力量值,使用动态结构数组初始化二乔.承太郎和乔鲁诺乔巴纳等人 ...

  8. 《C++primerplus》第4章练习题

    注:略过部分题目,修改了题设要求,实现差不多的功能 1.使用字符数组.要求用户输入姓名,等第和年龄,输出其姓名和年龄,等第降一级(即字母高一级). #include<iostream> u ...

  9. python第一章练习题

    本章总节 练习题 1.简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释 编译型:把源代码编译成机器语言的可执行文件,程序执行的时候执行可执行文件即可. 优点:程序执行不 ...

随机推荐

  1. Python 编程开发 实用经验和技巧

    文章目录 一.小数保留指定位小数 1.%f 方法 2.format函数 3.round()函数 4.直接截断 二.判断变量的数据类型的两种方法 1.type(): 2.isinstance() 三.p ...

  2. APM姿态控制流程

    对初学者了解控制流程有一定帮助 在主循环执行过程中(比如Pixhawk的任务调度周期2.5ms,400Hz:APM2.x为10ms,100Hz),每一个周期,程序会按下述步骤执行:• 首先,高层次文件 ...

  3. three.js尝试(一)模拟演唱会效果

    工作闲暇之余,偶然翻到了Three.js的官网,立刻被它酷炫的案例给惊艳到了,当即下定决心要试验摸索一番,于是看demo,尝试,踩坑,解决问题,终于搞定了,一个模拟演唱会场景. 主角围绕一个钢管在舞动 ...

  4. Centos7.6系统下docker的安装

    一.环境说明 系统:CentOS7.6 软件:Docker19.03 二.Docker的安装 2.1.在线安装 (1) 设置仓库,安装所需的软件包. yum-utils 提供了 yum-config- ...

  5. [Python]在当前目录下创建三个目录

    import os os.mkdir("2018-03-16-b018") os.mkdir("2019-03-16-b019") os.mkdir(" ...

  6. leetcode刷题-77组合

    题目 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2输出:[ [2,4], [3,4], [2,3], [1,2], [1,3 ...

  7. Java审计之XSS篇

    Java审计之XSS篇 0x00 前言 继续 学习一波Java审计的XSS漏洞的产生过程和代码. 0x01 Java 中XSS漏洞代码分析 xss原理 xss产生过程: 后台未对用户输入进行检查或过滤 ...

  8. day09记录

    今日内容大纲 毒鸡汤课 坚持.努力! 生成器 yield yeild return yeild from 生成器表达式 内置函数I 昨日内容回顾作业讲解 可迭代对象 可以更新得带的 实实在在的值. 内 ...

  9. [CF664A]Complicated GCD(数论)

    题目链接 http://codeforces.com/problemset/problem/664/A 题意 给两个数,找出它们的最大公因子d,使得从a到b之间的数都可以整除d. 题解 结论: 当gc ...

  10. 论如何学习Extjs

    可能现在学习Extjs相比于Vue,在网上的资料要少很多,不过一些旧的视频还是可以帮助你们了解到Extjs是怎么回事. 这里讲一下自己是如何开始学习Extjs语言的: 1.先从Ext的中文文档中学习怎 ...