首页 > 用户发贴区 > 编程问题提问区 > 哪位高手帮忙改下““
2008
10-09

哪位高手帮忙改下““

#include “stdio.h”                   
#include “stdlib.h”
#include “string.h”
#include <stdlib.h>
#include <io.h>
#include <conio.h>
#include <dos.h>
#include <signal.h>
#include “windows.h”
#define MAX 100                   /*N个用户*/ 
/*========
学员结构体
========*/
struct student                    
{
 char no[10];                  /*学员学号*/
 char name[50];                  /*学员姓名*/
 char sex[5];                  /*学员性别*/
 char class_no[10];                 /*学员班级*/
};
student init[MAX]={ {1,”大大”,”男”,”一班”},            /*对结构体数组进行初始化*/
     {2,”小小”,”女”,”二班”},
     {3,”东东”,”男”,”三班”},
     {4,”西西”,”女”,”四班”},
     {5,”南南”,”女”,”一班”},
     {6,”小明”,”难”,”二班”},
     };
student stu[MAX];
student null[MAX];                  /*用来读取student.txt文件所用的结构体*/
/*========
用户结构体
========*/
struct user                     
{
 char username[50];                 /*用户名*/ 
 char password[10];                 /*用户密码*/
};
user admin={{“cxl9908″,”93325645″},{“use”,”123″}};          /*初始化管理员*/
/*==========
密码输入函数
==========*/
void code(char mm[9])
{
 for (int i=0;i<9;i++)
 {
  mm[i]=getch();                 /*接收一个字符*/
  printf(“*”);                 /*显示一个**/
 }
 mm[i]=’\0′;
}
/*==========
创建文件函数
==========*/
void newfile(FILE *fp)                 
{
 fp=fopen(“student.txt”,”wb”);              /*打开student.txt文件*/
 fclose(fp);                   /*关闭student.txt文件*/
 fp=NULL;
}
/*==================
添加内容函数(向文件)
==================*/
void addfile(FILE *fp , stu a)               
{
 fp=fopen(“student.txt”,”a”);              /*打开student.txt文件*/             
 fwrite(&a,sizeof(stu),1,fp);              /*写入student.txt文件*/
 fclose(fp);                   /*关闭student.txt文件*/
 fp=NULL;
}
/*=================================
1.添加函数:添加学号-姓名-性别-班级
=================================*/
void add()
{
 FILE *fp;
 fp=fopen(“student.txt”,”w”);              /*打开student.txt文件*/
 char ch;
 int flag=0;                   /*没有重复的标志*/
 if((fp=fopen(“student.txt”,”ab+”))==NULL)
 {
  puts(“★=>文件打开出错!<=★”);exit(1);
 }
 puts(“★=>请输入学号:<=★”);
 scanf(“%s”,&stu.no);
 while(fread(&stu,sizeof(stu),1,fp)==1)
 {
  if(strcmp(stu.no,init.no)==0)
  {
   flag=1;                  /*如果有重复的标志*/
   puts(“★=>你输入的学号重复!要继续输入吗?(y/n)<=★”);
   scanf(” %c”,&ch);
   if(ch==’y'|| ch==’Y')
   {
    fclose(fp);
    append();
   }
   else
   {
    break;
   }
  } 
 }
 if(flag==0)                   /*如果没的重复的标志*/
 {
  puts(“★=>请输入姓名:<=★”);
  scanf(“%s”,&stu.name);
  puts(“★=>请输入性别:<=★”);
  scanf(“%s”,&stu.sex);
  puts(“★=>请输入班级:<=★”);
  scanf(“%s”,&stu.class_no);
  printf(“★=>你添加的信息如下:<=★\n\t\t学号:%s-姓名:%s-性别:%s-班级:%s\n”,stu.no,stu.name,stu.sex,stu.class_no);
  printf(“★=>是否确认添加:按(y)键确认!按(n)撤消添加操作<=★”);
  scanf(” %c”,&ch);
  if(ch!=’n'&& ch!=’N')
  fwrite(&stu,sizeof(stu),1,fp);
 }
 fclose(fp);                   /*关闭student.txt文件*/
 fp=NULL;
}
/*==========================================
2.删除函数:1.按学号删除2.按姓名删除3.按班级
==========================================*/
void remove()
{
 char ch;
 int flag=0;                   /*没有找到要删除的学生的标志*/
 if((fp=fopen(“student.txt”,”rb+”))==NULL)
 {
  puts(“★=>文件打开出错!<=★”);
  exit(1);
 }
 puts(“=>请输入要删除的学号<=:”);
 scanf(“%s”,&stu.no);
 while(fread(&stu,sizeof(stu),1,fp)==1)
 {
  if(strcmp(stu.no,stu1.no)==0)
  {
   flag=1;                  /*找到了要删除的学生的标志*/
   printf(“★=>你要删除的信息如下:<=★\n\t\t学号:%s\t姓名:%s-性别:%s-班级:%s\n”,stu.no,stu.name,stu.sex,stu.class_no);
   puts(“★=>请确认是否删除:(y/n)<=★”);
   scanf(” %c”,&ch);
   if(ch==’y’ || ch==’Y')
   { 
    continue;
   }
  }
  fwrite(&stu,sizeof(stu),1,fp);
 }
 fclose(fp);
 remove(“student.txt”);
 rename(“student.txt”);
 if(flag==1)
 {
  puts(“★=>删除成功!<=★”);
 }
 else if(flag==0)
 {
  puts(“★=>没有你要删除的学生信息,请确认输入是否正确!<=★”);system(“pause”);
 }
 fclose(fp);                   /*关闭student.txt文件*/
 fp=NULL;
}
/*==================================================================
3.修改函数:1.修改学员学号2.修改学员姓名3.修改学员性别4.修改学员班级
==================================================================*/
void revised()
{
 FILE *fp;
 fp=fopen(“student.txt”,”r”);              /*打开student.txt文件*/
 int i;
 int n;
 int k;
 int no1;                   /*修改的新学号*/
 char name1[50];                  /*修改的新姓名*/
 char sex1[5];                  /*修改的新性别*/
 int class_no1[10];                 /*修改的新班级*/
 scanf(“%d”,&i);
 for (k=0;k<15;k++)
 {
  fread(&null[k],sizeof(stu),1,fp);
  if (null[k].no!=0)
  {      
   printf(“%d\t%s\t%s\t%s\n”,null[k].no,null[k].name,null[k].sex,null[k].class_no);
  }
 } 
 fclose(fp);                   /*关闭student.txt文件*/
 fp=NULL;
 switch (i)
 {
  
  case 1:printf(“\t\t★1===========>学号修改<=★\n
        \t\t★2===========>姓名修改<=★\n
        \t\t★3===========>性别修改<=★\n
        \t\t★4===========>班级修改<=★\n
      “);
      scanf(“%d”,&n);
      switch (n)
      {
      
     case 1:{
        printf(“★=>请输入修改的学号<=★\n”);     /*学号修改*/
        scanf(“%d”,&no1);
        null[0].no=no1;
        printf(“★=>修改成功!<=★\n”);
         };break;
     case 2:{
        printf(“★=>请输入修改的姓名<=★\n”);     /*姓名修改*/
        scanf(“%s”,&name1);
        strcpy(null[0].name,name1);
        printf(“★=>修改成功!<=★\n”);
         };break;
     case 3:{
        printf(“★=>请输入修改的性别<=★\n”);     /*性别修改*/
        scanf(“%s”,&sex1);
        strcpy(null[0].sex,sex1);
        printf(“★=>修改成功!<=★\n”);
         };break;
     case 4:{
        printf(“★=>请输入修改的班级<=★\n”);     /*班级修改*/
        scanf(“%s”,&class_no1);
        strcpy(b[0].class_no,class_no1);
        printf(“★=>修改成功!<=★\n”);
         };break;
      };break;
  case 2:printf(“\t\t★1===========>学号修改<=★\n
        \t\t★2===========>姓名修改<=★\n
        \t\t★3===========>性别修改<=★\n
        \t\t★4===========>班级修改<=★\n
      “);
    scanf(“%d”,&n);
    switch (n)
    {
     case 1:{
        printf(“★=>请输入修改的学号<=★\n”);     /*学号修改*/
        scanf(“%d”,&no1);
        null[0].no=no1;
        printf(“★=>修改成功!<=★\n”);
         };break;
     case 2:{
        printf(“★=>请输入修改的姓名<=★\n”);     /*姓名修改*/
        scanf(“%s”,&name1)
        strcpy(null[0].name,name1);
        printf(“★=>修改成功!<=★\n”);
         };break;
     case 3:{
        printf(“★=>请输入修改的性别<=★\n”);     /*性别修改*/
        scanf(“%s”,&sex1);
        strcpy(null[0].sex,sex1);
        printf(“★=>修改成功!<=★\n”);
         };break;
     case 4:{
        printf(“★=>请输入修改的班级<=★\n”);     /*班级修改*/
        scanf(“%s”,&class_no1)
        strcpy(b[0].class_no,class_no1);
        printf(“★=>修改成功!<=★\n”);
         };break;
      };break;
  case 3:printf(“\t\t★1===========>学号修改<=★\n
        \t\t★2===========>姓名修改<=★\n
        \t\t★3===========>性别修改<=★\n
        \t\t★4===========>班级修改<=★\n
      “);
      scanf(“%d”,&n);
      switch (n)
      {
     case 1:{
        printf(“★=>请输入修改的学号<=★\n”);     /*学号修改*/
        scanf(“%d”,&no1);
        null[0].no=no1;
        printf(“★=>修改成功!<=★\n”);
         };break;
     case 2:{
        printf(“★=>请输入修改的姓名<=★\n”);     /*姓名修改*/
        scanf(“%s”,&name1);
        strcpy(null[0].name,name1);
        printf(“★=>修改成功!<=★\n”);
         };break;
     case 3:{
        printf(“★=>请输入修改的性别<=★\n”);     /*性别修改*/
        scanf(“%s”,&sex1);
        strcpy(null[0].sex,sex1);
        printf(“★=>修改成功!<=★\n”);
         };break;
     case 4:{
        printf(“★=>请输入修改的班级<=★\n”);     /*班级修改*/
        scanf(“%s”,&class_no1);
        strcpy(null[0].class_no1,class_no1);
        printf(“★=>修改成功!<=★\n”);
         };break;
      };break;
  case 4:printf(“\t\t★1===========>学号修改<=★\n
        \t\t★2===========>姓名修改<=★\n
        \t\t★3===========>性别修改<=★\n
        \t\t★4===========>班级修改<=★\n
      “);
      scanf(“%d”,&n);
      switch (n)
      {
     case 1:{
        printf(“★=>请输入修改的学号<=★\n”);     /*学号修改*/
        scanf(“%d”,&no1);
        null[0].no=no1;
        printf(“★=>修改成功!<=★\n”);
         };break;
     case 2:{
        printf(“★=>请输入修改的姓名<=★\n”);     /*姓名修改*/
        scanf(“%s”,&name1);
        strcpy(null[0].name,name1);
        printf(“★=>修改成功!<=★\n”);
         };break;
     case 3:{
        printf(“★=>请输入修改的性别<=★\n”);     /*性别修改*/
        scanf(“%s”,&sex1);
        strcpy(null[0].sex,sex1);
        printf(“★=>修改成功!<=★\n”);
         };break;
     case 4:{
        printf(“★=>请输入修改的班级<=★\n”);     /*班级修改*/
        scanf(“%s”,&class_no1)
        strcpy(null[0].class_no,class_no1);
        printf(“★=>修改成功!<=★\n”);
         };break;
      };break;
  case 5:printf(“\t\t★1===========>学号修改<=★\n
        \t\t★2===========>姓名修改<=★\n
        \t\t★3===========>性别修改<=★\n
        \t\t★4===========>班级修改<=★\n
      “);
      scanf(“%d”,&n);
      switch (n)
      {
     case 1:{
        printf(“★=>请输入修改的学号<=★\n”);     /*学号修改*/
        scanf(“%d”,&no1);
        null[0].no=no1;
        printf(“★=>修改成功!<=★\n”);
         };break;
     case 2:{
        printf(“★=>请输入修改的姓名<=★\n”);     /*姓名修改*/
        scanf(“%s”,&name1);
        strcpy(null[0].name,name1);
        printf(“★=>修改成功!<=★\n”);
         };break;
     case 3:{
        printf(“★=>请输入修改的性别<=★\n”);     /*性别修改*/
        scanf(“%s”,&sex1);
        strcpy(null[0].sex,sex1);
        printf(“★=>修改成功!<=★\n”);
         };break;
     case 4:{
        printf(“★=>请输入修改的班级<=★\n”);     /*班级修改*/
        scanf(“%s”,&class_no1);
        strcpy(null[0].class_no,class_no1);
        printf(“★=>修改成功!<=★\n”);
         };break;
      };break;
  case 6:printf(“\t\t★1===========>学号修改<=★\n
        \t\t★2===========>姓名修改<=★\n
        \t\t★3===========>性别修改<=★\n
        \t\t★4===========>班级修改<=★\n
      “);
      scanf(“%d”,&n);
      switch (n)
      {
     case 1:{
        printf(“★=>请输入修改的学号<=★\n”);     /*学号修改*/
        scanf(“%d”,&no1);
        null[0].no=no1;
        printf(“★=>修改成功!<=★\n”);
         };break;
     case 2:{
        printf(“★=>请输入修改的姓名<=★\n”);     /*姓名修改*/
        scanf(“%s”,&name1);
        strcpy(null[0].name,name1);
        printf(“=>修改成功!<=\n”);
         };break;
     case 3:{
        printf(“★=>请输入修改的性别<=★\n”);     /*性别修改*/
        scanf(“%s”,&sex1);
        strcpy(null[0].sex,sex1);
        printf(“★=>修改成功!<=★\n”);
         };break;
     case 4:{
        printf(“★=>请输入修改的班级<=★\n”);     /*班级修改*/
        scanf(“%s”,&class_no1);
        strcpy(null[0].class_no,class_no1);
        printf(“★=>修改成功!<=★\n”);
         };break;
      };break;
 }
 k=0;
 fp=fopen(“student.txt”,”w”);
 for (k=0;k<12;k++)
 {
  fwrite(&null[k],sizeof(stu),1,fp);            /*分别将初始化的结构体信息写入文件中student.txt*/
 }
 fclose(fp);
 fp=NULL;
}
/*====================================================================
4.查询函数:1.按学号查询2.按姓名查询3.按性别查询4.按班级查询5.全部显示
====================================================================*/
void find()
{
 char a[50];                   /*选择查询方式变量*/
 char b[50];                   /*条件查询选项变量*/
 char s[50];                   /*输入的查询内容*/
 FILE *fp;
 fp=fopen(“student.txt”,”r”);              /*打开student.txt文件*/
 for (i=0;i<15;i++)
 {
  fread(&null[i],sizeof(stu),1,fp);
 }
 fclose(fp);                   /*关闭student.txt文件*/
 fp=NULL;
 printf(“\t\t★1===========>条件查询<=★\n\t\t★2===========>全部显示<=★\n”);
 printf(“★=>请选择<=★:”);
 scanf(” %s”,&a);
 if(strcmp(“1″,a)==0)
 {
  printf(“\t\t★1===========>按学号查询<=★\n
    \t\t★2===========>按姓名查询<=★\n
    \t\t★3===========>按性别查询<=★\n
    \t\t★4===========>按班级查询<=★\n
    ”);
  scanf(” %s”,&b);
  if(strcmp(“1″,b)==0)               /*按学号查询*/
  {
   prinf(“★=>请输入要查找学号<=★:”);           
   scanf(” %s”,&s);
   for(int i=0;i<14;1++)
   {
    if(strcmp(null[i].no,s)==0))
    {
     printf(“\t\t学号:%s-姓名:%s-性别:%s-班级:%s\n”,null[i].no,null[i].name,null[i].sex,null[i].class_no);
    }
   }
  }
  if(strcmp(“2″,b)==0)               /*按姓名查询*/
  {
   prinf(“★=>请输入要查找姓名<=★:”);
   scanf(” %s”,&s);
   for(int i=0;i<14;1++)
   {
    if(strcmp(null[i].name,s)==0))
    {
     printf(“\t\t学号:%s-姓名:%s-性别:%s-班级:%s\n”,null[i].no,null[i].name,null[i].sex,null[i].class_no);
    }
   }
  }
  if(strcmp(“3″,b)==0)               /*按性别查询*/
  {
   prinf(“★=>请输入要查找性别<=★:”);
   scanf(” %s”,&s);
   for(int i=0;i<14;1++)
   {
    if(strcmp(null[i].sex,s)==0))
    {
     printf(“\t\t学号:%s-姓名:%s-性别:%s-班级:%s\n”,null[i].no,null[i].name,null[i].sex,null[i].class_no);
    }
   }
  }
  if(strcmp(“4″,b)==0)               /*按班级查询*/
  {
   prinf(“★=>请输入要查找班级<=★:”);
   scanf(” %s”,&s);
   for(int i=0;i<14;1++)
   {
    if(strcmp(null[i].class_no,s)==0))
    {
     printf(“\t\t学号:%s-姓名:%s-性别:%s-班级:%s\n”,null[i].no,null[i].name,null[i].sex,null[i].class_no);
    }
   }
  }
 }
 if(strcmp(“2″,a)==0)                /*全部显示*/
 {
  printf(“★=>你添加的信息如下:<=★\n
  for(int i=0;i<14;i++)
  {
   if(null[i].no!=0)
   {
    printf(“\t\t学号:%s-姓名:%s-性别:%s-班级:%s\n”,null[i].no,null[i].name,null[i].sex,null[i].class_no);
   }
  }
 }
}
/*======
清屏函数
======*/
void clear()
{
    system(“cls”);
}
/*===========================================
创建文件写入函数login.txt存放学员的账号和密码
===========================================*/
void create(FILE *fp)
{
 fp=fopen(“login.txt”,”wb”);               /*打开login.txt文件*/
    fclose(fp);                   /*关闭login.txt文件*/
 fp=NULL;
}
/*==========
用户登陆验证
==========*/
void authen()
{
 FILE *fc;
 create(f);                   /*调用文件写入函数*/
 fc=fopen(“login.txt”,”wb”);
 char a[50];
 char v[10];
 int judge=0; 
 while(true)
 {
  printf(“\t\t=>请输入用户名<=:”);            /*输入用户名字*/
        scanf(” %s”,a);
        printf(“\t\t=>请输入密码<=:”);             /*输入用户密码*/
        code(v);                  /*调用密码函数*/
  for(int i=0;i<MAX;i++)
  {
   if(strcmp(n,a[i].name)==0 && strcmp(b,a[i].password)==0)                          
   {
    judge=1;                /*判断输入时的正确是否 返回值, 正确返回1 ,不正确返回0*/
       if(strcmp(a[i].name,”cxl9908″)==0)
    {
     admins=1;
    }
    else
    {
     admins=2;
    }
    break;
   }
    }
       if(judge==0)
    { 
     printf(“\t\t★=>输入有误请重新输入!<=★\n”);        /*输入错误提示语言*/
    }
    if(judge==1)
    {
     break;
    }    
 }
 fclose(fc);
 fc=NULL;
}
/*==========
开始画面函数
==========*/
void landing()
{
 printf(“\t\t★=>系统加载ing……………<=★\n\n\n\n”);
 for(int i=0;i<20;i++)
 {
  printf(“■”);
  Sleep(100);
 }
}
/*====
主函数
====*/
void main()
{
 int i;                    /*选择变量*/
 char t[50];                   /*添加变量*/
 char s[50];                   /*删除变量*/
 char g[50];                   /*修改变量*/
 char c[50];                   /*查询变量*/
 landing();                   /*调用开始画面函数*/
 clear();                   /*调用清屏函数*/
 printf(“\n\t■□■□■□■□■□■□■□■□■□■□■□■□■□■□■\n”);   /*主界面信息*/
    printf(“\n\t□■□■□■□■☆1======>添加学员信息<=☆■□■□■□■□\n”);
    printf(“\n\t■□■□■□■□★2======>查询学员信息<=★□■□■□■□■\n”);
    printf(“\n\t□■□■□■□■☆3======>修改学员信息<=☆■□■□■□■□\n”);
    printf(“\n\t■□■□■□■□★4======>删除学员信息<=★□■□■□■□■\n”);
    printf(“\n\t□■□■□■□■☆5======>  退出程序  <=☆■□■□■□■□\n”);
    printf(“\n\t■□■□■□■□■□■□■□■□■□■□■□■□■□■□■\n”);
 FILE *fp;
 fp=fopen(“student.txt”,”w”);
  for(i=0;i<MAX;i++)
 {
  fwrite(&s[i],sizeof(stu),1,fp);                                                 /*分别将初始化的结构体信息写入文件中student.txt*/     
 }
    fclose(fp);
 fp=NULL;
 prinf(“★=>请选择<=★\n”)
 scanf(” %d”,&i);
 if(admins=1)
 {
  switch(i)
  {
   case 1:add(t);break;              /*调用添加函数*/
   case 2:remove(s);break;              /*调用删除函数*/
   case 3:revised(g);break;             /*调用修改函数*/
   case 4:find(c);break;              /*调用查询函数*/
   case 5:exit(0);break;
  }
 }
 if(admins=2)
 {
  find(c);
 }
}
有些问题我自己笨没法解决`望高手指点指点


能用就行了“改好了发我QQ:286394794也行


留下一个回复