gpt4 book ai didi

C++如何将函数连接到程序?

转载 作者:太空宇宙 更新时间:2023-11-04 15:43:53 25 4
gpt4 key购买 nike

我是 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;
}

最佳答案

从 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;
}

关于C++如何将函数连接到程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19755527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com