发新话题
打印

[讨论]运行时老是提示出错 大家研究研究

[讨论]运行时老是提示出错 大家研究研究

议题提交:傲雪
信息来源:邪恶八进制信息安全团队

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#define N sizeof(struct student)
FILE*fp;
struct student
{
char name[20];
char num[10];
char sex;
float mark;
struct student *next;
};

struct student *head=NULL;
/*dummyFunction这个函数是为了避免结构体类型输入时float型连接错误,而特意写的一个函数,此函数没有被任何函数调用,但起到了补丁

的作用*/
void dummyFunction()
  {
     float dummyFloat;
     scanf("%f",&dummyFloat);
     printf("%f",dummyFloat);
  }

/*这是刚建立學生信息时调用的函数,这也是个基本的功能,相信大家都能实现,被主函数调用*/
struct student *create_message()
{
struct student *p1,*p2;
int i;
int k;
printf("input the student&#39;s sum:");
scanf("%d",&k);
head=NULL;
if(k>0) ;
{
head=p1=p2=(struct student *)malloc(N);
printf("input the student&#39;s name,cardnum,sex,mark:\n");
scanf("%s %s %s %f",
p1->name,p1->num,&p1->sex,&p1->mark);       /*每当有输入或修改學生的信息时,被调用*/

for(i=1;i<k;i++)
{
  p1=(struct student *)malloc(N);
  fflush(stdin);
  scanf("%s %s %s %f",
  p1->name,p1->num,&p1->sex,&p1->mark);    /*每当有输入或修改學生的信息时,被调用*/
  p2->next=p1;
  p2=p1;
}
p1->next=NULL;
printf("finish input!");
}
return (head);
}


/*函数功能:输出學生信息*/
struct student *output_message()
{
struct student *p1;
if(head==NULL) return;
else
printf("\nname  cardnum sex  mark:\n");
p1=head;
do
{
  printf("%s %s %s %f\n",
  p1->name,p1->num,p1->sex,p1->mark);

  p1=p1->next;
}while(p1!=NULL);

printf("press any key!!");
getch();
}



/*函数功能:删除某个或多个學生信息,当然其中必定先要查找的功能,查找又分两种查询方式:1,卡号2,學生名;查找以后再删除,

当然完成这个程序选用的结构是:单链表。所以删除也比较方便,被主函数调用*/
struct student *delete_message()
{
struct student *p1,*p2;
int x;
char del_num[10];
char del_name[20];
char del[10];
char cpy1[10],cpy2[20];
system("cls");
system("cls");
if(head==NULL)
return(NULL);
else
do
{
do
{
system("cls");
printf("input the student&#39;s messages:\n" );
printf(" *1.the student&#39;s card number\n" );
printf(" *2.the student&#39;s name     \n" );
printf(" choice the number between 1 and2:");
fflush(stdin);
scanf("%d" ,&x);
switch(x)
{
case 1:printf("input the student&#39;s card number:\n" );
fflush(stdin);
scanf("%s" ,del_num);break;
case 2:printf("input student&#39;s name:\n" );
fflush(stdin);
scanf("%s%s" ,del_name);break;
}
}while(x!=1&&x!=2);
p2=p1=head;
if(x==1)
while(p1!=NULL)            /*寻找和输入信息相同的學生信息*/
  {
  strcpy(cpy1,p1->num);
  if(!strcmp(cpy1,del_num))
   { if(p1==head)
    head=p1->next;
    else
    p2->next=p1->next;
    printf("delete successful!\n" ); free(p1);
    free(p1);
    break;
   }
  p2=p1;
  p1=p1->next;
  }


else
while(p1!=NULL)
  {
  strcpy(cpy2,p1->name);
  if(!strcmp(cpy2,del_name))
   { if(p1==head)
    head=p1->next;
    else
    p2->next=p1->next;
    printf("delete successful!\n" );
    free(p1);
    break;
   }
  p2=p1;
  p1=p1->next;
  }

if(p1==NULL)
printf("\n donot find it\n\n" );
printf ("are you continues?\n input yes or no:\n");
scanf("%s",del);
}while(!strcmp(del,"yes"));
system("cls");
printf("delete successful!\n please press any key!:\n");
getch ();
return head;
}

/*函数功能:插入學生信息结点。是按卡号字符串的大小的顺序插入的。*/
struct student *insert_message()
{
struct student *p1, *p2,*p;
char answer[5];
char cpy1[10],cpy2[10];
system("cls");
/*fp=fopen("student.txt","wb");
if(head==NULL) return(NULL);
else                */
do
{
  system("cls");
  p=(struct student *)malloc(N);
  printf("input name,card number,mark \n");
  scanf("%s %s %c %s %s %f %f",
  p->name,p->num,&p->sex,&p->mark);
if(head==NULL)
  {
   head=p;
   p->next=NULL;
   return ;
  }
  else
  {
   p1=p2=head;
   strcpy(cpy1,p1->num);
   strcpy(cpy2,p->num);
   while(strcmp(p1->num,cpy2)<0)
    {

    p2=p1;
    p1=p1->next;
    if (p1==NULL)
    {
     break;
    }
    strcpy(cpy1,p1->num);
    }
   if(strcmp(cpy1,cpy2)>=0)
    {
    if(p1==head)
     {
      head=p;
      p->next=p1;
     }
    else
     {
      p2->next=p;
      p->next=p1;
     }
    }
   if(p1==NULL)
    {
    p2->next=p;
    p->next=NULL;
    }
   }
printf("insert successful! are you continues?\n input yes or no:\n");
fflush(stdin);
scanf("%s",answer);
system("cls");
}while(!strcmp(answer,"yes"));
printf("insert successful! please press any key!:\n");
getch ();
return;
}




/*函数功能:查询學生信息。当然也和删除一样,要先找到要查询的學生,然后将其输出。被主函数调用*/
struct student *inquire_message()
{
struct student *p1;
int x;
char cpy1[10],cpy2[20];
char name_[20];
char num_[10];
char answer[5];
system("cls");
printf("input the student&#39;s messages:\n");

system("cls");
fp=fopen("student.txt","wb");
if(head==NULL);
else do
{
do
{ system("cls");
printf("choose the inquire form:\n");
printf(" *1.the student&#39;s card number \n");
printf(" *2.the student&#39;s name \n");
printf(" choice the number between 1 and 2:");
scanf("%d",&x);
switch(x)
{
case 1:printf("input the student&#39;s card number:\n");
scanf("%s",num_);break;
case 2:printf("input teacher&#39;s name :\n");
scanf("%s",name_);break;
}
}while(x!=1&&x!=2);
p1=head;
if(x==1)
while(p1!=NULL)
  {
  strcpy(cpy1,p1->num);
  if(!strcmp(cpy1,num_))
   break;
  p1=p1->next;
  }


else
while(p1!=NULL)
  {
  strcpy(cpy2,p1->name);
  strcpy(cpy1,p1->part);
  if(!strcmp(cpy2,name_))
    break;
  p1=p1->next;
  }
if(p1!=NULL)
{
printf("\n inquire student&#39;s messages successful!:\n");
printf("name,card number,mark:\n");
printf("%s %s %s %f",p1->name,p1->num,p1->sex,p1->mark) ;
}
else
printf("\n donot find it\n\n");
printf("are you continues?\n input yes or no:\n");
scanf("%s",answer);
}while(!strcmp(answer,"yes"));
system("cls");
printf (" please press any key!:");
getch ();
return (head);
}




/*函数功能:修改學生信息。当然也先要先找到要修改的學生,然后选择學生的某项信息进行修改,当然修改后要调用那些函数。被主函数调

用*/
struct student *revise_message()
{
struct student *p1, *p2;
int x;
int y;
char cpy1[10],cpy2[20];
char name_[20];
char num_[10];
char answer[5];
system("cls");
printf("input the student&#39;s messages:\n");
system("cls");
/*fp=fopen("student.txt","wb");*/
if(head==NULL);
else do
{
do
{ system("cls");
printf("choose the inquire form:\n");
printf(" *1.the student&#39;s card number:\n");
printf(" *2.the teacher&#39;s name:\n");
printf(" choice the number between 1 and 2:");
scanf("%d",&x);
switch(x)
{
case 1:printf("input the student&#39;s card number:\n");
scanf("%s",num_);break;
case 2:printf("input student&#39;s name:\n");
scanf("%s",name_);break;
}
}while(x!=1&&x!=2);
p1=head;
if(x==1)
while(p1!=NULL)
  {
  strcpy(cpy1,p1->num);
  if(!strcmp(cpy1,num_))
   break;
  p1=p1->next;
  }


else
while(p1!=NULL)
  {
  strcpy(cpy2,p1->name);
  if(!strcmp(cpy2,name_))
    break;
  p1=p1->next;
  }
if(p1!=NULL)
{
printf("\n find student&#39;s messages:");
printf("\n 3.name\n");
printf("\n 4.card number\n");
printf("\n 5.mark\n");
printf("\n choose the number between 3 and 4:\n");
scanf("%d",&y);
switch(y)
{
case 3:printf("revise name:\n");
scanf("%s",p1->name);break;
case 4:printf("revise number:\n");
scanf("%s",p1->num);break;
case 5:printf("revise mark:\n");
scanf("%f",p1->mark);break;
}              /*每当有输入或修改學生的信息时,被调用*/
printf("\n reviser successful! \n") ;
}
else printf("\n donot find it\n\n");
printf ("are you continues?input yes or no:\n");
scanf("%s",answer);
}while(!strcmp(answer,"yes"));
system("cls");
printf (" please press any key!:\n");
getch ();
return head;
}




/*函数的功能:将员工的信息保存到硬盘。每当将退出系统的时候调用*/
save_message()
{
struct student *p1=head;
clrscr();
fp=fopen("student.txt","wb");
if(fp==NULL)
{
  printf("building fail!\n ");
  getch();
  return;
}
while(p1!=NULL)
{
  if(fwrite(p1,sizeof(struct student),1,fp)!=1)
  {
   printf("write wrong!\n");
   return;
  }
  p1=p1->next;
}
fclose(fp);
printf("save successful!\n press any key!:\n");
getch();
}

/*函数功能:将學生信息从硬盘中读取出来。这个上面函数正好是个互补的过程,每当系统刚启动的时候被调用*/
openfile()
{
struct student *p1, *p2;
int n=1;
if((fp=fopen("student.txt","rb"))==NULL)
{
  printf("donot have the student&#39;s messages!\n press any key:\n");
  getch();
  return(NULL);
}
p1=p2=(struct student *)malloc(N);
if(p1==NULL)
{
  printf("the room donot enough!!\n press any key:\n");
  getch();
  return(NULL);
}

if(fread(p1,sizeof(struct student),1,fp)!=1)
{
  printf("please create the student&#39;s date before!!\n press any key :\n");
  getch();
  return(NULL);
}

if(p1->next==NULL) head=p1;
else
{ do
  {
  if(n==1) head=p1;
  else p2->next=p1;
  n=n+1;
  p2=p1;
  if((p1=(struct student*)malloc(N))==NULL)
   {
    printf("the room donot enough!!\n press any key:\n");
    getch();
    return(NULL);
   }
  if(fread(p1,sizeof(struct student),1,fp)!=1)
   {
    printf("out put the messages fail!!\n press any key:\n");
    getch();
    return(NULL);
   }
  }while(p1->next!=NULL);
  p2->next =p1;
}
fclose(fp);
return(NULL);
}




/*主函数*/
main()
{
int choice;
system("cls");
openfile();/*调用读取學生信息函数,如果硬盘里面没有存储信息,它将提示你建立學生信息*/
do
{/*菜单选项:建立信息,输出信息,删除信息,插入信息,查询信息,修改信息*/
  printf("\n\n ****jiaoshigongziguanlixitong****\n\n");
  printf(" 1.create student&#39;s message\n\n");
  printf(" 2.output student&#39;s message\n\n");
  printf(" 3.delete student&#39;s message\n\n");
  printf(" 4.insert student&#39;s message\n\n");
  printf(" 5.inquire student&#39;s message\n\n");
  printf(" 6.revise student&#39;s message\n\n");
  printf(" 0.exit system\n");
  printf(" choice the number between 0 and 6:");
  fflush(stdin);
  scanf("%d", &choice);

  switch(choice)
  {
  case 1: create_message();break;
  case 2: output_message();break;
  case 3: delete_message();break;
  case 4: insert_message();break;
  case 5: inquire_message();break;
  case 6: revise_message();break;
  case 0: break;
  }
}while(choice!=0);
save_message();/*存储學生信息函数被调用*/
printf("\n jiaoshigongziyichucong!\n");
}
[fly]。。我。。什么也不缺。。。就。。缺你。。。   。。你。。什么都缺。。。就。。不缺我。。。[/fly]

TOP

发新话题