7.1 计算调和平均数
#include<iostream>
using namespace std;
double cal(int x,int y);
int main(){
int x,y;
double ave;
while(1){
cout << "Please enter two number:";
cin>>x>>y;
if(x==0||y==0)
{
cout << "Input terminated\nQuit\n";
// break会跳出循环
break;
}
else{
ave=cal(x,y);
cout << "The harmonic average is " << ave << endl;
}
}
return 0;
}
double cal (int x,int y){
double ave;
// 注意类型转换double,不然x和y会以int 类型进行计算,计算的ave的结果也是int ,不过有2.0跟2的差别
// ave = 2.0 * double(x * y) / (x + y);
ave = 2.0* x * y / (x + y);
// 当乘以的是2的时候是整数,乘以是2.0的时候结果是浮点数
// ave = 2* x * y / (x + y);
return ave;
}
注意数据类型的定义和返回,以及隐式转换
7.2 记录高尔夫成绩
#include<iostream>
using namespace std;
const int MAX = 10;
//会返回实际输入的元素的个数
int input(double ar[],int limit);
//显示不能改变数组,要用const,n代表实际的数组元素个数,毕竟允许用户提前结束输入
void display(const double ar[],int n);
double cal(double ar[],int n);
//不同函数作用,其返回值类型也不同
int main()
{
double scores[MAX];
int size = input(scores,MAX);
display(scores,size);
double ave;
ave = cal(scores, size);
cout << "\nThe average score is " << ave << endl;
return 0;
}
int input(double ar[], int limit)
{
int i;
double score;
cout << "Please enter the scores, 'q' to quit.\n";
for (i = 0; i < limit; i++)
{
cout << "Enter the score #" << (i + 1) << ": ";
cin >> score;
// 当输入的类型不符合的时候cin会返回false
if (!cin)
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Input process terminated\nQuit\n";
break;
}
ar[i] = score;
}
return i;
}
void display(const double ar[], int n)
{
cout << "You entered " << n << " scores.\n";
cout << "The scores are:\n";
for (int i = 0; i < n; i++)
{
cout << ar[i] << "\t";
}
}
double cal(double ar[], int n)
{
// 注意类型是double
double ave;
double sum = 0.0;
for (int i = 0; i < n; i++)
{
sum += ar[i];
}
ave = sum / n;
return ave;
}
注意类型是double,总是犯这种小错误!
当输入的类型不符合的时候cin会返回false
if (!cin)
三个函数返回类型因为作用不同,而不同
7.3 结构体与函数
#include <iostream>
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
void display(box temp);
float cal(box * temp);
using namespace std;
int main()
{
cout << "The following is the example box.\n";
box temp;
cout << "Please enter the box maker: ";
cin >> temp.maker;
cout << "Please enter the height: ";
cin >> temp.height;
cout << "Please enter the width: ";
cin >> temp.width;
cout << "Please enter the length: ";
cin >> temp.length;
cout << "Please enter the volume: ";
cin >> temp.volume;
display(temp);
float vol;
vol = cal(&temp);
cout << "\nBut after the calculation, the volume of the box is " << vol << endl;
return 0;
}
void display(box temp)
{
cout << "\nThe maker of the box is " << temp.maker << "." << endl;
cout << "The height of the box is " << temp.height << "." << endl;
cout << "The width of the box is " << temp.width << "." << endl;
cout << "The length of the box is " << temp.length << "." << endl;
cout << "The volume of the box is " << temp.volume << "." << endl;
}
float cal(box * temp)
{
float vol;
vol = temp->height * temp->width * temp->length;
return vol;
}
注意传递box结构的地址,vol = cal(&temp);
7.4 彩票头奖几率
#include <iostream>
using namespace std;
double cal(int num1, int num2, int bingo1, int bingo2);
const int Num1 = 47;
const int Num2 = 27;
const int Bingo1 = 5;
const int Bingo2 = 1;
int main()
{
cout << "Your Probability to win the first prize in this game is\t";
double pro;
pro = cal(Num1, Num2, Bingo1, Bingo2);
cout << pro << " !!!" << endl;
return 0;
}
double cal(int num1, int num2, int bingo1, int bingo2)
{
double pro, pro1, pro2;
pro1 = double(bingo1) / double(num1);
pro2 = double(bingo2) / double(num2);
pro = pro1 * pro2;
return pro;
}
传参有关数量的是int,但计算的时候要转换为double,因为计算的是概率啊
pro1 = double(bingo1) / double(num1);
pro2 = double(bingo2) / double(num2);
7.5 递归函数
#include<iostream>
using namespace std;
int f(int n){
if(n==0){
return 1;
}
return f(n-1)*n;
}
int main(){
int n;
while(1)
{ cout << "Please enter the number for factorial: ";
cin>>n;
if(!cin){
cout << "Input terminated.\nQuit\n";
break;
}
else{
cout << "The factorial of " << n << " is " << f(n) << endl;
}
}
return 0;
}
7.6 数组函数
#include <iostream>
int Fill_array(double ar[], int size);
void Show_array(double ar[], int size);
void Reverse_array(double ar[], int size);
using namespace std;
const int Max = 10;
int main()
{
cout << "Please enter the data of array. 'q' to quit.\n";
double ar[Max];
int size;
size = Fill_array(ar, Max);
Show_array(ar, size);
Reverse_array(ar, size);
cout << "\nAfter the reverse.";
Show_array(ar, size);
Reverse_array(ar, size);
cout << "\nAfter the reverse.";
Reverse_array(ar + 1, size - 2);
Show_array(ar, size);
cout << endl;
return 0;
}
int Fill_array(double ar[], int size)
{
int i;
double j;
int num;
for (i = 0; i < size; i++)
{
cin >> j;
if (!cin)
{
cout << "Input terminated.\nQuit.\n";
break;
}
else
{
ar[i] = j;
}
num = i + 1;
}
return num;
}
void Show_array(double ar[], int size)
{
int i;
cout << "\nThe array is:\n";
for (i = 0; i < size; i++)
{
cout << ar[i] << " ";
}
}
void Reverse_array(double ar[], int size)
{
int i;
double temp;
for (i = 0; i < (size / 2); i++)
{
temp = ar[i];
ar[i] = ar[size - 1 - i];
ar[size - 1 - i] = temp;
}
}
但是最后一个功能的时候,我们在调用反转函数Reverse_array()时,使用的参数将有所变化,因为它要反转的是除去第一个元素和最后一个元素之外的元素,所以长度肯定比之前小2,而且起始也比之前往后移了1位,所以我们最后一个调用应该是形如以下这样的语句:
Reverse_array(ar + 1, size - 2);
7.9 函数练习
int getinfo(student pa[], int n){
int count = 0;
for (int i = 0; i<n; i++)
{ cout << "Please enter the fullname:";
cin.getline(pa[i].fullname, 30);
cout << "Please enter the hobby:";
cin >> pa[i].hobby; cout << "Please enter the ooplevel:";
cin >> pa[i].ooplevel;
cin.get();
count++;
}
cout << "Enter end!" << endl; return count;
}
需要注意的是getinfo()函数里面,由于要求输入全名fullname,所以肯定要允许输入多个单词,所以对于char数组要使用cin.getline()函数,而hobby和ooplevel的内容又是使用cin>>的方式输入,所以必须在cin.getline()函数前面一个cin>>的后面加一句cin.get()函数来抵消掉由于二者联合使用,而cin>>在前而导致的缓冲区内多出来的一个回车符
本文部分参考链接:https://blog.csdn.net/leowinbow/article/details/80980489