⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 student_management_system.txt

📁 学生成绩管理系统,用C编的,比较简单,但很经典
💻 TXT
📖 第 1 页 / 共 2 页
字号:
 STUDENT *p1,*p2,*t,*temp;      /*定义临时指针*/
 temp=head->next;               /*将原表的头指针所指的下一个结点作头指针*/
 head->next=NULL;               /*第一个结点为新表的头结点*/
 while(temp!=NULL)              /*当原表不为空时,进行排序*/
   {
    t=temp;             /*取原表的头结点*/
    temp=temp->next;    /*原表头结点指针后移*/
    p1=head;             /*设定移动指针p1,从头指针开始*/
    p2=head;             /*设定移动指针p2做为p1的前驱,初值为头指针*/
    while(t->average<p1->average&&p1!=NULL)  /*作成绩平均分比较*/
      {
       p2=p1;            /*待排序点值小,则新表指针后移*/
       p1=p1->next;
      }
    if(p1==p2)      /*p1==p2,说明待排序点值大,应排在首位*/
      {
       t->next=p1;     /*待排序点的后继为p*/
       head=t;         /*新头结点为待排序点*/
      }
     else    /*待排序点应插入在中间某个位置p2和p1之间,如p为空则是尾部*/
       {
        t->next=p1;   /*t的后继是p1*/
        p2->next=t;   /*p2的后继是t*/
       }
     }
 p1=head;         /*已排好序的头指针赋给p1,准备填写名次*/
 while(p1!=NULL)  /*当p1不为空时,进行下列操作*/
   {
    i++;           /*结点序号*/
    p1->order=i;   /*将结点序号赋值给名次*/
    p1=p1->next;   /*指针后移*/
   }
 printf("Sorting is sucessful.\n");   /*排序成功*/
 return (head);
}

/*插入记录函数*/
STUDENT  *insert(STUDENT *head,STUDENT *new)
{STUDENT *p0,*p1,*p2;
 int n,sum1,i;
 p1=head;  /*使p1指向第一个结点*/
 p0=new;   /*p0指向要插入的结点*/
 printf("\nPlease enter a new record.\n");      /*提示输入记录信息*/
 printf("Enter the num:");
 scanf("%s",new->num);
 printf("Enter the name:");
 scanf("%s",new->name);
 printf("Please enter the %d scores.\n",3);
 sum1=0;    /*保存新记录的总分,初值为0*/
 for(i=0;i<3;i++)
   {
    do{
       printf("score%d:",i+1);
       scanf("%d",&new->score[i]);
       if(new->score[i]>100||new->score[i]<0)
       printf("Data error,please enter again.\n");
      }while(new->score[i]>100||new->score[i]<0);
    sum1=sum1+new->score[i];    /*累加各门成绩*/
   }
 new->sum=sum1;    /*将总分存入新记录中*/
 new->average=(float)sum1/3;
 new->order=0;
 if(head==NULL)  /*原来的链表是空表*/
   {head=p0;p0->next=NULL;}  /*使p0指向的结点作为头结点*/
 else
   {while((p0->average<p1->average)&&(p1->next!=NULL))
      {p2=p1;   /*使p2指向刚才p1指向的结点*/
       p1=p1->next;   /*p1后移一个结点*/
      }
    if(p0->average>=p1->average)
      {if(head==p1)head=p0;   /*插到原来第一个结点之前*/
       else p2->next=p0;      /*插到p2指向的结点之后*/
       p0->next=p1;}
    else
      {p1->next=p0;p0->next=NULL;} /*插到最后的结点之后*/
   }
 n=n+1; /*结点数加1*/
 head=sort(head);  /*调用排序的函数,将学生成绩重新排序*/
 printf("\nStudent  %s have been inserted.\n",new->name);    
 printf("Don't forget to save the new file.\n");
 return(head);
}

/*保存数据到文件函数*/
void save(STUDENT *head)
{FILE *fp;        /*定义指向文件的指针*/
 STUDENT *p;      /* 定义移动指针*/
 char outfile[10];
 printf("Enter outfile name,for example c:\\score\n");
 scanf("%s",outfile);
 if((fp=fopen(outfile,"wb"))==NULL)   /*为输出打开一个二进制文件,为只写方式*/
   {
    printf("Cannot open the file\n");
    return;    /*若打不开则返回菜单*/
   }
 printf("\nSaving the file......\n");
 p=head;                    /*移动指针从头指针开始*/
 while(p!=NULL)        /*如p不为空*/
   {
    fwrite(p,LEN,1,fp);     /*写入一条记录*/
    p=p->next;        /*指针后移*/
   }
 fclose(fp);      /*关闭文件*/
 printf("Save the file successfully!\n");
}

/* 从文件读数据函数*/
STUDENT *load()
{STUDENT *p1,*p2,*head=NULL;    /*定义记录指针变量*/
 FILE *fp;            /* 定义指向文件的指针*/
 char infile[10];
 printf("Enter infile name,for example c:\\score\n");
 scanf("%s",infile);
 if((fp=fopen(infile,"rb"))==NULL)   /*打开一个二进制文件,为只读方式*/
   {
    printf("Can not open the file.\n");
    return(head);
   }
 printf("\nLoading the file!\n");
 p1=(STUDENT *)malloc(LEN);   /*开辟一个新单元*/
 if(!p1)
   {
    printf("Out of memory!\n");
    return(head);
   }
 head=p1;         /*申请到空间,将其作为头指针*/
 while(!feof(fp))  /*循环读数据直到文件尾结束*/
   {
    if(fread(p1,LEN,1,fp)!=1) break;  /*如果没读到数据,跳出循环*/
    p1->next=(STUDENT *)malloc(LEN);  /*为下一个结点开辟空间*/
    if(!p1->next)
      {
       printf("Out of memory!\n");
       return (head);
      }
 p2=p1;         /*使p2指向刚才p1指向的结点*/
 p1=p1->next;   /*指针后移,新读入数据链到当前表尾*/
   }
 p2->next=NULL;   /*最后一个结点的后继指针为空*/
 fclose(fp);
 printf("You have success to read data from the file!\n");
 return (head);
}



人事记录

 
#include <stdio.h>
#include <conio.h>

int main(void)
{
   char label[20];
   char name[20];
   int entries = 0;
   int loop, age;
   double salary;

   struct Entry_struct
   {
      char  name[20];
      int   age;
      float salary;
   } entry[20];

/* Input a label as a string of characters restricting to 20 characters */
   printf("\n\nPlease enter a label for the chart: ");
   scanf("%20s", label);
   fflush(stdin);  /* flush the input stream in case of bad input */

/* Input number of entries as an integer */
  printf("How many entries will there be? (less than 20) ");
  scanf("%d", &entries);

  fflush(stdin);   /* flush the input stream in case of bad input */

/* input a name restricting input to only letters upper or lower case */
   for (loop=0;loop<entries;++loop)
   {
      printf("Entry %d\n", loop);
      printf("  Name   : ");
      scanf("%[A-Za-z]", entry[loop].name);
      fflush(stdin);  /* flush the input stream in case of bad input */

/* input an age as an integer */
      printf("  Age    : ");
      scanf("%d", &entry[loop].age);
      fflush(stdin);  /* flush the input stream in case of bad input */

/* input a salary as a float */
      printf("  Salary : ");
      scanf("%f", &entry[loop].salary);
      fflush(stdin); /* flush the input stream in case of bad input */
   }

/* Input a name, age and salary as a string, integer, and double */
   printf("\nPlease enter your name, age and salary\n");
   scanf("%20s %d %lf", name, &age, &salary);


/* Print out the data that was input */
   printf("\n\nTable %s\n",label);
   printf("Compiled by %s  age %d  $%15.2lf\n", name, age, salary);
   printf("-----------------------------------------------------\n");

   for (loop=0;loop<entries;++loop)
      printf("%4d | %-20s | %5d | %15.2lf\n",
         loop + 1,
         entry[loop].name,
         entry[loop].age,
         entry[loop].salary);
   printf("-----------------------------------------------------\n");
   getch();
   return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -