| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 624 人关注过本帖
标题:救命啊~!程序出错了
只看楼主 加入收藏
txijin
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2004-11-5
收藏
 问题点数:0 回复次数:2 
救命啊~!程序出错了

劳驾各位大虾帮我看一下,我写的这个程序那有问题啊,为什么是出错啊

谢谢

搜索更多相关主题的帖子: 救命 
2004-12-12 20:17
txijin
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2004-11-5
收藏
得分:0 

#include <stdio.h> #include <malloc.h> #include <stdlib.h> #include <string.h>

#define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2

//Status 是函数的类型,其值是函数结果状态代码 typedef int Status;

//---------线性表的动态分配顺序存储结构 #define LIST_INIT_SIZE 100 //线性表存储空间的初始分配量 #define LISTINCREMENT 10 //线性表存储空间的分配增量 #define COURSE_COUNT 2 //一次记录的商品的类型数

//定义商品的数据类型 typedef struct{ char name[20];//商品代码 float price;//商品单价 }course_merchandise;

//定义当日记录的数据类型 typedef struct{ int No; //销售编号 char date; //记录的日期 course_merchandise merchandise[COURSE_COUNT]; //存放COURSE_COUNT }ElemType2;

//顺序存储结构表指针类型 typedef struct { ElemType2 *elem; //表体的起始地址 int length; //表中结点数 int listsize; //当前分配的存储容量(以sizeof(Elemtype1)为单位 }SqList;

Status InitList_Sq(SqList *L){ //构造一个空的线性表L。 L->elem=(ElemType2 *)malloc(LIST_INIT_SIZE*sizeof(ElemType2)); if(! L->elem) return(OVERFLOW); //存储分配失败 L->length=0; //空表长度为0 L->listsize=LIST_INIT_SIZE; //初始存储容量(单位:结点数) return OK; } // InitList_Sq

int ListLength(SqList L){ //返回线性表L中数据元素的个数 return L.length; }//ListLength

GetElem(SqList L,int i,ElemType2 *e){ //返回线性表L中第i个数据元素的数据 *e=L.elem[i-1]; }//GetElem

Status ListInsert_Sq(SqList *L,int i,ElemType2 e){ //在顺序线性表L中第i个位置之前插入新的的元素e , //i的合法值为1<=i<=ListLength_Sq(L)+1 ElemType2 *newbase; ElemType2 *p,*q; if(i<1||i>L->length+1) return ERROR; // i 值不合法 if(L->length>=L->listsize){ //当前存储空间已满,增加分配 newbase=(ElemType2 *) realloc (L->elem, (L->listsize+LISTINCREMENT)*sizeof(ElemType2)); if(!newbase)exit(OVERFLOW); //存储分配失败 L->elem=newbase; //新基址 L->listsize+=LISTINCREMENT; //增加存储容量 }; q=&(L->elem[i-1]); //q为插入位置 for(p=&(L->elem[(L->length)-1]);p>=q;--p) *(p+1)=*p; //插入位置及之后的元素后移 *q=e; //插入e ++(L->length); //表长增1 return OK; }//Listinsert_Sq

Status Print(SqList L,int i){ //输出顺序存储结构线性表L中的第i个数据元素 int j; ElemType2 e; if(i<1 || i>ListLength(L)) return ERROR; //如果i超出范围,则返回 e=L.elem[i-1]; //输出格式:结点编号 销售编号 记录日期 商品代码 商品单价 printf(" 结点 %3d :%10ld %-10s ",i,e.No,e.date); for(j=0;j<COURSE_COUNT;j++) printf(" %-10s %-3.1f ",e.merchandise[j].name,e.merchandise[j].price);

printf("\n"); return OK; }//print

void PrintList(SqList L){ //输出顺序存储结构线性表L中所有数据元素的数据 int list_len,i; char aa[10];

list_len=ListLength(L); printf("共记录 %d 次\n\n",list_len); printf(" 销售编号 记录日期 商品代码 商品单价 \n"); for(i=1;i<=list_len;i++){ Print(L,i); } printf("\n\n"); printf("按任意符号后,再按回车键,返回.....");scanf("%s",aa); }//PrintList

void MergeList(SqList La,SqList Lb,SqList *Lc){ //已知线性表La和Lb中的数据元素按非递减排列。 //归并La和Lb得到新的Lc,Lc的数据元素也按值非递减排列

int i,j,k,La_len,Lb_len; ElemType2 ai,bj; i=j=1; k=0; La_len=ListLength(La); Lb_len=ListLength(Lb);

while((i<=La_len)&&(j<=Lb_len)){ GetElem(La,i,&ai); GetElem(Lb,j,&bj); if(ai.No<=bj.No){ ListInsert_Sq(Lc,++k,ai); ++i; } else{ ListInsert_Sq(Lc,++k,bj); ++j; } } while(i<=La_len){ GetElem(La,i++,&ai); ListInsert_Sq(Lc,++k,ai); }

while(j<=Lb_len){ GetElem(Lb,j++,&bj); ListInsert_Sq(Lc,++k,bj); } }//MergeList

main(){ SqList La,Lb,Lc; FILE *ftp,*ftp1,*ftp2; char file_name[40]; int i,j,n,k; char choice; ElemType2 e; //初始化线性表 La,Lb,Lc InitList_Sq(&La); InitList_Sq(&Lb); InitList_Sq(&Lc); do{ printf("*****************************************************************\n"); printf("\ta.记录当次销售\n"); //在已存在的线性表La中添加一个新结点 printf("\tb.遗漏补记\n"); //在已存在的线性表Lb中添加一个新结点 printf("\tc.即时保存(为了确保数据不丢失请即时保存)\n"); //将La、Lb中的数据各自存入一个数据文件 printf("\td.整理当日销售记录\n"); //合并线性表La和线性表Lb,结果存入线性表Lc中 printf("\te.保存当日销售记录\n"); //将最终的Lc存入一个数据文件中 printf("\tf.在屏幕上输出当日销售记录\n"); //在屏幕上输出Lc的所有数据 printf("\tg.清除上日已保存的销售记录\n"); //清除La、Lb和Lc中的所有数据 printf("\th.帮助\n"); //查询商品代码 printf("\tx.退出\n"); printf("*****************************************************************\n"); printf("请选择操作(a,b,c,d,e,f,g,h,x): "); scanf("%c",&choice);

switch(choice){ case 'a':while(e.No!=0){ printf("请输入销售商品的数据:\n"); printf("销售编号:");scanf("%ld",&(e.No)); printf("记录的日期:"); scanf("%s",e.date); for(j=0;j<COURSE_COUNT;j++){ printf("商品代码:",j+1); scanf("%s",e.merchandise[j].name); printf("商品单价:",j+1); scanf("%f",&(e.merchandise[j].price)); } } ListInsert_Sq( &La, ListLength(La)+1, e); break; case 'b':while(e.No!=0){ printf("请输入销售商品的数据:\n"); printf("销售编号:");scanf("%ld",&(e.No)); printf("记录的日期:"); scanf("%s",e.date); for(j=0;j<COURSE_COUNT;j++){ printf("商品代码:",j+1); scanf("%s",e.merchandise[j].name); printf("商品单价:",j+1); scanf("%f",&(e.merchandise[j].price)); } } ListInsert_Sq( &La, ListLength(La)+1, e); break; case 'c':printf("\n请输入数据文件的路径和文件名:"); scanf("%s",file_name); //生成线性表LA //strcpy(file_name,"d:\\List_data1.txt"); if((ftp=fopen(file_name,"w"))==NULL) { printf("不能建立数据文件!\n"); //exit(0); break; } n=ListLength(La); for(i=1;i<=n;++i){ GetElem(La, i,&e); fprintf(ftp,"%ld\n",e.No); fprintf(ftp,"%s\n",e.date); for(j=0;j<COURSE_COUNT;j++){ fprintf(ftp,"%s\n",e.merchandise[j].name); fprintf(ftp,"%f\n",e.merchandise[j].price); } } fprintf(ftp,"%ld\n",0); //本程序的数据文件必须以0结束 fclose(ftp); break;

case 'd':MergeList(La,Lb,&Lc); break;

case 'e':printf("\n请输入数据文件的路径和文件名:"); scanf("%s",file_name); //生成线性表LA //strcpy(file_name,"d:\\List_data1.txt"); if((ftp=fopen(file_name,"w"))==NULL) { printf("不能建立数据文件!\n"); //exit(0); break; } n=ListLength(Lc); for(i=1;i<=n;++i){ GetElem(La, i,&e); fprintf(ftp1,"%ld\n",e.No); fprintf(ftp1,"%s\n",e.date); for(j=0;j<COURSE_COUNT;j++){ fprintf(ftp1,"%s\n",e.merchandise[j].name); fprintf(ftp1,"%f\n",e.merchandise[j].price); } } fprintf(ftp1,"%ld\n",0); //本程序的数据文件必须以0结束 fclose(ftp1); break;

case 'f':PrintList( Lc); break;

case 'g':La.length=0; break;

case 'h':ftp2=("help","r"); do{ printf("关闭[0]"); scanf("%d",&k); }while(k!=0); fclose(ftp2); break; } }while(choice!='x');

}


------------------------------- ☆ 人人为我~!☆ ☆ 我为人人~!☆ ------------------------------
2004-12-12 20:37
牛虻
Rank: 1
等 级:新手上路
威 望:1
帖 子:472
专家分:0
注 册:2004-10-1
收藏
得分:0 

晕啊~~这么长啊,难怪没人帮你看,偶来只是给你个建议的,你认为最有可能出错的地方截取出来或标出来~


土冒
2004-12-21 20:42
快速回复:救命啊~!程序出错了
数据加载中...
 
   



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

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