【问题标题】:Sorting a sentence using arrays and strings使用数组和字符串对句子进行排序
【发布时间】:2016-04-22 14:10:19
【问题描述】:

抱歉,伙计们预先警告我编码很烂,但有一个大项目需要帮助!

输入:一个完整​​的句子。

输出:句子的排序顺序(ASCii Chart Order)(忽略大小写。)

输出以下类别的直方图:
1) 元音
2) 辅音
3) 标点符号
4) 大写字母
5) 小写字母

我什至不知道该怎么办

【问题讨论】:

  • Stack Overflow 并不是一个真正开始提问的地方。我们一般解决具体的编程问题。
  • 您能否向我们提供一个示例,说明您到目前为止所获得的代码以及您遇到的任何错误,请注意,这是一个学习的地方。如果您想要另一个人,那就不要来了为您编写代码
  • 一如既往地研究一下这个项目是否已经存在。还要搜索是否已经编写了项目的某些部分。对于学生作业和家庭作业,请搜索现有示例。
  • 用您遇到问题的特定项目更新您的帖子。例如:你知道元音是什么吗?你知道如何输入一个句子吗?你知道std::string 是什么吗?你知道什么是直方图吗?你知道怎么排序吗?您可以使用库中的排序功能吗?

标签: c++ arrays string histogram acsl


【解决方案1】:

由于您对自己的问题含糊不清,我建议您采用以下流程:

审核要求

始终查看要求(作业)。如果有您不理解或与您的客户(讲师)有相同理解的项目,请与您的客户讨论。

编写一个简单的main 程序。

写一个简单的main 或“Hello World!”程序来验证您的 IDE 和其他工具。在继续之前让它工作。保持简单。

这是一个例子:

#include <iostream>
#include <cstdlib>  // Maybe necessary for EXIT_SUCCESS.

int main()
{
   std::cout << "Hello World!\n";
   return EXIT_SUCCESS;
}

更新程序以输入文本和验证。

添加代码以执行输入、验证输入并回显到控制台。

#include <iostream>
#include <cstdlib>  // Maybe necessary for EXIT_SUCCESS.
#include <string>

int main()
{
   std::string sentence;
   do
   {
      std::cout << "Enter a sentence: ";
      std::getline(cin, sentence);
      if (sentence.empty())
      {
          std::cout << "\nEmpty sentence, try again.\n\n"
      }
   } while (sentence.empty());
   std::cout << "\nYou entered: " << sentence << "\n";

   // Keep the console window open until Enter key is pressed.
   std::cout << "\n\nPaused. Press Enter to finish.\n";
   std::cin.ignore(100000, '\n');

   return EXIT_SUCCESS;
}

添加功能,一次一项并进行测试。

为一个简单的需求添加代码,编译和测试。
运行后,进行备份。
重复,直到所有要求都得到实施。

【讨论】:

    【解决方案2】:

    为了对字符串进行排序,您可以使用标准的 c qsort 函数。要计算元音、辅音、标点符号……您需要一个简单的 for 循环。

    这是一个工作示例:

    #include <iostream.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int cmp(const void* pc1, const void* pc2)
    {
        if(*(char*)pc1 < *(char*)pc2) return -1;
        if(*(char*)pc1 > *(char*)pc2) return  1;
        return 0;
    }
    
    void main(int argc, char* argv[])
    {
        char pczInput[2000] = "A complete sentence.";
    
        cout << endl << "Input: '" << pczInput << "'";
    
        qsort(pczInput, strlen(pczInput), sizeof(char), cmp);
    
        cout << endl << "Result: '" << pczInput << "'";
    
        int iCapital     = 0;
        int iLowerCase   = 0;
        int iPunctuation = 0;
        int iVowels      = 0;
        int iConsonants  = 0;
    
        for(unsigned int ui = 0; ui < strlen(pczInput); ++ui)
        {
            if(isupper(pczInput[ui])) ++iCapital;
            if(islower(pczInput[ui])) ++iLowerCase;
            if(ispunct(pczInput[ui])) ++iPunctuation;
            if(strchr("aeiouAEIOU", pczInput[ui]) != NULL) ++iVowels;
            if(strchr("bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ", pczInput[ui]) != NULL) ++iConsonants;
        }
    
        cout << endl << "Capital chars: "     << iCapital;
        cout << endl << "Lower case chars: "  << iLowerCase;
        cout << endl << "Punctuation chars: " << iPunctuation;
        cout << endl << "Vowels chars: "      << iVowels;
        cout << endl << "Consonants chars: "  << iConsonants;
        cout << endl;
    }
    

    请注意,我使用 C 标准函数来计算大写、小写和标点符号,而我必须使用 strchr 函数来计算元音和辅音,因为标准 C 库中缺少这些函数。

    程序的输出是:

    Input: 'A complete sentence.'
    Result: '  .Acceeeeelmnnopstt'
    Capital chars: 1
    Lower case chars: 16
    Punctuation chars: 1
    Vowels chars: 7
    Consonants chars: 10
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-14
      • 1970-01-01
      • 2015-07-27
      • 1970-01-01
      • 2015-01-16
      • 1970-01-01
      • 1970-01-01
      • 2016-07-18
      相关资源
      最近更新 更多