0

我是 C++ 的初学者,我想做一个找到 3 个数字中最大的程序的程序。由于我无法定义主要发现该功能的功能,我该如何在不使用类的情况下进行操作?感谢您的时间!

#include<iostream>
#include<cmath>
using namespace std;

int maximum(int x, int y, int z);

int main()
{
    int x,y,z;
    int max=y;
    int min=x;

    cout<<"Enter 3 numbers to find out which one is bigger. First: ";
    cin>>x;
    cout<<"Second: ";
    cin>>y;
    cout<<"Third: ";
    cin>>z;
    cout<<"Biggest is: "<<max<<endl;

    cout<<"Earliest time to meet is: "<<max<<endl;

    return 0;
}

//thats the function that checks the biggest number
int maximum (int x, int y, int z)
{
    int max = x;

    if (y > max) {
        max = y;
    }
    if (z > max) {
        max = z;
    }

    return max;
}
4

3 回答 3

3

只需添加对您的函数的调用,例如

 max = maximum(x,y,z);

在显示之前

 cout<<"Biggest is: "<<max<<endl;
于 2013-11-03T17:06:20.057 回答
3

从 main() 函数调用最大函数。简单的。

#include<iostream>
#include<cmath>
using namespace std;

int maximum(int x, int y, int z);

int main()
{
int x,y,z;

cout<<"Enter 3 numbers to find out which one is bigger. First: ";
cin>>x;
cout<<"Second: ";
cin>>y;
cout<<"Third: ";
cin>>z;
cout<<"Biggest is: "<< maximum (x, y, z) << endl;

return 0;
}

//thats the function that checks the biggest number
int maximum (int x, int y, int z) 
{
int max = x;

if (y > max) {
    max = y;
}
if (z > max) {
    max = z;
}

return max;
}
于 2013-11-03T17:12:06.853 回答
0
#include<iostream>
#include<cmath>
using namespace std;

int maximum(int x, int y, int z);

int main()
{
   int x,y,z;
   // get values x, y, and z from user input
   int max = maximum(x, y, z);
   // ...
}
于 2013-11-03T17:06:22.000 回答