單鏈表怎麼編寫一個學生成績表
㈠ 求:(C語言)用鏈表編寫學生考試成績單(其中包含學號,姓名,性別,出生年月,5門課程的成績)
struct students
{
char num[7];
char name[20];
int scores[3];
float ave;
float max;
};
main()
{
struct students stu[5];
int m,n,i=0;
float sum,max=0;
for(m=0;m<5;m++)
{
printf("input the No:%d student's:\n",m+1);
printf("school_num name:\n");
scanf("%s%s",stu[m].num,stu[m].name);
printf("Scores of subject:\n");
for(n=0;n<4;n++)
{
scanf("%d",&stu[m].scores[n]);
}
}
for(m=0;m<5;m++)
{
sum=0;
for(n=0;n<4;n++)
{
sum+=stu[m].scores[n];
stu[m].ave=sum/3.0;
}
printf("%s'saverage is %5.2f.\n",stu[m].name,stu[m].ave);
}
for(m=0;m<5;m++)
{
if(stu[m].ave>max)
{
max=stu[m].ave;
i=m;
}
}
printf("%s %s's the max is %5.2f.\n",stu[i].num,stu[i].name,stu[i].ave);
for(n=0;n<4;n++)
{
printf("%-5d",stu[i].scores[n]);
}
}
㈡ 編寫一個學生成績管理程序 c++ 鏈表
#include<iostream>
#include<string>
using namespace std;
//=============<開始定義結構體>===================================================
struct combox{
int num;
int mark;
string name;
combox *next;
};
//=============<結束定義結構體>===================================================
//=============<開始定義Commonbox類>==============================================
//-----類體開始------------------------
class Commonbox{
private:
combox *head;
void Swap(combox *,combox *); //交換兩個combox變數的數據域
void Print(combox *); //輸出一combox指定的記錄
combox *Find(int); //查找條例條件的記錄,並返回該記錄的指針
public:
Commonbox(){head=NULL;}
int ListCount(); //統計當前鏈表的記錄總數,返回一個整數
void AddItem(int num, string name, int mark); //添加一條記錄到表尾
void RemoveItem(int); //刪除一條指定的記錄
void List(); //列出當前鏈表中的所有記錄
void Sort(); //對當前鏈表進行排序
void Search(int); //在當前鏈表查找指定記錄並輸出
float Average(); //計算平均成績
};
//-----類體結束------------------------
//-----類成員函數開始----------------------------------
int Commonbox::ListCount(){//統計當前鏈表的記錄總數,返回一個整數
if(! head)return 0;
combox *p=head;
int n=0;
while(p){
n++;
p=p->next;
}
return n;
}
void Commonbox::AddItem(int num, string name, int mark){//添加一條記錄到表尾
if(! head){
head=new combox;
head->mark=mark;
head->num=num;
head->name=name;
head->next=NULL;
return;
}
combox *t=head;
while(t && t->num!=num)
t=t->next;
if(t){
cout<<"操作失敗:學號為"<<num<<"的記錄已經存在!"<<endl;
return;
}
combox *p=head;
while(p->next)p=p->next;
combox *p1=new combox;
p1->num=num;
p1->mark=mark;
p1->name=name;
p1->next=NULL;
p->next=p1;
return;
}
void Commonbox::RemoveItem(int num){//刪除一條指定的記錄
combox *t=Find(num);
if(! t)return;
combox *p=head;
//如果要刪除的記錄位於表頭
if(head==t){
head=head->next;
delete p;
cout <<"成功刪除學號為 "<<num<<" 的記錄!"<<endl<<endl;
return;
}
while(p->next!=t)p=p->next;
combox *p1=p->next;
p->next=p1->next;
delete p1;
cout <<"成功刪除學號為 "<<num<<" 的記錄!"<<endl<<endl;
return;
}
void Commonbox::Print(combox *p){//輸出一combox指定的記錄
cout<p->num<<"\t\t";
cout<<p->name<<"\t\t";
cout<<p->mark<<endl;
return;
}
void Commonbox::List(){//列出當前鏈表中的所有記錄
if (ListCount==0){
cout <"錯誤:當前的列表為空!"<<endl;
return;
}
combox *p=head;
cout<"共有記錄:"<<ListCount()<endl;
cout<"學號\t\t姓名\t\t分數"<<endl;
while(p){
Print(p);
p=p->next;
}
cout <<endl;
return;
}
void Commonbox::Search(int num){//在當前鏈表查找指定記錄並輸出
cout <"Searching...."<<endl;
combox *p=Find(num);
if(p){
cout<"學號\t\t姓名\t\t分數"<<endl;
Print(p);
}
cout <endl;
}
combox *Commonbox::Find(int num){
if (ListCount()==0){
cout <"錯誤:當前的列表為空!"<<endl;
return NULL;
}
combox *p=head;
while(p){
if(p->num==num)break;
p=p->next;
}
if(! p){
cout <<"錯誤:找不到該記錄!\n";
return NULL;
}
return p;
}
void Commonbox::Swap(combox *p1, combox *p2){//交換兩個combox變數的數據域
combox *temp=new combox;
temp->num=p1->num;
temp->mark=p1->mark;
temp->name=p1->name;
p1->num=p2->num;
p1->mark=p2->mark;
p1->name=p2->name;
p2->num=temp->num;
p2->mark=temp->mark;
p2->name=temp->name;
}
void Commonbox::Sort(){//對當前鏈表進行排序
cout <<"Sorting..."<<endl;
if(ListCount()2) return;
combox *temp=NULL,*p=NULL,*p1=NULL,*p2=NULL,*k=NULL;
int n=ListCount(),i,j;
p=head;
for(i=1;i<n;i++){
k=p;
p1=p->next;
for(j=0;j<n-i;j++){
if(k->num > p1->num){
k=p1;
}
p1=p1->next;
}
if(p!=k)Swap(k,p);
p=p->next;
}
cout <<"Complete successfully!"<<endl<<endl;
return;
}
float Commonbox::Average(){ //計算平均成績
if (ListCount()==0){
cout <"錯誤:當前的列表為空!"<<endl;
return -1;
}
int sum=0,n=0;
combox *p=head;
while(p){
sum += p->mark;
p=p->next;
n++;
}
return float(sum)/n;
}
//-----類成員函數結束----------------------------------
//=============<結束定義Commonbox類>==============================================
Commonbox student; //定義全局變數
int Menu(){
cout <<"===========[主選單:]==========="<<endl;
int n=1,select=-1;
cout <n++<".輸入學生成績;"<<endl<<endl;
cout <n++<".按學號排序;"<<endl<<endl;
cout <n++<".按學號查找記錄;"<<endl<<endl;
cout <n++<".刪除由學號指定的記錄;"<<endl<<endl;
cout <n++<".列出所有記錄;"<<endl<<endl;
cout <n++<".計算平均成績;"<<endl<<endl;
cout <"0.退出;"<<endl<<endl;
cout <"[請選擇(輸入相應數字)]:";
cin >>select;
return select;
}
char Exit(){ //返回一個字元患,用於確認退出
char s;
cout<<"確定要退出程序嗎?[Y/N]:";
cin >>s;
return s;
}
void Input(int *num, string *name, int *mark){ //輸入學生信息
cout <<"請輸入 學號 姓名 分數:";
cin >>*num;
if(*num==-1)return;
cin >>*name>>*mark;
return;
}
void AddNew(){ //增加記錄
int num=0,mark=0;
string name="";
cout<<endl<<"當輸入的學號為-1時表示結束輸入."<<endl;
Input(&num, &name, &mark);
while(num!=-1){
student.AddItem(num,name,mark);
Input(&num, &name, &mark);
}
return;
}
void DoFind(){ //按學號查找
int num;
cout<endl<<"當輸入的學號為-1時表示結束輸入."<<endl;
do{
cout <"請輸入要查找的學生的學號: ";
cin>>num;
if(num==-1)continue;
student.Search(num);
}while(num!=-1);
return;
}
void DoDelete(){ //刪除記錄
cout<<endl<<"當輸入的學號為-1時表示結束輸入."<<endl;
int num;
do{
cout <"請輸入要刪除的學生的學號:";
cin>>num;
if(num==-1)continue;
student.RemoveItem(num);
}while(num!=-1);
return;
}
void ShowAverage(){ //輸出平均數
float avr=student.Average();
if(avr>0){
cout<<"共有記錄:\t"<<student.ListCount()<endl<<endl;
cout<"平均成績:\t"<<avr<<endl<<endl;
}
return;
}
//-------******主函數開始>******-------
int main(){
cout<<"Welcome!\n學生成績管理系統\nVer 1.01\nBy FondBoy\n\n";
int select;
char s;
while(1){
select=Menu();
switch(select){
case 0: //退出程序
s=Exit();
if(s=='y' || s=='Y')return 0;
break;
case 1: //輸入學生成績
AddNew();
break;
case 2: //按學號排序
student.Sort();
break;
case 3: //按學號查找記錄
DoFind();
break;
case 4: //刪除由學號指定的記錄
DoDelete();
break;
case 5: //列出所有記錄
student.List();
break;
case 6: //輸出平均成績
ShowAverage();
break;
default:
cout<<"無效輸入!"<<endl;
}
}
return 0;
}
//-------******主函數結束>******-------
//------!!!!!!---------EOF-[程序代碼結束]-------------------
㈢ c語言:寫一個程序建立一個含有學生(學號成績)數據的單向動態鏈表(我是個初學者希望可以解釋清楚點)
1.n的存在沒必要,直接在循環外面將head指向p1
2.新建節點順序錯誤。你應該先用p2=malloc(…)分配空間,然後輸入數據,最後將p1的next指向p2,最後令p1=p2就行了。之後進行循環
㈣ 用c語言鏈表編寫一個學生信息系統程序,要求輸出學生的學號,姓名,性別,還有三門課比如語,數,外的成績
/*
用c語言鏈表編寫一個學生信息系統程序,要求輸出學生的學號,姓名,性別,還有三門課比如語,數,外的成績
*/
//FileName: stuinfo.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SERIALLEN 20
#define COURSENUM 3
typedef struct
{
char course[SERIALLEN];
float score;
}_courseInfo;
typedef struct _stuinfo
{
char serial[SERIALLEN];
char name[SERIALLEN];
char sex[SERIALLEN];
_courseInfo courseInfo[COURSENUM];
struct _stuinfo *next;
}stuinfo;
int main(int argc, char **argv)
{
stuinfo *head=NULL,*ptr=NULL,*s=NULL;
char str[SERIALLEN];
int cycle=1;
int i=0;
memset(str,0,SERIALLEN);
printf("建立學生信息:\n");
head=(stuinfo *)calloc(1,sizeof(stuinfo));
if(!head)
{
perror("申請空間失敗,沒有足夠內存。");
return -1;
}
ptr=head;
while(cycle)
{
puts("輸入學生學號(0退出):");
scanf("%s",str);
if(strcmp(str,"0")) //如果學號為0,則退出鏈表的創建
{
s=(stuinfo *)calloc(1,sizeof(stuinfo));
if(!ptr)
{
perror("申請空間失敗,沒有足夠內存。");
return -1;
}
memset(s->serial,0,SERIALLEN);
strcpy(s->serial,str);
memset(s->name,0,SERIALLEN);
puts("輸入姓名:");
scanf("%s",s->name);
memset(s->sex,0,SERIALLEN);
puts("輸入性別:");
scanf("%s",s->sex);
for(i=0;i<COURSENUM;i++)
{
memset(s->courseInfo[i].course,0,SERIALLEN);
puts("輸入課程名稱:");
scanf("%s",s->courseInfo[i].course);
s->courseInfo[i].score=0.0f;
puts("輸入課程分數:");
scanf("%f",&(s->courseInfo[i].score));
}
ptr->next=s;
ptr=s;
}
else cycle=0;
}
ptr->next=NULL;
ptr=head;
head=head->next;
free(ptr);
//head=linkSort(head);
ptr=head;
printf("學號\t姓名\t性別");
for(i=0;i<COURSENUM;i++)
printf("\t課程[%d]",i);
printf("\n");
while(ptr!=NULL)
{
printf("%s\t%s\t%s",ptr->serial,ptr->name,ptr->sex);
for(i=0;i<COURSENUM;i++)
printf("\t%s[%.2f]",ptr->courseInfo[i].course,ptr->courseInfo[i].score);
printf("\n");
ptr=ptr->next;
}
return 0;
}
C:\mypro>gcc -g -Wall student.c -o student
C:\mypro>student
建立學生信息:
輸入學生學號(0退出):
007
輸入姓名:
zxsh
輸入性別:
male
輸入課程名稱:
chinese
輸入課程分數:
99
輸入課程名稱:
phy
輸入課程分數:
100
輸入課程名稱:
english
輸入課程分數:
98
輸入學生學號(0退出):
002
輸入姓名:
pipal
輸入性別:
female
輸入課程名稱:
chem
輸入課程分數:
98
輸入課程名稱:
math
輸入課程分數:
97
輸入課程名稱:
chinese
輸入課程分數:
100
輸入學生學號(0退出):
0
學號 姓名 性別 課程[0] 課程[1] 課程[2]
007 zxsh male chinese[99.00] phy[100.00] english[98.00]
002 pipal female chem[98.00] math[97.00] chinese[100.00]
C:\mypro>
㈤ 編寫一個學生成績的鏈表,學生成績包括:學號、姓名、成績等三個數據項,是一個復合數據類型。
#include <iostream>
using namespace std;
typedef struct Student
{ //構造一個學生信息的特殊數據類型。
char name[10];
char id[20];
double sorce;
Student *next;
}Student,*StudentLink;
void CreateStudent_S(StudentLink &S,int n=10)
{ //創造一個默認10個學生的鏈表。
int i=0;
StudentLink p;
cout<<"請你輸入"<<n<<"個學生的信息"<<endl;
S=(StudentLink)malloc(sizeof(Student));//創造一個空節點。
S->next=NULL;
while(i<n)
{
p=(StudentLink)malloc(sizeof(Student));//創造一個新的結點。
cout<<"請輸入一學生的姓名:"<<endl;
cin>>p->name;
cout<<"請輸入一學生的學號:"<<endl;
cin>>p->id;
cout<<"請輸入一學生的成績:"<<endl;
cin>>p->sorce;
p->next=S->next;
S->next=p;
i++;
}
}
void CompareSorce(StudentLink S)
{ //比較各個同學成績大小,最大者賦值給max,最小的賦值給min.
double max=S->next->sorce,min=S->next->sorce;//最大值和最小值初始值為第一位學生。
while(S->next!=NULL)
{
S=S->next;
if(S->sorce>max)//依次比較各個同學成績大小,較大者賦值給max,
max=S->sorce;
if(S->sorce<min)//依次比較各個同學成績大小,較小者賦值給mix,
min=S->sorce;
}
cout<<"這學生10個學生的最高的成績是:\n"<<max<<endl;
cout<<"這學生10個學生的最低的成績是:\n"<<min<<endl;
}
void AverageSorce(StudentLink S)
{
double total,average;
while(S->next!=NULL)//計算這十個學生的總分數。
{
S=S->next;
total+=S->sorce;
}
average=total/10;
cout<<"這十個學生的平均成績為:average="<<average<<endl;
}
void main()
{
StudentLink S;
CreateStudent_S(S);
CompareSorce(S);
AverageSorce(S);
}
不懂之處,可以於我溝通,我的郵箱是[email protected]。
㈥ 數據結構單鏈表應用:編寫一個完整的C++語言程序,輸入信息:學生的學號,姓名,成績。(要簡單明了)
給你個簡單的例子,雖然不是你想要的,你可以把這個看懂,然後改改就可以了
// 鏈表e.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string.h>
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
/*---------------------結構體定義部分------------------------------*/
struct Node
{
char name[10];
int score;
struct Node *next;
};
typedef struct Node ListNode;
/*---------------------函數聲明部分-----------------------------------*/
ListNode* CreateNode(int n);
void PrinfListNode(ListNode *h);
void InsertList(ListNode *h,int i,char name[],int e,int n);
void DeleteList(ListNode *h,int i,int n);
void DisplayNote(void);
/*---------------------函數實現部分-----------------------------------*/
int main(int argc, char* argv[])
{
ListNode *h;
int i=1,n,score;
char name[10];
int *m=0;
cout<<m<<endl;
while(i)
{
DisplayNote();
scanf("%d",&i);
switch(i)
{
case 1:
printf("Please input the number of the list\n");
scanf("%d",&n);
h=CreateNode(n);
printf("List elements is:\n");
PrinfListNode(h);
break;
case 2:
printf("Please input the position of the element:");
scanf("%d",&i);
printf("Please input name of the student:");
scanf("%s",&name);
printf("Please input score of the student:");
scanf("%d",&score);
InsertList(h,i,name,score,n);
printf("List elements is:\n");
PrinfListNode(h);
break;
case 3:
printf("Please input the position of the element:");
scanf("%d",&i);
DeleteList(h,i,n);
printf("List elements is:\n");
PrinfListNode(h);
break;
case 4:
printf("List elements is:\n");
PrinfListNode(h);
break;
case 0:
return 0;
break;
default:
printf("ERROR! Please try again");
}
}
return 0;
}
/*-----------------建立鏈表函數實現部分-----------------------------------*/
ListNode *CreateNode(int n)
{
ListNode *head;
ListNode *pre,*p;
int i;
head=(ListNode*)malloc(sizeof(ListNode));
head->next=NULL;
pre=head;
for (i=1;i<=n;i++)
{
printf("Please input name of the %d student:",i);
p=(ListNode*)malloc(sizeof(ListNode));
scanf("%s",&p->name);
printf("Please input score of the %d student:",i);
scanf("%d",&p->score);
pre->next=p;
pre=p;
}
p->next=NULL;
return head;
}
/*-----------------輸出鏈表函數實現部分-----------------------------------*/
void PrinfListNode(ListNode *h)
{
ListNode *p;
p=h->next;
while(p)
{
printf("name:%s\tscore:%d",p->name,p->score);
p=p->next;
printf("\n");
}
}
/*-----------------插入鏈表函數實現部分-----------------------------------*/
void InsertList(ListNode *h,int i,char name[],int e,int n)
{
ListNode *p,*q;
int j;
if (i<1||i>n+1)
{
printf("Error ! Please input again.\n");
}
else
{
j=0;p=h;
while(j<i-1)
{
p=p->next;
j++;
}
q=(ListNode *)malloc(sizeof(ListNode));
strcpy(q->name,name);
q->score=e;
q->next=p->next;
p->next=q;
}
}
/*-----------------刪除鏈表函數實現部分-----------------------------------*/
void DeleteList(ListNode *h,int i,int n)
{
ListNode *p,*q;
int j;
char name[10];
int score;
if (i<1||i>n)
{
printf("Error ! Please input again.\n");
}
else
{
j=0;p=h;
while(j<i-1)
{
p=p->next;
j++;
}
q=p->next;
p->next=q->next;
strcpy(name,q->name);
score=q->score;
free(q);
printf("name=%s,score=%d",name,score);
}
}
void DisplayNote(void)
{
printf("1--建立新的鏈表\n");
printf("2--添加元素\n");
printf("3--刪除元素\n");
printf("4--輸出當前鏈表中的內容\n");
printf("0--建立新的鏈表\n");
}
㈦ 需要一個用(c語言)鏈表編寫的學生成績管理系統
你上面要求的基本功能大多都有,但根據性別這一功能你自己改一下就行了。
下載的網盤地址:網頁鏈接pan..com/s/1c1740u8
㈧ C語言 建立一個動態學生成績的鏈表,
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int shoudsave=0; /* */
struct student
{
char() num[10];/* 學號 */
char() name[20];
char() sex[4];
int cgrade;
int mgrade;
int egrade;
int totle;
int ave;
char neartime[10];/* 最近更新時間 */
};
typedef struct node
{
struct student data;
struct node *next;
}Node,*Link;
void menu()
{
printf("********************************************************************************");
printf("\t1登記學生資料\t\t\t\t\t2刪除學生資料\n");
printf("\t3查詢學生資料\t\t\t\t\t4修改學生資料\n");
printf("\t5保存學生資料\t\t\t\t\t0退出系統\n");
printf("********************************************************************************\n");
}
void printstart()
{
printf("-----------------------------------------------------------------------\n");
}
void Wrong()
{
printf("\n=====>提示:輸入錯誤!\n");
}
void Nofind()
{
printf("\n=====>提示:沒有找到該學生!\n");
}
void printc() /* 本函數用於輸出中文 */
{
printf(" 學號\t 姓名 性別 英語成績 數學成績 C語言成績 總分 平均分\n");
}
void printe(Node *p)/* 本函數用於輸出英文 */
{
printf("%-12s%s\t%s\t%d\t%d\t%d\t %d\t %d\n",p->data.num,p->data.name,p->data.sex,p->data.egrade,p->data.mgrade,p->data.cgrade,p->data.totle,p->data.ave);
}
Node* Locate(Link l,char findmess[],char nameornum[]) /* 該函數用於定位連表中符合要求的接點,並返回該指針 */
{
Node *r;
if(strcmp(nameornum,"num")==0) /* 按學號查詢 */
{
r=l->next;
while(r!=NULL)
{
if(strcmp(r->data.num,findmess)==0)
return r;
r=r->next;
}
}
else if(strcmp(nameornum,"name")==0) /* 按姓名查詢 */
{
r=l->next;
while(r!=NULL)
{
if(strcmp(r->data.name,findmess)==0)
return r;
r=r->next;
}
}
return 0;
}
void Add(Link l) /* 增加學生 */
{
Node *p,*r,*s;
char num[10];
r=l;
s=l->next;
while(r->next!=NULL)
r=r->next; /* 將指針置於最末尾 */
while(1)
{
printf("請你輸入學號(以'0'返回上一級菜單:)");
scanf("%s",num);
if(strcmp(num,"0")==0)
break;
while(s)
{
if(strcmp(s->data.num,num)==0)
{
printf("=====>提示:學號為'%s'的學生已經存在,若要修改請你選擇'4 修改'!\n",num);
printstart();
printc();
printe(s);
printstart();
printf("\n");
return;
}
s=s->next;
}
p=(Node *)malloc(sizeof(Node));
strcpy(p->data.num,num);
printf("請你輸入姓名:");
scanf("%s",p->data.name);
getchar()();
printf("請你輸入性別:");
scanf("%s",p->data.sex);
getchar()();
printf("請你輸入c語言成績:");
scanf("%d",&p->data.cgrade);
getchar()();
printf("請你輸入數學成績:");
scanf("%d",&p->data.mgrade);
getchar();
printf("請你輸入英語成績:");
scanf("%d",&p->data.egrade);
getchar();
p->data.totle=p->data.egrade+p->data.cgrade+p->data.mgrade;
p->data.ave=p->data.totle / 3;
/* 信息輸入已經完成 */
p->next=NULL;
r->next=p;
r=p;
shoudsave=1;
}
}
void Qur(Link l) /* 查詢學生 */
{
int sel;
char findmess[20];
Node *p;
if(!l->next)
{
printf("\n=====>提示:沒有資料可以查詢!\n");
return;
}
printf("\n=====>1按學號查找\n=====>2按姓名查找\n");
scanf("%d",&sel);
if(sel==1)/* 學號 */
{
printf("請你輸入要查找的學號:");
scanf("%s",findmess);
p=Locate(l,findmess,"num");
if(p)
{
printf("\t\t\t\t查找結果\n");
printstart();
printc();
printe(p);
printstart();
}
else
Nofind();
}
else if(sel==2) /* 姓名 */
{
printf("請你輸入要查找的姓名:");
scanf("%s",findmess);
p=Locate(l,findmess,"name");
if(p)
{
printf("\t\t\t\t查找結果\n");
printstart();
printc();
printe(p);
printstart();
}
else
Nofind();
}
else
Wrong();
}
void Del(Link l) /* 刪除 */
{
int sel;
Node *p,*r;
char findmess[20];
if(!l->next)
{
printf("\n=====>提示:沒有資料可以刪除!\n");
return;
}
printf("\n=====>1按學號刪除\n=====>2按姓名刪除\n");
scanf("%d",&sel);
if(sel==1)
{
printf("請你輸入要刪除的學號:");
scanf("%s",findmess);
p=Locate(l,findmess,"num");
if(p)
{
r=l;
while(r->next!=p)
r=r->next;
r->next=p->next;
free(p);
printf("\n=====>提示:該學生已經成功刪除!\n");
shoudsave=1;
}
else
Nofind();
}
else if(sel==2)
{
printf("請你輸入要刪除的姓名:");
scanf("%s",findmess);
p=Locate(l,findmess,"name");
if(p)
{
r=l;
while(r->next!=p)
r=r->next;
r->next=p->next;
free(p);
printf("\n=====>提示:該學生已經成功刪除!\n");
shoudsave=1;
}
else
Nofind();
}
else
Wrong();
}
void Modify(Link l)
{
Node *p;
char findmess[20];
if(!l->next)
{
printf("\n=====>提示:沒有資料可以修改!\n");
return;
}
printf("請你輸入要修改的學生學號:");
scanf("%s",findmess);
p=Locate(l,findmess,"num");
if(p)
{
printf("請你輸入新學號(原來是%s):",p->data.num);
scanf("%s",p->data.num);
printf("請你輸入新姓名(原來是%s):",p->data.name);
scanf("%s",p->data.name);
getchar();
printf("請你輸入新性別(原來是%s):",p->data.sex);
scanf("%s",p->data.sex);
printf("請你輸入新的c語言成績(原來是%d分):",p->data.cgrade);
scanf("%d",&p->data.cgrade);
getchar();
printf("請你輸入新的數學成績(原來是%d分):",p->data.mgrade);
scanf("%d",&p->data.mgrade);
getchar();
printf("請你輸入新的英語成績(原來是%d分):",p->data.egrade);
scanf("%d",&p->data.egrade);
p->data.totle=p->data.egrade+p->data.cgrade+p->data.mgrade;
p->data.ave=p->data.totle/3;
printf("\n=====>提示:資料修改成功!\n");
shoudsave=1;
}
else
Nofind();
}
void Disp(Link l)
{
int count=0;
Node *p;
p=l->next;
if(!p)
{
printf("\n=====>提示:沒有資料可以顯示!\n");
return;
}
printf("\t\t\t\t顯示結果\n");
printstart();
printc();
printf("\n");
while(p)
{
printe(p);
p=p->next;
}
printstart();
printf("\n");
}
void Tongji(Link l)
{
Node *pm,*pe,*pc,*pt,*pa; /* 用於指向分數最高的接點 */
Node *r=l->next;
if(!r)
{
printf("\n=====>提示:沒有資料可以統計!\n");
return ;
}
pm=pe=pc=pt=pa=r;
while(r!=NULL)
{
if(r->data.cgrade>=pc->data.cgrade)
pc=r;
if(r->data.mgrade>=pm->data.mgrade)
pm=r;
if(r->data.egrade>=pe->data.egrade)
pe=r;
if(r->data.totle>=pt->data.totle)
pt=r;
if(r->data.ave>=pa->data.ave)
pa=r;
r=r->next;
}
printf("------------------------------統計結果--------------------------------\n");
printf("總分最高者:\t%s %d分\n",pt->data.name,pt->data.totle);
printf("平均分最高者:\t%s %d分\n",pa->data.name,pa->data.ave);
printf("英語最高者:\t%s %d分\n",pe->data.name,pe->data.egrade);
printf("數學最高者:\t%s %d分\n",pm->data.name,pm->data.mgrade);
printf("c語言最高者:\t%s %d分\n",pc->data.name,pc->data.cgrade);
printstart();
}
void Sort(Link l)
{
Link ll;
Node *p,*rr,*s;
ll=(Link)malloc(sizeof(Node)); /* 用於做新的連表 */
ll->next=NULL;
if(l->next==NULL)
{
printf("\n=====>提示:沒有資料可以排序!\n");
return ;
}
p=l->next;
while(p)
{
s=(Node*)malloc(sizeof(Node)); /* 新建接點用於保存信息 */
s->data=p->data;
s->next=NULL;
rr=ll;
while(rr->next!=NULL && rr->next->data.totle>=p->data.totle)
rr=rr->next;
if(rr->next==NULL)
rr->next=s;
else
{
s->next=rr->next;
rr->next=s;
}
p=p->next;
}
free(l);
l->next=ll->next;
printf("\n=====>提示:排序已經完成!\n");
}
void Save(Link l)
{
FILE* fp;
Node *p;
int flag=1,count=0;
fp=fopen("c:\\student","wb");
if(fp==NULL)
{
printf("\n=====>提示:重新打開文件時發生錯誤!\n");
exit(1);
}
p=l->next;
while(p)
{
if(fwrite(p,sizeof(Node),1,fp)==1)
{
p=p->next;
count++;
}
else
{
flag=0;
break;
}
}
if(flag)
{
printf("\n=====>提示:文件保存成功.(有%d條記錄已經保存.)\n",count);
shoudsave=0;
}
fclose(fp);
}
void main()
{
Link l;/* 連表 */
FILE *fp; /* 文件指針 */
int sel;
char ch;
char jian;
int count=0;
Node *p,*r;
printf("\t\t\t\t學生成績管理系統\n\t\t\t\t-------福建農業職業學院計應0501 黃歡(32號)\n");
l=(Node*)malloc(sizeof(Node));
l->next=NULL;
r=l;
fp=fopen("C:\\student","rb");
if(fp==NULL)
{
printf("\n=====>提示:文件還不存在,是否創建?(y/n)\n");
scanf("%c",&jian);
if(jian=='y'||jian=='Y')
fp=fopen("C:\\student","wb");
else
exit(0);
}
printf("\n=====>提示:文件已經打開,正在導入記錄......\n");
while(!feof(fp))
{
p=(Node*)malloc(sizeof(Node));
if(fread(p,sizeof(Node),1,fp)) /* 將文件的內容放入接點中 */
{
p->next=NULL;
r->next=p;
r=p; /* 將該接點掛入連中 */
count++;
}
}
fclose(fp); /* 關閉文件 */
printf("\n=====>提示:記錄導入完畢,共導入%d條記錄.\n",count);
while(1)
{
menu();
printf("請你選擇操作:");
scanf("%d",&sel);
if(sel==0)
{
if(shoudsave==1)
{ getchar();
printf("\n=====>提示:資料已經改動,是否將改動保存到文件中(y/n)?\n");
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
Save(l);
}
printf("\n=====>提示:你已經退出系統,再見!\n");
break;
}
switch(sel)
{
case 1:Add(l);break; /* 增加學生 */
case 2:Del(l);break;/* 刪除學生 */
case 3:Qur(l);break;/* 查詢學生 */
case 4:Modify(l);break;/* 修改學生 */
case 5:Save(l);break;/* 保存學生 */
case 9:printf("\t\t\t==========幫助信息==========\n");break;
default: Wrong();getchar();break;
}
}
}
㈨ 請用一個單鏈表來保存學生數據,鏈表中的每個結點包含的數據信息有學生的學號和一門課的成績。編寫一個crea
#include<stdio.h>
#include<stdlib.h>
struct stud
{
int num;
float score;
struct stud *next;
};
int main()
{
struct stud *create();
void print(struct stud *head);
struct stud *head;
head=create();
print(head);
}
struct stud *create()
{
struct stud *head,*p1,*p2;
int n=0;
head = NULL;
p2 = p1 = (struct stud*)malloc(sizeof(struct stud));
printf("Please input student's num and score:\n");
scanf("%d",&p1->num ); //修改的地方
if( p1->num == 0 ){ p2->next=NULL;return head;}
scanf("%f",&p1->score);
while(p1->num !=0)
{
n=n+1;
if(n==1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct stud*)malloc(sizeof(struct stud));
scanf("%d",&p1->num ); //修改的地方
if( p1->num == 0 ){ p2->next=NULL;return head;}
scanf("%f",&p1->score);
}
p2->next =NULL;
return head;
}
void print(struct stud *head)
{
struct stud *p;
p=head;
printf("Output student's num and score:\n");
while(p!=NULL)
{
printf("%-5d%5.1f\n",p->num ,p->score );
p=p->next ;
}
}
㈩ C語言編程 編寫程序,建立一個學生數據鏈表,學生的數據包括學號、姓名、成績。
#include<stdio.h>
#include<malloc.h>
#define NULL 0
struct stud
{
int no;
char name[12];
int age;
struct stud*next;
};
#define LEN sizeof(struct stud)
struct stud*creat(void)
{
struct stud*p1,*p2,*head;
int n=0;
p1=(struct stud*)malloc(LEN);
scanf("%d%s%d",&p1->no,p1->name,&p1->age);
while(p1->no>0){
n++;
if(n==1){head=p1;p2=p1;}
else{p2->next=p1;p2=p1;}
p1=(struct stud*)malloc(LEN);
scanf("%d",&p1->no);
if(p1->no==0)break;
scanf("%s%d",p1->name,&p1->age);
}
p2->next=NULL;
return head;
}
void main()
{
struct stud*del(struct stud*head,int no);
int num;
struct stud*p,*p0;
p0=creat();
scanf("%d",&num);
p=del(p0,num);
print(p);
}
struct stud*del(struct stud*head,int num)
{
struct stud *p1,*p2;
p1=head;
while(p1->next!=NULL){
if(p1->no==num) break;
p2=p1;
p1=p1->next;
}
if(p1==head){
p1->next=head;
}
else {
p2->next=p1->next;
}
return head;
}