The following program is a C link list example. you to need modify this program
ID: 3690030 • Letter: T
Question
The following program is a C link list example. you to need modify this
program to realize some new features.
#include <stdlib.h>
#include <stdio.h>
struct node{
int element;
struct node * next;
}; // node structure
struct node * head = NULL; // head node
void add_node(int num) {
if (head == NULL){
head = (struct node *) malloc(sizeof(struct node));
head->element = num;
}
else{
struct node * p = head;
while(p->next != NULL) p = p->next;
p->next = (struct node *) malloc(sizeof(struct node));
p->next->element = num;
}
}
void print_list() {
struct node * p = head;
while(p != NULL){
printf("%d ", p->element);
p = p->next;
}
}
void free_list() {
struct node * p = head;
while(head != NULL){
p = head->next;
free(head);
head = p;
}
}
int main(int argc, char const *argv[]) {
int n, i;
for (i = 0; i < 10; i++) {
scanf("%d", &n);
add_node(n);
}
print_list();
free_list();
return 0;
}
1, Create a new file named “bills_reader.c” and copy this program into this new file. The
following assignments are all working on the “bills_reader.c” file.
2, At the beginning of your program, write your name into a comment. (// … or /*…*/)
3, Modify node structure, the new structure has 8 features, which are recordID,
category, detail, amount, year, month, day, and next.
• recordID, year, month, and day are int numbers.
• category and detail are strings with maximum length of 255 characters.
• amount is a double numbers.
• next is a struct node pointer to next node.
4, Instead of reading from stdin, now your program should read input from a file named
“bills.txt” (that is the data file in your project).
• “bills.txt” may be inexistent, empty, or containing one or multiple lines.
• if “bills.txt” is inexistent, you program should print an message “Can not find bills.txt”.
• Each line in “bills.txt” is one record, the format is
“recordID%category%detail%amount%year%month%day”. ‘%’ is the separator.
• “bills.txt” file always contains an empty line at the end of this file.
• An example file can be downloaded from here ( http://grid.cs.gsu.edu/~bli6/teaching/
spring2016/bills.txt ). You can not modify this file, and your program should process
this file correctly.
5, You need rewrite your print_list() function, the output format should be:
RecordID: (recordID) Category: (category) Amount: $(amount)
Date: (month)-(day)-(year)
Detail: (detail)
Explanation / Answer
// Name
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct node{
int recordID;
char category[255];
char detail[255];
double amount;
int year;
int month;
int day;
struct node * next;
}; // node structure
struct node * head = NULL; // head node
void add_node(int rid, char cat[], char det[], double amt, int y, int m, int d) {
if (head == NULL){
head = (struct node *) malloc(sizeof(struct node));
head->recordID = rid;
strcpy(head->category, cat);
strcpy(head->detail, det);
head->amount = amt;
head->year = y;
head->month = m;
head->day = d;
}
else{
struct node * p = head;
while(p->next != NULL) p = p->next;
p->next = (struct node *) malloc(sizeof(struct node));
p->next->recordID = rid;
strcpy(p->next->category, cat);
strcpy(p->next->detail, det);
p->next->amount = amt;
p->next->year = y;
p->next->month = m;
p->next->day = d;
}
}
void print_list() {
struct node * p = head;
while(p != NULL){
printf("RecordID: %d Category: %s Amount: $%lf Date: %d-%d-%d Deaail: %s ", p->recordID, p->category, p->amount, p->month, p->day, p->year, p->detail);
p = p->next;
}
}
void free_list() {
struct node * p = head;
while(head != NULL){
p = head->next;
free(head);
head = p;
}
}
int main(int argc, char const *argv[]) {
int recordID;
char category[255];
char detail[255];
double amount;
int year;
int month;
int day;
int n, i;
FILE *fin = fopen("input.txt", "r");
if(!fin){
printf("Can not find bills.txt ");
return 0;
}
while(fscanf(fin, "%d%%%s%%%s%%%lf%%%d%%%d%%%d", &recordID, category, detail, &amount, &year, &month, &day) != EOF) {
add_node(recordID, category, detail, amount, year, month, day);
}
print_list();
free_list();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.