一个C语言结构体编程题

2024-11-24 20:41:57
推荐回答(3个)
回答1:

#include
#include
struct Student
{
int iNumber;
char cName[20];
int iMath;
int iChinese;
int iEnglish;
int iAverage;
struct Student *pNext;
};
int iCount;
struct Student *Create()
{
char ch;
struct Student *pHead = NULL;
struct Student *pNew, *pEnd;
pNew = pEnd = (struct Student *)malloc(sizeof(struct Student));
printf("please input the student number\n");
scanf("%d", &pNew->iNumber);
printf("please input the student name\n");
scanf("%s", pNew->cName);
printf("please input the student math\n");
scanf("%d", &pNew->iMath);
printf("please input the student Chinese\n");
scanf("%d", &pNew->iChinese);
printf("please input the student English\n");
scanf("%d", &pNew->iEnglish);
printf("Continue to input information(y)otherwise(n)");
scanf("%c", &ch);
if (ch == 'y')
{
while (pNew->iNumber != 0)
{
iCount++;
if (iCount == 1)
{
pNew->pNext = NULL;
pEnd = pNew;
pHead = pNew;
}
else
{
pNew->pNext = NULL;
pEnd->pNext = pNew;
pEnd = pNew;
}
pNew = (struct Student *)malloc(sizeof(struct Student));
printf("please input the student number\n");
scanf("%d", &pNew->iNumber);
printf("please input the student name\n");
scanf("%s", pNew->cName);
printf("please input the student math\n");
scanf("%d", &pNew->iMath);
printf("please input the student Chinese\n");
scanf("%d", &pNew->iChinese);
printf("please input the student English\n");
scanf("%d", &pNew->iEnglish);
}
free(pNew);
return pHead;
}
else
{
pNew->pNext = NULL;
pEnd = pNew;
pHead = pNew;
free(pNew);
return pHead;
}
}

void Print(struct Student *pHead, iCount)
{
struct Student *pTemp;
int iIndex = 1;
pTemp = pHead;
for (; iIndex <= iCount; iIndex++)
{
printf("the %d student", iIndex);
printf("number: %d\n", pTemp->iNumber);
printf("name: %s", pTemp->cName);
printf("math: %d", pTemp->iMath);
printf("Chinese: %d", pTemp->iChinese);
printf("English: %d\n", pTemp->iEnglish);
printf("\n");
pTemp = pTemp->pNext;
}
}

Delete(struct Student *pHead, count)
{
int i;
struct Student *pPre;
struct Student *pTemp;
pTemp = pHead;
pPre = pTemp;
for (i = 1; i < count; i++)
{
pPre = pTemp;
pTemp = pTemp->pNext;
}
pPre->pNext = pTemp->pNext;
free(pTemp);
iCount--;
}

struct Student *Insert(struct Student *pHead)
{
struct Student *pNew;
do
{
printf("input new student information\n");
pNew = (struct Student *)malloc(sizeof(struct Student));
printf("input new student number\n");
scanf("%d", &pNew->iNumber);
printf("input new student name\n");
scanf("%s", &pNew->cName);
printf("input new student math\n");
scanf("%d", &pNew->iMath);
printf("input new student Chinese\n");
scanf("%d", &pNew->iChinese);
printf("input new student English\n");
scanf("%d", &pNew->iEnglish);
pEnd->pNext = pNew;
pNew->pNext = NULL;
pEnd = pNew;
iCount++;
}
while (pNew->iNumber != 0);
return pHead;
}
void Print_80(struct Student *pHead)
{
int count;
struct Student *pTemp;
pTemp = pHead;
while (pTemp != NULL)
{
pTemp = pTemp->pNext;
count = (pTemp->iMath + pTemp->iChinese + pTemp->iEnglish;) / 3;
if (count >= 80)
printf("%d ", pTemp->iNumber); /* 超过80分的学生的学号 */
}
}

int main()
{
int iMum, iDel;
struct Student *pHead;
printf
("create student list 1\n delete student information 2\n insert student information 3\n print more than 80 of student 4\n exit 5\n");
while (1)
{
scanf("%d", &iMum);
switch (iMum)
{
case 1:
pHead = Create();
break;
case 2:
printf("input delete student number: ");
scanf("%d", &iDel);
Delete(pHead, iDel);
break;
case 3:
pHead = Insert(pHead);
break;
case 4:
Print_80();
break;
case 5:
printf("exit!\n");
exit(1);
default:
printf("error!\n");
}
}
return 0;
}
没调试过,不知有没有错。我是学生错了望原谅。。

回答2:

链表 +结构体,我上课也没听,那是迷宫都不会写。不过关键是自己要去主动学.

回答3:

你这傻X,这么简单的课堂作业也要问!想直接求代码吧?
自己动动脑子!不行直接问老师去,出这么白痴的题目。