當前位置:編程學習大全網 - 編程語言 - C語言編程,新手啊,求大神。

C語言編程,新手啊,求大神。

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <windows.h>

#define N 30

#define MAX_LEN 10

struct Student

{

long num; /*學號*/

char name[MAX_LEN];/*姓名*/

float score;/*課程成績*/

int order;

struct Student *next;

};

int Menu(void);

void AverSumofScore(struct Student *head);

void SortbyName(struct Student *head );

void SortbyScore(struct Student *head );

void SortbyNum(struct Student *head );

void SearchbyName(struct Student *head );

void SearchbyNum(struct Student *head );

void Level(struct Student *head,int n );

struct Student *AppendNode(struct Student *head,int n);

void DispilNode(struct Student *head);

void DispilNode_Order(struct Student *head);

void WritetoFile(struct Student *head);

void ReadfromFile(struct Student *head);

void DeleteMemory(struct Student *head);

int main()

{

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_GREEN);

int ch;

int n;

struct Student *head = NULL ;

system("color 3f");

printf("Input number of Students.\n");

scanf("%d",&n);

while (1)

{

ch = Menu();

switch(ch)

{

case 1 :

printf ("1.Append record\n");

printf("Input node data:\n");

head = AppendNode(head,n);

break;

case 2 :

printf ("2.Caculate total and average score of course\n");

AverSumofScore(head);

break;

case 3 :

printf ("3.Sort by score\n");

SortbyScore(head);

DispilNode(head);

WritetoFile(head);

break;

case 4 :

printf ("4.Sort by number\n");

SortbyNum(head);

DispilNode_Order(head);

break;

case 5 :

printf ("5.Sort in dictionary order by name\n");

SortbyName(head);

DispilNode_Order(head);

break;

case 6 :

printf ("6.Search by number\n");

SearchbyNum(head);

break;

case 7 :

printf ("7.Search by name\n");

SearchbyName(head);

break ;

case 8 :

printf ("8.Statistic analysis\n");

Level (head,n);

break ;

case 9 :

printf ("9.List record\n");

ReadfromFile(head);

DispilNode(head);

AverSumofScore(head);

break;

case 0 :

printf ("End of program!");

DeleteMemory(head);

exit(0);

default :

printf ("Input error!");

break ;

}

}

DeleteMemory(head);/*釋放所有動態分配的內存*/

return 0;

}

int Menu()/*打印菜單並返回用戶的選擇*/

{

int Ysl;

printf("\t\t\t\t Welcome to\n");

printf("\n\t\t The student score manage system\n");

printf("*************************************MENU***************************************\n");

printf ("1.Append record\n");

printf ("2.Caculate total and average score of course\n");

printf ("3.Sort by score\n");

printf ("4.Sort by number\n");

printf ("5.Sort in dictionary order by name\n");

printf ("6.Search by number\n");

printf ("7.Search by name\n");

printf ("8.Statistic analysis\n");

printf ("9.List record\n");

printf ("0.Exit\n");

printf ("Please enter your choice:\n");

scanf("%d",&Ysl);

return Ysl;

}

/*函數功能:新建壹個節點並添加到鏈表末尾,返回添加節點後的鏈表的頭指針*/

struct Student *AppendNode(struct Student *head,int n)

{

struct Student *p = NULL,*pr = NULL;

int i;

long num; /*學號*/

char name[MAX_LEN];/*姓名*/

float score;/*課程成績*/

head = (struct Student *)malloc(sizeof(struct Student ));

if(head == NULL)

{

printf("No enough memory to allocate!\n");

exit(0);

}

else

{

scanf("%ld%s%f",&num,name,&score);

getchar();

head->num = num;/*將新建節點的數據域賦值為輸入節點的數據值*/

strcpy(head->name,name);

head->score = score;

head->next = NULL;/*將新建節點置為表尾*/

pr=head;

for(i=1; i<n; i++)

{

p = (struct Student *)malloc(sizeof(struct Student ));

scanf("%ld%s%f",&num,name,&score);

getchar();

p->num = num;

strcpy(p->name,name);

p->score = score;

p->next = NULL;

pr->next = p;

pr=p;

}

}

return (head);/*返回添加節點後的鏈表的頭指針*/

}

/*函數功能:顯示鏈表中所有節點的節點號和該節點中的數據項內容並排序*/

void DispilNode(struct Student *head)

{

struct Student *p = head;

int j = 0;

printf("\n************************************STUDENT************************************\n");

printf("-------------------------------------------------------------------------------\n");

printf("Num\tName\tscore\torder\n");

while (p != NULL)

{

j++;

p->order=j;

printf("%ld\t%s\t%.1f\t%d\n",p->num,p->name,p->score,p->order);/*打印第j個節點的數據*/

p = p->next;

}

printf("-------------------------------------------------------------------------------\n");

printf("**************************************END**************************************\n");

}

/*函數功能:顯示鏈表中所有節點的節點號和該節點中的數據項內容*/

void DispilNode_Order(struct Student *head)

{

struct Student *p = head;

printf("\n************************************STUDENT************************************\n");

printf("-------------------------------------------------------------------------------\n");

printf("Num\tName\tscore\torder\n");

while (p != NULL)

{

printf("%ld\t%s\t%.1f\t%d\n",p->num,p->name,p->score,p->order);/*打印第j個節點的數據*/

p = p->next;

}

printf("-------------------------------------------------------------------------------\n");

printf("**************************************END**************************************\n");

}

/*函數功能:按分數排序*/

void SortbyScore(struct Student *head)

{

char name1[MAX_LEN];

long data1;

float temp;

struct Student *temp1,*temp2,*k; /*定義臨時指針*/

temp1=head;

temp2=temp1->next;

while(temp1->next!=NULL)

{

k=temp1;

while(temp2!=NULL)

{

if((temp2->score)>(k->score))

{

k=temp2;

}

temp2 = temp2->next;

}

if(k !=temp1)

{

temp= k->score;

k->score = temp1->score;

temp1->score = temp;

data1= k->num;

k->num = temp1->num;

temp1->num = data1;

strcpy((name1),(k->name));

strcpy((k->name),(temp1->name));

strcpy((temp1->name),(name1));

}

temp1 = temp1->next;

temp2 = temp1;

}

}

/*函數功能:按學號排序*/

void SortbyNum(struct Student *head )

{

int order1;

char name1[MAX_LEN];

long data1;

float temp;

struct Student *temp1,*temp2,*k; /*定義臨時指針*/

temp1=head;

temp2=temp1->next;

while(temp1->next!=NULL)

{

k=temp1;

while(temp2!=NULL)

{

if((temp2->num)<(k->num))

{

k=temp2;

}

temp2 = temp2->next;

}

if(k !=temp1)

{

temp= k->score;

k->score = temp1->score;

temp1->score = temp;

data1= k->num;

k->num = temp1->num;

temp1->num = data1;

strcpy((name1),(k->name));

strcpy((k->name),(temp1->name));

strcpy((temp1->name),(name1));

order1=k->order;

k->order= temp1->order;

temp1->order=order1;

}

temp1 = temp1->next;

temp2 = temp1;

}

}

/*函數功能:按姓名的ASCII碼排序*/

void SortbyName(struct Student *head)

{

int order1;

char name1[MAX_LEN];

long data1;

float temp;

struct Student *temp1,*temp2,*k; /*定義臨時指針*/

temp1=head;

temp2=temp1->next;

while(temp1->next!=NULL)

{

k=temp1;

while(temp2!=NULL)

{

if(strcmp((temp2->name),(k->name))<0)

{

k=temp2;

}

temp2 = temp2->next;

}

if(k !=temp1)

{

temp= k->score;

k->score = temp1->score;

temp1->score = temp;

data1= k->num;

k->num = temp1->num;

temp1->num = data1;

strcpy((name1),(k->name));

strcpy((k->name),(temp1->name));

strcpy((temp1->name),(name1));

order1=k->order;

k->order= temp1->order;

temp1->order=order1;

}

temp1 = temp1->next;

temp2 = temp1;

}

}

/*函數功能:按姓名查找*/

void SearchbyName(struct Student *head )

{

char name1[MAX_LEN];/*name1為查找的姓名*/

struct Student *p; /* 移動指針*/

printf("Please enter name for searching.\n");

scanf("%s",name1);

p=head; /*將頭指針賦給p*/

while(strcmp(p->name,name1) && p != NULL) /*當記錄的姓名不是要找的,或指針不為空時*/

{

p=p->next; /*移動指針,指向下壹結點*/

}

if(p!=NULL) /*如果指針不為空*/

{

printf("\n*************************************FOUND************************************\n");

printf("-------------------------------------------------------------------------------\n");

printf("Num\tName\tscore\torder\n");

printf("%ld\t%s\t%.1f\t%d\n",p->num,p->name,p->score,p->order);

printf("-------------------------------------------------------------------------------\n");

printf("***************************************END**************************************\n");

}

else

{

printf("\nThere is no called %s student on the list.\n",name1); /*顯示沒有該學生*/

}

}

/*函數功能:按學號查找*/

void SearchbyNum(struct Student *head )

{

long num1;

struct Student *p1; /* 移動指針*/

printf("Please enter num for searching.\n");

scanf("%ld",&num1);

p1 = head;

while((p1->num!=num1) && p1 != NULL) /*當記錄的學號不是要找的,或指針不為空時*/

{

p1=p1->next; /*移動指針,指向下壹結點*/

}

if(p1!=NULL) /*如果指針不為空*/

{

printf("\n*************************************FOUND************************************\n");

printf("-------------------------------------------------------------------------------\n");

printf("Num\tName\tscore\torder\n");

printf("%ld\t%s\t%.1f\t%d\n",p1->num,p1->name,p1->score,p1->order);

printf("-------------------------------------------------------------------------------\n");

printf("***************************************END**************************************\n");

}

else

{

printf("\nThere is no called %ld student on the list.\n",num1); /*顯示沒有該學生*/

}

}

void Level(struct Student *head,int n )

{

int mark1=0,mark2=0,mark3=0,mark4=0,mark5=0;

float per1=0,per2=0,per3=0,per4=0,per5=0;

struct Student *p4; /* 移動指針*/

p4=head;

while(p4!=NULL)

{

if((p4->score)>=90)

{

mark1++;

per1=(float)mark1*100/n;

}

else if((p4->score)>=80)

{

mark2++;

per2=(float)mark2*100/n;

}

else if((p4->score)>=70)

{

mark3++;

per3=(float)mark3*100/n;

}

else if((p4->score)>=60)

{

mark4++;

per4=(float)mark4*100/n;

}

else

{

mark5++;

per5=(float)mark5*100/n;

}

p4=p4->next; /*移動指針,指向下壹結點*/

}

printf(">=90\t\t%d\t%.2f%%\n",mark1,per1);

printf("<90&&>=80\t%d\t%.2f%%\n",mark2,per2);

printf("<80&&>=70\t%d\t%.2f%%\n",mark3,per3);

printf("<70&&>=60\t%d\t%.2f%%\n",mark4,per4);

printf("<60&&>=0\t%d\t%.2f%%\n",mark5,per5);

}

/*計算平均分與總分*/

void AverSumofScore(struct Student *head)

{

int i=0,sum=0;

float aver;

struct Student *p5; /* 移動指針*/

p5=head;

while(p5!=NULL)

{

sum=sum+(int)(p5->score);

p5 = p5->next;

i++;

}

aver = sum*1.0/i;

printf("average of score:%.1f\n",aver);

printf("-----------------------------------------------------------\n");

printf("sum of score:%d\n",sum);

}

/*函數功能:釋放head指向的鏈表中所有占用的內存*/

void DeleteMemory(struct Student *head)

{

struct Student *p = head,*pr = NULL;

while (p != NULL)

{

pr = p;/*在pr中保存當前節點的指針*/

p = p->next;/*讓p指向下個節點*/

free(pr);/*釋放pr指向的當前節點占用的內存*/

}

}

/*輸出學生的信息到文件student.txt中*/

void WritetoFile(struct Student *head)

{

FILE *fp;

struct Student *p6; /* 移動指針*/

p6=head;

if((fp=fopen ("student.txt","w"))==NULL)

{

printf ("Failture to open student.txt!\n");

exit(0);

}

while(p6!=NULL)

{

fprintf(fp,"%ld\t%s\t%f\t%d\n",p6->num,p6->name,p6->score,p6->order);

p6=p6->next;

}

fclose(fp);

}

/*從文件中讀取學生信息*/

void ReadfromFile(struct Student *head)

{

FILE *fp;

struct Student *p8; /* 移動指針*/

p8=head;

if((fp=fopen ("student.txt","r"))==NULL)

{

printf ("Failture to open student.txt!\n");

exit(0);

}

while(p8!=NULL)

{

fscanf(fp,"%ld\t%s\t%f\t%d\n",&p8->num,p8->name,&p8->score,&p8->order);

p8=p8->next;

}

fclose(fp);

}

先前寫過的代碼,意思差不多。改改就可以。

  • 上一篇:打字遊戲的代碼
  • 下一篇:編程 到底是什麽?詳細解釋
  • copyright 2024編程學習大全網