从文件中读取并按字母顺序排序

时间:2018-03-07 04:55:55

标签: c++ sorting alphabetical

我参加了一个基础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;
}

此处还有我正在阅读的文件:

萨姆

汤姆

比尔

玛丽

泽夫

倒钩

我主要坚持从文件读取并解释数据部分。

3 个答案:

答案 0 :(得分:1)

几点建议:

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

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

    cin >> studentName;
    

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

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

  4. 仔细使用作业。

    studentName = firstEntry;
    

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

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

  6. 最后提示,根据您的尝试判断,您可能已经考虑过了。由于您只需输出队列中的名字和姓氏,因此无需对整个列表进行排序,只需维护名字和姓氏。

  7. 祝你好运!

答案 1 :(得分:1)

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

首先将名字设为min,然后遍历整个文件

与初始名称进行比较

如果它更少替换max

中的存储

如果更换

,则将max与传入文本进行比较

如果我错了,请纠正我

答案 2 :(得分:0)

请教导你教授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;