中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > 综合其它
单向链表实现学生管理程序
作者:未知 时间:2005-07-27 23:31 出处:CSDN 责编:chinaitpower
              摘要:单向链表实现学生管理程序

/* TC 2.01 测试通过  非 tc 请自行修改  */
/* 没有全面测试  自行解决 嘿嘿       by碎心竹   */
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>

#define NAME 16
#define SEX  6

/*
#define NO   6
#define AGE  3
#define CLAS 3
*/
#define ERROR 0
#define OK    1

/*
1、学生名单管理
任务:可以实现学生名单的增加、删除、修改、显示、排序;

姓名、         性别、  年龄、  学号、 班级、 基础课成绩、专业课程成绩,总成绩
name            sex    age     no     clas   base        pro        sum
zhangwenhui      n      24   123456     5     70          60        130


*/

typedef struct nod
{
  int     no;
  char    name[NAME];
  char    sex[SEX];
  short   int age;
  short   int clas;
  int     base;
  int     pro;
  int     sum;
  struct nod *next;
}Nod;

int readfile(Nod *head,FILE *fp);   /*  从文件读取数据 */
int writefile(FILE *fp,Nod *head); /* 数据写入文件   */
Nod * createnod(void);   /*   创建空链表   */
int destroynod(Nod *head); /*销毁 链表 */
int getline(char *pch,int x); /* 字符输入 */
int getdig(int *i);   /*数字输入*/
int insetnod (Nod *head); /*  顺序插入一个学员 */
int deletenod(Nod *head); /*删除一个学员 */
int sortnod(Nod *head);          /*排序 列表 */
int printnod(Nod *head);        /*显示所有 具有分屏功能*/
modifynod(Nod *head); /* 修改学员资料 */
void mainlist(int open,FILE *fp);       /* 主菜单*/


main(void)
{
   int open = 0;    /* 文件打开 */
   FILE *fp;
   fp = fopen ("data.txt","r");
   if (!fp) {
      open = 1;
   }
  
   textbackground(BLUE);
   textcolor(YELLOW);
   clrscr();
  
   printf(" \n\n\n\n\n\n\n\n\n\t\t\t *************************\n"
          " \t\t\t *                       *\n"
          " \t\t\t *                       *\n"
          " \t\t\t *   Welcom  to this  !  *\n"
          " \t\t\t *                       *\n"
          " \t\t\t *                       *\n"
          " \t\t\t *************************\n"
          "\n\n\n\n\n\n\n\n\n\nPlease any key to continue........\n");
   getch();
   mainlist(open,fp);
  
   return 0;
}/* main() */

void mainlist(int open,FILE *fp)
{
 int ch;
  Nod *head = NULL;
  if (fp){       
    head = createnod();
    readfile(head,fp);          
  }
 
  while(1){
    textcolor(YELLOW);
    clrscr();
    printf("\n\n\n "
           "\t\t\t*******************************\n"
           "\t\t\t*                             *\n"
           "\t\t\t*   1. Creat a list           *\n"
           "\t\t\t*   2. Add a student          *\n"
           "\t\t\t*   3. Del a student          *\n"
           "\t\t\t*   4. Print list             *\n"
           "\t\t\t*   5. Modify a student       *\n"
           "\t\t\t*   6. Sort list for No       *\n"
           "\t\t\t*   7. Load File * do't use!  *\n"
           "\t\t\t*   8. Wirte File for list    *\n"
           "\t\t\t*                             *\n"
           "\t\t\t*******************************\n"
          
          
           "\t\t\tOther is  Exit !!! \n"
    );
   
    if(!open)
       printf("\n\n\n\n FILE   IS OPEN !!!!!!\n");
    else
       printf("\n\n\n\n FILE NOT OPEN   Now is Write Mode !!\n");
    ch=getch();

    switch(ch){
      case '1':if (!open){
                  textcolor(WHITE);
                  clrscr();
                 
                  printf("\n\nFile Is Open Not Write \n\nPlease  Input 7 \n\n\n Please any key to continue\n");
                  getch();
                  continue;
               }
               else if(head){
                  textcolor(WHITE);
                  clrscr();
                  printf("Lis Is have Now  Not be Create \n\n\nPlease any key to continue\n");
                  getch();
                  continue;
               }
                  head = createnod();
                  textcolor(WHITE);
                  clrscr();
                  printf("\nCreat   List  OK! \n\nPlease Input 2 for Add a studen \n\n\nPlease any key to continue\n");
                  getch();
               break;
      case '2':if (!head){
                  textcolor(WHITE);
                  clrscr();
                  printf("\n\nList is Empty\n\n Please Input 1\n\n\n\n Please any key to continue\n");
                  getch();
                  continue;
               }
               else{
                  clrscr();                 
                  insetnod (head);
                }
               break;

      case '3':if (!head){
                  textcolor(WHITE);
                  clrscr();
                  printf("\n\nList is Empty \n\nPlease Input 1\n\n\n\n Please any key to continue\n");
                  getch();
                  continue;
               }
              else{
                  clrscr();
                  deletenod(head);
              }
              break;

      case '4':if (!head){
                  textcolor(WHITE);
                  clrscr();
                  printf("\n\nList is Empty\n\n Please Input 1\n\n\n\n Please any key to continue\n");
                  getch();
                  continue;
               }
               else{
                  clrscr();
                  printnod(head);
               }
               break;

      case '5':if (!head){
                  textcolor(WHITE);
                  clrscr();
                  printf("\n\nList is Empty Please Input 1\n\n\n Please any key to continue\n");
                  getch();
                  continue;
               }
               else{
                 
                  textcolor(YELLOW);
                  clrscr();
                 
                  modifynod(head);
                  printf("modif stdent OK\n\n\n Please any key to continue\n");
                  getch();
               }
               break;

      case '6':if (!head){
                  textcolor(WHITE);
                  clrscr();
                  printf("List is Empty \n\nPlease Input 1\n\n\n Please any key to continue\n");
                  getch();
                  continue;
               }
               else{
                 
                  textcolor(YELLOW);
                  clrscr();
                  sortnod(head);
                  printf("Sort stdent OK\n\n\n Please any key to continue\n");
                  getch();
                 
               }
               break;
/*
      case '7':if (open){
                  clrscr();
                  printf("File is Not Find  Please Create a list to Write it\n\n\n Please any key to continue\n");
                  getch();
                  continue;
               }
               else if (head){
                  clrscr();
                  printf("List is being Don't Read file \n\n\n  Please any key to continue\n");
                  getch();
                  continue;
               }
               else{
                  clrscr();
                  readfile(fp);
               }
               break;
*/
      case '8': if (!head){

                  textcolor(WHITE);
                  clrscr();
                  printf("\n\nList is Empty \n\nPlease Input 1\n\n\n Please any key to continue\n");
                  getch();
                  continue;
               }
               else{

                  textcolor(YELLOW);
                  clrscr();
                  writefile(fp,head);    
                  printf("File Write OK ! \n\n\n\nPlease any key to continue\n");
                  getch(); 
               }
               break;

    default :  destroynod(head);  exit(0);  
   }
  
   /*break; */
  }
 

  
}/* mainlist() */

Nod * createnod(void)
{

  Nod *head ;
  head = (Nod *) malloc( sizeof(Nod) );
  if (!head)
     return NULL;
   
  head->no   = 0;
  head->name[0] = '\0';
  head->sex[0]  = '\0';
  head->age  = 0;
  head->clas = 0;
  head->base = 0;
  head->pro  = 0;
  head->sum  = 0;
  head->next = NULL;
  return head;
}/* createnod() */

int insetnod (Nod *head)
{
 
  int no;
  Nod *curr, *front, *temp;

  if(!head)
    return ERROR;
   
  front = curr = head;

  textcolor(YELLOW);
  clrscr();
  printf("Please input this  student No.\n");  /*  输入学号  */
  getdig(&no);
 

 
  while(NULL != curr && curr->next->no < no){ /* 寻找 插入位置 */
    front = curr;
    curr  = curr->next;   
  }
 
  temp = (Nod *) malloc( sizeof(Nod) );
  if(!temp)     
      return ERROR;

  temp->no = no;
  /*  printf("temp no:%d\n",temp->no); */ 
  printf("Please Input this student Name:\n");
  getline(temp->name,NAME);
  /* printf("temp name :%s\n",temp->name);*/
  printf("Please Input this student Sex:\n");
  getline(&temp->sex,SEX);
  printf("Please Input this student Age:\n");
  getdig(&temp->age);
  printf("Please Input this student class:\n");
  getdig(&temp->clas);
  printf("Please Input this student base:\n");
  getdig(&temp->base);
  printf("Please Input this student Pro:\n");
  getdig(&temp->pro);
  temp->sum = temp->base + temp->pro;

  temp->next = front->next;
  front->next = temp;
  ++head->sum;  /* 链表总数+1 */

  return OK;
}/* insetnod() */


int getdig(int *no)
{
  int ch;
  *no = 0;

  while ( (ch = getchar()) != '\n' && ch != EOF){

    if ( isdigit(ch) && (*no * 10 + (ch -'0')) > 0 ){
       *no = *no * 10 + (ch - '0');
    }
  }
    /*  printf("no is : %d\n",*no);*/
  return OK;
}/* getdig() */


int getline(char *pch,int x)
{
  int i,ch;
   x -= 1;
   i = 0;
  if (x < 0)
     return ERROR;
 
  while ( (ch = getchar()) != '\n' && ch != EOF){
   
    if (i < x ){
      pch[i] = ch;
       /*  printf("pch is : %c\n",pch[i]);*/
      ++i;
    }   
  } 
 
    if (x > 1 ){
      pch[i] = '\0';   
     /* printf(" \  0 is input\n"); */
    }

  return OK;

}/* getline() */

int destroynod(Nod *head)   /* 销毁 */
{
    Nod *temp;
    temp = head;
    do{        
         head = temp;
         temp = head->next;
         printf("destroy %d\n",temp);
         free(head);
    }while (temp);
    printf("See You \n");
    return OK;
}/* destroynod() */

int deletenod(Nod *head)  /* 删除 */
{
    Nod *curr,*temp;
    int no;
    temp = curr = head;

    printf("Please input this  student No.\n");  /*  输入要删除学号  */
    getdig(&no);


    while(NULL != curr) { /* 寻找 位置 */
        if (curr->no == no){
           head->next = curr->next;
           free(curr);
           --temp->sum;
           return OK;
        }
       
        head = curr;
        curr  = curr->next;   
    }
    printf("Nod Find No.%d  \n\n\n\nPlease any key to continue\n",no);
    getch();
    return ERROR;

} /* deletenod() */

int printnod(Nod *head)
{
    if (!head)
       return ERROR;   
    printf("all student is %d\n",head->sum);
    head = head->next;
    printf("%6s %10s %5s %5s %5s %5s %5d %5s\n",
            "No","name","sex","age","class","base","pro", "sum");    

    while (head){

        printf("%6d %10s %5s %5d %5d %5d %5d %5d\n",
        head->no,head->name,head->sex,head->age,head->clas,
        head->base,head->pro,head->sum);

        head = head->next;

       }   
    printf("Please any key to continue\n");
    getch();
    return OK;
}


int writefile(FILE *fp,Nod *head)
{

   if (!head)
      return ERROR;
  
   fp = fopen ("data.txt","w");
  
   head = head->next;
  
   while(head != NULL){

        fputs(itoa(head->no,"",10),fp);
        fputc(' ',fp);
        fputs(head->name,fp);
        fputc(' ',fp);
        fputs(head->sex,fp);
        fputc(' ',fp);
        fputs(itoa(head->age,"",10),fp);
        fputc(' ',fp);
        fputs(itoa(head->clas,"",10),fp);
        fputc(' ',fp);
        fputs(itoa(head->base,"",10),fp);
        fputc(' ',fp);
        fputs(itoa(head->pro,"",10),fp);
        fputc(' ',fp);
        fputs(itoa(head->sum,"",10),fp);
        fputc('\n',fp);
        head = head->next;
   }

    fclose(fp);

    return OK;
}/* writefile() */


int readfile(Nod *head,FILE *fp)
{
    Nod *temp, *suu;
    char atmp[20];

    if (!fp || !head)
       return NULL;

    suu = head;
    while ( fscanf(fp,"%s",atmp) != EOF ){
/*        fscanf(fp,"%s",atmp);  */
        temp =(Nod *) malloc( sizeof(Nod) );
        temp->no = atoi(atmp);
        fscanf(fp,"%s",temp->name) ;
        fscanf(fp,"%s",temp->sex);
        fscanf(fp,"%s",atmp);
        temp->age  = atoi( atmp );
        fscanf(fp,"%s",atmp);
        temp->clas = atoi( atmp );
        fscanf(fp,"%s",atmp);
        temp->base = atoi( atmp );
        fscanf(fp,"%s",atmp);
        temp->pro  = atoi( atmp );
        fscanf(fp,"%s",atmp);
        temp->sum  = atoi( atmp );
        temp->next = NULL;
        ++suu->sum;                /*        */
        head->next = temp;
        head = head->next;
    }
    return OK;
}/* readfile() */


modifynod(Nod *head)/* 修改学员资料 */
{

    Nod *temp;
    int no;
    temp = head;

    printf("Please input this  student No.\n");  /*  输入要修改学号  */
    getdig(&no);


    while(NULL != head) { /* 寻找 位置 */
        if (temp->no == no){
           
           printf("Please input this  student No.\n");  /*  输入学号  */
           getdig(&no);
           temp->no = no;
           printf("Please Input this student Name:\n");
           getline(temp->name,NAME); 
           /* printf("temp name :%s\n",temp->name);*/
           printf("Please Input this student Sex:\n");
           getline(&temp->sex,SEX);
           printf("Please Input this student Age:\n");
           getdig(&temp->age);
           printf("Please Input this student class:\n");
           getdig(&temp->clas);
           printf("Please Input this student base:\n");
           getdig(&temp->base);
           printf("Please Input this student Pro:\n");
           getdig(&temp->pro);
           temp->sum = temp->base + temp->pro;
                     
           return OK;
        }
       
        temp = temp->next;   
    }
    printf("Nod Find No.%d  \n\n\nPlease any key to continue\n",no);
    getch();
    return ERROR;  
   
}/* modifynod() */


int sortnod(Nod *head)
{

  Nod *list,*r, *temp, *hhead;
  int max, i, l, ts;

  hhead = head;
  max = head -> sum-1;
 
  printf("max: %d\n",max);

  for (i = 0; i < max; i++){
    head = hhead;
    ts = 0; /* 测试 检查 */

   for (l = 0; l < max;l++){

    list = head -> next;
    r = list -> next;

     if(list->no > r->no){

    temp = head->next;
    head->next = list->next;
    list -> next = r->next;
    r->next = temp;
    ++ts;
     }
    
    head = head->next;
   }
   if (0 == ts)
        return OK;
  }
  return OK;
}/* sortnod() */


关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有