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. c++ binding code generator based on clang

    google it http://www.swig.org/Doc3.0/CSharp.html http://samanbarghi.com/blog/2016/12/06/generate-c-i ...

  2. 面试【JAVA基础】其他

    1.自定义注解 @target 说明了Annotation所修饰的对象范围: constructor.method.field.package.type等等. @retention 定义了该Annot ...

  3. Maven 仓库镜像设置

    <mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> <name>al ...

  4. 03 父子组件sync&update

    父组件传给子组件是基本数据类型. 父组件 <template> <el-container class="consele-container"> <e ...

  5. 写给.NET开发者的Python教程(三):运算符、条件判断和循环语句

    本节会介绍Python中运算符的用法,以及条件判断和循环语句的使用方法. 运算符 运算符包括算术运算符.赋值运算符.比较运算符.逻辑运算符等内容,大部分用法和C#基本一致,下面我们来看一下: 算数运算 ...

  6. Dubbo系列之 (六)服务订阅(3)

    辅助链接 Dubbo系列之 (一)SPI扩展 Dubbo系列之 (二)Registry注册中心-注册(1) Dubbo系列之 (三)Registry注册中心-注册(2) Dubbo系列之 (四)服务订 ...

  7. java中变量在内存的位置

    package day02; /* * 成员变量:在堆内存中,因为对象的存在,才在内存中存在:作用于整改类中 * 局部变量:在栈内存中:作用于函数中,或者语句中 * */ class car{ //描 ...

  8. css3渐变色实现小功能 ------ css(linaer-gradient)

    由沿直线两种或多种颜色之间的渐进转换的图像.它的结果是数据类型的对象,这是一种特殊的类型. 与任何梯度一样,线性梯度没有内在维度 ; 即,它没有天然或优选的尺寸,也没有优选的比例.其具体尺寸将与其适用 ...

  9. AWD 第二弹

    前言 小组第二次awd训练 万能密码登陆 首页发现一个登录界面,使用万能密码登陆 ' or 1=1#' 登陆成功后,发现Flag 任意文件上传 刚刚的后台界面存在上传按钮,可直接上传一句话木马 文件包 ...

  10. meta生成器 —— 表单元素组件

    手写代码? meta(json)需要手写吗?别闹,手写多麻烦呀,我这么懒怎么可能手写,这辈子都别想,所以要弄个工具出来,咱们说干就干. 这个工具,说白了本身就是一个表单,一个meta属性对应一个met ...