在switch语句中,错误是Switch Quantity不是整数

时间:2015-10-29 03:02:09

标签: c switch-statement

我正在尝试将C#代码转换为C

原始C#代码

从下面的代码中调用Hex2Binary方法

import time

class Card(object):
    """ A playing card. """
    RANKS = ["A", "2", "3", "4", "5", "6", "7",
             "8", "9", "10", "J", "Q", "K"]
    SUITS = ["c", "d", "h", "s"]

    def __init__(self, rank, suit):
        self.rank = rank 
        self.suit = suit

    def __str__(self):
        rep = self.rank + self.suit
        return rep



class Hand(object):
    """ A hand of playing cards. """
    def __init__(self):
        self.cards = []

    def __str__(self):
        if self.cards:
           rep = ""
           for card in self.cards:
               rep += str(card) + "  "
        else:
            rep = "<empty>"
        return rep


    def clear(self):
        self.cards = []

    def add(self, card):
        self.cards.append(card)

    def give(self, card, other_hand):
        self.cards.remove(card)
        other_hand.add(card)



def passAgain(passAgain):
    if passAgain == "y" or passAgain == "yes":
        card1 = Card("A", "s")
        card2 = Card("2", "s")
        card3 = Card("3", "s")
        card4 = Card("4", "s")
        card5 = Card("5", "s")


        firstHand = Hand()
        secondHand = Hand()
        thirdHand = Hand()
        fourthHand = Hand()
        lastHand = Hand()


        firstHand.add(card1)
        firstHand.add(card2)
        firstHand.add(card3)
        firstHand.add(card4)
        firstHand.add(card5)

        print ("First hand contains:",firstHand,)

        firstHand.give(card1, secondHand)
        print ("First hand gives first card to second hand and now has:", firstHand,)
        print ("Second hand now has:", secondHand,)

        firstHand.give(card2, secondHand)
        print ("First hand gives second card to second hand and now has:", firstHand,)
        print ("Second hand now has:", secondHand,)

        firstHand.give(card3, secondHand)
        print ("First hand gives third card to second hand and now has:", firstHand,)
        print ("Second hand now has:", secondHand,)

        firstHand.give(card4, secondHand)
        print ("First hand gives fourth card to second hand and now has:", firstHand,)
        print ("Second hand now has:", secondHand,)

        firstHand.give(card5, secondHand)
        print ("First hand gives last card to second hand and now has:", firstHand,)
        print ("Second hand now has:", secondHand,)
        print("\n")
        time.sleep(2)




        secondHand.give(card1, thirdHand)
        print ("Second hand gives first card to third hand and now has:", secondHand,)
        print ("Third hand now has:", thirdHand,)

        secondHand.give(card2, thirdHand)
        print ("Second hand gives second card to third hand and now has:", secondHand,)
        print ("Third hand now has:", thirdHand,)


        secondHand.give(card3, thirdHand)
        print ("Second hand gives third card to third hand and now has:", secondHand,)
        print ("Third hand now has:", thirdHand,)

        secondHand.give(card4, thirdHand)
        print ("Second hand gives fourth card to third hand and now has:", secondHand,)
        print ("Third hand now has:", thirdHand,)

        secondHand.give(card5, thirdHand)
        print ("Second hand gives last card to third hand and now has:", secondHand,)
        print ("Third hand now has:", thirdHand,)
        print("\n")
        time.sleep(2)



        thirdHand.give(card1, fourthHand)
        print ("Third hand gives first card to fourth hand and now has:", thirdHand,)
        print ("Fourth hand now has:", fourthHand,)

        thirdHand.give(card2, fourthHand)
        print ("Third hand gives second card to fourth hand and now has:", thirdHand,)
        print ("Fourth hand now has:", fourthHand,)

        thirdHand.give(card3, fourthHand)
        print ("Third hand gives third card to fourth hand and now has:", thirdHand,)
        print ("Fourth hand now has:", fourthHand,)

        thirdHand.give(card4, fourthHand)
        print ("Third hand gives fourth card to fourth hand and now has:", thirdHand,)
        print ("Fourth hand now has:", fourthHand,)

        thirdHand.give(card5, fourthHand)
        print ("Third hand gives last card to fourth hand and now has:", thirdHand,)
        print ("Fourth hand now has:", fourthHand,)
        print("\n")
        time.sleep(2)


        fourthHand.give(card1, lastHand)
        print ("Fourth hand gives first card to last hand and now has:", fourthHand,)
        print ("Last hand now has:", lastHand,)

        fourthHand.give(card2, lastHand)
        print ("Fourth hand gives second card to last hand and now has:", fourthHand,)
        print ("Last hand now has:", lastHand,)

        fourthHand.give(card3, lastHand)
        print ("Fourth hand gives third card to last hand and now has:", fourthHand,)
        print ("Last hand now has:", lastHand,)

        fourthHand.give(card4, lastHand)
        print ("Fourth hand gives fourth card to last hand and now has:", fourthHand,)
        print ("Last hand now has:", lastHand,)

        fourthHand.give(card5, lastHand)
        print ("Fourth hand gives last card to last hand and now has:", fourthHand,)
        print ("Last hand now has:", lastHand,)
        print("\n")




def main():
    while True :
        print ("Do you want to pass cards? Press y or yes")
        print("\n")

        ans = input ()
        passAgain(ans)


main()

// Hex2Binary Method

private string DEtoBinary(string HexDE)
        {
            string deBinary = "";
            for (int I = 0; I <= 15; I++)
            {
                deBinary = deBinary + Hex2Binary(HexDE.Substring(I, 1));

            }

            return deBinary;

        }

但是当我用C语言写作时,我接受如下所示的论证

private string Hex2Binary(string DE)
        {

            string myBinary = "";
            switch (DE)
            {
                case "0":
                    myBinary = "0000";
                    break;

                case "1":
                    myBinary = "0001";
                    break;
                    .
                    .
                    .
                 }
      }

我收到错误了 切换数量不是整数。

3 个答案:

答案 0 :(得分:1)

C&C的开关仅适用于整数。在您的情况下,您似乎可以将开关arg转换为整数:

#include <stdlib> // for strtol
/* char* is a more typical string representation than char[] */
char *Hex2Binary(char* DE)
{
    char *myBinary;
    long de_as_long = strtol(DE, NULL, 16);
    switch (de_as_long)
    {
        case 0:
         myBinary = "0000";
         break;
        /* ... */

仅当DE的所有可能值都可以转换为整数时才有效。

答案 1 :(得分:0)

switch()只接受整数。

你必须传递switch的整数而不是字符数组。

DE是一个字符数组。改变它以给出你想传递给开关的位置或整数。

答案 2 :(得分:0)

C不允许您打开char数组或字符串,但它确实允许您打开char s:

switch(DE[0]) {
case '0':
    myBinary = "0000";
    break;
case '1':
    myBinary = "0001";
    break;
/* ... */
}

但实际上,有一种更简单,更简洁的方法:

long val = strtol(DE, NULL, 16);
char myBinary[5] = {"01"[(val >> 3) & 1],
                    "01"[(val >> 2) & 1],
                    "01"[(val >> 1) & 1],
                    "01"[val & 1],
                    '\0' /* null terminator */
};

你可以使用循环来进一步简化这一过程。