把信变成文字

Letter into words

本文关键字:文字      更新时间:2023-10-16

我将放置我的类来显示我需要使用的其他东西。如果我做打字,它会起作用吗?或者我只需要学习弦乐?

class NumberBox
{
private:
    int number;
    char letter;
public:
    NumberBox *next_ptr;
    void setNumber(int number)
    {
        this->number = number;
    }
    void setLetter(char letter)
    {
        this->letter = letter;
    }

    int getNumber()
    {
        return number;
    }
    int getLetter()
    {
        return letter;
    }
};
int main()
    cout << "Please Give the faction for the first Card" << endl;
    cin >> faction[0];
    if(faction [0] == 's' || faction [0] == 'c' || faction [0] == 'h' || faction [0] == 'd')
    {
        if(faction[0] == 's')
        {
            faction[0] = "spade";
            factionHead_ptr->setLetter("spade");
        }
    }

你是怎么做到的?就像如果用户输入's',那么它将是Spade。

您可以为此使用std::map。您将构建一个从charstd::string的映射,其中包含一个值集schd

如何使用它的一个小例子是:

#include <iostream>
#include <map>
typedef std::map<char, std::string> suitmap;
int main()
{
    suitmap suits;
    suits['s'] = "spades";
    suits['h'] = "hearts";
    char in;
    std::cout << "Suit?" << std::endl;
    if (std::cin >> in) {
        suitmap::const_iterator it = suits.find(in);
        if (it != suits.end())
            std::cout << it->second << std::endl;
        else
            std::cout << "not found" << std::endl;
    } else {
        std::cout << "no input" << std::endl;
    }
    return 0;
}

根据您的问题进行调整,假设factionchars:的数组

cin >> faction[0];
// see if we have a match for faction[0] in the map
suitmap::const_iterator it = suits.find(faction[0]);
if (it == suits.end()) {
   // no match
   // print an error or something
} else {
   // match! the string is in it->second
   std::string suit = it->second;
   factionHead_ptr->setLetter(suit);
}

现在,这对NumberBox类根本不起作用,因为setLetter函数需要一个char,而letter成员也是一个char。所以你需要把这两个改成std::strings。

如果你只想要一个字母,那么你的letter成员就可以了,但你不能把整个字符串放在一个字母中,所以你在main中调用setLetter没有多大意义。因此,如果你只需要一个字母,而不是从一个字母到一个完整的西装名称的映射,只需使用switch语句:

cin >> faction[0];
switch (faction[0]) {
  case 's': case 'c': case 'h': case 'd':
    factionHead_ptr->setLetter(faction[0]);
    break;
  default:
    // invalid input given, do something about it
    break;
}

此行不起作用:

if(faction [0] == 's' && 'c' && 'h' && 'd')

因为if语句的每一部分都是由它自己评估的。例如:"c"将始终计算为true,因此该语句没有任何效果。应该是这样的:

if(faction [0] == 's' || faction [0] == 'c' || faction [0] == 'h' || faction [0] == 'd')

你可以在这里完美地使用switch语句:

switch(faction[0]) {
case 's':
    factionHead_ptr->setLetter("spade");
    break;
case 'c':
    ...
    break;
...
default:
    // code here if none of s, c, h, d
    break;
}

编辑:还要小心您的数据类型。例如,我不确定faction[0] = "spade";是否会像预期的那样工作,因为faction[0]的类型似乎是char,而不是字符串。

您不能以这种方式链接条件。条件是,如果faction的第一个字母是s"派系"的第一个字符是c。。。

像这样:

if(faction [0] == 's' || faction [0] == 'c' || faction [0] == 'h' || faction [0] == 'd')

您可以使用的switch语句:

string faction;
char type;
cin >> type;
switch (type)
{
case 's':
    faction = "spade";
    break;
case 'c':
    faction = "clib";
    break;
case 'h':
    faction = "heart";
    break;
case 'd':
    faction = "diamond";
    break;
default:
    cout << "Illegal card typen";
    break;
}

上面的代码还修复了您遇到的另一个问题,即将faction[0]同时用作字符串和字符。由于您没有指定faction的类型,我在上面的示例中将其作为字符串。

switch(functin[0]) {
  case 's': faction = "spade";factionHead_ptr->setLetter("spade");break;
  case 'c': faction = "clubs";factionHead_ptr->setLetter("clubs");break;
  case 'h': faction = "hearts";factionHead_ptr->setLetter("hearts");break;
  case 'd': faction = "diamonds";factionHead_ptr->setLetter("diamonds");break;
  default: cerr<<"wrong suite entered";
}