我如何在main下面写一个函数?

0 c++ declaration function-prototypes

我有一位老师/教授只喜欢在 main 下方而不是在它上方编写函数。我将如何重写它以编写 main.js 下面的函数?一直在下面教怎么做,我可以提前声明吗?如果是这样,它会是什么样子?

#include <iostream>
using namespace std;
/* Output
Enter 4 test score numbers
80 90 80 90
Average is 86.6667

Enter 4 test score numbers
70 80 -60 90
Bad Data

*/
bool input(double& test1, double& test2, double& test3, double& test4)
{
cout << "Enter 4 test score numbers" << endl;
cin >> test1;
cin >> test2;
cin >> test3;
cin >> test4;

if(test1 < 0 || test2 < 0 || test3 < 0 || test4 < 0)
return false;
else
return true;
}

double average(double test1, double test2, double test3, double test4, bool data)
{
if(data == false)
{
cout << "Bad Data" << endl;
return -1;
}
double low = test1;
if(test2 < low)
{
low = test2;
}
if(test3 < low)
{
low = test3;
}
if(test4 < low)
{
low = test4;
}
return (test1+test2+test3+test4-low)/3;
}

int main()
{
double test1, test2, test3, test4;
bool data;
data = input(test1, test2, test3, test4);
double avg = average(test1, test2, test3, test4, data);
if(avg != -1)
{
cout << "Average is " << avg << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)

Wer*_*nze 6

只需将声明放在之前main

bool input(double& test1, double& test2, double& test3, double& test4);
double average(double test1, double test2, double test3, double test4, bool data);
Run Code Online (Sandbox Code Playgroud)

以及之后的定义main

bool input(double& test1, double& test2, double& test3, double& test4)
{
    ....
}

double average(double test1, double test2, double test3, double test4, bool data)
{
    ....
}
Run Code Online (Sandbox Code Playgroud)