所以我正在尝试编写这个函数,read_deck,它是:
void read_deck(char *filename, queue *deck)
这个函数接受一个文件名的字符串。它打开文件文件名以供阅读。然后它一次一张地将卡片读入队列卡组,以便它们出现在文件中的顺序与它们出现在队列中的顺序相同。读取卡片直到到达文件末尾,此时文件关闭并且函数返回。
int main(int argc, char **argv)
Main 将设置队列甲板并使用 read_deck 用第一个命令行参数指定的文件中的卡片填充它。然后它将打印牌组大小,其中的所有卡片(函数 length 和 print_list)将对此有用。
我目前的计划:未完成,不知道如何进一步处理
#include "libcardlist.h"
#include <stdio.h>
void read_deck(char *filename, queue *deck);
int main(int argc, char **argv){
char *filename = argv[1];
FILE *f = fopen(filename, "r"); // open file
char buf[1024];
stack *deck = new_stack(); // new deck
int status;
card cards;
//how to set up the queue
read_deck( f , deck ); // what would be the fields here?
}
void read_deck(char *filename, queue *deck){
int status;
card cards;
for (status = fscanf(f,"%s", buf);
status != EOF;
status = fscanf(f,"%s", buf))
{
fscanf (f,"%d%c", &cards.face,&cards.suit);
printf (" %d%c\n",cards.face,cards.suit);
}
}
对于我当前的 read_deck,由于某种原因,它只打印其他卡片。这是所需的输出:
gcc p2.c libcardlist.c
lila [program1]% cat smalldeck.cards
14S
2D
13C
10H
5H
11C
13S
4D
13D
lila [program1]% a.out smalldeck.cards
Deck 9: 14S 2D 13C 10H 5H 11C 13S 4D 13D
我现在的输出:
2D
10H
11C
4D
4D
libcardlist.c(正确)
/* This file contains functions to operate on a lists, stacks, and
queues of cards */
#include <stdio.h>
#include <stdlib.h>
/* Report an error and exit the program with a failure */
void cardlist_error(char *msg){
fprintf(stderr,"libcardlist: %s\n",msg);
exit(EXIT_FAILURE);
}
/* Basic type for a card */
typedef struct {
int face; /* 2-14 */
char suit; /* C H S D */
} card;
/* Convert a string like 14D into a card */
card str2card(char *buf){
card c;
sscanf(buf,"%d%c",&c.face,&c.suit);
return c;
}
/* Given a card c, put a string like 14D in buf representing it. Good
for printing */
char *card2str(card c, char *buf){
sprintf(buf, "%d%c", c.face, c.suit);
return buf;
}
/* Lists are of cards */
typedef card node_data;
/* List Functions */
/* Basic type for a list: data and next */
typedef struct node {
node_data data;
struct node *next;
} node;
/* Returns how many nodes are in a list */
int length(node *l){
int n = 0;
while(l != NULL){
n++;
l = l->next;
}
return n;
}
/* Reverses a list, creates a fresh and distinct copy of the list */
node *reverse(node *l){
node *r = NULL;
while(l != NULL){
node *new = malloc(sizeof(node));
new->data = l->data;
new->next = r;
r = new;
l = l->next;
}
return r;
}
/* Print a list of cards to a file pointer */
void print_list(node *l, FILE *f){
char buf[1024]; /* Use this for string conversion */
while(l != NULL){ /* Til end of list */
fprintf(f,"%s ", card2str(l->data,buf)); /* Convert to string and print */
l = l->next; /* Advance to next */
}
fprintf(f,"\n");
}
/* Stack functions */
/* Basic type for a stack */
typedef struct stack {
node *top;
} stack;
/* Make a new stack: allocate memory and set its top pointer to
initially be NULL for an empty stack */
stack *new_stack(){
stack *s = malloc(sizeof(stack));
s->top = NULL;
return s;
}
/* Return 1 if the stack is empty and 0 otherwise */
int stack_empty(stack *s){
return s->top == NULL;
}
/* Push something on the top of the stack */
void stack_push(stack *s, node_data p){
node *new = malloc(sizeof(node)); /* New node for the new data */
new->data = p; /* New node gets the new data */
new->next = s->top; /* new will be on top, point it at current top */
s->top = new; /* new is on top now */
}
/* Remove the top element of the stack */
void stack_pop(stack *s){
if(!stack_empty(s)){ /* If the stack is not empty */
node *remove = s->top; /* Track what's being removed */
s->top = s->top->next; /* Advance the top down one */
free(remove); /* Get rid of the old top node */
}
}
/* Retrive data from the top of the stack */
node_data stack_top(stack *s){
if(!stack_empty(s)){ /* If the stack is not empty */
return (s->top->data); /* Return the data */
}
else{ /* Otherwise there is an error */
cardlist_error("stack_top called on empty stack");
}
}
/* Queue functions */
/* Basic type for the queue data structure */
typedef struct queue {
node *front; /* Front of the line */
node *rear; /* Back of the line */
} queue;
/* Make a new queue which is initially empty */
queue *new_queue(){
queue *q = malloc(sizeof(queue));
q->front = NULL;
q->rear = NULL;
return q;
}
/* Returns 1 if the queue is empty and 0 otherwise */
int queue_empty(queue *q){
return q->front == NULL;
}
/* Add something to the front of the queue */
void queue_add(queue *q, node_data p){
node *new = malloc(sizeof(node)); /* Adding a new node */
new->data = p; /* Set new node's data */
new->next = NULL; /* It will be the end of the line */
if(queue_empty(q)){ /* First node to be added */
q->front = new; /* Front and back are new node */
q->rear = new;
}
else { /* Not first node */
q->rear->next = new; /* Current rear is second to last */
q->rear = new; /* new guy is last */
}
}
/* Remove first element of the queue */
void queue_remove(queue *q){
if(!queue_empty(q)){ /* If the queue is not empty */
node *remove = q->front; /* Track who is being removed */
q->front = q->front->next; /* Second in line is now at front */
free(remove); /* Remove the old front */
}
}
/* Get the data for the front of the queue */
node_data queue_front(queue *q){
if(!queue_empty(q)){ /* If queue is not empty */
return (q->front->data); /* Get data for front node */
}
else{ /* Otherwise this is an error */
cardlist_error("queue_front called on empty queue");
}
}
错误:
segmentation fault