2012-03-26 40 views
1

好的,这是一个由于输出反向显示而被扣除的分配。我应该提示用户输入,然后按降序显示输入,但我遇到了显示问题。我有两个数组,一年[]来保存月份和一个month[totalMonths]来保存用户输入。当我排序和显示输入year[]不符合月份,它是固定的。例如,如果用户在Jan输入1,在Feb输入2,在Mar输入3,则显示为; 扬:3 月:2 三月:1排序输入反向工作

如何我能得到个月与对显示其应有的输入对应任何想法?这里是排序和显示功能:

void sortArray(double month[], string year[], int totalMonths) 
{ 
    int temp; 
    bool swap; 
    do 
    { 
     swap = false; 
     for(int count = 0; count < totalMonths - 1; count++) 
     { 
      if(month[count] < month[count + 1]) 
      { 
       temp = month[count]; 
       month[count] = month[count + 1]; 
       month[count + 1] = temp; 
       swap = true; 
      } 
     } 
    } while(swap); 
    cout << "------------------------------------------------------------" << endl; 
    cout << "Here are the months rainfall statistics sorted from highest to lowest: " << endl; 

    for (int index = 0; index < totalMonths; index++) 
     cout << year[index] << "\t " << setw(5) << month[index] << endl; 
} 

这里是我的string year[]定义:

string year[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 
      "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; 
+0

我suppost你的年数组包含诸如:{“jan”,“feb”,“mar”,...}?你可以打印你的年份表定义plz – grifos 2012-03-26 16:32:56

+0

我刚在我的文章中更新了我的年份[]定义。 – Gmenfan83 2012-03-26 16:35:23

+0

这将最大的元素放在顶部;当你交换你应该交换的月份和年份(以及任何其他领域你绑在一起) – Adrian 2012-03-26 17:02:37

回答

1

你被允许重新排列year阵列?在您的排序例程中,在交换month值时,可以交换year阵列中的相应值。

如果你不想改变year数组,你可以添加一个间接级别。在monthyear数组中定义一系列索引并对索引进行排序。

int index[12] = { 0,1,2,3,4,5,6,7,8,9,10,11 }; 

// inside your sort routine... 

if(month[index[count]] < month[index[count + 1]]) 
{ 
    temp = index[count]; 
    index[count] = index[count + 1]; 
    index[count + 1] = temp; 
    swap = true; 
} 

// print the arrays... 

for (int count = 0; count < totalMonths; count++) 
    cout << year[index[count]] << "\t " << setw(5) << month[index[count]] << endl; 
+0

谢谢!这工作完美。感谢大家的时间和帮助,非常感谢! – Gmenfan83 2012-03-26 17:17:59

2

Blastfurnace指出,你有你的排序一年阵列月份相匹配。 或者如果你不能,你可以创建一个小结构来表示你的月份数据。就像这样:

typedef struct _monthData{ 
    double data; 
    int monthIndex; 
} monthData; 

void sortArray(monthData month[], string year[], int totalMonths) 
{ 
    int temp; 
    bool swap; 
    do 
    { 
     swap = false; 
     for(int count = 0; count < totalMonths - 1; count++) 
     { 
      if(month[count].data < month[count + 1].data) 
      { 
       temp = month[count]; 
       month[count] = month[count + 1]; 
       month[count + 1] = temp; 
       swap = true; 
      } 
     } 
    } while(swap); 
    cout << "------------------------------------------------------------" << endl; 
    cout << "Here are the months rainfall statistics sorted from highest to lowest: " << endl; 

    for (int index = 0; index < totalMonths; index++) 
     cout << year[month[index].monthIndex] << "\t " << setw(5) << month[index] << endl; 
} 

问候

0

你唯一需要改变month[count] < month[count + 1])month[count] > month[count + 1])。所以你的完整的代码将给出如下:

#include<iostream> 
#include<string> 
#include<string.h> 
#include<iomanip> 

using namespace std; 

void sortArray(double month[], string year[], int totalMonths) 
{ 
    int temp; 
    bool swap; 
    do 
    { 
     swap = false; 
     for(int count = 0; count < totalMonths - 1; count++) 
     { 
      if(month[count] > month[count + 1]) 
      { 
       temp = month[count]; 
       month[count] = month[count + 1]; 
       month[count + 1] = temp; 
       swap = true; 
      } 
     } 
    } while(swap); 
    cout << "------------------------------------------------------------" << endl; 
    cout << "Here are the months rainfall statistics sorted from highest to lowest: " << endl; 

    for (int index = 0; index < totalMonths; index++) 
     cout << year[index] << "\t " << setw(5) << month[index] << endl; 
} 

int main() 
{ 
    string year[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 
      "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; 
    double month[] = {12,11,10,9,8,7,6,5,4,3,2,1}; 
    sortArray(month,year,12); 
    return 0; 
}