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. laravel使用snappy生成pdf问题及过程(本地环境)

    1.安装过程可以按照这里进行https://packagist.org/packages/barryvdh/laravel-snappy 其中要按装好 "barryvdh/laravel-s ...

  2. Unity3D中可重载虚函数的总结

    重载虚函数:Unity3D中所有控制脚本的基类MonoBehaviour有一些虚函数用于绘制中事件的回调,也可以直接理解为事件函数,例如大家都很清楚的Start,Update等函数,以下做个总结. A ...

  3. 【平台开发】— 2.前端:vue-element-admin

    前端我虽然没怎么写过,但也并不陌生.之前做web自动化,网页结构没少看,html,css,js也都断断续续了解过. 如果从空白开始写,肯定还是需要花不少时间的. 好在网上有了不少成熟的后台系统的前端脚 ...

  4. js 基础面试

    1.['1', '2', '3'].map(parseInt) let arr = ['1', '2', '3'] let res1 = arr.map(Number) // [ 1, 2, 3 ] ...

  5. BasicInterpreter2 改进版,简化了print函数

    源码:https://files.cnblogs.com/files/heyang78/BasicInterpreter2-20200601-3.rar 改进后使得变量和字符串可以一起输出了. 输入脚 ...

  6. 如何让SpringBoot工程在log/控制台中实时打印MyBatis执行的SQL语句

    工程下载:https://files.cnblogs.com/files/xiandedanteng/gatling20200429-4.zip 其实就是一句话设置的事情,实现步骤: 在applica ...

  7. path.resolve和path.join的区别

    // test.js const path = require('path') let x1 = path.resolve('/目录1/目录2', '/目录3/目录4/') let x2 = path ...

  8. [Java并发编程之美]第2章 synchroized关键字

    ###synchronized关键字 synchronized块是Java提供的一种原子性内置锁,每个对象都可以把它当同步锁来用.线程在进入synchronized块钱会自动获取内部锁,这时候其他线程 ...

  9. IDEA使用maven搭建SSM框架整合项目(超级详细,值得一看)

    目录 温馨提示 简单介绍下SSM 搭建过程 一.框架介绍 二.下载Maven 三.创建Maven项目 四.Maven工程需要引入的Jar 包 五.整合SSM框架.需要的相关配置文件配置项目 六.工程导 ...

  10. 【Nginx】冰河又一本超硬核Nginx PDF教程免费开源!!

    写在前面 在 [冰河技术] 微信公众号中的[Nginx]专题,更新了不少文章,有些读者反馈说,在公众号中刷 历史文章不太方便,有时会忘记自己看到哪一篇了,当打开一篇文章时,似乎之前已经看过了, 但就是 ...