#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int paixu(int ,int);//洗牌声明
int main(){
  int a[4][13],d,i,j,m,num=0,t=0,b[52]={0};
  srand(time(NULL));
  for(i=0;i<4;i++){
      for(j=0;j<13;j++){
            a[i][j]=j+1;
      }  
  }
  for(;num<=52;){
      d=rand()%52;
      for(m=0;m<num;m++){
            if(d==b[m]){
                break;
            }
      }
      if(m==num){
            b[num]=d;
            switch(d/13){
                case 0:printf("红桃");break;
                case 1:printf("方块");break;
                case 2:printf("草花");break;
                case 3:printf("黑桃");break;
            }
            num++;
        b[t++]=a[d/13][d%13];
            printf("%2d%c",a[d/13][d%13],num%13==0 && num!=0?'\n':' '); 

      }

    paixu(0,13);paixu(13,26);paixu(26,39);paixu(39,52);
        }
       }return 0;
 } 


    int paixu(int m,int n)  //洗牌函数
    { for(i=m;i<n;i++)
        for(j=n-1;j>i;j++)
        if(b[j-1]>b[j])
        {
        t=b[j];b[j]=b[j-1];b[j-1]=t;
        x=0;
        for(x<13) printf("%2d",b[x++]); return 0;
        }
      }

Dani AI

Generated

Quick diagnostic summary for 's card program: the intent is clear (shuffle a 52‑card deck and deal four 13‑card hands) but the posted source contains both syntax and logic errors that produce compiler failures and incorrect behavior. 's reformatting was helpful (brace alignment makes many problems visible), but there are additional, concrete issues: an off‑by‑one loop condition that will write past the end of the deck array, a sort routine that references variables local to main (scope error), malformed for‑loop syntax and a full‑width punctuation character (Unicode U+FF1B) that kills compilation, and reuse of one array for two different roles (deck indices vs. hand contents).

Concrete, actionable fixes:

  • Replace the draw loop condition num <= 52 with num < 52 (or use for (num = 0; num < 52; ++num)) to avoid writing b[52].
  • Stop reusing the same array for different purposes. Use int deck[52]; int hand[4][13]; so the deck holds card IDs (0..51) and hand[p][k] holds each player's cards.
  • Make paixu a proper function that either accepts an array pointer and indices or operates on a clearly declared global. Do not reference i, j, t, b, etc. from main without passing them.
  • Fix malformed loops and punctuation: replace for(x<13) with for (x = 0; x < 13; ++x) and remove any non‑ASCII punctuation.
  • Ensure the sort routine does not return from inside the inner swap loop; place return only after the function finishes.
  • Compile with warnings enabled (gcc -Wall -Wextra -std=c11 -pedantic) and fix every warning.

Recommended workflow (robust and simple): build a deck array 0..51, apply Fisher–Yates shuffle, deal sequentially into hand[4][13], then sort each hand[p] by rank (id % 13) using qsort or an insertion sort. Example shuffle/deal snippet:

for (int i = 0; i < 52; ++i) deck[i] = i;
/* Fisher‑Yates */
for (int i = 51; i > 0; --i) {
    int j = rand() % (i + 1);
    int tmp = deck[i]; deck[i] = deck[j]; deck[j] = tmp;
}
/* deal */
for (int p = 0; p < 4; ++p)
    for (int k = 0; k < 13; ++k)
        hand[p][k] = deck[p*13 + k];

Also ensure the source file encoding matches the chosen literal text (ASCII or UTF‑8) if non‑ASCII suit names are kept. Once the above fixes are applied the program will be far easier to debug and the paixu sorting step will behave correctly.

Recommended Answers

All 5 Replies

1. Learn to use code tags. The code you posted is almost impossible to read without them.

2. What is the problem with your program? What does it not do that you want it to do? Are there any compiler errors? If yes, then what are thy?

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int paixu(int ,int);//洗牌声明
int main(){
int a[4][13],d,i,j,m,num=0,t=0,b[52]={0};
srand(time(NULL));
for(i=0;i<4;i++){
for(j=0;j<13;j++){
a[i][j]=j+1;
} 
}
for(;num<=52{
d=rand()%52;
for(m=0;m<num;m++){
if(d==b[m]){
break;
}
}
if(m==num){
b[num]=d;
switch(d/13){
case 0:printf("红桃");break;
case 1:printf("方块");break;
case 2:printf("草花");break;
case 3:printf("黑桃");break;
}
num++;
b[t++]=a[d/13][d%13];
printf("%2d%c",a[d/13][d%13],num%13==0 && num!=0?'\n':' '); 

}

paixu(0,13);paixu(13,26);paixu(26,39);paixu(39,52);
}
}return 0;
} 


int paixu(int m,int n) //洗牌函数
{ for(i=m;i<n;i++)
for(j=n-1;j>i;j++)
if(b[j-1]>b[j])
{
t=b[j];b[j]=b[j-1];b[j-1]=t;
x=0;
for(x<13) printf("%2d",b[x++]); return 0;
}
}

Well, you used code tags, but the formatting is still horrible. I you can't bothe to indent your program properly then I won't bother to read it.

First,thank you any way !
I am very sorry,because it's my first time to come this community .
but,you teach me form a good coding habit!thanks for your enthusiasm .

There is a style that I like to use. Note that { and } braces are ligned up on the same position and you normally do not put more than one statement on the same line -- there are some exceptions as you will see in the switch statement.

This kind of coding style makes it very easy to find mismatches { and } characters and other coding problems. I found at least two when I was reformatting your code.

int main()
{
    int a[4][13],d,i,j,m,num=0,t=0,b[52]={0};
    srand(time(NULL));
    for(i=0;i<4;i++)
    {
        for(j=0;j<13;j++)
        {
            a[i][j]=j+1;
        } 
    }
    for(;num<=52;)
    {
        d=rand()%52;
        for(m=0;m<num;m++)
        {
            if(d==b[m])
            {
                break;
            }
        }
        if(m==num)
        {
            b[num]=d;
            switch(d/13)
            {
                case 0:printf("红桃");break;
                case 1:printf("方块");break;
                case 2:printf("草花");break;
                case 3:printf("黑桃");break;
            }
            num++;
            b[t++]=a[d/13][d%13];
            printf("%2d%c",a[d/13][d%13],num%13==0 && num!=0?'\n':' '); 

        }

        paixu(0,13);
        paixu(13,26);
        paixu(26,39);
        paixu(39,52);
    }
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.