简体   繁体   中英

How can I implement strcpy() and sorting of a struct in C?

I am making a small ANSI C application using GCC in Ubuntu which uses strcpy() and sorting.

My header:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define DECKSZ 52

typedef struct card {
    enum {ACE=1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING} pips;
    enum {SPADES, CLUBS, HEARTS, DIAMONDS} suit;
    char cardName[20];
} card;

card deck[DECKSZ];

void initDeck(card[]);
void labelCards();
void shuffleDeck(card[]);
void swap(card*,card*);

My main file:

#include "CardOps.h"

void initDeck(card deck[]) {
    int counter;
    for (counter = 0; counter < DECKSZ; counter++) {
        deck[counter].pips = (const)((counter % 13) + 1);
        deck[counter].suit = (const)(counter / 13);
    }
}

void labelCards() {
    char pips[13][6] = {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
    char suits[4][9] = {"Spades","Hearts","Diamonds","Clubs"};
    int i;
    card cardName;
    for (i = 0; i < DECKSZ; i++) {
        strcpy(cardName, pips[i]);
        strcpy(cardName, suits[i]);
    }
}

int displayCards(int numCards) {
    int i, countCards;
    if (numCards > 52)
        countCards = 52;
    else
        countCards = numCards;
    for (i = 0; i < countCards; i++) {
        printf(cardName);
    }
    return countCards;
}

void shuffleDeck(card deck[]) {
    int i, j;
    for (i = 0; i < DECKSZ; i++) {
        j = rand() % DECKSZ;
        swap(&deck[i], &deck[j]);
    }
}

void SortCards() {

}

void swap(card *c1, card *c2) {
    card temp;
    temp = *c1;
    *c1 = *c2;
    *c2 = temp;
}

int main(void) {
    initDeck(deck);
    shuffleDeck(deck);
    return EXIT_SUCCESS;
}

I am trying to implement the following functionality:

A function called LabelCards() that takes as an argument a void and returns a void. Use each of the two enumeration type members in the deck to assign a string to cardName, ie “Queen of Hearts”. (You'll want to create an array of strings “Ace”, “Two”, “Three”, etc. for the pips and a similar array for the suits to handle the string processing.) Note that you'll need to use strcpy() to make the actual assignment to cardName.

A function called DisplayCards() that takes as an argument an integer N, and returns an integer. The function should display the cardName of first the N cards in the deck. The function returns the number of cards displayed, which could be less than the actual deck size. For example, 53 cards cannot be displayed in a deck of 52; I need to check for this and return the actual number of cards displayed.

A function called SortDeck() that takes and returns a void. My function should use the swap() algorithm to organize the cards in the deck first by their pips, and then according to their suit value.

Would somebody please help me get the functionality working? Thanks!

Don't put the declaration of deck in the header. You should make it an extern , and move the declaration into the main file.

Your initDeck function is indexing the array with DECKSZ which is constant and out of bounds. You probably meant to use counter instead.

labelCards is trying to copy the entire array into a single card. The types don't match, and the loop variable isn't used.

displayCards prints cardName , which doesn't exist. Presumably you meant to print the cardName of a specific card.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM