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

📄 main.c

📁 关于学生数据管理系统用C编的代码
💻 C
📖 第 1 页 / 共 4 页
字号:
      	            else
      	              frontClass->next=currentClass;
      	            frontClass=currentClass;
      	            
      	            fscanf(file,"%s",currentClass->ID);
      	            break;
      	  case '~': currentStud=(struct stud *)malloc(sizeof(struct stud));
      	            if(currentStud==NULL)
      	          {
      	              printf("Can't assign the place for the student!\n");
      	              fclose(file);
      	              exit(-1);
      	          }
      	           
      	            currentStud->next=NULL;
      	            
      	            if(currentClass->student==NULL)
      	              currentClass->student=currentStud;
      	            else
      	              frontStud->next=currentStud;
      	            frontStud=currentStud; 
      	            
      	            fgets(string,256,file);
      	            token=strtok(string,step);
      	            for(r=1;token!=NULL;r++)
      	          {
      	              switch(r)
      	            {
      	            	case 1: strcpy(currentStud->name,token);
      	            	        break;
      	            	case 2: strcpy(currentStud->ID,token);
      	            	        break;
      	            	case 3: strcpy(currentStud->sex,token);
      	            	        break;
      	            	case 4: strcpy(currentStud->age,token);
      	            	        break;
      	            	case 5: strcpy(currentStud->dorm,token);
      	            	        break;
      	            	case 6: strcpy(currentStud->tel,token);
      	            	        break;
      	            	case 7: currentStud->scores=(struct score *)malloc(sizeof(struct score));
      	            	        if(currentStud->scores==NULL)
      	            	      {
      	            	      	  printf("Can't assign the place for the student's scores!\n");
      	            	      	  fclose(file);
      	            	      	  exit(-1);
      	            	      }
      	            	        flag=1;
      	            	        currentStud->scores->Math=atoi(token);
      	            	        break;
      	                case 8: currentStud->scores->Physics=atoi(token);
      	                        break;
      	                case 9: currentStud->scores->English=atoi(token);
      	                        break;
      	                case 10: currentStud->scores->Computer=atoi(token);
      	                         break;
      	                case 11: currentStud->scores->average=atoi(token);
      	                         break;
      	                default: printf("Something wrong!\n");
      	                         fclose(file);
      	                         exit(-1);
      	            }
      	              token=strtok(NULL,step);
      	          }
      	            if(flag!=1)
      	              currentStud->scores=NULL;
      	            break;
      	  case ' ':
      	  case '\n': break;
      	  default: printf("Error!\n");
      	           fclose(file);
      	           exit(-1);
      }
    }
        fclose(file);
}


/*给各班同学按学号排序*/
    int rankStudents()
{      
      struct stud *behindStud=NULL;
      struct stud *tempStud=NULL;
      struct stud *lastStud=NULL;
      double frontNum=0,currentNum=0;
      int p=0;
      
      currentClass=head;
      for(;currentClass!=NULL;)  /*遍历班级表*/
    {
    	currentStud=currentClass->student;
    	if(currentStud->next==NULL)
      {
      }
        else
      {
          lastStud=tempStud=frontStud=currentClass->student;
          currentStud=frontStud->next;
          behindStud=currentStud->next;
          do              /*插入排序*/
        {
      	    tempStud=frontStud=currentClass->student;      	    
      	    frontNum=atof(frontStud->ID);
      	    currentNum=atof(currentStud->ID);
      	    
      	    for(p=1;currentNum>=frontNum&&frontStud!=currentStud&&frontStud->next!=NULL;p++)  
      	  {                                               /*寻找插入点*/
      	      tempStud=frontStud;
      	      frontStud=tempStud->next;
      	      frontNum=atof(frontStud->ID);
      	  }
      	    if(p==1&&currentNum<frontNum)  /*插入点为该班学生链表的首部*/
      	  {
      	      currentClass->student=currentStud;
      	      currentStud->next=frontStud;
      	      lastStud=(atof(lastStud->ID)>atof(currentStud->ID)?lastStud:currentStud);
      	      currentStud=behindStud;
      	  }
      	    if(p!=1&&currentNum<frontNum)  /*插入点为该班学生链表的中间部分*/
      	  {
      	      tempStud->next=currentStud;
      	      currentStud->next=frontStud;
      	      lastStud=(atof(lastStud->ID)>atof(currentStud->ID)?lastStud:currentStud);
      	      currentStud=behindStud;
      	  }
      	    if(frontStud==currentStud||frontStud->next==NULL)   /*插入点为该学生的当前位置*/
      	  {
      	      frontStud->next=currentStud;
      	      lastStud=(atof(lastStud->ID)>atof(currentStud->ID)?lastStud:currentStud);
      	      currentStud=behindStud;
      	  }
      	    if(currentStud!=NULL)
      	      behindStud=currentStud->next; 
      	    else
      	      behindStud=NULL;     	    
      	    lastStud->next=NULL;      	    
      	} while(currentStud!=NULL);
      }
        frontClass=currentClass;
        currentClass=frontClass->next;
    }
}


/*插入某班某个学生的基本情况的函数*/
    int insertStud()
{
      char classID[20];
      char temp='';
      char continueStud='';
      char rb[30];
      
    continueFind1:
      clrscr();
      printf("\n\n");
      printf("Please input the number of the class in which you want to add student: ");
      gets(classID);
      
      if(head==NULL)     /*库中没有数据*/
    {
    	printf("No information!\n\n");
    	printf("Go to the main page (Y/N)? ");
    	temp='';
    	scanf("%c%*c",&temp);
    	if(temp=='Y'||temp=='y')
    	  mainPage();
    	else
    	  exit(-1);
    }
      currentClass=head;
      while(currentClass->next!=NULL&&strcmp(currentClass->ID,classID)!=0) /*遍历链表*/
    {
    	frontClass=currentClass;
    	currentClass=frontClass->next;
    }
      if(currentClass->next==NULL&&strcmp(currentClass->ID,classID)!=0) /*找不到此班*/
    {
    	printf("There is no accordant class!\n\n");
    	printf("Continue to find the class (Y/N)? ");
    	temp='';
    	scanf("%c%*c",&temp);
    	if(temp=='Y'||temp=='y')
    	  goto continueFind1;
    	else
          mainPage();
    }
    
    /*找到此班,为其添加成员*/
     /*指到该班成员的最后一名,以便将新加入的作为最后一名*/
      frontStud=NULL;
      for(frontStud=currentClass->student;frontStud->next!=NULL;) 
    {    
    	frontStud=frontStud->next;       
    }
        for(continueStud='Y';continueStud=='Y'||continueStud=='y';)
      {         	      
          currentStud=(struct stud *)malloc(sizeof(struct stud));
          if(currentStud==NULL)
        {
      	    printf("Can't assign the place for the new student!\n");
      	    exit(-1);
        }
   
       /*录入学生的基本情况*/  
          if(currentClass->student==NULL)
            currentClass->student=currentStud;
          
          printf("\nPlease input the name of the student: ");
          gets(currentStud->name);
          printf("Please input the ID of the student: ");
          gets(currentStud->ID);
        currentSex1:
          printf("If male then input \"M\" or else \"F\": ");
          scanf("%c%*c",&temp);
          switch(temp)
        {
    	    case 'M':
    	    case 'm': strcpy(currentStud->sex,"Male");
    	              break;
    	    case 'F':
    	    case 'f': strcpy(currentStud->sex,"Female");
    	              break;
    	    default: gets(rb);
    	             goto currentSex1;
        }
          printf("Please input the age of the student: ");
          gets(currentStud->age);
          printf("Please input the number of the student's dormitory: ");
          gets(currentStud->dorm);
          printf("Please input the telephone of the student: ");
          gets(currentStud->tel);          
          currentStud->next=NULL;
          currentStud->scores=NULL;
          
          frontStud->next=currentStud;          
          frontStud=currentStud;
      
          printf("\nContinue to insert students (Y/N)? ");
          scanf("%c%*c",&continueStud);
      }
        rankStudents();
        saveFile();
        mainPage();
}


/*删除某班的某个学生*/
    int delStud()
{
      char classID[20];
      char studentName[20];
      char temp='',c='';
      int flag2=0;
      
    continueFindClass:
      clrscr();
      printf("\n\n");
      printf("Please input the number of the class in which you want to delete student: ");
      gets(classID);
      
      if(head==NULL)     /*库中没有数据*/
    {
    	printf("No information!\n\n");
    	printf("Go to the main page (Y/N)? ");
    	temp='';
    	scanf("%c%*c",&temp);
    	if(temp=='Y'||temp=='y')
    	  mainPage();
    	else
    	  exit(-1);
    }
      currentClass=head;
      while(currentClass->next!=NULL&&strcmp(currentClass->ID,classID)!=0) /*遍历链表*/
    {
    	frontClass=currentClass;
    	currentClass=frontClass->next;
    }
      if(currentClass->next==NULL&&strcmp(currentClass->ID,classID)!=0) /*找不到此班*/
    {
    	printf("There is no accordant class!\n\n");
    	printf("Continue to find the class (Y/N)? ");
    	temp='';
    	scanf("%c%*c",&temp);
    	if(temp=='Y'||temp=='y')
    	  goto continueFindClass;
    	else
          mainPage();
    }
    
    /*找到该班,准备查找某学生*/
      printf("\nFind the class successfully!\n\n");
    continueFindStud:
      printf("Please input the name of the student whom you want to delete: ");
      gets(studentName);
      
      
      if(currentClass->student==NULL)     /*该班没有学生*/
    {
    	printf("No student in this class!\n\n");
    	printf("Go to the main page (Y/N)? ");
    	temp='';
    	scanf("%c%*c",&temp);
    	if(temp=='Y'||temp=='y')
    	  mainPage();
    	else
    	  exit(-1);
    }
    
      currentStud=currentClass->student;
      flag2=0;
    sameName:
      while(currentStud->next!=NULL&&strcmp(currentStud->name,studentName)!=0) /*遍历链表*/
    {
    	frontStud=currentStud;
    	currentStud=frontStud->next;
    }
      if(currentStud->next==NULL&&strcmp(currentStud->name,studentName)!=0) /*找不到这个人*/
    {                                                                  /*或者没有重名的人*/
    	if(flag2==0)
      {                                                          
    	  printf("There is no accordant student!\n\n");
    	  printf("Continue to find the student (Y/N)? ");
    	  temp='';
    	  scanf("%c%*c",&temp);
    	  if(temp=='Y'||temp=='y')
    	    goto continueFindStud;
    	  else
            mainPage();
      }
        else
      {
      	  printf("\nNo other \"%s\"!\n",studentName);
      	  printf("Go to the main page!\n");
      	  getchar();
      	  mainPage();
      }
    }
    
      /*找到该学生并删除他*/
      printf("\nFound the student! NAME--%s   ID--%s\n",currentStud->name,currentStud->ID);
      printf("Do you really want to delete him/her (Y/N)? \n");
      c=getchar();
      getchar();
      if(currentStud->next==NULL&&strcmp(currentStud->name,studentName)==0) /*此人是该班最后一个*/
    {
    	if(c=='Y'||c=='y')
      {
    	  frontStud->next=NULL;
    	  saveFile();
    	  printf("\nDelete the student successfully!\n");
    	  printf("Goto the main page!\n");
    	  getchar();
    	  mainPage();
      }
    	else
      {
      	  printf("\nNo other \"%s\"!\n",studentName);
      	  printf("Go to the main page!\n");
      	  getchar();
      	  mainPage();
      }
    }
      if(currentStud->next!=NULL&&strcmp(currentStud->name,studentName)==0) /*此人在中间*/
    {
    	if(c=='Y'||c=='y')
      {
    	  frontStud->next=currentStud->next;
    	  saveFile();
    	  printf("\nDelete the student successfully!\n");
    	  printf("Goto the main page!\n");
    	  getchar();
    	  mainPage();
      }
    	else
      {
      	  frontStud=currentStud;
    	  currentStud=frontStud->next;
    	  flag2=1;
    	  goto sameName;  /*可能有重名的学生*/
      }
    }
      if(currentStud->next!=NULL&&strcmp(currentStud->name,studentName)==0) /*此人是该班第一个*/
    {
    	if(c=='Y'||c=='y')
      {

⌨️ 快捷键说明

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