坐等高手用C语言帮我编两道简单的题!!!
1.输入一行字符,按输入时的逆序建立一个链表,然后将链表中的结点一一输出,并释放全部结点
2.定义学生结构,包含学号、姓名、四项成绩和平均分。readrec函数通过键盘输入学生的学号、姓名、四项成绩,然后计算出平均分放入平均分成员中。writerec函数输出学生信息。在主函数中调用readrec函数读入5名学生的信息,再通过writerec函数将之输出。
[解决办法]
第一题
#include <stdio.h>typedef struct Node{ char ch; Node* pNext;}NODE;// 创建链表NODE* CreateList(){ NODE* pHead = NULL; NODE* p = NULL; char ch; // 输入\n停止输入字符 while ( (ch = getchar()) != '\n') { if ( NULL == p ) { pHead = p = new NODE(); } else { p->pNext = new NODE(); p = p->pNext; } p->ch = ch; p->pNext = NULL; } return pHead;}// 打印链表void PrintList(NODE* pHead){ while( NULL != pHead) { printf("%c ", pHead->ch); pHead = pHead->pNext; } printf("\n");}// 删除链表void DestroyList(NODE** pHead){ NODE* p = NULL; while ( NULL != *pHead) { p = *pHead; *pHead = (*pHead)->pNext; delete p; }}int main(){ NODE* pHead = CreateList(); PrintList(pHead); DestroyList(&pHead); return 0;}
[解决办法]
#include<stdio.h>#include<stdlib.h>#define MAXSIZE 40struct inputdata{ char data; struct inputdata *next;};int main(){ int i=0; char input[MAXSIZE]={0}; struct inputdata *head=NULL; struct inputdata *current=NULL; head=(struct inputdata *)malloc(sizeof(struct inputdata)); head->next=NULL; if(head==NULL) return -1; puts("Enter your data:"); if(gets(input)!=NULL) while(input[i]!='\0') { current=(struct inputdata *)malloc(sizeof(struct inputdata)); current->data=input[i]; current->next=head->next; head->next=current; i++; } else return -1; current=head->next; while(current!=NULL) { printf("%c",current->data); current=current->next; } current=head->next; while(head->next!=NULL) { head->next=current->next; free(current); current=head->next; } return 0;}好久不写了,下午做做下一道
[解决办法]
第二题:
/*定义学生结构,包含学号、姓名、四项成绩和平均分。readrec函数通过键盘输入学生的学号、姓名、四项成绩,然后计算出平均分放入平均分成员中。writerec函数输出学生信息。在主函数中调用readrec函数读入5名学生的信息,再通过writerec函数将之输出。*/#include <stdio.h>#include <stdlib.h>#define NUMOFSTU 5 //number of student#define STUIDLEN 15 //the lenth of student id#define NAMELEN 20 //the lenth of nametypedef struct student{ char stuID[STUIDLEN]; //student id char name[NAMELEN]; //student name float grade1; // float grade2; // float grade3; // float grade4; // float averGrade; //average grade}Stu;void readrec(Stu* stu[]);void writerec(Stu* stu[]);void getAverGrade(Stu* stu); //get the average gradeint main(){ int i; Stu* students[NUMOFSTU]; for(i=0;i<NUMOFSTU;i++) { students[i] = (Stu*)malloc(sizeof(Stu)); } readrec(students); writerec(students); for(i=0;i<NUMOFSTU;i++) { free(students[i]); } return 0;}void readrec(Stu* stu[]){ char firstNotice[60] = "Please input the information of the 1st student:\n"; char secondNotice[60] = "Please input the information of the 2nd student:\n"; int i=0; for(i=0;i<NUMOFSTU;i++) { if(i==0) { printf(firstNotice); } else if(i == 1) { printf(secondNotice); } else { printf("Please input the information of the %dth student:\n",i+1); } printf("\tPlease input the ID:\n"); scanf("%s",stu[i]->stuID); printf("\tPlease input the name:\n"); scanf("%s",stu[i]->name); printf("\tPlease input grade1:\n"); scanf("%f",&(stu[i]->grade1)); printf("\tPlease input grade2:\n"); scanf("%f",&(stu[i]->grade2)); printf("\tPlease input grade3:\n"); scanf("%f",&(stu[i]->grade3)); printf("\tPlease input grade4:\n"); scanf("%f",&(stu[i]->grade4)); getAverGrade(stu[i]); }}void writerec(Stu* stu[]){ int i; char firstNotice[60] = "The information of the 1st student:\n"; char secondNotice[60] = "The information of the 2nd student:\n"; for(i=0;i<NUMOFSTU;i++) { if(i==0) { printf(firstNotice); } else if(i == 1) { printf(secondNotice); } else { printf("The information of the %dth student:\n",i+1); } printf("\tThe student ID is:\t%s\n",stu[i]->stuID); printf("\tThe student name is:\t%s\n",stu[i]->name); printf("\tThe grade1 is:\t%f\n",stu[i]->grade1); printf("\tThe grade2 is:\t%f\n",stu[i]->grade2); printf("\tThe grade3 is:\t%f\n",stu[i]->grade3); printf("\tThe grade4 is:\t%f\n",stu[i]->grade4); printf("\tThe average grade is:\t%f\n",stu[i]->averGrade); }}void getAverGrade(Stu* stu){ stu->averGrade = (stu->grade1+stu->grade2+stu->grade3+stu->grade4)/4;}
[解决办法]
fzc_crystal的答案是正确的,只是他那个写法是c++的,不是c的。
1\添加#include <stdlib.h>
2\修改struct定义,Node* pNext为struct Node* pNext
3\将new语句改成(NODE *)malloc(sizeof(NODE));
4\将delete语句改成free(p);
#include <stdio.h>#include <stdlib.h>typedef struct Node{ char ch; struct Node* pNext;}NODE;// 根据输入字符逆序创建链表NODE* CreateList(){ NODE* pHead = NULL; NODE* p = NULL; NODE* q = NULL; char ch; // 输入\n停止输入字符 while ( (ch = getchar()) != '\n') { if ( NULL == pHead ) { q = pHead = (NODE *)malloc(sizeof(NODE)); } else { pHead = (NODE *)malloc(sizeof(NODE)); pHead->pNext = p; } pHead->ch = ch; p = pHead; } q->pNext = NULL; return pHead;}// 打印链表void PrintList(NODE* pHead){ while( NULL != pHead) { printf("%c", pHead->ch); pHead = pHead->pNext; } printf("\n");}// 删除链表void DestroyList(NODE** pHead){ NODE* p = NULL; while ( NULL != *pHead) { p = *pHead; *pHead = (*pHead)->pNext; free(p); }}int main(){ NODE* pHead = CreateList(); PrintList(pHead); DestroyList(&pHead); system("pause"); return 0;}