Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/ / / = = = = = = = = = = = = PART 1 : FIXED code = = = = =

///============ PART 1: FIXED code ==========///
///====== DON'T CHANGE THIS PART ============///
#include
#include
#define MAX_WORD_CHAR 100
#define GPA "GPA"
#define DATE "DATE"
struct Date
{
int year;
int month;
int day;
};
typedef struct Date Date;
struct Node
{
Date dob;
float gpa;
struct Node *next;
};
typedef struct Node Node;
void readData(FILE *, char **, float *, Node **, Node **);
Node *mergeList(Node *, Node *);
Node *filterList(Node *, char *, float);
Node *reverseList(Node *);
void printList(Node *);
int main()
{
FILE *fp = fopen("DATA.IN","r");
Node *headL1= NULL;
Node *headL2= NULL;
char *controlString;
float targetGPA;
readData(fp, &controlString, &targetGPA, &headL1, &headL2);
printList(headL1);
printf("
");
printList(headL2);
printf("
");
Node *head = mergeList(headL1, headL2);
head = filterList(head, controlString, targetGPA);
head = reverseList(head);
printList(head);
fclose(fp);
}
///============ PART 2: Student works ==========///
// Define supported functions here.
int strLen(char *word)
{
int i =0;
while (*(word + i)!='\0')
i++;
return i;
}
Node *swap(Node *ptr1, Node *ptr2)
{
Node *tmp = ptr2->next;
ptr2->next = ptr1;
ptr1->next = tmp;
return ptr2;
}
Node *readOneLine(FILE* fp)
{
// TODO: read data of student (date of birth and GPA) in 'one line'
// The new node will be inserted at the end of list
// return the head of this list.
Node *head = NULL;
Node *pre = NULL;
while (1)
{
int month, day, year;
float gpa;
int res = fscanf(fp,"%*[^0-9]%02d/%02d/%04d%*[^0-9]%f%*[^
]", &month, &day, &year, &gpa);
if (res =='
'|| res == EOF) break;
Node *new_node =(Node *)malloc(sizeof(Node));
new_node->dob.year = year;
new_node->dob.month = month;
new_node->dob.day = day;
new_node->gpa = gpa;
new_node->next = NULL;
if (head == NULL)
{
head = new_node;
}
else
{
pre->next = new_node;
}
pre = new_node;
}
return head;
}
void readData(FILE *fp, char **controlString, float *targetGPA, Node **headL1, Node **headL2)
{
// TODO: read data with file pointer fp:
// LINE1: controlString targetGPA
//(LT or GT)(float number)
// LINE2: headL1 is the head of list
// LINE3: headL2 is the head of list
*controlString = malloc(10);
// Read controlString and targetGPA from the first line of the file
fscanf(fp,"%s %f",*controlString, targetGPA);
fgetc(fp);
*headL1= readOneLine(fp);
*headL2= readOneLine(fp);
}
Node *mergeList(Node *headL1, Node *headL2)
{
// TODO: add list 2 into the end of list 1, return the head of result list
if (headL1== NULL) return headL2;
Node* cur = headL1;
while (cur->next != NULL){
cur = cur->next;
}
cur->next = headL2;
return headL1;
}
Node *filterList(Node *head, char *controlString, float targetGPA)
{
/*
TODO: Filter the third list based on GPA criteria.
If controlString is "LT", filter the students with a GPA less than targetGPA.
If controlString is "GT", filter the students with a GPA greater than targetGPA.
return head of filtered list
*/
Node* cur = head;
Node* pre = NULL;
while (cur != NULL){
if ((controlString[0]=='G' && cur->gpa <= targetGPA)||(controlString[0]=='L' && cur->gpa >= targetGPA)){
if (pre == NULL){
head = cur->next;
}
else {
pre->next = cur->next;
}
Node *temp = cur;
cur = cur->next;
free(temp);
}
else {
pre = cur;
cur = cur->next;
}
}
return head;
}
Node *reverseList(Node *head)
{
// TODO: reverse the list
// return the new head
Node *pre = NULL;
Node *cur = head;
Node *next = NULL;
while (cur != NULL){
next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
return pre;
}
void printList(Node *head)
{
// TODO: print the linked list to stdout
Node *cur = head;
while (cur != NULL)
{
if (cur != head)
{
printf("%02d/%02d/%04d,%1.2f", cur->dob.month, cur->dob.day, cur->dob.year, cur->gpa);
}
else
{
printf("%02d/%02d/%04d,%1.2f", cur->dob.month, cur->dob.day, cur->dob.year, cur->gpa);
}
cur = cur->next;
}
}
SAMPLE INPUT 1
GT 3.00
abcd11/11/2014kkdsd-3.47aa 03/11/2000-dfgr4.00g
07/15/2001a-2.48fd xe01/07/2004kdl-dw4.00
SAMPLE OUTPUT 1
11/11/2014,3.4703/11/2000,4.00
07/15/2001,2.4801/07/2004,4.00
01/07/2004,4.0003/11/2000,4.0011/11/2014,3.47
I didn't get the same output, can you fix my code?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

3. What is a Duchenne smile?

Answered: 1 week ago