In: Computer Science
Problem: Make linkedList.h and linkList.c in Programming C language
Project description
This project will require students to generate a linked list of playing card based on data read from
a file and to write out the end result to a file.
linkedList.h |
Create a header file name linkedList |
Include the following C header files:
Create the following macros:
|
|
Add the following function prototypes:
|
|
Add a typedef struct definition named card and aliased as Card as follows:
|
|
Add a global variable:
|
|
linkedList.c |
Create a C source code file named linkedList.c |
Include the user defined header file names linkedList.h |
|
main() |
1. Read data file 2. Display deck of cards 3. Write data file 4. Exit Program
|
readDataFile |
|
addCard() |
|
display() |
|
writeDataFile() |
|
Code:
//linkedList.h
Code as text:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FACES 13
#define SUITS 4
void addCard(char suit, char face);
void displayCards();
void readDataFile();
void writeDataFile();
typedef struct card {
char suit;
char face;
struct card *next;
} Card;
Card head;
//linkedList.C
Code as text:
#include "linkedList.h"
int main() {
int ch;
head.next = NULL;
while(TRUE) {
printf("1. Read data file\n");
printf("2. Display deck of cards\n");
printf("3. Write data file\n");
printf("4. Exit Program\n");
printf("Enter choice: ");
scanf("%d", &ch);
if (ch == 1) {
readDataFile();
}
else if (ch == 2) {
displayCards();
}
else if (ch == 3) {
writeDataFile();
}
else {
printf("Exiting..\n");
exit(0);
}
printf("\n");
}
return 0;
}
void readDataFile() {
char face, suit;
char faces[FACES], suits[SUITS];
char *fileName = "Assignment8Input.txt";
FILE *filePointer;
int s = 0, f = 0;
char c;
filePointer = fopen(fileName, "r");
if (filePointer == NULL) {
printf("Error opening file!\n");
return;
} else {
printf("File opened successfully.\n");
}
while(((c = fgetc(filePointer)) != EOF) && s < SUITS) {
suits[s] = c;
s++;
}
while(((c = fgetc(filePointer)) != EOF) && f < FACES) {
faces[f] = c;
f++;
}
for (s = 0; s < SUITS; s++) {
for (f = 0; f < FACES; f++) {
suit = suits[s];
face = faces[f];
addCard(suit, face);
}
}
fclose(filePointer);
}
void addCard(char suit, char face) {
Card temp;
temp.suit = suit;
temp.face = face;
temp.next = head.next;
head = temp;
}
void displayCards() {
Card set = head;
char suit[9];
char face[6];
while (set.next != NULL) {
switch(set.suit) {
case 'C': printf("Clubs ");
break;
case 'D': printf("Diamonds ");
break;
case 'H': printf("Hearts ");
break;
case 'S': printf("Spades ");
break;
}
switch(set.face) {
case 'A': printf("Ace");
break;
case '2': printf("Two");
break;
case '3': printf("Three");
break;
case '4': printf("Four");
break;
case '5': printf("Five");
break;
case '6': printf("Six");
break;
case '7': printf("Seven");
break;
case '8': printf("Eight");
break;
case '9': printf("Nine");
break;
case 'T': printf("Ten");
break;
case 'J': printf("Jack");
break;
case 'Q': printf("Queen");
break;
case 'K': printf("King");
break;
}
printf("\n");
set = *set.next;
}
}
void writeDataFile() {
char *fileName = "Assignment8Output.txt";
FILE *filePointer;
Card *current = head.next;
filePointer = fopen(fileName, "w");
if(filePointer == NULL) {
printf("Error opening output file.\n");
return;
}
else {
printf("Output file succesfully opened.\n");
}
while (current != NULL) {
switch(current->suit) {
case 'C': fprintf(filePointer, "Clubs ");
break;
case 'D': fprintf(filePointer, "Diamonds ");
break;
case 'H': fprintf(filePointer, "Hearts ");
break;
case 'S': fprintf(filePointer, "Spades ");
break;
}
switch(current->face) {
case 'A': fprintf(filePointer, "Ace");
break;
case '2': fprintf(filePointer, "Two");
break;
case '3': fprintf(filePointer, "Three");
break;
case '4': fprintf(filePointer, "Four");
break;
case '5': fprintf(filePointer, "Five");
break;
case '6': fprintf(filePointer, "Six");
break;
case '7': fprintf(filePointer, "Seven");
break;
case '8': fprintf(filePointer, "Eight");
break;
case '9': fprintf(filePointer, "Nine");
break;
case 'T': fprintf(filePointer, "Ten");
break;
case 'J': fprintf(filePointer, "Jack");
break;
case 'Q': fprintf(filePointer, "Queen");
break;
case 'K': fprintf(filePointer, "King");
break;
}
fprintf(filePointer, "\n");
current = current->next;
}
fclose(filePointer);
}
Sample run:
P.s. i don't have input file to check, but it will work. if it doesn't then tell in comments and upload input file i will fix it.