0

第一次在这里发帖,有点像 C++ 的新手。让我难过的是我编写了这个程序来从 .txt 文件中获取信息并对其进行排序。这部分代码有效,我现在需要弄清楚如何打印 StarsArray 的星号,而不是文件中的实际数字。任何帮助表示赞赏!

#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>

using namespace std;

// Function prototypes
int buildCandyArrays(int[], int[], int[]);
void printCandyArrays(int[], int[], int[], int); 
void sortCandyArrays(int [], int [], int[], int );
void printCandyArrays( int numstars);
int main()
 {

 const int ArSize=500;
   int LevelArray[ArSize];
   int ScoreArray[ArSize];
   int StarsArray[ArSize];
   int numgames;
   int numstars;

   numgames = buildCandyArrays(LevelArray, ScoreArray, StarsArray);
   cout<<"Unsorted Array"<<endl;

   printCandyArrays(LevelArray, ScoreArray, StarsArray, numgames);
   sortCandyArrays(LevelArray, ScoreArray, StarsArray, numgames);
    cout<<endl<<endl<<"Sorted Array"<<endl;
   printCandyArrays(LevelArray, ScoreArray, StarsArray, numgames);
   return 0;
 }

int buildCandyArrays(int LevelArray[], int ScoreArray[], int StarsArray[])
 {               
   ifstream inFile;. 
   int Level, Score, Stars;

   int count = 0;

  inFile.open("candycrush.txt");
   if (!inFile)
     {
      cout << "Error - unable to open input file\n";
      exit(1);
     }

   inFile >> Level;
   while (inFile&&count<10)
    {
      inFile >> Score;
      inFile >> Stars;

     LevelArray[count] = Level;
     ScoreArray[count] = Score;
     StarsArray[count] = Stars;

     count++;      

     inFile >> Level;
    }

   inFile.close();

   return count;
  }

void printCandyArrays(int LevelArray[], int ScoreArray[], int StarsArray[], int numgames)
 {
 int i;
 cout << fixed << setprecision(2);

 for (i = 0; i < numgames; i++)
   {
    cout << LevelArray[i] << "  --  " << ScoreArray[i] << "  --  "<<StarsArray[i]<<endl;
   }
 }
void sortCandyArrays(int LevelArray[], int ScoreArray[], int StarsArray[], int numgames)
 {
  int i, j, min, temp;
  int stemp;

  for (i = 0; i < numgames - 1; i++)
  {
  min = i;

  for (j = i+1; j < numgames; j++)
     {
     if (ScoreArray[j]>ScoreArray[min])
        min = j;
     }

  stemp = LevelArray[i];
  LevelArray[i] = LevelArray[min];
  LevelArray[min] = stemp;

  stemp = ScoreArray[i];
  ScoreArray[i] = ScoreArray[min];
  ScoreArray[min] = stemp;

  temp = StarsArray[i];
  StarsArray[i] = StarsArray[min];
  StarsArray[min] = temp;
  }
} 
4

0 回答 0