Edit question The following program is a C link list example. In this assignment
ID: 3690328 • Letter: E
Question
Edit question
The following program is a C link list example. In this assignment, you 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)
6. Make sure you free all pointers before your program terminated.
Comment
Explanation / Answer
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct node{
int recordID, year, month, day;
char category[255], detail[255];
double amount;
struct node *next;
}; // node structure
FILE *open();
struct node * head = NULL; // head node
void add_node(int id, int year, int month, int day, char cat[],char dtls[], double amt) {
if (head == NULL){
head = (struct node *) malloc(sizeof(struct node));
head->recordID = id;
head->year=year;
head->month=month;
head->day=day;
strcpy(head->category,cat);
strcpy(head->detail,dtls);
head->amount=amt;
head->next=NULL;
}
else{
struct node * p = head;
while(p->next != NULL) p = p->next;
p->next = (struct node *) malloc(sizeof(struct node));
p->next->recordID = id;
p->next->year=year;
p->next->month=month;
p->next->day=day;
strcpy(p->next->category,cat);
strcpy(p->next->detail,dtls);
p->next->amount=amt;
p->next->next=NULL;
//p->next->element = num;
}
puts("add complite");
}
void print_list() {
struct node * p = head;
while(p != NULL){
printf("RecordID=%d ", p->recordID);
printf("Category=%s ", p->category);
printf("Amount=$%f ", p->amount);
printf("Date=%d/%d/%d ", p->month, p->day, p->year);
printf("Details=%s ", p->detail);
p = p->next;
}
}
void free_list() {
struct node * p = head;
while(head != NULL){
p = head->next; free(head);
head = p;
}
}
// HERE IS THE MAIN FUNCTION
int main(int argc, char const *argv[]) {
int n, i, count=0;
int recordID=0, year=0, month=0, day=0;
char category[255], detail[255];
double amount=0.0;
char ch, line[200];
char a[10];
FILE *ptr=open();
puts("starts");
while((ch=fgetc(ptr))!=EOF) {
if(ch!=' ') {
line[count]=ch;
count++;
puts("inside if");
}
else {
puts("inside else");
int i=0, k=0;
line[count]='';
while(line[i]!='%')
recordID=(recordID*10)+(line[i++]-'0');
i++;
while(line[i]!='%')
category[k++]=line[i++];
i++;
k=0;
while(line[i]!='%')
detail[k++]=line[i++];
i++;
k=0;
while(line[i]!='%'){
a[k++]=line[i++];
}
a[k]='';
amount=atof(a);
i++;
while(line[i]!='%')
year=(year*10)+(line[i++]-'0');
i++;
while(line[i]!='%')
month=(month*10)+(line[i++]-'0');
i++;
while(line[i]!='')
day=(day*10)+(line[i++]-'0');
i++;
count=0;
add_node(recordID, year, month, day, category, detail, amount);
}
}
/*for (i = 0; i < 10; i++) {
scanf("%d", &n);
add_node(n); }*/
print_list();
free_list();
return 0;
}
FILE *open() {
FILE *ptr;
char name[100];
puts("Enter your file name");
gets(name);
ptr = fopen(name, "rt");
if(ptr==NULL)
{
printf("Can not find bills.txt");
return NULL;
}
return ptr;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.