005
29.06.2007, 17:08 Uhr
Karldin Shinowa
Professional Noob
|
C++: |
#include<string> #include<iostream> #include<fstream> #include<vector> #include<list> using namespace std;
class Item { protected: string name; int value; int weight; public: Item(string name,int value,int weight) :name(name),value(value),weight(weight) {}
virtual string getType()const =0; };
class Weapon:public Item { public: Weapon(string name,int value,int weight) :Item(name,value,weight) {} string getType() const { return "Weapon"; } };
class Food:public Item { public: Food(string name,int value,int weight) :Item(name,value,weight) {} string getType() const { return "Food"; } };
class Player { string playername; int maxWeight; int curWeight;
list<Item*> items;
public: Player(string playername,int maxWeight,int curWeight) :playername(playername),maxWeight(maxWeight),curWeight(curWeight) {}
void addFood(string name,int value, int weight) { items.push_back(new Food(name,value,weight)); }
void addWeapon(string name,int value,int weight) { items.push_back(new Weapon(name,value,weight)); } }; //////////////////////////////////////
class ReadConfigs { private: fstream file; vector<Player*>players; string buffer;
public: void newPlayer(string name) { int curWeight,maxWeight; getline(file,buffer); curWeight=atoi(buffer.c_str()); getline(file,buffer); maxWeight=atoi(buffer.c_str());
players.push_back(new Player(name,maxWeight,curWeight)); while(true) { if(file.eof()) break; getline(file,buffer); if(buffer.find(':',0)) newPlayer(buffer.substr(1,buffer.length()-1)); if(buffer.find('!',0))continue; if(buffer.find('#',0)) newFood(buffer.substr(1,buffer.length()-1)); if(buffer.find('*',0)) newWeapon(buffer.substr(1,buffer.length()-1)); } return; }
void newFood(string name) { int value,weight;
getline(file,buffer); value= atoi(buffer.c_str());
getline(file,buffer); weight=atoi(buffer.c_str());
players.back()->addFood(name,value,weight); return; } void newWeapon(string name) { int value,weight;
getline(file,buffer); value= atoi(buffer.c_str());
getline(file,buffer); weight=atoi(buffer.c_str());
players.back()->addWeapon(name,value,weight); return; } public: vector<Player*> read(string filename) { file.open("config.txt",ios::in); getline(file,buffer); newPlayer(buffer.substr(1,buffer.length()-1)); return players; } };
int main() { ReadConfigs* reader=new ReadConfigs; vector<Player*>players=reader->read("configs.txt"); delete reader; }
|
Wäre mein Vorschlag das ganze zu regeln. Syntaxfehler sin schon raus genommen :-) -- Ich will die Welt verbessern, doch Gott gibt mir nicht den Code. Dieser Post wurde am 29.06.2007 um 17:09 Uhr von Karldin Shinowa editiert. |