[英]C++ Enum values from string input
到现在已经有一段时间了,不胜感激。
我要基于字符串输入来创建和反对具有枚举值Rank
和Suit
的Card
。 该字符串将分别为两个CLUBS和ACE DIAMONDS的格式为“ 2C,AD”等。
我尝试使用在StackOverflow上其他地方找到的线性搜索功能,该功能在其中,但它只是返回-1,因为它似乎无法获得正确的输入类型。
我已经可以使用char *数组进行另一种转换,但是我一生无法做相反的事情。 参见以下实现:
Card.h
#ifndef _card_h
#define _card_h
#include <string>
#include <iostream>
using namespace std;
enum Rank{ TWO, THREE, FOUR, FIVE, SIX, SEVEN,
EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE};
enum Suit{ CLUBS, DIAMONDS, HEARTS, SPADES};
//Character sets used to convert enums to strings
static const char * RankStrings[] = { "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A",};
static const char * SuitStrings[] = { "C", "D", "H", "S" };
class Card {
public:
///Constructors and destructors
Card();
~Card();
///Accessors
Rank getRank();
Suit getSuit();
string getSuitString();
string toString(); ///No longer used, but left for testing
int linearSearch(const char**, const char*, int);
///Mutators
Card(Rank rank, Suit suit);
Card(string cardStr);
///Operators
bool operator()(Card*, Card*);
friend ostream& operator<<(ostream&, Card&);
private:
Suit suit;
Rank rank;
};
#endif // _random_h
Card.cpp
#include "card.h"
#include <string>
#include <stdlib.h>
#include <cstring>
#include <iostream>
#include <sstream>
using namespace std;
/**CONSTRUCTORS*/
///Noarg constructor that sets default card values
Card::Card(){
suit = CLUBS;
rank = TWO;
}
Card::~Card(){} //Destructor
/**ACCESSORS*/
Rank Card::getRank(void){
return rank;
}
Suit Card::getSuit(void){
return suit;
}
string Card::getSuitString(){
return SuitStrings[suit];
}
/**From tutorial, was used during testing but has been reimplemented
* as an overloaded operator<< below
*/
string Card::toString(){
string cardValues;
cardValues += RankStrings[rank];
cardValues += SuitStrings[suit];
return cardValues;
}
/**MUTATORS*/
///Create a new card with given Rank and Suit
Card::Card(Rank cardRank, Suit cardSuit){
rank = cardRank;
suit = cardSuit;
}
int Card::linearSearch (const char **Array, const char *searchKey, int arraySize) {
for (int i = 0; i < arraySize; ++i) {
if (strcmp(Array[i], searchKey) == 0)
return i;
}
// We didn't find the searchKey in the Array
return -1;
}
///Sets card rank and suit based on string input
Card::Card(string cardStr){
stringstream ss;
string rankStr;
char *c = &cardStr.at(0);
cout << *c << endl; //This prints correct value but doesnt work in method below
int index = linearSearch(RankStrings, c, 13);
cout << index << endl;
}
/**OPERATORS*/
///Functor to compare two cards for their value
bool Card::operator()(Card* card1, Card* card2){
return card1->getRank() > card2->getRank();
}
///Puts a string representation of input card on the output stream
ostream& operator<<(ostream& out, Card& card){
out << RankStrings[card.rank] << SuitStrings[card.suit];
return out;
}
问题是您要在“ 2C”中获取“ 2”的地址,然后像使用完整的C字符串一样使用它。 然后,当您在linearSearch中使用strcmp时,它会尝试匹配整个字符串(“ 2C”)并失败。
解决此问题的一种方法是将SuitStrings
或RankStrings
更改为char
而不是char *
,但这也将涉及更改其他方法。
另一种方法是将要使用的字符复制到一个单独的字符串中,以便strcmp可以工作。 这样的事情应该做到这一点:
char rankStr[2];
rankStr[0] = cardStr[0];
rankStr[1] = '\0'; // NULL terminated.
int index = linearSearch(RankStrings, rankStr, 13);
另一种方法是将linearSearch更改为仅查看两个字符串的第一个字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.