CAN YOU PLEASE CHANGE THIS INTO C++. BECAUSE WHEN I DO IT. IT KEPT GIVING ME ERROR. THANK YOU IN ADVANCE #include #incl
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
CAN YOU PLEASE CHANGE THIS INTO C++. BECAUSE WHEN I DO IT. IT KEPT GIVING ME ERROR. THANK YOU IN ADVANCE #include #incl
case 3: case 4: case 5: case 6: default: } } return 0; } typedef struct { break; find(L); break; display(L); break; sort(L); break; write(L); free (L->head); //The header node is destroyed free(L); //The linked list was destroyed return 0; printf("%d Is an invalid number, please re-enter!\n\n", code); break; //Student number //mathematics //Total score //Used to store information about individual char name[NAME_LEN + 1];//full name int number; int chinese; //chinese int math; int english; //English int average; //average int sum; } student; students typedef struct node { student stu; struct node* next; } studentNode; typedef struct { studentNode* head; studentNode* tail; int count; } studentList; //Data field to store student information //Pointer field, pointing to the next node //Student node //Head pointer //Tail pointer //Total number of student nodes //Student list
void initialize(studentList* L) { //Create header node studentNode* s = (studentNode*)malloc(sizeof(studentNode)); s->next = NULL; //Initialize linked list: //The head and tail pointers refer to the head node, and the initial length is zero L->head = s; L->tail=S; L->count = 0; } void enter(studentList* L) { //Create a new node studentNode* s = (studentNode*)malloc(sizeof(studentNode)); //Type information into the new node printf("Please enter student name:"); scanf("%s", s->stu.name); printf("Please enter student ID:"); scanf("%d", &s->stu.number); printf("Please enter your language score:"); scanf("%d", &s->stu.chinese); printf("Please enter your math score:"); scanf("%d", &s->stu.math); printf("Please enter your English score:"); scanf("%d", &s->stu.english); s->stu.sum = s->stu.chinese + s->stu.math + s->stu.english; s->stu.average = s->stu.sum / 3; //If the linked list is empty, point the tail pointer to the new node if (L->head == L->tail) { L->tail = s; } //Insert the new node into the head of the linked list (head insertion method) s->next = L->head->next; L->head->next = s; L->count++; //Output interactive information printf("Information entered successfully!\n\n"); } void display(studentList* L) { printf("share%d Group student data:\n", L->count); printf("full name\t\t Student number\t\t chinese\t\t mathematics\t\t English\t\t Total score\t\t average\n"); //Create a node pointer to the head node studentNode* p; p = L->head; //Traversal list output while (p->next) {
p = p->next; printf("%s", p->stu.name); printf("\t\t%d", p->stu.number); printf("\t\t%d", p->stu.chinese); printf("\t\t%d", p->stu.math); printf("\t\t%d", p->stu.english); printf("\t\t%d", p->stu.sum); printf("\t\t%d", p->stu.average); printf("\n"); //Let the user enter the student to find printf("Please enter student name:"); char name[NAME_LEN + 1]; scanf("%s", name); //Traverse the linked list and compare names studentNode* p = L->head->next; while (p) { English\t\t Total score\t\t average\n"); } printf("\n"); } void find(studentList* L) { //If it matches, output and end the function. if (strcmp(p->stu.name, name) == 0) { printf("full name\t\t Student number\t\t chinese\t\t mathematics\t\t printf("%s", p->stu.name); printf("\t\t%d", p->stu.number); printf("\t\t%d", p->stu.chinese); printf("\t\t%d", p->stu.math); printf("\t\t%d", p->stu.english); printf("\t\t%d", p->stu.sum); printf("\t\t%d", p->stu.average); printf("\n\n"); return; } //If the name doesn't match, it's next p = p->next; } //I didn't find the name in the traversal printf("I didn't find this %s Information!\n\n", name); //Let the user enter the student to modify printf("Please enter student name:"); char name[NAME_LEN + 1]; scanf("%s", name); //Traverse the linked list and compare names } void modify(studentList* L) {
studentNode* p = L->head->next; while (p) { //If yes, let the user retype and end the function if (strcmp(p->stu.name, name) == 0) { printf("Please re-enter the information:\n"); printf("Please enter student ID:"); scanf("%d", &p->stu.number); printf("Please enter your language score:"); scanf("%d", &p->stu.chinese); printf("Please enter your math score:"); scanf("%d", &p->stu.math); printf("Please enter your English score:"); scanf("%d", &p->stu.english); p->stu.sum = p->stu.chinese + p->stu.math + p->stu.english; p->stu.average = p->stu.sum / 3; printf("Information modified successfully!\n\n"); return; } //If the name doesn't match, it's next p = p->next; } //I didn't find the name in the traversal printf("I didn't find this %s Information!\n\n", name); //Neither node is in fart order. if (L->count < 2) { printf("List sorting completed!\n"); display(L); return; } //Insert sort studentNode* p, pre,* tmp; //p points to the second student node p = L->head->next; //The linked list is disconnected from the head node and the first student node L->head->next = NULL; //Cycle back from the first student node while (p) { //Save the pointer of the next node mp = p->next; //Find insertion location pre = L->head; while (pre->next != NULL && pre->next->stu.average > p->stu.average) pre pre->next; //Update tail pointer if (pre->next == NULL) { } void sort(studentList* L) {
L->tail = p; } //insert p->next = pre->next; pre->next = p; //Skip to next p = tmp; } printf("List sorting completed!\n"); display(L); } void write(studentList* L) { //Open file flow FILE* fp = fopen(FILE_NAME, "w"); if (fp == NULL) { printf("file%s Open failed\n", FILE_NAME); exit(EXIT_FAILURE); } //Output the total number of student nodes on the first line fprintf(fp,"%d\n", L->count); //Create a node pointer to the head node studentNode* p; p = L->head->next; //Traverse the linked list and output a set of data as a row while (p) { fprintf(fp,"%s", p->stu.name); fprintf(fp,"%d ", p->stu.number); fprintf(fp,"%d", p->stu.chinese); fprintf(fp,"%d ", p->stu.math); fprintf(fp,"%d ", p->stu.english); fprintf(fp,"%d", p->stu.sum); fprintf(fp,"%d ", p->stu.average); fprintf(fp,"\n"); //Free node space after output studentNode* next = p->next; free(p); p = next; } //Close file stream fclose(fp); //Interactive information printf("Data saved! Thanks for using. Bye!\n"); } void read(studentList* L) { //Open file stream FILE* fp = fopen(FILE_NAME, "r");
} if (fp == NULL) { printf("file%s Open failed\n", FILE_NAME); exit(EXIT_FAILURE); } //Read the total number of student nodes in the first row fscanf(fp,"%d", &L->count); //The data is read circularly, and the number of cycles is count for (int i = 1; i <= L->count; i++) { //Create a new node studentNode* s = (studentNode*) malloc(sizeof(studentNode)); //Read data fscanf(fp,"%s", s->stu.name); fscanf(fp,"%d", &s->stu.number); fscanf(fp,"%d", &s->stu.chinese); fscanf(fp,"%d", &s->stu.math); fscanf(fp,"%d", &s->stu.english); fscanf(fp,"%d", &s->stu.sum); fscanf(fp,"%d", &s->stu.average); //Insert the new node into the tail of the linked list (tail insertion method) s->next = NULL; L->tail->next = s; L->tail = s; } //Close file stream fclose(fp);