2013-02-12 65 views
0

所以我有这个文件,它有一些字符串和数字,它从西班牙足球甲级联赛是:从文件读取字符串,并在C++不同的变量保存的不同部分

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() { 


    } 

我在使用功能的问题 '无效actualitzacioGols(ifstream的&楼EquipLliga & E)'。我不知道如何对它进行编码,以便它读取到第一个空格,然后将其保存到第一个团队变量'equipA',然后将第一个数字保存到第一个目标变量'golsf'中,并与另外两个。

任何想法或有用的提示来解决这个问题? 我对C++很陌生。

+1

如果是C++,不要将其标记为C. C程序员可能不会能够帮助您使用C++。 – netcoder 2013-02-12 15:44:37

+0

哎呀我以为它会在C中一样,对不起; p! – 2013-02-12 15:53:22

+0

是您提前知道的球队名称? – 2013-06-04 02:57:32

回答

1

我建议你看看这篇文章解释how to split a string using a delim

在你的情况,你可能想使用一个空间来分割你的字符串。既然你有一个固定的格式,你可以通过访问矢量元素作为一个数组,使用硬编码索引来检索你想要的信息(团队和目标),如果你确定你的文件总是具有相同的格式。

0

我会使用FindSubstr

你可以找到的第一个空间,然后从0获得子到那个空间。然后获得空格后+1的数字,然后从其余字符串中创建一个新字符串,并使用其他名称执行相同的操作。

0

这是您的问题的完整解决方案。希望能帮助到你!

#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 

*/ 
相关问题