| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1053 人关注过本帖
标题:求解!运行程序停止工作
只看楼主 加入收藏
hhj438921
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2018-1-16
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
求解!运行程序停止工作
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define H1 p->data.clss,p->data.name,p->data.xh,p->data.cyuyan,p->data.jisuanji,p->data.wangluojichu,p->data.zhiyeshengya,p->data.tiyu
#define H2 p->data.clss,p->data.name,p->data.xh,p->data.cyuyan,p->data.jisuanji,p->data.wangluojichu,p->data.zhiyeshengya,p->data.tiyu
#define T1 "|      班级      |  姓名  |  学号  |  C语言  |  计算机英语  |  网络基础  |  职业生涯  |  体育  |\n"
#define T2 "|%10s|%8s|%8s|%8d|%10d|%8d|%8d|%8d|\n"
#define T3 "------------------------------------------------------------------------------------------------\n"

#define MAX 10
struct book{
    char clss[MAX];  /*班级*/
    char name[MAX];  /*姓名*/
    char xh[MAX];  /*学号*/
    int cyuyan;  /*C语言*/
    int jisuanji;  /*计算机英语*/
    int wangluojichu;  /*网络基础*/
    int zhiyeshengya;  /*职业生涯*/
    int tiyu;  /*体育*/
};

typedef struct node
{
    struct book data;
    struct node *next;
}Node,*Link;

/*数据输出*/
void Output(Link head)
{
    Node *p;
    p=head->next;
    if(p==NULL)
    {
        printf("\n文件为空\n");
        getchar();
        return;
    }
    printf(T3);
    printf(T1);
    while(p!=NULL)
    {
        printf(T3);
        printf(T2,H2);
        p=p->next;
    }
    printf(T3);
}

/*数据输入*/
void Input(Link head)  /*链表头做参数传入*/
{
    int flag=1;  /*判断是否继续输入的变量*/
    Node *p;
    Node *ptr;
    FILE *fp;
    Output(head);
    fp=fopen("H:\\C语言.txt","a");
    if(fp==NULL)
    {
        printf("file open error");
        exit(0);
    }
    p=head;
    ptr=p;
    while(p->next!=NULL)
    {
        p=p->next;
        ptr=p;
    }
    while(flag)
    {
        p=(Node*)malloc(sizeof(Node));
        printf("输入学生信息:班级,姓名,学号,C语言,计算机英语,网络基础,职业生涯,体育:\n");
        scanf("%s %s %s %d %d %d %d %d",H1);
        printf("\n是否继续输入(1.是,0.否)");
        scanf("%d",&flag);
        fprintf(fp,"%s",p->data.clss);    /*写入文件*/
        fprintf(fp,"%s",p->data.name);
        fprintf(fp,"%s",p->data.xh);
        fprintf(fp,"%d",p->data.cyuyan);
        fprintf(fp,"%d",p->data.jisuanji);
        fprintf(fp,"%d",p->data.wangluojichu);
        fprintf(fp,"%d",p->data.zhiyeshengya);
        fprintf(fp,"%d",p->data.tiyu);
        
        ptr->next=p;  /*将p写入链表*/
        p->next=NULL;
        ptr=ptr->next;
    }
    fclose(fp);  /*关闭文件*/
 }
 
 /*学号,姓名查询函数*/
 void Search(Link head)  /*链表头做参数传入*/
 {
     Node *p;
     int choice;  /*用户选择变量*/
     int flag=0;  /*判断是否存在查询的信息*/
     char xh[20];  /*存储学号的变量*/
     char name[20];  /*存储姓名的变量*/
     p=head->next;
     printf("1:按学号查询;2:按姓名查询:");
     scanf("%d",&choice);
     if(choice==1)  /*以学号搜索*/
     {
         printf("请输入学号:");
         scanf("%s",xh);
         while(p!=NULL)
         {
             if(strcmp(xh,p->data.xh)==0)  /*找到节点*/
             {
                 printf(T3);
                 printf(T1);
                 printf(T3);
                 printf(T2,H2);  /*输出节点内容*/
                printf(T3);
                flag=1;
              }
              p=p->next;
         }
         if(flag=0)  /*没有该学号的信息*/
         printf("\n没有该学号学生信息\n");
      }
      else
      if(choice==2)  /*以姓名搜索*/
      {
          printf("请输入姓名:");
          scanf("%s",name);
          while(p!=NULL)
          {
              if(strcmp(name,p->data.name)==0)  /*找到节点*/
              {
                  printf(T3);
                  printf(T1);
                  printf(T3);
                  printf(T2,H2);  /*输出节点内容*/
                printf(T3);
                flag=1;
               }
               p=p->next;
          }
          if(flag==0)  /*没有该姓名的信息*/
          printf("\n没有该姓名学生信息\n");
       }
       else
       printf("\n你的选择有错误,请不要搞事情!\n");  /*没有选择1或2,输入提示*/
        
  }

/*数据信息更改*/
void Update(Link head)  /*链表头做参数传入*/
{
    Node *p,*ptr;
    FILE *fp;
    int n=1;  /*判断是否继续更改*/
    int flag=0;  /*判断姓名是否存在的参数*/
    char name[20];  /*存储修改的姓名*/
    fp=fopen("H:\\C语言.txt","w");  /*文件打开*/
    if(fp==NULL)
    {
        printf("File open error!");
        getchar();
        exit(0);
     }
     while(n!=0)
     {
         p=head->next;
         if(p==NULL)
         {
             printf("\n文件为空\n");
             getchar();
             return;
         }
         printf("输入你想修改的姓名:");
         scanf("%s",name);
         while(p!=NULL)
         {
             if(strcmp(name,p->data.name)==0)  /*找到节点*/
             {
                 printf(T3);
                 printf(T1);
                 printf(T3);
                 printf(T2,H2);  /*原有信息输出*/
                 printf(T3);
                 printf("输入新的学生信息:班级,姓名,学号,C语言,计算机英语,网络基础,职业生涯,体育:");  /*更新内容*/
                 scanf("%s %s %s %d %d %d %d %d",H1);
                 flag=1;
                 printf("是否继续更改(1.是,0.否):");
                 scanf("%d",&n);
                 break;
              }
              p=p->next;
         }
         if(flag==0)  /*没有信息*/
         {
             printf("没有修改学生信息\n");
             printf("是否继续更改(1.是,0否):");
             scanf("%d",&n);
          }
      }
      ptr=head->next;
      while(ptr!=NULL)  /*重新将链表内容写入文件*/
      {
          fprintf(fp,"%s",ptr->data.clss);
          fprintf(fp,"%s",ptr->data.name);
          fprintf(fp,"%s",ptr->data.xh);
          fprintf(fp,"%d",ptr->data.cyuyan);
          fprintf(fp,"%d",ptr->data.jisuanji);
          fprintf(fp,"%d",ptr->data.wangluojichu);
          fprintf(fp,"%d",ptr->data.zhiyeshengya);
          fprintf(fp,"%d",ptr->data.tiyu);
          ptr=ptr->next;
       }
       fclose(fp);  /*关闭文件*/
 }
 
 /*数据删除*/
 void Delete(Link head)
 {
     FILE *fp;
     Node *p,*ptr;
     char name[20];
     int flag=0;
     fp=fopen("H:\\C语言.txt","w");  /*打开文件*/
     if(fp==NULL)
     {
         printf("File open error!");
         getchar();
         exit(0);
      }
      ptr=head;
      p=head->next;
      printf("输入你想删除的学生姓名:");  /*姓名删除学生信息*/
      scanf("%s",name);
      while(p!=NULL)
      {
          if(strcmp(name,p->data.name)==0)  /*找到学生信息的节点*/
          {
              ptr->next=p->next;  /*实现删除功能*/
              p->next=NULL;
              free(p);  /*释放P的内存空间*/
              flag=1;  /*找到节点*/
              break;
           }
           else
           {
               ptr=p;
               p=p->next;
           }
       }
       if(flag=0)  /*没有此信息,输出提示*/
       printf("\n没有你想删除的学生信息\n");
       ptr=head->next;
       while(ptr!=NULL)  /*链表内容重新写入文件*/
       {
           fprintf(fp,"%s",ptr->data.clss);
           fprintf(fp,"%s",ptr->data.name);
           fprintf(fp,"%s",ptr->data.xh);
           fprintf(fp,"%d",ptr->data.cyuyan);
           fprintf(fp,"%d",ptr->data.jisuanji);
           fprintf(fp,"%d",ptr->data.wangluojichu);
           fprintf(fp,"%d",ptr->data.zhiyeshengya);
           fprintf(fp,"%d",ptr->data.tiyu);
           ptr=ptr->next;
        }
 }
 
 /*选择错误提醒函数*/
 void wrong()
 {
     printf("\n你不要搞事情!!!\n");
  }
  
/*菜单函数*/
void menu()
{
    system("cls");
    printf("\n============班级信息系统============\n");
    printf("------------------------------------\n");
    printf("|          1.录入学生信息          |\n");
    printf("------------------------------------\n");
    printf("|          2.查看学生信息          |\n");
    printf("------------------------------------\n");
    printf("|          3.查询学生信息          |\n");
    printf("------------------------------------\n");
    printf("|          4.修改学生信息          |\n");
    printf("------------------------------------\n");
    printf("|          5.删除学生信息          |\n");
    printf("------------------------------------\n");
    printf("|          0.退出                  |\n");
    printf("------------------------------------\n");   
 }
 
/*main主函数*/
int main(void)
{
    Link head;
    int choice=1;
    Node *p;
    Node *tail;
    FILE *fp;
    fp=fopen("H:\\C语言.txt","a+");
    if(fp==NULL)  /*打开文件*/
    {
        exit(0);
     }
     head=(Node*)malloc(sizeof(Node));  /*链表头建立*/
     if(!head)
     {
         return 0;
      }
    head->next=NULL;
    tail=head;
    while(fgetc(fp)!=EOF)
    {
        fseek(fp,-1,1);
        p=(Node*)malloc(sizeof(Node));
        if(!p)
        {
            return 0;
        }
        fscanf(fp,"%s",p->data.clss);
        fscanf(fp,"%s",p->data.name);
        fscanf(fp,"%s",p->data.xh);
        fscanf(fp,"%d",p->data.cyuyan);
        fscanf(fp,"%d",p->data.jisuanji);
        fscanf(fp,"%d",p->data.wangluojichu);
        fscanf(fp,"%d",p->data.zhiyeshengya);
        fscanf(fp,"%d",p->data.tiyu);
        p->next=NULL;
        tail->next=p;
        tail=p;
     }
     fclose(fp);  /*关闭文件*/
     while(1)
     {
         menu();
         printf("按回车结束\n");  /*提示任意键退出*/
         printf("请选择:");  /*选择操作*/
         scanf("%d",&choice);
         if(choice==0)  /*选择0,退出*/
         {
             printf("感谢你的使用,再见!\n");
             printf("按任意键退出\n");
             getchar();
             break;
          }
          switch(choice)
          {
              case 1:Input(head);
              break;
              case 2:Output(head);
              getch();
              break;
              case 3:Search(head);
              getch();
              break;
              case 4:Update(head);
              getch();
              break;
              case 5:Delete(head);
              getch();
              break;
              default:wrong();
              break;
          }
      }
      getchar();
      return 0;
}
搜索更多相关主题的帖子: data name next printf fprintf 
2018-01-16 22:19
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10607
专家分:43186
注 册:2014-5-20
收藏
得分:20 
逐个模块调试,较容易发现问题所在。
2018-01-17 06:21
hhj438921
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2018-1-16
收藏
得分:0 
Code,Dev-c++,VC6.0都试过,win7,xp,win10系统也试过。就是不行
2018-01-17 09:20
快速回复:求解!运行程序停止工作
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.043986 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved