您的位置:首页 > 编程语言 > C语言/C++

第七章 函数——C++得编程模块

2017-03-14 22:30 495 查看

第七章 函数——C++得编程模块

第一题:键盘输入两个数据,使用一个函数计算其调和平均数,输入数据中有0则停止。

#include<iostream>
using namespace std;
double tiaohe(double a, double b);
int main()
{
double a, b;
cout << "Please Enter two number (quit with 0): \n";
cout << "Number 1: ";
cin >> a;
cout << "Number 2: ";
cin >> b;
while (a&&b)
{
cout << "The result is: "<<tiaohe(a, b)<<endl;
cout << "Next two number (quit with 0): \n";
cout << "Number 1: ";
cin >> a;
cout << "Number 2: ";
cin >> b;
}
cout << "Program ends.\n";
system("pause");
return 0;
}

double tiaohe(double a, double b)
{
return 2 * a*b / (a + b);
}




第二题:键盘输入成绩,最多输入10个,用数组存储,最后计算平均值,并显示所有数据,使用3个函数分别用来记录,显示,和计算平均数。

#include<iostream>
using namespace std;
const int Arsize=10;
int input_data(double arr[], int Arsize);
void disp_data(double arr[], int size);
double aver_data(double arr[], int size);
int main()
{
double goals[Arsize],aveg;
int real_size;
real_size=input_data(goals, Arsize);
disp_data(goals, real_size);
aveg=aver_data(goals, real_size);
cout << "The average is "<<aveg<<" .\n";
system("pause");
return 0;
}

int input_data(double arr[], int size)
{
double temp;
int i;
for (i = 0; i < size;i++)
{
cout << "Enter N0."<<i+1<<" record (quit wiht no number): ";
cin >> temp;
if (!cin)
break;
else
arr[i] = temp;
}
cout << "Input is over.";
return i;
}
void disp_data(double arr[], int size)
{
cout << "\nALL the record are here : ";
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << "Over\n";
}
double aver_data(double arr[], int size)
{
double sum = 0;
int i=0;
for (; i < size; i++)
sum += arr[i];
return sum / i;
}






第三题:使用规定的结构体,写一个函数按值传递值给该函数并显示,再写一个函数按地址计修改其中的值,调用这两个函数完成程序。

#include<iostream>
using namespace std;
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
box read_and_disp_data(box a,int i);
void volu(box *a);
int main()
{
box test = {0};
test=read_and_disp_data(test,1);
volu(&test);
read_and_disp_data(test,0);
system("pause");
return 0;
}

box read_and_disp_data(box a,int i)
{
if (i == 1)
{
cout << "Enter the maker: ";
cin.getline(a.maker, 40);
cout << "Enter the height,width,and length: ";
cin >> a.height >> a.width >> a.length;
return a;
}
else
{
cout << "Maker is: " << a.maker << endl;
cout << "Height is: " << a.height << endl;
cout << "Width is: " << a.width << endl;
cout << "Length is: " << a.length << endl;
cout << "Volume is: " << a.volume << endl;
}

}

void volu(box *a)
{
a->volume = a->height*a->length*a->width;
}




第四题:调用函数,计算从指定数据中挑选正好挑中某几个数据的概率。计算同时完成47选5和27选1的概率

#include<iostream>
using namespace std;
long double sixorone(int total, int picks);
i
100c3
nt main()
{

int totala = 47, picksa = 5;
int totalb = 27, picksb = 1;
long double resulta, resultb;
resulta = sixorone(totala, picksa);
resultb = sixorone(totalb, picksb);
cout << "Chance to get first reward is: ";
cout << resulta*resultb<<endl;
system("pause");
return 0;
}

long double sixorone(int total,int picks)
{
long double result=1;
long double n;
unsigned p;
for (n=total,p=picks; p>0;n--, p--)
result = result*n / p;
return result;
}




第五题:使用递归计算阶乘,输入非数字退出。

#include<iostream>
using namespace std;
long int jie_cheng(int a);
int main()
{
cout << "Enter the number(quit with no number):  ";
int a;
cin >> a;
while (cin)
{
cout <<"The result is: "<< jie_cheng(a) << endl;
cin.get();
cout << "Enter the number(quit with no number): ";
cin >> a;
}
cout << "Program ends.\n";
system("pause");
return 0;
}
long int  jie_cheng(int a)
{

long int result = a;
if (a > 0)
{
a--;
result = result*(jie_cheng(a));
return result;
}
return 1;
}




第六题:编写三个函数,一个用来填充数组,一个用来显示数组,一个蒋数组中初第一个和最后一个数以外的数反转。

#include<iostream>
using namespace std;
int Fill_arry(double arr[], int arsize);
void Show_arry(double arr[], int arsize);
void Reverse_arry(double arr[], int arsize);
int main()
{
const int goodsize=10;
double good[goodsize];
int realsize=Fill_arry(good, goodsize);
Show_arry(good, realsize);
Reverse_arry(good, realsize);
Show_arry(good, realsize);
system("pause");
return 0;
}
int  Fill_arry(double arr[], int arsize)
{
cout << "Enter number(divide with space and quit with no number): \n";
int i = 0;
double temp;
cin >> temp;
while (cin)
{
if (i < arsize)
{
arr[i] = temp;
i++;
}
else
{
break;
}
cin >> temp;
}
cout << "Total number is " << i << ".Input is over."<< endl;
return i;
}

void Show_arry(double arr[], int realsize)
{
cout << "The arry is: " << endl;
for (int i = 0; i < realsize; i++)
cout << arr[i]<<" ";
cout << endl;
}

void Reverse_arry(double arr[], int arsize)
{
double temp;
for (int i = 1,j=arsize-1; i < (arsize / 2); i++)
{
temp = arr[i];
arr[i] = arr[j - i];
arr[j - i] = temp;
}
}


ps:程序设计为最多读入10个数据,输入非数字时结束。





第七题:修改第六题,使用指针来表示数组区间,处理完后返回指针,显示实际输入个数,指向最后被填充得位置,其他函数调用该指针进行处理。

#include<iostream>
using namespace std;
double* Fill_arry(double *arrstar, double *arrend);
void Show_arry(double arr[], double *arrend);
void Reverse_arry(double arr[], double *arrend);
int main()
{
const int goodsize = 10;
double good[goodsize];
double *addend = Fill_arry(good, good+goodsize);
Show_arry(good, addend);
Reverse_arry(good, addend);
Show_arry(good, addend);
system("pause");
return 0;
}
double* Fill_arry(double *arrstar, double *arrend)
{
cout << "Enter number(divide with space and quit with no number): \n";
double temp;
cin >> temp;
int i = 0;
while (cin)
{
if (arrstar < arrend)
{
*arrstar = temp;
arrstar++;
i++;
}
else
{
break;
}
cin >> temp;
}
cout << "Total number is " << i << ".Input is over." << endl;
return arrstar;
}

void Show_arry(double arr[], double *arrend)
{
cout << "The arry is: " << endl;
for (; arr < arrend; arr++)
cout << *arr << " ";
cout << endl;
}

void Reverse_arry(double arr[], double *arrend)
{
double temp,*star;
star = arr;
for (double *ared = arrend - 2; arr != (star-1+(arrend-star)/2);)
{
arr++;
temp = *arr;
*arr = *ared;
*ared = temp;
ared--;
}
}






第八题:根据题目所给伪代码编写函数,完成程序。在输入名字时按回车结束输入。

#include<iostream>
#include<string>
using namespace std;
const int SLEN = 30;
struct student{
char fullname[SLEN];
char hobby[SLEN];
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 << "Done\n";
system("pause");
return 0;
}

int getinfo(student pa[], int n)
{
int i = 0;
cout << "#" << i+1 << endl;
cout << "Enter the name of the student: ";
cin.getline(pa[i].fullname,SLEN);
while (cin)
{
if (i < n)
{
cout << "Enter the hobby : ";
cin.getline(pa[i].hobby, SLEN);
cout << "Enter the opplevel : ";
cin >> pa[i].ooplevel;
cin.get();
}
else
{
break;
}
i++;
cout << "#" << i+1<< endl;
cout << "Enter the name of the student: ";
if (cin.get() != '\n')
cin.getline(pa[i].fullname, SLEN);
else
break;
}
cout << "Enter is over.\n";
return i;
}

void display1(student st)
{
cout << "#Show info by value.\n";
cout << "Student name: " << st.fullname<<endl;
cout << "Student hobby: " << st.hobby << endl;
cout << "Student opplevel: " << st.ooplevel << endl;
}
void display2(const student * ps)
{
cout << "#Show info by point.\n";
cout << "Student name: " << ps->fullname << endl;
cout << "Student hobby: " << ps->hobby << endl;
cout << "Student opplevel: " << ps->ooplevel << endl;
}

void display3(const student pa[], int n)
{
for (int i = 0; i < n; i++)
{
cout << "#Student " << i+1 << endl;
cout << "Name: " << pa[i].fullname << endl;
cout << "Hobby: " << pa[i].hobby << endl;
cout << "Opplevel: " << pa[i].ooplevel << endl;
}
}




第九题:设计三个函数,其中两个分别接受两个double型数据并返回一个double型数据,第三个函数接受两个double型数据和一个指向函数得指针,并返回一个double型数据。

#include<iostream>
using namespace std;
double fun(double a, double b);
double add(double x, double y);
double calculate(double a, double b, double (*p)(double a,double b));
int main()
{
double q,p;
cout << "Please enter two number: ";
cin >> q >> p;
while (cin)
{
cout << "add is " << calculate(p, q, add) << endl;
cout << "fun is " << calculate(p, q, fun) << endl;
cout << "Please enter two number: ";
cin >> q >> p;
}
cout << "Program ends.\n";
system("pause");
return 0;
}
double add(double x, double y)
{
return x + y;
}
double fun(double a, double b)
{
return a + b+1000;
}
double calculate(double a, double b, double(*p)(double a, double b))
{
return (*p)(a,b);
}




ps:函数add返回两个输入值之和,函数fun返回1000与两个输入值得总和。

第九++题:使用函数数组,利用循环来调用add和fun函数。

#include<iostream>
using namespace std;
double fun(double a, double b);
double add(double x, double y);
double calculate(double a, double b, double(*p)(double a, double b));

int main()
{
double(*pf[2])(double, double);
pf[0] = fun;
pf[1] = add;
double q, p;
cout << "Please enter two number: ";
cin >> q >> p;
while (cin)
{
for (int i = 0; i < 2; i++)
{
cout << "Result "<<i<<" is " << calculate(p, q, *pf[i]) << endl;
}
cout << "Please enter two number: ";
cin >> q >> p;
}
cout << "Program ends.\n";
system("pause");
return 0;
}

double calculate(double a, double b, double(*p)(double a, double b))
{
return (*p)(a, b);
}

double add(double x, double y)
{
return x + y;
}
double fun(double a, double b)
{
return a + b + 1000;
}




第七章结束。~。~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ 编程练习