【问题标题】:Sorting Arrays/File I/O C++排序数组/文件 I/O C++
【发布时间】:2012-11-05 19:16:05
【问题描述】:

我想知道是否有人可以帮助我对这个数组进行排序,我对如何在这个项目中准确地实现它有点迷茫。因为它是硬件,所以不要透露整个答案,而是将我推向正确的方向。项目如下: 编写一个程序,读取一行文本并输出文本中出现的所有字母的列表以及每个字母在该行中出现的次数。以用作标记值的句点结束该行。字母应按以下顺序使用:从最高到最低。假设输入使用所有小写字母。 几个问题。 1.我在对数组进行排序时是否正确? 2.在将排序数组放入我的代码之前,当代码编译时,它会出现一个空白屏幕。有什么办法可以解决这个问题?

如果写得不好,请见谅,提前感谢您的帮助!

inlcude <iostream>
#inlcude <fstream>
using namespace std;
void initialize(int list[]);
void Sort(int list[],int& num);
void characterCount(char ch, int list[]);
void readText(ifstream& intext, char& ch, int list[]);
void totalCount(int list[]);
int main()
{
int index,letterCount[26];
char ch;
ifstream inFile;

infile.open("C:/temp/Data_Chapter_7_8.txt");

if (!inFile)
{
   cout << " Cannot open file." <<endl;
}
initialize(letterCount);
infile.get(ch);

while (inFile)
{
  int index;
  readText(inFile,ch,letterCount)
  index++;
  inFile.get(ch);
  }
  totalCount(index, letterCount);

  inFile.close();

  system("PAUSE");
  return 0;
  }
  //initializes array letterCount to 0
  void initialize(int list[])
  {
 for(int x = 0;x<26;x++)
 list[x] = 0
 }
 //increments the letter count. Makes sure counting letters.
 void characterCount (char ch, int list[])
 {
 int index;
 ch = tolower(ch);
 if(static_cast<int>(ch)>=97&&(static_cast<int>(ch)<=122))
  letterCount[static_cast<int>(ch)-97]++;  
  }
  void readText(ifstream& intext, char& ch, int list[])
  { 
  while (ch != '.')
  {
  characterCount (ch,list);
  intext.get(ch);
  }
  }
  //displays data
  void totalCount(int list[])
  {
 for(int x=0;x<26;x++)
 if(letterCount[x]>0)  
 cout<<static_cast<char>(x+97)<<" "<<letterCount[x]<<endl;
 }
 void Sort(int list[],int& num)
      {
 int i,j,flag = 1;
 int temp;
 int numLength = num.length();
 for (i=1;(i<=numLength)&&flag; i++)
 {
     flag = 0;
     for (j=o; j<(numLength-1);j++0
     {
         if(num[j+1]>num[j])
         {
             temp = num[j];
             num[j] = num[j+1];
             num[j+1]=temp;
             flag = 1;
         }
     }
 }
             return;
             }               

【问题讨论】:

  • 人们没有给出完整、高质量的答案是家庭作业标签现在被弃用的部分原因。把它扔出去。
  • 给我们一个你的文件的例子,这样我们就有了一些事情要做。
  • 文件是 Just is Oliver 是一只金毛猎犬,它的毛又长又金。
  • (a) inFile 未定义。 (b) 在函数characterCount() 中,变量letterCount 未定义,(c) 在Sort() 函数o 中未定义(for 循环的初始化程序),(d) 在`main() 中调用totalCount()与它的原型不匹配。摘要:当它甚至不编译时,你怎么知道它不起作用?

标签: c++ arrays io


【解决方案1】:

我们可以简单地跟踪每个字母出现的次数,而不是使用混乱的冒泡排序和其他有趣的东西,因为只有 26 种可能性。这应该会导致代码更简洁(并且更快):

int numOccur[26];
...
for (int i = 0; i < numCh; i ++)
    numOccur[letters[i] - 'a'] ++;
for (i = 25; i >= 0; i --)
    if (i > 0)
        cout<<static_cast<char>(i+97)<<" "<<numOccur[i]<<endl;

当然,你应该用适当的文件读取循环替换 for 循环。

【讨论】:

  • 'a' - 'a' != 0'z' - 'a' &gt;= 26 的任何系统?
  • @jma127:是的——在 IBM 大型机上(使用 EBCDIC)'z'-'a' 将大于 26(如果内存服务,它大约是 32,但不要引用我的话)。
  • @jma127 IBM AS/400、IBM OS/390 等等(请参阅 this link 并进行计算。)
  • 感谢您指出这一点;以前从来不知道。不过似乎有点晦涩。
  • @jma127: 不——while (!infile.eof()) 本质上总是一个错误(就像他现​​在的while (infile) 一样糟糕)。请参阅我的答案以获取正确的形式。
【解决方案2】:

只有几厘米。

  1. 我认为这里的大部分选角都没有任何充分的理由。
  2. 使用isalphaisupperislower 来检查字母,并使用tolowertoupper 来识别它们(注意:使用这些函数是进行强制转换的少数理由之一)。
  3. 使用 `int lettercount[26] ={0}; 初始化 lettercount 数组可能最简单;
  4. 除非您绝对需要编写自己的排序例程,否则请使用 std::sort
  5. 在定义 ifstream 时通常最容易打开文件: std::ifstream infile("whatever");
  6. 不要使用while (infile)。这几乎是一个有保证的错误。读一读,检查是否成功,例如:while (infile.get(ch)) ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-10
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多