1.编写一个程序,不断要求用户输入2个数,直到其中的一个数为0.对于每两个数,程序将使用一个函数调用计算他们的平均值,并返回给main(),而后者将直接报告结果。调和平均数是指倒数平均值的倒数,计算公式如下:
调和平均数=2.0xy/(x+y);
#include <iostream>
using namespace std;
double jishu(double x, double y);
int main()
{
cout << "please enter liangge shuzi zhidao 0jieshu";
double x, y;
cin >> x;
cin.get();
cin >> y;
cin.get();
cout << "tiaohe ping junhsu :" << jishu(x, y) << endl;
while (x != 0 && y != 0)
{
cin >> x;
cin.get();
cin >> y;
cin.get();
cout << "tiaohe ping junhsu :" << jishu(x, y) << endl;
}
system("pause");
return 0;
}
double jishu(double x, double y)
{
return 2.0*x*y / (x + y);
}
2.编写一个程序,要求用户输入最多10个高尔夫成绩,并将其储存在一个数组中。程序允许用户提早结束输入,并在一行显示所有成绩,然后报告平均成绩。请使用3个数组处理函数分别进行输入显示和计算平均值。
#include <iostream>
using namespace std;
void show(double p);
double jisuan(double p, int i);
int main()
{
cout << "please enter your gaoerf chengji zuiduo 10 ci,shuru 0 jieshu " << endl;
int i = 0,k=0;
double *j=new double[10];
double sum = 0,q;
while (i <10)
{
double in_char;
cin >> in_char;
if (cin.good())
{
j[i] = in_char;
i++;
}
else
{
break;
}
}
cout << " i de shu chu " << i << endl;
cout << "your de chengji wei : ";
for (int f = 0; f < i; f++)
{
show(j[f]);
}
cout << endl;
for (int f = 0; f < i; f++)
{
q = jisuan(j[f], i);
sum = sum + q;
}
cout << "ni de ping jun cheng ji wei :" << sum << endl;
system("pause");
return 0;
}
void show(double p)
{
cout << p << " ";
}
double jisuan(double k,int i)
{
return k / i;
}
3.下面是一个结构声明;
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
}
编写一个函数,按值传递box结构,并显示每个成员值。
编写一个函数,传递box结构的地址,并将volume成员设置为其他三维长度的乘积。
编写一使用这2个函数的程序。
#include <iostream>
using namespace std;
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
void show(char maker[40],float heignt,float width,float length);
double jisuan(double i, double j, double k);
int main()
{
double j;
box p = { "one", 10, 20, 30 };
show(p.maker, p.height, p.width, p.length);
j=jisuan(p.height, p.length, p.width);
cout << "ta men de cheng ji wei : " << j;
system("pause");
return 0;
}
void show(char maker[40], float heignt, float width, float length)
{
cout << "ge ge dui xiang" << endl;
cout << "name " << maker << " height " << heignt << " width " << width << " length " << length; << endl;
}
double jisuan(double i, double j, double k)
{
return i*j*k;
}
4.许多州的彩票发行机构都使用如程序清单7.4所示的简单彩票的变体,在这些玩法中,玩家从一组被称为域号码的号码中选择几个,例如,可以从域号码1-47中选择5个号码;还可以从第二个区间1-27选择一个号码称为特选号码。要赢得头奖,必须正确猜中所有号码。中头奖的几率是选中所有域号码的几率与选择特选号码几率的乘积。例如在这些例子中,中头奖的几率是从47个号码中正确选取6个号码的几率与从27个号码中正确选择1个号码的几率的乘积。请修改程序清单7.4,以计算中得到这种彩票头奖的概率。
#include <iostream>
using namespace std;
long double probability(unsigned number, unsigned picks, unsigned number1, unsigned picks1);
int main()
{
double total, choices ,total1, choices1;
cout << "Enter the total number of choices on the game card and \n"
"the number of picks allowed:\n";
while ((cin >> total >> choices>>total1>>choices1) && choices <= total)
{
cout << "You have one chance in";
cout << probability(total, choices, total1, choices1);
cout << " of winning.\n";
cout << "Next two number (q to quit):";
}
cout << "bye\n";
system("pause");
return 0;
}
long double probability(unsigned number, unsigned picks, unsigned number1, unsigned picks1)
{
long double result = 1.0;
long double n,n1;
unsigned p,p1;
for (n = number, p = picks,n1=number1,p1=picks1;p1>0, p > 0;n1--,p1--, n--, p--)
result = result *n / p;
return result;
}
5.定义一个递归函数,接受一个整体参数,并返回该参数的阶乘。前面讲过,3的阶乘写作3!,等于32!,以此类推;而0的阶乘为1.通用的计算公式为,如果n大于0,则n!=n(n-1)!。在程序中对该函数进行测试,程序使用循环让用户输入不同的值,程序将报告这些值的阶乘。
#include <iostream>
using namespace std;
double jisuan(double j);
int main()
{
double p;
cout << "please enter your number,shu ru q jieshu : ";
cin >> p;
cin.get();
cout << "ni de " << p << "!" << "wei :" << jisuan(p);
while (p != 'q')
{
cin >> p;
cin.get();
cout << "ni de " << p << "!" << "wei :" << jisuan(p);
}
system("pause");
return 0;
}
double jisuan(double j)
{
double sum=1;
for (; j > 0; j--)
{
sum = sum*j;
}
return sum;
}
6.编写一个程序,它使用下列函数:
Fill——array()将一个double数组的名称和长度作为参数。它提示用户输入double值,并将这些值存储到数组中,当数组被填满或用户输入非数字时,输入将停止,并返回实际输入了多少个数字。
Show—array()将一个double数组的名称和长度作为参数,并显示改数组的内容。
Reverse-array()将一个double数组的名称和长度作为参数,并显示改数组的内容。
程序将使用这些函数来填充数组,然后显示数组;翻转数组,然后显示数组;翻转数组中除第一个和最后一个元素外的所有芫荽,然后显示数组。
#include <iostream>
#include <cctype>
using namespace std;
int Fill_array(double j[],int i);
void Show_array(double j[], int i);
void Reverse_array(double j[], int i);
int main()
{
double j[20];
int i, k;
cout << "please enter double number" << endl;
cout << "please enter ni de shuzu chang " << endl;
cin >> i;
cin.get();
i=Fill_array(j,i);
Show_array(j, i);
Reverse_array(j, i);
system("pause");
return 0;
}
int Fill_array(double j[], int i)
{
cout << "this " << endl;
int k=0;
double in_char;
cin >> in_char;
if (cin.good())
{
j[k] = in_char;
k++;
}
while (k<i)
{
cin >> in_char;
if (cin.good())
{
j[k] = in_char;
k++;
}
else
{
break;
}
}
return k;
}
void Show_array(double j[],int i)
{
int k=0;
cout << "ni de shu zu wei :";
while (k < i)
{
cout << " " << j[k];
k++;
}
}
void Reverse_array(double j[], int i)
{
int k=0;
double change;
i = i - 1;
cout << "shu zu de fan zhaun wei :" << endl;
while (i > k)
{
change = j[k];
j[k] = j[i];
j[i] = change;
k++; i--;
}
for (int p = 0; p <= i+1; p++)
{
cout << " " << j[p];
}
}
7.修改程序清单7.7中的3个数组处理函数,使之使用两个指针参数来表示区间。fill—array()函数不返回实际读取了多少个数字,而是返回一个指针,该指针指向最后被填充的位置;其他的函数可以将该指针最为第二个参数,以标识数据结尾。
#include <iostream>
const int Max = 5;
using namespace std;
double *fill_array(double *ar,double *limit);
void show_array(const double *ar, double *n);
void revalue(double r, double *ar, double *n);
int main()
{
double properties[Max];
double *size = fill_array(properties, properties+ Max);
show_array(properties, size);
if (size > 0)
{
cout << "Enter revaluation factor: ";
double factor;
while (!(cin>>factor))
{
cin.clear();
while (cin.get() != '\n')
{
continue;
}
cout << "Bad input ;Please enter a number :";
}
revalue(factor, properties,size);
show_array(properties, size);
}
cout << "Done .\n";
cin.get();
cin.get();
system("pause");
return 0;
}
double *fill_array(double *ar, double *limit)
{
double temp;
int i=0;
double *d;
for (d=ar; d < limit; d++,i++)
{
cout << "Enter value #" << (i + 1) << ":";
cin >> temp;
if (!cin)
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input ;input process terminated.\n";
break;
}
else if (temp < 0)
break;
*d = temp;
}
return d;
}
void show_array(const double *ar, double *n)
{
int i = 0;
for (const double *d = ar; d < n; d++,i++)
{
cout << "Property #" << (i + 1) << ": $";
cout << *d << endl;
}
}
void revalue(double r, double *ar, double* n)
{
for (double *d = ar ; d < n; d++)
{
*d *= r;
}
}
8.1在不使用array类的情况下完成清单7.15所做的工作。编写亮这样的版本:
a。使用const char数组存储表示季度名称的字符串,并使用double数组存储开支。
b.使用const char数组存储表示季度名称的字符串,并使用一个结构,该结构只有一个成员——一个用于存储开支的double数组。这种与使用array类的基本设计类似。
#include <iostream>
#include <string>
using namespace std;
const int seasons = 4;
const char *snames[seasons] = { "spring", "summer", "fall", "winter" };
void fill(double j[]);
void show(double j[]);
int main()
{
double k[seasons];
fill(k);
show(k);
system("pause");
return 0;
}
void fill(double j[])
{
for (int i = 0; i < seasons; i++)
{
cout << "Enter " << snames[i] << "expenses: ";
cin >> j[i];
}
}
void show(double j[])
{
double total = 0.0;
cout << "\nexp\n";
for (int i = 0; i < seasons; i++)
{
cout << snames[i] << ":$" << j[i] << endl;
total += j[i];
}
cout << "Total Expenses:$" << total << endl;
}
8.2
#include <iostream>
using namespace std;
const int seasons =4;
struct MyStruct
{
double expense[seasons];
};
const char*snames[seasons] = { "spring", "summer", "fall", "winter" };
void fill(MyStruct *k);
void show(MyStruct *k);
int main()
{
MyStruct p;
fill(&p);
show(&p);
system("pause");
return 0;
}
void fill(MyStruct *k)
{
for (int i = 0; i < seasons; i++)
{
cout << "Enter " << snames[i] << "expense: ";
cin >> k->expense[i];
}
}
void show(MyStruct *k)
{
double total = 0;
cout << "\nEXPENSES\n";
for (int i = 0; i < seasons; i++)
{
cout << snames[i] << ": $" << k->expense[i] << endl;
total += k->expense[i];
}
cout << "Total Expense: $" << total << endl;
}
9.这个练习让你编写处理数组和结构的函数。下面是程序的框架,请提供其中描述的函数,以完成该程序。
#include <iostream>
using namespace std;
const int SLEM = 30;
struct student
{
char fullname[SLEM];
char hobby[SLEM];
int ooplevel;
};
int getinfo(student pa[], int n);
void display1(student st);
void display2(const student *ps);
void display3(const student pa[], int n);
int main()
{
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 << "Eone\n";
system("pause");
return 0;
}
int getinfo(student pa[], int n)
{
int count = 0;
for (int i = 0; i < n; i++)
{
cout << "please enter your full name";
cin.getline(pa[i].fullname, SLEM);
cout << "please enter your hobby:";
cin.getline(pa[i].hobby, SLEM);
cout << "please enter your ooplevel";
cin >> pa[i].ooplevel;
cin.get();
count = count + 1;
}
cout << "shuchu huan hang" << endl;
return count;
}
void display1(student st)
{
cout << "\ndisplay1: fullname :" << st.fullname << " hobby: " << st.hobby << " ooplevel" << st.ooplevel << endl;
}
void display2(const student *ps)
{
cout << "\ndisplay2: fullname :" << ps->fullname << " hobby: " << ps->hobby << " ooplevel" << ps->ooplevel << endl;
}
void display3(const student pa[], int n)
{
cout << "\ndisplay3:" << endl;
for (int i = 0; i < n; i++)
{
cout << pa[i].fullname << " hobby: " << pa[i].hobby << " ooplevel" << pa[i].ooplevel << endl;
}
}
10.设计一个名为calculate()的函数,它接受2个double值和指向函数的指针,而被指向的函数接受两个double参数,并返回一个double值。calculate()函数的类型也是double,并返回被指向的函数使用calculate()两个double参数计算得到值。例如假设add()函数的定义如下:
double add(double x,double y)
{
return x+y;
}
则下述代码中的函数调用将导致calculate()把2.5和10.4传递给add()函数,并返回add()的返回值(12.9):
double q=calculate(2.5,10.4,add);
请编写一个程序,它调用上述两个函数和至少另一个与add()类似的函数。该程序使用循环来让用户成对地输入数字。对于每对数字,程序都使用calculate()来调用add()和至少一个其他的函数。如果读者爱冒险,可以尝试创建一个指针数组,其中的指针指向add()样式的函数,并编写一个循环,使用这些指针连续让calculate()调用这些函数。提示:下面是声明这种指针数组的方式,其中包含三个指针:
double (*pf【3】)(double,double);
可以采用数组初始化语法,并将函数名作为地址来初始化这样的数组。
#include<iostream>
using namespace std;
double add(double i, double j);
double x(double i, double j);
double calculate(double i, double j, double(*p)(double k, double u));
int main()
{
double i, j;
while (1)
{
cout << "please enter two number: ,shuru q jieshu" << endl;
cin >> i >> j;
if (!cin.good())
break;
double p = calculate(i, j, add);
double q = calculate(i, j, x);
cout << i << " + " << j << " = " << p;
cout << i << " * " << j << " = " << q;
}
system("pause");
return 0;
}
double add(double i, double j)
{
return i + j;
}
double x(double i, double j)
{
return i*j;
}
double calculate(double i, double j, double(*p)(double k, double u))
{
double t;
t = (*p)(i, j);
return t;
}