【问题标题】:Reading string from a file and saving different parts of it in different variables in C++从文件中读取字符串并将其不同部分保存在 C++ 中的不同变量中
【发布时间】:2013-02-12 15:42:11
【问题描述】:

所以我有一个包含一些字符串和数字的文件,它来自西班牙足球联赛:

Malaga 1 Levante 1
Malaga 1 Osasuna 1
Osasuna 1 Deportivo 1
Osasuna 1 Madrid 2
Osasuna 1 Levante 1
Osasuna 1 Malaga 1
#

好的,我要做的就是阅读这篇文章,然后将不同的球队(马拉加、莱万特、奥萨苏纳、拉科鲁尼亚和马德里)保存在 5 个不同的变量中,我还必须将他们的进球保存在每个球队的一个变量中以及他们在每个团队中获得的另一个目标。 这是我的代码:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
const char FI='#';
const int MAX_EQUIPS=20;


struct Equip {
   string nomEquip;
   int golsf;
   int golsc;
   int punts;
};

typedef Equip TaulaEquip[MAX_EQUIPS];

struct EquipLliga {
    TaulaEquip t;
    int n;
};
int cercaEquip(EquipLliga l, string equip) {
// Pre: --
// Post: si equip no hi es a d.t, retorna -1
//       altrament retorna posicio de l'equip de nom equip a d.t
   int ret=l.n-1;
   bool trobat= false;
   while (ret>=0 and not trobat) {
      if (l.t[ret].nomEquip.compare(equip)==0) trobat= true;
      else ret--;
   }
   return ret;
}
void llegir(ifstream & f) {

    string string1;
    f.open("Lliga.txt");
    char output;
    if (f.is_open()) {
        while (!f.eof()) {
            getline(f,string1);
            cout << string1 << endl;
        }
    }
    f.close();
}
void actualitzacioGols(ifstream & f, EquipLliga & e) {
// Pre: f obert
// Post: ha llegit totes les dades de f, incorporat altes i traspasos a al, i els
//       ingresos i despeses dels equips per altes, baixes i traspasos a d
    char tipus;

string equipA, equipB;
int golsf=0, golsc=0, cerca;
e.n=0;
f >> tipus;

while (tipus!=FI) {  // per cada equip
    cerca=cercaEquip(e,equipA);
    if (cerca=-1)
    {
        e[n].e.nomEquip=equipA;
        e[n].e.golsf=l[n].e.golsf+golsA;
        e[n].e.golsf=l[n].e.golsf+golsB;
    }
    else
    {
        e[cerca].e.golsf=l[cerca].e.golsf+golsA;
        e[cerca].e.golsc=l[cerca].e.golsc+golsB;
    }
    lliga.n++;
    cerca=cercaEquip(e,equipB);
    if (cerca=-1)
    {
        e[n].e.nomEquip=equipB;
        e[n].e.golsf=l[n].e.golsf+golsA;
        e[n].e.golsf=l[n].e.golsf+golsB;
    }
    else
    {
        e[cerca].e.golsf=l[cerca].e.golsf+golsA;
        e[cerca].e.golsc=l[cerca].e.golsc+golsB;
    }

}
int main() {


    }

我遇到了函数“void actualitzacioGols(ifstream & f, EquipLliga & e)”的问题。我不知道如何对其进行编码,以便读取到第一个空格,然后将其保存到第一个团队变量“equipA”,然后将第一个数字保存到第一个目标变量“golsf”,与其他两个。

有解决此问题的想法或有用的提示吗? 我对 C++ 有点陌生。

【问题讨论】:

  • 如果是 C++,请不要将其标记为 C。C 程序员可能无法帮助您使用 C++。
  • 哎呀,我以为在 C 语言中也差不多,抱歉;p!
  • 球队的名字是你提前知道的吗?

标签: c++ string file


【解决方案1】:

我建议你看看这篇解释how to split a string using a delim的帖子

在您的情况下,您可能希望使用空格来拆分字符串。由于您具有固定格式,因此您可以通过使用硬编码索引将向量的元素作为数组访问来检索您想要的信息(团队和目标),前提是您确定您的文件将始终具有相同的格式。

【讨论】:

    【解决方案2】:

    我会使用FindSubstr

    您可以找到第一个空格,然后获取从 0 到该空格的子字符串。然后得到空格后+1的数字,然后从字符串的其余部分中创建一个新字符串,并对另一个名称执行相同的操作。

    【讨论】:

      【解决方案3】:

      这是您问题的完整解决方案。希望对您有所帮助!

      #include <iostream>
      #include <iomanip>
      #include <string>
      #include <map>
      #include <algorithm>
      
      using namespace std;
      #define WIDTH 10
      
      int main()
      {
          string team1, team2; 
          int   team1goals, team2goals;
      
           // Data structure for storing Team name and scores
           map<string, pair<int, int>> scores;
      
          while (cin >> team1 >> team1goals >> team2 >> team2goals ) {
                scores[team1].first  +=   team1goals; 
                scores[team2].second  +=  team1goals;
      
                scores[team2].first += team2goals;
                scores[team1].second += team2goals;
          }
      
          cout << endl << setw (WIDTH) << "Team Name"  << setw(WIDTH) << "given" << setw(WIDTH) << "received";
          for_each( begin(scores), end(scores), [&]( pair<string, pair<int,int>> i ) {
              cout << endl << setw(WIDTH) << i.first  << setw(WIDTH) << i.second.first << setw(WIDTH) << i.second.first;
          });
      
          return 0;
      }
      

      结果如下

      /*
      input file: inputFile.txt
      -------------------------------
      Malaga 1 Levante 1
      Malaga 1 Osasuna 1
      Osasuna 1 Deportivo 1
      Osasuna 1 Madrid 2
      Osasuna 1 Levante 1
      Osasuna 1 Malaga 1
      -------------------------------
      
      runs as:
       yourexename < inputFile.txt
      -------------------------------
      output:
      
       Team Name     given  received
       Deportivo         1         1
         Levante         2         2
          Madrid         2         2
          Malaga         3         3
         Osasuna         5         5
      
      */
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-09
        • 2020-08-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多