【问题标题】:Reading from a file and sorting alphabetically从文件中读取并按字母顺序排序
【发布时间】:2018-03-07 04:55:54
【问题描述】:

我正在学习基础 C++ 大学课程,但我完全被我的一项作业困住了。

我需要从包含 (1-25) 个名称列表的文件中读取输入,按字母顺序对名称进行排序,然后输出哪些人将位于行的前面(例如:Amy)和后面行的(例如:Zora)。我的教授很讲究,他严格禁止我们使用我们在课堂上没有学到的任何东西。我们只学过cin、cout、if语句、循环、基本操作符、fstream和基本字符串。

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

int main()
{

    //Intialize variables
    string studentName;
    string firstEntry;
    string secondEntry;
    string first;
    string last;
    ifstream inputFile;
    string filename;
    int students;

    //Ask for amount of students
    cout << "Please enter the number of students in the class.\n(The number must be a whole number between 1 and 25.)\n";
    cin >> students;

    //Input validation
    while (students < 1 || students > 25)
    {
        cout << "\nInvalid value. Please enter a value between 1 and 25.\n";
        cin >> students;
    }

    //Get file name from user
    cout << "Please enter the name of the file with the list of students\n";
    cin >> filename;

    //Open the file
    inputFile.open(filename);
    if (inputFile)
    {
        while (inputFile >> studentName)
        {
            cin >> studentName;
            studentName = firstEntry;
            cin >> studentName;
            studentName = secondEntry;

            if (firstEntry < secondEntry)
            {
                firstEntry = first;
                secondEntry = last;
            }
        }
        cout << first << " is the first student in line.";
        cout << last << " is the last student in line.";
    }

    else
    {
    cout << "Error opening the file.\nPlease restart the program and try again.";
    return 1;
    }
    inputFile.close();
    return 0;
}

这也是我正在阅读的文件:

杰基

山姆

汤姆

比尔

玛丽

保罗

泽夫

倒钩

我主要停留在从文件读取和解释数据部分。

【问题讨论】:

  • 那么我们应该如何知道您在课堂上学到了什么,即允许或不允许什么?您最好列出到目前为止您上过的每节课,这样我们就不会给您提供您尚未学习的建议。
  • 如果不知道您所学的主题,将很难为您提供建议。既然是作业,那就用你的技能解决手头的问题吧。有很多方法可以解决问题..但如果你自己做。那么这将有助于您了解基本的编程结构。
  • 如果只需要第一个和最后一个,就不用担心对整个列表进行排序。类似的问题是返回整数列表中的最小值和最大值。

标签: c++ sorting alphabetical


【解决方案1】:

几个建议:

  1. 您应该仔细考虑变量类型和用法。由于您选择使用std::string,您应该从documentation 熟悉它的基础知识。

  2. 周到的变量命名可以帮助您避免一些明显的编程错误。例如,您使用

    cin >> studentName;
    

    显然应该是字符串 studentName1 而不是 int。

  3. 规划变量的使用。你真的需要所有的整数吗?您是按字母顺序排列名称本身,而不是数字。

  4. 谨慎使用作业。

    studentName = firstEntry;
    

    表示您正在分配 firstEntry 的值,即 0 到 studentName,有效地替换它之前打算包含的任何内容。

  5. 对于实际排序,您可以选择在可用的std::string 函数之间进行选择,或者如果在您的类约束中禁止这样做,则可以选择经典字符串迭代。在后者的情况下,最大的优势是字符串可以作为字符数组进行迭代。然后排序简单地简化为字符比较。

  6. 最后一个提示,从您的尝试来看,您可能已经想到了。由于只需要输出队列中的名字和姓氏,因此不需要对整个列表进行排序,只需维护名字和姓氏即可。

祝你好运!

【讨论】:

  • 我编辑了代码以反映您的建议,但是,只要到达读取文件的位置,代码就会停止。我确定该文件位于正确的位置,因为我已经使用 cout 对其进行了测试。此外,我们只允许使用基本字符串函数。我们在您发布的链接中没有学到任何东西。
【解决方案2】:

如果您只需要 max 和 mi,只需比较字符串并存储在 2 个单独的变量中

最初设置名字为 min 然后遍历整个文件

与初始名称比较

如果是 less 则替换 else 存储在 max 中

如果更大替换,则将 max 与传入文本进行比较

如果我错了请纠正我

【讨论】:

    【解决方案3】:

    请你的教授教你std::setstd::getline,然后你就可以让C++的力量为你工作。

    #include <iostream>
    #include <fstream>
    #include <set>
    
    int main()
    {
        std::ifstream input("names.txt"); ///< open input file
        if (!input.is_open())
        {
            std::cout << "Error opening file!" << std::endl;
            return 1;
        }
    
        std::set<std::string> sortedNames; ///< set is sorted by default!
        std::string name;
        while (std::getline(input, name)) ///< get every name in file line by line
        {
            sortedNames.insert(name); ///< insert name into sorted set
        }
    
        for (const auto& n : sortedNames) ///< print sorted names
        {
            std::cout << n << std::endl;
        }
        return 0;
    }
    

    编辑:如果您对完整排序的名称集不感兴趣并且只关心第一个和最后一个条目,则可以简单地为集合中的这些元素设置迭代器,然后取消引用它们以获取它们的值。

     auto iter = sortedNames.begin();   ///< iterator at first element
     auto riter = sortedNames.rbegin(); ///< iterator at last element
     std::cout << "The first student is: " << *iter << std::endl;
     std::cout << "The last student is: " << *riter << std::endl;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-11
      • 1970-01-01
      • 2019-06-12
      • 1970-01-01
      • 2015-03-20
      • 1970-01-01
      相关资源
      最近更新 更多