| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 394 人关注过本帖
标题:学了c语言半年多,好不容易编个程序,结果不知为何程序老是出错,谢谢了
只看楼主 加入收藏
微笑现在
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2014-11-24
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
学了c语言半年多,好不容易编个程序,结果不知为何程序老是出错,谢谢了
程序代码:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
int N=0;
#define NA 20
struct train_information
{
    int id;    //%5d
    int seat[100][NA];   //%1d
    int node;    //%5d
    char name[20][NA];
    int month;
    int day;
    struct Time
    {
        int hour;
        int min;
    }Time[NA];
    struct train_information *next;
};




void save_traininformation(struct train_information *head)
{ 
    int i,j;
    struct train_information *p;
    FILE *fp;
    p=head;  
    fp=fopen("D:\\车次信息.txt","w");
    if(fp==NULL)
    {
        printf("error to open the file !\n");
        return ;
    }
    fprintf(fp,"%2d",N);
    while(p!=NULL)
    {
        fprintf(fp,"%5d",p->id);
        fprintf(fp,"%5d",p->node);
        for(j=0;j<100;j++)
        {
            for(i=0;i<p->node+1;i++)
            {
                fprintf(fp,"%1d",p->seat[j][i]);
            }
        }
        for(i=0;i<p->node+2;i++)
        {
            fprintf(fp,"%20s",p->name[i]);
        }
        fprintf(fp,"%5d",p->month);
        fprintf(fp,"%5d",p->day);
        for(i=0;i<p->node+2;i++)
        {
            fprintf(fp,"%5d",p->Time[i].hour);
            fprintf(fp,"%5d",p->Time[i].min);
        }
        p=p->next;
    }
    fclose(fp);
}


void print(struct train_information *head)
{
    int k;
    struct train_information *p;             
    p=head;
    printf("列车的基本信息如下:\n");
    while(p!=NULL)
    {
        printf("%d次列车   从%s出发   终点站%s\n",p->id,p->name[0],p->name[p->node+1]);
        printf("出发时间:%d月%d日\n",p->month,p->day);
        for(k=0;k<=p->node+1;k++)
        {
            printf("%s--    %d  %d\n",p->name[k],p->Time[k].hour,p->Time[k].min);
        }
        p=p->next;
    }
}



struct train_information* duqu()
{
    int j,i,count=0;
    FILE *fp;
    struct train_information *p,*pr,*head=NULL;
    p=pr=(struct train_information *)malloc(sizeof(struct train_information));
    fp=fopen ("D:\\车次信息.txt","r");
    fscanf(fp,"%2d",&N);
    printf("%d\n",N);
    while(count<N)
    { 
        fscanf(fp,"%5d",&p->id);
        fscanf(fp,"%5d",&p->node);
        printf("%d",p->node);
        for(j=0;j<100;j++)
        {
            for(i=0;i<p->node+1;i++)
            {
                fscanf(fp,"%1d",&p->seat[j][i]);
            }
        }
        for(i=0;i<p->node+2;i++)
        {
            fscanf(fp,"%20s",&p->name[i]);
        }
        fscanf(fp,"%5d",&p->month);
        fscanf(fp,"%5d",&p->day);
        for(i=0;i<p->node+2;i++)
        {
            fscanf(fp,"%5d",&p->Time[i].hour);
            fscanf(fp,"%5d",&p->Time[i].min);
        }
        count++;
        if(count==1)
        { 
            p->next=NULL;
            pr=p;
            head=p;
        }
        else 
        {       
            p->next=NULL;
            pr->next=p;
            pr=p;
        }
        p=(struct train_information *)malloc(sizeof(struct train_information));                   
    }
    free(p);
    return head;
}

void input_train()
{
    int j,k,n,w;
    FILE *fp;
    struct train_information *p,*pr,*head;
    p=pr=(struct train_information*)malloc(sizeof(struct train_information));
    fp=fopen ("D:\\车次信息.txt","w");
    if(fp==NULL)
    {
        printf("error to open the file!\n");
        return ;
    }
    for(w=0;;w++)
    {
        system ("cls");
        printf("写入第%d辆火车的信息:",w+1);
        printf("列车编号:");
        scanf("%d",&p->id);
        printf("输入列车中途停靠站点数量:");
        scanf("%d",&p->node);
        for(k=0;k<100;k++)
        {
                    for(j=0;j<p->node+1;j++)
                    {
                            p->seat[k][j]=1;    //train_information.seat表示座位状态
                    }
        }
        printf("输入列车始发站:");
        scanf("%s",&p->name[0]);
        for(k=1;k<p->node+1;k++)
        {
            printf("输入列车的下一站:");
            scanf("%s",&p->name[k]);
        }
        printf("输入列车终点站:");
        scanf("%s",&p->name[p->node+1]);
        printf("输入列车出发时间:\n");
        printf("month:");
        scanf("%d",&p->month);
        printf("day:");
        scanf("%d",&p->day);
        for(k=0;k<=p->node+1;k++)
        {
            printf("第%d站的出发时间:\n",k+1);
            printf("hour:");
            scanf("%d",&p->Time[k].hour);
            printf("minute:");
            scanf("%d",&p->Time[k].min);
        }
        if(w==0)
        {
            p->next=NULL;
            pr=p;
            head=p;
        }
        else
        {
            pr->next=p;
            pr=p;
        }
        printf("是否开始下一辆列车(0继续,1退出):");
        scanf("%d",&n);
        if(n==1)
        {
            break;
        }
        p=(struct train_information *)malloc(sizeof(struct train_information));

    }
    N=w+1;
    pr->next=NULL;
    save_traininformation(head);
    free(p);
    fclose(fp);
}

int main()
{
    struct train_information *head;
    input_train();
    head=duqu();
    print(head);
    return 0;
}
搜索更多相关主题的帖子: c语言 
2015-03-23 11:05
微笑现在
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2014-11-24
收藏
得分:0 
当运行到print函数时,老是出现一大堆莫名其妙的东西,结果程序就运行不了了
2015-03-23 11:07
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:20 
估摸着 你的“出错”应该是运行期错误吧   这个需要自己调试

DO IT YOURSELF !
2015-03-23 11:07
微笑现在
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2014-11-24
收藏
得分:0 
回复 3楼 wp231957
调试了很久,还是弄不好啊
2015-03-23 11:09
快速回复:学了c语言半年多,好不容易编个程序,结果不知为何程序老是出错,谢谢 ...
数据加载中...
 
   



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

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