写一个重新排序函数

Writing a reorder function

本文关键字:函数 新排序 排序 一个      更新时间:2023-10-16

我必须编写一个函数,将三个变量从最低到最高重新排序。函数名称是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;
}

我将把这三个值放在一个向量中并调用排序算法。这提供了一个整洁的函数,没有if语句

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void reorder(int &min, int &med, int &max)
{
    vector<int> vec;
    vec.push_back(min);
    vec.push_back(med);
    vec.push_back(max);
    sort(vec.begin(), vec.end());
    min = vec[0];
    med = vec[1];
    max = vec[2];
}
int main()
{
    int min = 1, med = 1, max = 3;
    cout << min << " " << med << " " << max << endl;
    reorder(min, med, max);
    cout << min << " " << med << " " << max << endl;
    system("pause");
    return 0;
}

我不认为使用min3int()max3int()排序是一种简单的方法,因为它们没有告诉你任何关于其他变量的顺序。

我建议写一些简单易懂的东西,比如:

void reorder(int &a, int &b, int &c)
{
    if (b < a) std::swap(b, a);
    if (c < a) std::swap(c, a);
    if (c < b) std::swap(c, b);
}

可以在没有if的情况下比较值,但我更喜欢清晰的代码,除非测量显示需要英勇的优化。也可以只使用两次交换对三个变量进行排序(参见这个问题)。