2015-11-15 34 views
0

我有一個帶有2個「成對」整數數組newNumerator []和newDenominator []的程序,它們都有9個整數。我寫了一個按升序對它們進行排序的函數,但是我不確定它是否有效,因爲我還沒有成功編譯它。我也遇到了類型轉換的一些問題。下面是函數定義 -C++對兩個成對整數數組的「百分比」進行排序

void sortData(int *newNumerator[], int *newDenominator[], int newSize) 
{ 
    int temp1; 
    int temp2; 
    bool swap; 
    int count = 0; 
    double percentageLeft = 100.0 * static_cast<double>(newNumerator[count])/newDenominator[count]; 
    double percentageRight = 100.0 * static_cast<double>(newNumerator[count + 1])/newDenominator[count + 1]; 

    do 
    { swap = false; 
     for(count = 0; count < (newSize - 1); count++) 
     { 
      if(percentageLeft > percentageRight) 
      { 
       temp1 = *newNumerator[count]; 
       *newNumerator[count] = *newNumerator[count + 1]; 
       *newNumerator[count + 1] = temp1; 

       temp2 = *newDenominator[count]; 
       *newDenominator[count] = *newDenominator[count + 1]; 
       *newDenominator[count + 1] = temp2; 

       swap = true; 
      } 
     } 
    } while (swap); 
} 

我遇到的類型轉換問題是percentageLeft和percentageRight因爲newNumerator []和newDenominator []是整數指針,但要獲得「百分比」其中我需要它雙倍。不知道如何去做這件事。基本上我只需要弄清楚如何解決這個問題,並且知道我的功能是否能達到它的目的。任何幫助表示讚賞,並請讓我知道如果有什麼我可以進一步明確

+0

請始終在這個問題的錯誤信息也 –

+0

現在我得到的只有一個「161:38:從的static_cast‘INT *’到‘雙’是不允許「 – Sosa

回答

1

你擁有的主要錯誤是,你要static_cast<>給錯誤的價值觀。稍後,您可以通過解除引用來正確使用它們,但是在劇組中您缺少該選項。

你需要的是:

double percentageLeft = 100.0 * static_cast<double>((*newNumerator)[count])/*newDenominator[count]; 
double percentageRight = 100.0 * static_cast<double>((*newNumerator)[count + 1])/*newDenominator[count + 1]; 

加括號使其清晰明確的提領是什麼。

此外,如果您不更改實際的數組指針,但僅更改數組的內容,則可以完全刪除解除引用的麻煩。如果您將函數定義爲

void sortData(int newNumerator[], int newDenominator[], int newSize) 

您可以在每次使用時都使用正常索引而不需要解除引用。

+0

謝謝,解決了類型轉換問題,但是現在當我調用函數」sortData(newNumerator,newDenominator,newSize);「時,它表示不匹配調用sortData,表示」候選函數不可行:第一個參數沒有從'int'到'int **'的已知轉換「我如何解決這個問題? – Sosa

+1

@Sosa這意味着你給它一個int,而不是一個指向int數組的指針,所以你需要添加關於你如何調用函數和使用什麼參數類型的信息 –

+0

這裏是一個全部代碼的pastebin 。錯誤來自第45行http://pastebin.com/R4LW5a51 – Sosa

1

你說:

我有2「配對」整型數組程序newNumerator []和newDenominator []

然而,你的函數定義爲:

void sortData(int *newNumerator[], int *newDenominator[], int newSize) 

我認爲應該是:

void sortData(int newNumerator[], int newDenominator[], int newSize) 

如果確實如此,那麼行:

temp1 = *newNumerator[count]; 
*newNumerator[count] = *newNumerator[count + 1]; 
*newNumerator[count + 1] = temp1; 

temp2 = *newDenominator[count]; 
*newDenominator[count] = *newDenominator[count + 1]; 
*newDenominator[count + 1] = temp2; 

需求是:

temp1 = newNumerator[count]; // Remove the * 
newNumerator[count] = newNumerator[count + 1]; 
newNumerator[count + 1] = temp1; 

temp2 = newDenominator[count]; 
newDenominator[count] = newDenominator[count + 1]; 
newDenominator[count + 1] = temp2; 
2

我建議使用STL進行排序。使事情變得更簡單,更容易理解。

#include <vector> 
#include <algorithm> 

struct Ratio 
{ 
    int numerator; 
    int denominator; 

    double toDouble() const 
    { 
     return static_cast<double>(numerator) 
      /static_cast<double>(denominator); 
    } 

    bool operator < (Ratio const& other) const 
    { 
     return toDouble() < other.toDouble(); 
    } 
}; 

void main() 
{ 
    std::vector<Ratio> ratios = { {1, 2}, {5, 7}, {2, 11} }; 
    std::sort(ratios.begin(), ratios.end()); 
} 

處理原始數組和手動排序幾乎總是比較單調和容易出錯的方法。

http://en.cppreference.com/w/cpp/algorithm/sorthttp://en.cppreference.com/w/cpp/container/vector參考