Alter the Checkbook program from Assignment 5 using C (3 points) o At the top of
ID: 3753827 • Letter: A
Question
Alter the Checkbook program from Assignment 5 using C (3 points) o At the top of the file include the following comments Your name -The purpose (core concept found below) of the program . The date created o Create a Check node structure. Include: -Check number (should be an integer) - Date (use type char[ ]) . To Amount Description Pointer to the next Check node o Add functions to: Add a Check to the checkbook. Collect all information for a single check and add it to the checkbook. Call the function to display the values for the check before adding the check. Delete a check from the checkbook. Use the check number to identify the check to be deleted Display the values for a single check. Format the amount for two decimal places Display the values for the entire checkbook, one check at a time o In the main function: Create a checkbook. It should be a linked list. Repeat until the user quits. Give the user these options Add a check. Delete a check. . Display a single check. Display the checkbook .Quit o Compile and run your code o Submit your source code as a plain text file with a .c extension. Make sure it compiles without error before submitting itExplanation / Answer
#include <stdio.h>
#include <stdlib.h>
// Defines a structure to store Check information
struct Check
{
int checkNumber;
char date[12];
char to[30];
double amount;
char description[100];
// Self reference pointer to point to next Check
struct Check *link;
};// End of structure
// Function to accept a check information and returns it
struct Check inputCheck()
{
// Creates a temporary object of Check
struct Check c;
// Accepts check data
printf("Enter the check details: ");
printf("Check Number: ");
scanf("%d", &c.checkNumber);
printf("Date: ");
scanf("%s", c.date);
printf("Pay To: ");
getchar();
gets(c.to);
printf("Amount: ");
scanf("%lf", &c.amount);
printf("Description: ");
getchar();
gets(c.description);
// Returns the check object
return c;
}// End of function
// Function to display a check information based on the parameter check number
void displayCheck(struct Check *headNode, int checkNo)
{
// Initialize the found status to -1 for not found
int found = -1;
// Creates a current node points to head
// PrevNode to store the previous node
struct Check* currentNode = headNode, *prevNode;
// Search the check number to be deleted, keep track of the
// previous node as we need to change 'prevNode->link'
// Loops till end of the list
while (currentNode != NULL)
{
// Checks if the current node check number is equals to parameter check number
if(currentNode->checkNumber == checkNo)
{
// Set the found status to 1
found = 1;
printf(" ************ Check number %d information ************ ", checkNo);
// Display the found node information
printf(" Check Number: %d Date: %s Given To: %s Check Amount: %.2f Check details: %s",
currentNode->checkNumber, currentNode->date, currentNode->to,
currentNode->amount, currentNode->description);
// Come out of the loop
break;
}// End of if condition
// Otherwise not matched
else
{
// Stores the current node in previous node
prevNode = currentNode;
// Move to next node
currentNode = currentNode->link;
}// End of else
}// End of while loop
// Checks if the found status is -1 then check number not found
if(found == -1)
printf(" Check number %d not found.", checkNo);
}// End of function
//Function to display the contents of the delivered linked list.
void displayCheckBook(struct Check *headNode)
{
printf(" ************ Check Book information ************ ");
// Creates a current node points to head node
struct Check *currentNode = headNode;
// Loops till end of the linked list
while(currentNode->link != NULL)
{
// Move next
currentNode = currentNode->link;
//Display the current node information
printf(" Check Number: %d Date: %s Given To: %s Check Amount: %.2f Check details: %s",
currentNode->checkNumber, currentNode->date, currentNode->to,
currentNode->amount, currentNode->description);
}//End of while loop
}//End of function
// Function to add a check to the check book
void addCheck(struct Check *headNode)
{
// Creates a new node
struct Check *newNode = (struct Check *) malloc(sizeof(struct Check));
// Creates a current node points to head node
struct Check *currentNode = headNode;
// Loops till end of the linked list
while(currentNode->link != NULL)
{
// Move next
currentNode = currentNode->link;
}//End of while loop
// Calls the function inputCheck() to accept check information
*newNode = inputCheck();
// newNode link part is pointing to the currentNode link
newNode->link = currentNode->link;
// currentNode link is pointing to newNode
currentNode->link = newNode;
}// End of function
// Function to delete a check from check book based on the parameter checkNo
void deleteCheck(struct Check *headNode, int checkNo)
{
// Creates a current node pointing to head node
// prevNode to store previous node address
struct Check* currentNode = headNode, *prevNode;
//First node itself holds the checkNo to be deleted
// Checks if current is not null and current node check number is equals to parameter checkNo
if (currentNode != NULL && currentNode->checkNumber == checkNo)
{
// Assign the current node link to head node
headNode = currentNode->link;
// Delete the current node
free(currentNode);
return;
}// End of if condition
// Search for the checkNo to be deleted, keep track of the
// previous node as we need to change 'prevNode->link'
// Loops till current is not null and current node check number is not equals to parameter checkNo
while (currentNode != NULL && currentNode->checkNumber != checkNo)
{
// Stores the current node in previous node
prevNode = currentNode;
// Move next
currentNode = currentNode->link;
}// End of while loop
// If current node is NULL then checkNo is not present in linked list
if (currentNode == NULL)
{
printf(" Check number %d not available.", checkNo);
return;
}// End of if condition
// Unlink the node from linked list by assigning current node link to previous node link
prevNode->link = currentNode->link;
// Delete the current node
free(currentNode);
}// End of function
// Function to accept choice, display menu, and return choice
int menu()
{
// To store choice
int ch;
// Display menu
printf(" 1 - Add a check.");
printf(" 2 - Delete a check.");
printf(" 3 - Display a single check.");
printf(" 4 - Display the check book.");
printf(" 5 - Quit.");
// Accept user choice
printf(" Enter your choice: ");
scanf("%d", &ch);
// Return choice
return ch;
}// End of function
// main function definition
int main()
{
// Creates a head pointer
struct Check *header;
// Allocate memory for header node
header = (struct Check *) malloc(sizeof(struct Check));
// Initializes head node to nNULL
header->link = NULL;
// To store the check number entered by the user
int chechNo;
// Loops till user choice is not 5
do
{
// Calls the menu() function to accept user choice
// Calls the appropriate function based on user choice
switch(menu())
{
case 1:
addCheck(header);
break;
case 2:
printf(" Enter the check number to delete: ");
scanf("%d", &chechNo);
deleteCheck(header, chechNo);
break;
case 3:
printf(" Enter the check number to display: ");
scanf("%d", &chechNo);
displayCheck(header, chechNo);
break;
case 4:
displayCheckBook(header);
break;
case 5:
exit(0);
default:
printf(" Invalid option.");
}// End of switch case
}while(1);// End of do - while loop
}// End of main function
Sample Output:
1 - Add a check.
2 - Delete a check.
3 - Display a single check.
4 - Display the check book.
5 - Quit.
Enter your choice: 1
Enter the check details:
Check Number: 111
Date: 12/2/2010
Pay To: Sunnita
Amount: 12000
Description: Fee
1 - Add a check.
2 - Delete a check.
3 - Display a single check.
4 - Display the check book.
5 - Quit.
Enter your choice: 1
Enter the check details:
Check Number: 222
Date: 11/5/2014
Pay To: Anil
Amount: 500
Description: Electic Bill
1 - Add a check.
2 - Delete a check.
3 - Display a single check.
4 - Display the check book.
5 - Quit.
Enter your choice: 4
************ Check Book information ************
Check Number: 111
Date: 12/2/2010
Given To: Sunnita
Check Amount: 12000.00
Check details: Fee
Check Number: 222
Date: 11/5/2014
Given To: Anil
Check Amount: 500.00
Check details: Electic Bill
1 - Add a check.
2 - Delete a check.
3 - Display a single check.
4 - Display the check book.
5 - Quit.
Enter your choice: 1
Enter the check details:
Check Number: 333
Date: 1/4/2011
Pay To: Ram
Amount: 450
Description: Purchase shirt
1 - Add a check.
2 - Delete a check.
3 - Display a single check.
4 - Display the check book.
5 - Quit.
Enter your choice: 4
************ Check Book information ************
Check Number: 111
Date: 12/2/2010
Given To: Sunnita
Check Amount: 12000.00
Check details: Fee
Check Number: 222
Date: 11/5/2014
Given To: Anil
Check Amount: 500.00
Check details: Electic Bill
Check Number: 333
Date: 1/4/2011
Given To: Ram
Check Amount: 450.00
Check details: Purchase shirt
1 - Add a check.
2 - Delete a check.
3 - Display a single check.
4 - Display the check book.
5 - Quit.
Enter your choice: 3
Enter the check number to display: 111
************ Check number 111 information ************
Check Number: 111
Date: 12/2/2010
Given To: Sunnita
Check Amount: 12000.00
Check details: Fee
1 - Add a check.
2 - Delete a check.
3 - Display a single check.
4 - Display the check book.
5 - Quit.
Enter your choice: 3
Enter the check number to display: 333
************ Check number 333 information ************
Check Number: 333
Date: 1/4/2011
Given To: Ram
Check Amount: 450.00
Check details: Purchase shirt
1 - Add a check.
2 - Delete a check.
3 - Display a single check.
4 - Display the check book.
5 - Quit.
Enter your choice: 3
Enter the check number to display: 222
************ Check number 222 information ************
Check Number: 222
Date: 11/5/2014
Given To: Anil
Check Amount: 500.00
Check details: Electic Bill
1 - Add a check.
2 - Delete a check.
3 - Display a single check.
4 - Display the check book.
5 - Quit.
Enter your choice: 3
Enter the check number to display: 444
Check number 444 not found.
1 - Add a check.
2 - Delete a check.
3 - Display a single check.
4 - Display the check book.
5 - Quit.
Enter your choice: 2
Enter the check number to delete: 555
Check number 555 not available.
1 - Add a check.
2 - Delete a check.
3 - Display a single check.
4 - Display the check book.
5 - Quit.
Enter your choice: 2
Enter the check number to delete: 111
1 - Add a check.
2 - Delete a check.
3 - Display a single check.
4 - Display the check book.
5 - Quit.
Enter your choice: 4
************ Check Book information ************
Check Number: 222
Date: 11/5/2014
Given To: Anil
Check Amount: 500.00
Check details: Electic Bill
Check Number: 333
Date: 1/4/2011
Given To: Ram
Check Amount: 450.00
Check details: Purchase shirt
1 - Add a check.
2 - Delete a check.
3 - Display a single check.
4 - Display the check book.
5 - Quit.
Enter your choice: 9
Invalid option.
1 - Add a check.
2 - Delete a check.
3 - Display a single check.
4 - Display the check book.
5 - Quit.
Enter your choice: 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.