设计题目:用C语言实现成绩统计程序的设计 〔问题描述〕给出n个学生的
答案:1 悬赏:10 手机版
解决时间 2021-11-10 20:07
- 提问者网友:鐵馬踏冰河
- 2021-11-10 10:24
设计题目:用C语言实现成绩统计程序的设计 〔问题描述〕给出n个学生的
最佳答案
- 五星知识达人网友:梦中风几里
- 2021-11-10 11:51
以前做过一个差不多的程序(不过是用链表实现的) 跟你稍微改了下
用了3个文件.(改成一个也很简单的^_^)
希望你用的上吧^_^
文件list.h
#ifndef _LIST_H
#define _LIST_H
struct Student
{
char szName[20]; // 姓名
int nScore1; // 科目1
int nScore; // 科目2
int nNumber; //平均分
////
Student* pNext; // 指针
};
// 声明链表头指针
extern Student* g_pHead;
Student* CreateNode();
void AddToTail(Student* pNew);
void AddToHead(Student* pNew);
void InsertNode(Student* pNew);
void DeleteNode(int nNumber);
Student* FindNode(int nNumber);
void ShowList();
void FreeList();
void Save();
void Load();
void Init();
文件list.cpp
#include
#include "List.h"
// 定义链表头指针
Student* g_pHead = NULL;
//------------------------------
Student* CreateNode()
{
printf("请输入姓名,科目1,科目2:");
Student* pNew = new Student;
scanf("%s",pNew->szName);
scanf("%d",&pNew->nScore1);
scanf("%d",&pNew->nScore);
pNew->nNumber = (pNew->nScore1 + pNew->nScore)/2;
pNew->pNext = NULL;
return pNew;
}
//------------------------------
void AddToTail(Student* pNew)
{
if (g_pHead == NULL) // 空链表
{
g_pHead = pNew;
}
else // 非空链表
{
Student* pTail = g_pHead;
while (pTail->pNext != NULL)
{
pTail = pTail->pNext;
}
pTail->pNext = pNew;
}
}
//------------------------------
void AddToHead(Student* pNew)
{
if (g_pHead == NULL) // 空链表
{
g_pHead = pNew;
}
else // 非空链表
{
pNew->pNext = g_pHead;
g_pHead = pNew;
}
}
//------------------------------
void InsertNode(Student* pNew)
{
if (g_pHead == NULL) // 空链表
{
g_pHead = pNew;
}
else if (pNew->nNumber > g_pHead->nNumber) // 比头节点小
{
pNew->pNext = g_pHead;
g_pHead = pNew;
}
else // 其他非空普通情况
{
Student* pCur = g_pHead;
while (pCur->pNext != NULL)
{
if (pCur->nNumber >= pNew->nNumber &&
pCur->pNext->nNumber < pNew->nNumber)
{
// 找到了,在此处插入.
pNew->pNext = pCur->pNext;
pCur->pNext = pNew;
break;
}
else
{
pCur = pCur->pNext;
}
} // end of while.
// 处理比尾节点还大的情况
if ((pCur->pNext == NULL) // 必须的!!!
&&(pNew->nNumber < pCur->nNumber))
{
pCur->pNext = pNew;
}
} // end of if-else.
}
//------------------------------
void DeleteNode(int nNumber)
{
Student* pCur = g_pHead;
Student* pPre = g_pHead;
while (pCur != NULL)
{
if (pCur->nNumber == nNumber)
{
// 找到了,删除他.
pPre->pNext = pCur->pNext;
if (pCur == g_pHead)
{
g_pHead = pPre->pNext;
delete pCur;
}
else
{
delete pCur;
}
break; // !!!
}
else
{
pPre = pCur; // !!!
pCur = pCur->pNext;
}
}
}
//------------------------------
Student* FindNode(int nNumber)
{
Student* pCur = g_pHead;
while (pCur != NULL)
{
if (pCur->nNumber == nNumber)
break;
pCur = pCur->pNext;
}
return pCur;
}
//------------------------------
void ShowList()
{
Student* pTail = g_pHead;
while(pTail != NULL)
{
printf("姓名:%s,科目1:%d,科目2:%d,平均分:%d\n",
pTail->szName,
pTail->nScore1,
pTail->nScore,
pTail->nNumber);
pTail = pTail->pNext;
}
}
//------------------------------
void FreeList()
{
Student* pTail = g_pHead;
while (pTail != NULL)
{
Student* pTemp = pTail->pNext;
delete pTail;
pTail = pTemp;
}
g_pHead = NULL;
}
//------------------------------
void Save()
{
FILE* pf = fopen("student.txt", "w");
Student* pTail = g_pHead;
while(pTail != NULL)
{
fprintf(pf, "%s %d %d %d\n",
pTail->szName,
pTail->nScore1,
pTail->nScore,
pTail->nNumber);
pTail = pTail->pNext;
}
fclose(pf);
}
//------------------------------
void Load()
{
FILE* pf = fopen("student.txt", "r");
while(NULL != pf)
{
Student* pNew = new Student;
pNew->pNext = NULL;
fscanf(pf, "%s",pNew->szName);
fscanf(pf, "%d",&pNew->nScore1);
fscanf(pf, "%d",&pNew->nScore);
int nRes = fscanf(pf, "%d",&pNew->nNumber);
if(nRes == EOF)break;
InsertNode(pNew);
}
fclose(pf);
}
//------------------------------
void Init()
{
printf("\t\t学员管理系统\n\n");
printf("\t1.新增学员\n");
printf("\t2.删除学员\n");
printf("\t3.查找学员\n");
printf("\t4.显示全部学员\n");
printf("\t5.保存到文件\n");
printf("\t6.从文件中读取\n\n");
}
文件main.cpp
#include
#include "list.h"
void main(void)
{
int nN = 0; //菜单选择
int nNumber = 0;
Student* FTemp, *pNew;
Init();
while(1)
{
printf("请选择操作:** **\b\b\b");
scanf("%d", &nN);
switch(nN)
{
case 1:
pNew = CreateNode();
InsertNode(pNew);
printf("\n");
break;
case 2:
printf("请输入欲删除的学生的平均分:");
scanf("%d", &nNumber);
DeleteNode(nNumber);
printf("删除完毕\n\n");
break;
case 3:
printf("请输入欲查找的学生的平均分:");
scanf("%d", &nNumber);
FTemp = FindNode(nNumber);
if( FTemp != NULL )
{
printf("姓名:%s 科目1:%d 科目2:%d 平均分\n:%d",
FTemp->szName,
FTemp->nScore1,
FTemp->nScore,
FTemp->nNumber);
printf("\n");
}
break;
case 4:
ShowList();
printf("\n");
break;
case 5:
Save();
printf("保存完毕\n\n");
break;
case 6:
Load();
printf("读取完毕\n\n");
break;
}
}
}
用了3个文件.(改成一个也很简单的^_^)
希望你用的上吧^_^
文件list.h
#ifndef _LIST_H
#define _LIST_H
struct Student
{
char szName[20]; // 姓名
int nScore1; // 科目1
int nScore; // 科目2
int nNumber; //平均分
////
Student* pNext; // 指针
};
// 声明链表头指针
extern Student* g_pHead;
Student* CreateNode();
void AddToTail(Student* pNew);
void AddToHead(Student* pNew);
void InsertNode(Student* pNew);
void DeleteNode(int nNumber);
Student* FindNode(int nNumber);
void ShowList();
void FreeList();
void Save();
void Load();
void Init();
文件list.cpp
#include
#include "List.h"
// 定义链表头指针
Student* g_pHead = NULL;
//------------------------------
Student* CreateNode()
{
printf("请输入姓名,科目1,科目2:");
Student* pNew = new Student;
scanf("%s",pNew->szName);
scanf("%d",&pNew->nScore1);
scanf("%d",&pNew->nScore);
pNew->nNumber = (pNew->nScore1 + pNew->nScore)/2;
pNew->pNext = NULL;
return pNew;
}
//------------------------------
void AddToTail(Student* pNew)
{
if (g_pHead == NULL) // 空链表
{
g_pHead = pNew;
}
else // 非空链表
{
Student* pTail = g_pHead;
while (pTail->pNext != NULL)
{
pTail = pTail->pNext;
}
pTail->pNext = pNew;
}
}
//------------------------------
void AddToHead(Student* pNew)
{
if (g_pHead == NULL) // 空链表
{
g_pHead = pNew;
}
else // 非空链表
{
pNew->pNext = g_pHead;
g_pHead = pNew;
}
}
//------------------------------
void InsertNode(Student* pNew)
{
if (g_pHead == NULL) // 空链表
{
g_pHead = pNew;
}
else if (pNew->nNumber > g_pHead->nNumber) // 比头节点小
{
pNew->pNext = g_pHead;
g_pHead = pNew;
}
else // 其他非空普通情况
{
Student* pCur = g_pHead;
while (pCur->pNext != NULL)
{
if (pCur->nNumber >= pNew->nNumber &&
pCur->pNext->nNumber < pNew->nNumber)
{
// 找到了,在此处插入.
pNew->pNext = pCur->pNext;
pCur->pNext = pNew;
break;
}
else
{
pCur = pCur->pNext;
}
} // end of while.
// 处理比尾节点还大的情况
if ((pCur->pNext == NULL) // 必须的!!!
&&(pNew->nNumber < pCur->nNumber))
{
pCur->pNext = pNew;
}
} // end of if-else.
}
//------------------------------
void DeleteNode(int nNumber)
{
Student* pCur = g_pHead;
Student* pPre = g_pHead;
while (pCur != NULL)
{
if (pCur->nNumber == nNumber)
{
// 找到了,删除他.
pPre->pNext = pCur->pNext;
if (pCur == g_pHead)
{
g_pHead = pPre->pNext;
delete pCur;
}
else
{
delete pCur;
}
break; // !!!
}
else
{
pPre = pCur; // !!!
pCur = pCur->pNext;
}
}
}
//------------------------------
Student* FindNode(int nNumber)
{
Student* pCur = g_pHead;
while (pCur != NULL)
{
if (pCur->nNumber == nNumber)
break;
pCur = pCur->pNext;
}
return pCur;
}
//------------------------------
void ShowList()
{
Student* pTail = g_pHead;
while(pTail != NULL)
{
printf("姓名:%s,科目1:%d,科目2:%d,平均分:%d\n",
pTail->szName,
pTail->nScore1,
pTail->nScore,
pTail->nNumber);
pTail = pTail->pNext;
}
}
//------------------------------
void FreeList()
{
Student* pTail = g_pHead;
while (pTail != NULL)
{
Student* pTemp = pTail->pNext;
delete pTail;
pTail = pTemp;
}
g_pHead = NULL;
}
//------------------------------
void Save()
{
FILE* pf = fopen("student.txt", "w");
Student* pTail = g_pHead;
while(pTail != NULL)
{
fprintf(pf, "%s %d %d %d\n",
pTail->szName,
pTail->nScore1,
pTail->nScore,
pTail->nNumber);
pTail = pTail->pNext;
}
fclose(pf);
}
//------------------------------
void Load()
{
FILE* pf = fopen("student.txt", "r");
while(NULL != pf)
{
Student* pNew = new Student;
pNew->pNext = NULL;
fscanf(pf, "%s",pNew->szName);
fscanf(pf, "%d",&pNew->nScore1);
fscanf(pf, "%d",&pNew->nScore);
int nRes = fscanf(pf, "%d",&pNew->nNumber);
if(nRes == EOF)break;
InsertNode(pNew);
}
fclose(pf);
}
//------------------------------
void Init()
{
printf("\t\t学员管理系统\n\n");
printf("\t1.新增学员\n");
printf("\t2.删除学员\n");
printf("\t3.查找学员\n");
printf("\t4.显示全部学员\n");
printf("\t5.保存到文件\n");
printf("\t6.从文件中读取\n\n");
}
文件main.cpp
#include
#include "list.h"
void main(void)
{
int nN = 0; //菜单选择
int nNumber = 0;
Student* FTemp, *pNew;
Init();
while(1)
{
printf("请选择操作:** **\b\b\b");
scanf("%d", &nN);
switch(nN)
{
case 1:
pNew = CreateNode();
InsertNode(pNew);
printf("\n");
break;
case 2:
printf("请输入欲删除的学生的平均分:");
scanf("%d", &nNumber);
DeleteNode(nNumber);
printf("删除完毕\n\n");
break;
case 3:
printf("请输入欲查找的学生的平均分:");
scanf("%d", &nNumber);
FTemp = FindNode(nNumber);
if( FTemp != NULL )
{
printf("姓名:%s 科目1:%d 科目2:%d 平均分\n:%d",
FTemp->szName,
FTemp->nScore1,
FTemp->nScore,
FTemp->nNumber);
printf("\n");
}
break;
case 4:
ShowList();
printf("\n");
break;
case 5:
Save();
printf("保存完毕\n\n");
break;
case 6:
Load();
printf("读取完毕\n\n");
break;
}
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯