【发布时间】: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!
-
球队的名字是你提前知道的吗?