2015-10-16 35 views
0

我必須編寫一個函數,將從最低到最高三個變量重新排序。函數的名字是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; 
} 
+0

您如何期望從'void'函數返回'min,med,max;'? – CoryKramer

+0

我意識到,我發佈後,我不想返回那些即時通訊新的cpp,所以這是一個用戶錯誤:P –

回答

1

我把三個值中的向量並調用排序算法。這給出了一個整潔乾淨的功能,沒有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; 
} 
+0

你仍然有'if'(和循環)他們只是隱藏在'std :: sort '。這也做內存分配。 – Blastfurnace

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對值進行比較,但我更喜歡清楚的代碼,除非測量顯示需要英雄的優化。也可以僅用兩次交換對三個變量進行排序(see this question)。