我在 deck[i][j] 附近的第 11 行收到一条错误消息
I'm getting an error message in line 11 near deck[i][j]
我在第 9 行代码中收到以下错误消息 (deck[i][j] = ...)
二进制 + 的无效操作数(具有“char *”和“char *”)。
我该如何解决这个问题?
#include <stdio.h>
char* suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};
char* face[13] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
"Ten", "Jack", "Queen", "King"};
char* deck[4][13];
void initialiseDeck(){
for(int i = 0; i < 4; ++i)
for(int j = 0; j < 13; ++j)
deck[i][j] = face[j] + "of" + suit[i];
}
void display(){
for(int i = 0; i < 4; ++i)
for(int j = 0; j < 13; ++j)
printf("%17s\n", deck[i][j]);
}
void shuffleDeck(){
int shuffler[52];
for(int i = 0; i < 52; ++i) shuffler[i] = i;
for(int i = 0; i < 52; ++i){
int ind = i+(rand()%(52-i));
int deckI = ind/13;
int deckJ = ind/13;
swap(deck[i/13][i%13], deck[deckI][deckJ]);
}
}
我们来了!
所以我添加了宏以使代码更具可读性,并且还编写了函数 create_card_name
这是它的作用:它为您的卡的全名分配内存并创建它:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define DECK_SIZE 52
#define SUIT_SIZE 4
#define FACE_SIZE 13
char *suit[SUIT_SIZE] = {"Hearts", "Diamonds", "Clubs", "Spades"};
char *face[FACE_SIZE] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
"Ten", "Jack", "Queen", "King"};
char *deck[SUIT_SIZE][FACE_SIZE];
char * card_name_create(char* face, char* suit)
{
size_t face_len = strlen(face);
size_t suit_len = strlen(suit);
size_t of_len = 4;
char of[5] = " of ";
// The size id the size of two strings, than the size of " of " and the '[=10=]'
char * full_name = malloc(sizeof(char)*(face_len+suit_len+of_len+1));
for (size_t i = 0; i < face_len; i++)
{
full_name[i] = face[i];
}
for (size_t i = face_len; i < face_len+of_len; i++)
{
full_name[i] = of[i-face_len];
}
for (size_t i = face_len+of_len; i < face_len+of_len+suit_len; i++)
{
full_name[i] = suit[i - (face_len+of_len) ];
}
full_name[face_len+of_len+suit_len] = '[=10=]';
return full_name;
}
void initialiseDeck()
{
for (int i = 0; i < SUIT_SIZE; i++)
{
for (int j = 0; j < FACE_SIZE; j++)
{
char * c = card_name_create(face[j],suit[i]);
deck[i][j] = c;
}
}
}
void display()
{
for (int i = 0; i < SUIT_SIZE; i++)
{
for (int j = 0; j < FACE_SIZE; j++)
{
printf("%17s\n", deck[i][j]);
}
}
}
另外,尽量不使用三重指针也是一件好事,你的套牌就是这种情况。您可以使用简单的指针实现所有字符串,并使用 %
循环遍历它们。
我在第 9 行代码中收到以下错误消息 (deck[i][j] = ...) 二进制 + 的无效操作数(具有“char *”和“char *”)。 我该如何解决这个问题?
#include <stdio.h>
char* suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};
char* face[13] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
"Ten", "Jack", "Queen", "King"};
char* deck[4][13];
void initialiseDeck(){
for(int i = 0; i < 4; ++i)
for(int j = 0; j < 13; ++j)
deck[i][j] = face[j] + "of" + suit[i];
}
void display(){
for(int i = 0; i < 4; ++i)
for(int j = 0; j < 13; ++j)
printf("%17s\n", deck[i][j]);
}
void shuffleDeck(){
int shuffler[52];
for(int i = 0; i < 52; ++i) shuffler[i] = i;
for(int i = 0; i < 52; ++i){
int ind = i+(rand()%(52-i));
int deckI = ind/13;
int deckJ = ind/13;
swap(deck[i/13][i%13], deck[deckI][deckJ]);
}
}
我们来了!
所以我添加了宏以使代码更具可读性,并且还编写了函数 create_card_name
这是它的作用:它为您的卡的全名分配内存并创建它:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define DECK_SIZE 52
#define SUIT_SIZE 4
#define FACE_SIZE 13
char *suit[SUIT_SIZE] = {"Hearts", "Diamonds", "Clubs", "Spades"};
char *face[FACE_SIZE] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
"Ten", "Jack", "Queen", "King"};
char *deck[SUIT_SIZE][FACE_SIZE];
char * card_name_create(char* face, char* suit)
{
size_t face_len = strlen(face);
size_t suit_len = strlen(suit);
size_t of_len = 4;
char of[5] = " of ";
// The size id the size of two strings, than the size of " of " and the '[=10=]'
char * full_name = malloc(sizeof(char)*(face_len+suit_len+of_len+1));
for (size_t i = 0; i < face_len; i++)
{
full_name[i] = face[i];
}
for (size_t i = face_len; i < face_len+of_len; i++)
{
full_name[i] = of[i-face_len];
}
for (size_t i = face_len+of_len; i < face_len+of_len+suit_len; i++)
{
full_name[i] = suit[i - (face_len+of_len) ];
}
full_name[face_len+of_len+suit_len] = '[=10=]';
return full_name;
}
void initialiseDeck()
{
for (int i = 0; i < SUIT_SIZE; i++)
{
for (int j = 0; j < FACE_SIZE; j++)
{
char * c = card_name_create(face[j],suit[i]);
deck[i][j] = c;
}
}
}
void display()
{
for (int i = 0; i < SUIT_SIZE; i++)
{
for (int j = 0; j < FACE_SIZE; j++)
{
printf("%17s\n", deck[i][j]);
}
}
}
另外,尽量不使用三重指针也是一件好事,你的套牌就是这种情况。您可以使用简单的指针实现所有字符串,并使用 %
循环遍历它们。