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. Unity动画优化

    Unity动画优化 https://blog.csdn.net/TracyZly/article/details/79991593 Unity中Animator做UI动画的一些细节 https://b ...

  2. Mac本地生成SSHKey的方法

    1. 查看秘钥是否存在 打开终端查看是否已经存在SSH密钥:cd ~/.ssh 如果没有密钥则不会有此文件夹,有则备份删除,   也可以直接删除, 2.生成新的秘钥, 命令如下 $ssh-keygen ...

  3. 【原创】经验分享:一个Content-Length引发的血案(almost....)

    前言 上周在工作中遇到一个问题,挺有意思,这里记录一下.上周在工作中遇到一个问题,挺有意思,这里记录一下.标题起的很唬人,这个问题差点引发血案,花哥还是很严谨的一个人,后面备注了almost.... ...

  4. docker 容器中部署 Go 服务时,请求 https 文件时抛错

    错误提示: Get https://res.ddkt365.com/ddktRes/imageRes/wx_headimg/0f1d9e55913c22bcaf7cca9b38048d29.jpeg: ...

  5. Fragment时长统计那些事

    注:本文同步发布于微信公众号:stringwu的互联网杂谈 frament时长统计那些事 页面停留时长作为应用统计的北极星指标里的重要指标之一,统计用户在某个页面的停留时长则变得很重要.而Fragme ...

  6. golang开发:CSP-WaitGroup Mutex

    CSP 是 Communicating Sequential Process 的简称,中文可以叫做通信顺序进程,是一种并发编程模型,最初于Tony Hoare的1977年的论文中被描述,影响了许多编程 ...

  7. Linux实战(1):装机一键设置脚本-初级版

    #!/bin/bash #此脚本作用是装完centos后所执行的一系列设置,基本是必须设置的操作,主要分为几大功能:修改yum源,安装ifconfig,安装ping,修改selinux配置 #第一部分 ...

  8. 云计算openstack核心组件——keystone身份认证服务(5)

    一.Keystone介绍:       keystone 是OpenStack的组件之一,用于为OpenStack家族中的其它组件成员提供统一的认证服务,包括身份验证.令牌的发放和校验.服务列表.用户 ...

  9. TS数据类型:类型别名/联合类型/字面量类型/类型推论等纲要

    在学C/C++  Java等强类型语言时,变量类型是唯一的,需要先指定.PHP JavaScript等弱类型语言时,无需指定变量类型 但是,TypeScript里面的联合类型 (Union Type) ...

  10. nginx的安装和启动

    https://www.cnblogs.com/wyd168/p/6636529.html nginx开机启动 https://www.cnblogs.com/gimin/p/8893559.html