我必须编写一个函数,将从最低到最高三个变量重新排序。函数的名字是void reorder,我很好奇,如果无论如何,我可以在这个函数里面调用我的max和min函数来压缩我的代码,或者如果有一种方法我不使用IF语句编写它。我已经开始了,我想知道继续之前该怎么做。 谢谢编写重新排序功能
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;
/*
*
*/
int min3int(int a, int b, int c)
// function returns the minimum of 3 integer inputs
//@
//@
{
if(a<b)
{
if(a<c)
{
return a;
}
}
else if(b<a)
{
if(b<c)
{
return b;
}
}
else if (c<a)
{
if(c<b)
{
return c;
}
}
}
int max3int(int a, int b, int c)
// function returns the maximum of 3 integer inputs
//@
//@
{
if(a>b)
{
if(a>c)
{
return a;
}
}
else if(b>a)
{
if(b>c)
{
return b;
}
}
else if (c>a)
{
if(c>b)
{
return c;
}
}
}
void reorder(int min,int med,int max);
// function reorders the 3 reference parameters so that they are in ascending order
//@
//@
int temp;
if(min<med)
{
if(med<max)
{
if(min<max)
{
min=min;
med=med;
max=max;
}
else if(med<min)
{
if (med<max)
{
if (max<)
}
}
}
}
bool isFactor(int num1,int num2);
// function checks if the first int is a factor of the second int.
int main(int argc, char** argv) {
int dividend;
int factor1, factor2, factor3;
cout << "Enter the number to be divided: ";
cin >> dividend;
cout << endl << "Enter the first possible factor: ";
cin >> factor1;
cout << endl << "Enter the second possible factor: ";
cin >> factor2;
cout << endl << "Enter the third possible factor: ";
cin >> factor3;
cout << endl << "Possibilites entered are in the range of " << min3int(factor1,factor2,factor3)
<< " to " << max3int(factor1,factor2,factor3) << "." << endl;
reorder(factor1,factor2,factor3);
cout << endl << "The following numbers were determined to be factors of " << dividend << ": ";
if(isFactor(factor1,dividend))
cout << factor1 << " ";
if(isFactor(factor2, dividend))
cout << factor2 << " ";
if(isFactor(factor3, dividend))
cout << factor3;
cout << endl << endl;
return 0;
}
您如何期望从'void'函数返回'min,med,max;'? – CoryKramer
我意识到,我发布后,我不想返回那些即时通讯新的cpp,所以这是一个用户错误:P –