Question

In: Computer Science

Problem: Make linkedList.h and linkList.c in Programming C language Project description This project will require students...

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:

  1. stdio.h
  2. stdlib.h
  3. string.h

Create the following macros:

  1. TRUE 1
  2. FACES 13
  3. SUITS 4

Add the following function prototypes:

  1. addCard
  2. displayCards
  3. readDataFile
  4. writeDataFile

Add a typedef struct definition named card and aliased as Card as follows:

  1. char suit;
  2. char face;
  3. struct card *next;

Add a global variable:

  1. Card head;

linkedList.c

Create a C source code file named linkedList.c

Include the user defined header file names linkedList.h

main()

  1. Create an integer local variable to store the user selection
  2. Initialize the global head of the linked list to NULL
  3. Loop while TRUE is true
  4. Provide the user a menu of the following options:

1. Read data file

2. Display deck of cards

3. Write data file

4. Exit Program

  1. Use a conditional statement to evaluate the user’s selection and call the appropriate function

readDataFile

  1. Create the following local variables
    1.     char face;
    2.     char suit;
    3.     char faces[FACES];
    4.     char suits[SUITS];
    5.     char *fileName = "Assignment8Input.txt";
    6.     FILE *filePointer;
    7.     int s = 0;
    8.     int f = 0;
    9.     char c;
  2. Set variable filePointer equal to function call fopen() passing variable filename as an argument
  3. Write an if statement to determine if filePoint is NULL,
    1. If true output that there was an issue opening the file
    2. Otherwise output that the file was opened successfully
  4. Write a for loop that loops four times to read in each character for the four suits in a deck of cards, store the values in the array named suits
  5. Write a for loop that loop 13 times to read in each character for the 13 faces in a deck of cards, store the values in the array named faces
  6. Write a nested for loop to loop through the suits array and faces array, for each iteration call function addCard() passing the current suit and face as arguments
  7. Close the input file

addCard()

  1. Parameter list should include two characters, one for the face of the card and one for the suit of the card
  2. Declare a variable of data type Card named temp set equal to the appropriate memory allocation function call
  3. Set the linked list node member suit equal to the parameter representing the card suit
  4. Set the linked list node member face equal to the parameter representing the card face
  5. Set the linked list node member next equal to the global variable head
  6. Set the head node equal to the temp node

display()

  1. Declare local variables
    1. Card set equal to the global head
    2. char suit[9];
    3. char face[6];
  2. Traverse the linked list, for each node do the following:
    1. Write decision making logic to evaluate the value of the linked list node member suit, if
      1. C, output Clubs
      2. D, output Diamonds
      3. H, output Hearts
      4. S, output Spades
    2. Write decision making logic to evaluate the value of the linked list node member face, if
      1. A, output Ace
      2. 2, output Two
      3. 3, output Three
      4. 4, output Four
      5. 5, output Five
      6. 6, output Six
      7. 7, output Seven
      8. 8, output Eight
      9. 9, output Nine
      10. T, output Ten
      11. J, output Jack
      12. Q, output Queen
      13. K, output King

writeDataFile()

  1. Declare local variables
    1.     char *fileName = "Assignment8Output.txt";
    2.     FILE *filePointer;
    3.     Card * current = head;
  2. Set variable filePointer equal to function call fopen() passing variable filename as an argument
  3. Write an if statement to determine if filePointer is NULL,
    1. If true output that there was an issue opening the file
    2. Otherwise output that the file was opened successfully
  4. Traverse the linked list, for each node do the following:
    1. Write decision making logic to evaluate the value of the linked list node member suit, if
      1. C, output Clubs
      2. D, output Diamonds
      3. H, output Hearts
      4. S, output Spades
    2. Write decision making logic to evaluate the value of the linked list node member face, if
      1. A, output Ace
      2. 2, output Two
      3. 3, output Three
      4. 4, output Four
      5. 5, output Five
      6. 6, output Six
      7. 7, output Seven
      8. 8, output Eight
      9. 9, output Nine
      10. T, output Ten
      11. J, output Jack
      12. Q, output Queen
      13. K, output King
  5. Close the output file

Solutions

Expert Solution

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.


Related Solutions

C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured...
Using C programming make one for loop Description For this problem you will be figuring out...
Using C programming make one for loop Description For this problem you will be figuring out if it is more beneficial to pay off your loans first before investing or if you should only make the minimum payments and invest the rest. Some things to pay attention to Interest rates given are annual interests rates but we will be assuming that interest is compounded monthly so the real rates to use will be 1/12 of what we are given We...
Class object in C++ programming language description about lesson inheritance example.
Class object in C++ programming language description about lesson inheritance example.
Class object in C++ programming language description about lesson inheritance example.
Class object in C++ programming language description about lesson inheritance example.
The Programming Language is C++ Objective: The purpose of this project is to expose you to:...
The Programming Language is C++ Objective: The purpose of this project is to expose you to: One-dimensional parallel arrays, input/output, Manipulating summation, maintenance of array elements. In addition, defining an array type and passing arrays and array elements to functions. Problem Specification: Using the structured chart below, write a program to keep records and print statistical analysis for a class of students. There are three quizzes for each student during the term. Each student is identified by a four-digit student...
Code in C-language programming description about convert binary number to decimal number.
Code in C-language programming description about convert binary number to decimal number.
Class object in C++ programming language description about lesson Overloading function example.
Class object in C++ programming language description about lesson Overloading function example.
Class object in C++ programming language description about lesson copy constructor example.
Class object in C++ programming language description about lesson copy constructor example.
Class object in C++ programming language description about lesson static variable example.
Class object in C++ programming language description about lesson static variable example.
Code in C++ programming language description about read and write data to memory example.
Code in C++ programming language description about read and write data to memory example.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT