读取文件并根据该信息创建对象向量
Reading a file and making a vector of objects based on that info
我有 2 个扑克牌值枚举,它们的等级和花色。我想获取一个包含如下数据的文件:
2C 6D 7小时 10 秒 JC
此信息确定扑克牌。
6c是俱乐部的6个。
JC是俱乐部的杰克,等等。(大小写无关紧要)。
如何获取输入的字符并使用它们制作新的扑克牌?为每个可能的字符串都有一个大小写似乎有很多代码行。可以以某种方式简化吗?
#include "header1card.h"
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
int main() {
Card card;
}
class Card {
private:
enum struct ranks {two, three, four, five, six, seven, eight,
nine, ten, jack, queen, king, ace };
enum struct suits {diamonds, clubs, hearts, spades };
suits su; //do what with these two?
ranks ra;
};
int fileparser(vector<Card> & readcards, char * filename) {
ifstream filereading;
filereading.open(filename, ios::in);
if (filereading.is_open()) {
//while read the file here. save what is read to a string.
char temp;
while (!filereading.eof()) {
//how make a card based off of the inputtedr chars?
char readchar = filereading.get(temp );
switch (readchar) {
case '2' :
break;
case '3' :
break;
//and so on to make every card? Seems like a lot of lines of code.
}
readcards.pop_back( /*After making the card, put it in vector*/ );
}
filereading.close();
}
else {
//throw error, cannot find file.
cout << "Could not find file " + filename << endl; //error on ' + filename'. Says Expression must have integral or unscoped enum type.
cerr << "COuld not find file " << endl;
}
}
首先,我建议重写输入代码,使其如下所示:
Card temp;
while (filereading >> temp) {
readcards.push_back(temp); // note: push_back, not pop_back
}
请参阅此问题以了解为什么不检查eof()
。
因此,我们只需要为我们的类编写一个operator>>
:
class Card {
public:
friend istream& operator>>(istream& is, Card& c) {
std::string name;
if (is >> name) {
if (name.size() == 2) {
// rank is name[0], suit is name[1]
// go process
}
else {
// set fail state
is.setstate(std::ios::failbit);
}
}
return is;
}
};
那么我们如何获得等级和花色呢?这是你问题的主要部分。对于西装来说,最简单的就是一个开关:
switch (toupper(name[1])) {
case 'D':
c.suit = Card::diamonds;
break;
case 'S':
// etc.
default:
// set fail state, maybe log some error too
is.setstate(std::ios::failbit);
}
对于排名,我们可以通过以下方式简化2, 3, ..., 9
部分:
if (name[0] >= '2' && name[0] <= '9') {
int idx = name[0] - '2'; // now it's an int, with value 0 through 7
c.rank = Card::two + idx; // because enums are ordered
}
else {
// fallback to switch
switch (toupper(name[0])) {
case 'T':
c.rank = Card::ten;
break;
case 'J':
// etc
default:
// set fail state
is.setstate(std::ios::failbit);
}
相关文章:
- 使用基类指针创建对象时,缺少派生类析构函数
- 如何创建对象函数指针C++映射?
- C++创建对象数组
- 在 C++ 的 Switch Case 中创建对象后对对象调用方法
- 如何获取在 main() 函数中构造的类的创建对象?
- 基于文件中的条目创建对象
- 错误:创建对象后无法分配区域
- C++ 通过输入创建对象
- 堆还是堆栈用于创建对象?
- 使用 C++ 创建对象数组
- 使用unique_ptr创建对象
- C++递归地在类构造函数中创建对象
- 通过向构造函数其他对象引用页面来创建对象
- ReactiveX (rx) - 在对象上应用可观察对象,而不是在可观察对象中创建对象
- 如何在OSX上正确创建C++对象文件(.o)
- 编译器是否会创建vtable,而不考虑在c++中创建对象
- 创建对象并防止被破坏
- 在C++中,友元类可以从友元类创建对象吗
- 读取文件并根据该信息创建对象向量
- (c++)读取CSV文件并使用信息创建对象(这将成为一个链表)