程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c++ 显示给文件中得分最高的用户大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决c++ 显示给文件中得分最高的用户?

开发过程中遇到c++ 显示给文件中得分最高的用户的问题如何解决?下面主要结合日常开发的经验,给出你关于c++ 显示给文件中得分最高的用户的解决方法建议,希望对你解决c++ 显示给文件中得分最高的用户有所启发或帮助;

我有一个名为“scoreS.TXT”的文件,其中包含:

Player      - score
--------------------
John Miles  - 132
Henry       - 90
JulIEt P    - 110

程序必须向用户显示人名和相应的分数,例如:

John Miles has a score of 132
Henry has a score of 90
JulIEt P has a score of 110

我有以下代码,但无法正常工作。变量昵称只获取第一个名字,如果我添加一个字符串变量来获取第二个名字,程序将无法在只有一个名字的行中运行。

#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
    string filename = "scoreS.TXT",line,nickname;
    char c;
    unsigned int score;
    unsigned short int i = 0;
    ifstream file(filename);
    while (getline(file,line)) { 
        if (i < 2) {              //
            i++;                  // ignoring the header
        }                         //
        else {
            stringstream s(line);
            s >> nickname >> c >> score;
            cout << nickname  << " has a score of " << score;
        }
    }
    file.close();
    return 0;
}

解决方法

扫描复杂输入行的一种解决方案是使用 std::regex 这非常灵活,但可能效率不高。

一些提示:

您不应该使用 using namespace std;

file.close() 不是明确需要的,因为在 main 的范围结束时,ifstream 超出范围并将自动销毁,这也会关闭文件,但这不是失败之前手动关闭它。

请参阅以下示例,了解如何使用 regex 扫描字符串中的模式以及如何获取搜索结果:

很清楚:这只是一千种可能的解决方案之一!如前所述,它非常灵活但效率较低。

#include <string>
#include <iostream>
#include <fstream>
#include <regex>

int main()
{
    std::string fileName = "SCORES.TXT";
    std::string line;
    // we search the input line for the following regular expression:
    // first everything which is a charater -> a-z or A-Z and a space and as mutch as we find *
    // after that we search for - and also the following whitspaces
    // next we pick the number
    // the () arround the expression forwards the result of each () in a separate result m[]
    std::regex re("([a-zA-Z ]*)[- ]*([0-9]*)",std::regex::extended );

    std::ifstream file(fileName);
    while (std::getline(file,line)) 
    {
        try  // if regex did not find correct patterns,we ignore that
        {
            std::smatch m;
            std::regex_search( line,m,re );

            // we expect 3 parts as result,first is the full string,second is our nickname,last is the number
            if ( m.size() == 3 ) 
            {
                // as we have also the trailing whitespaces in the nickname part,we remove them here
                std::string nickname = std::regex_replace(std::string(m[1]),std::regex(" +$"),"");
                // create an int from found number string
                int number = std::stoi(m[2]);

                std::cout << nickname << " has a score of " << number << std::endl;
            }
        } catch(...){}
    }   

    return 0;
}

您可以测试在线正则表达式,这可能有助于熟悉复杂的语法。有很多 sites 可以帮助您!请注意,某些正则表达式在某些语言中略有不同或不可用。

大佬总结

以上是大佬教程为你收集整理的c++ 显示给文件中得分最高的用户全部内容,希望文章能够帮你解决c++ 显示给文件中得分最高的用户所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: