回复 4楼 tlliqi
好吧,我把我的函数全部发上来吧,虽然还是残次品,我还想着一个一个问题的解决呢。
#include<stdio.h>
#include<malloc.h>
#include<process.h>
#include<string.h>
#define Max_kind 10
#define Kind_increase 2
#define OK 1
#define ERROR -1
#define OVERFLOW -1
typedef struct
{char k;
int p;
}Foodkp;
typedef struct
{Foodkp *kp;
int kindth;
int All_kinds;
}food;
//初始化
int InitKind_f(food *L)
{L->kp=(Foodkp*)malloc(Max_kind*sizeof(Foodkp));
if(!L->kp)
exit(OVERFLOW);
L->kindth=0;
L->All_kinds=Max_kind;
return 0;
}
//插入
int KindInsert_f(food *L,int i,Foodkp d)
{if((i<1)||(i>L->kindth+1))
return ERROR;
Foodkp *Newbase;
if(L->kindth>=L->All_kinds)
{Newbase=(Foodkp*)malloc((L->All_kinds+Kind_increase)*sizeof(Foodkp));
if(!Newbase)
exit(OVERFLOW);
L->kp=Newbase;
L->All_kinds+=Kind_increase;
}
Foodkp *m,*n;
m=&(L->kp[i-1]);
for(n=&(L->kp[L->kindth-1]);n>=m;m--)
*(n+1)=*n;
*m=d;
++L->kindth;
return OK;
}
//删除
int KindDelete_f(food *L,int i,Foodkp d)
{if((i<1)||(i>L->kindth))
return ERROR;
Foodkp *m,*n;
m=&(L->kp[i-1]);
d=*m;
n=L->kp+L->kindth-1;
for(m++;m<=n;m++)
*(m-1)=*m;
--L->kindth;
return OK;
}
//查找定位删除
/*int FindDelete_f(food L,Foodkp d)
{int i=1;
while((i<=L.kindth)&&(L.k[i-1]!=d))
i++;
if(i<=L->kindth)
KindDelete_f(L,i,d)&&return i;
else return 0;
}*/
//返回给定位置的元素
int KindReturn_f(food L,int i)
{Foodkp d;
d=L.kp[i];
printf("%c%6d\n",d.k,d.p);
return 0;
}
//合并两个线性表
/*void MergeKind_f(food La,food Lb,food L)
{Foodkp *ga,*gb,*g,*ga_last,*gb_last;
ga=La.k;
gb=Lb.k;
L.All_kinds=L.kindth=La.kindth+Lb.kindth;
g=L.k=(Foodkp*)malloc(L.All_kinds*sizeof(Foodkp));
if(!L.k)
exit(OVERFLOW);
ga_last=La.k+La.kindth-1;
gb_last=Lb.k+Lb.kindth-1;
while(ga<=ga_last&&gb<=gb_last)
{if(*ga<=*gb)
*g++=*ga++;
else
*g++=*gb++;
}
while(ga<=ga_last)
*g++=*ga++;
while(gb<=gb_last)
*g++=*gb++;
}*/
main()
{printf("
欢迎来到食物系统\n\n");
food *L=(food*)malloc(Max_kind*sizeof(food));
InitKind_f(L);
//L->kp->k="鱼";
//L->k[3]={{"鱼",16}{"饭",8}{"汤",5}{"粥",1}};
printf("1.插入一种食物及其价格。\n");
printf("2.删除一种食物及其价格。\n");
printf("3.定位你想要查找的食物与价格并进行删除。\n");
printf("4.查找给定编号上的食物及价格。\n");
printf("5.合并2个食物与价格列表。\n");
int i;
printf("请输入你想实现的功能编号:");
scanf("d%\n",&i);
switch(i)
{case 1:{};break;
case 2:{};break;
case 3:{};break;
case 4:{};break;
case 5:{};break;
default:printf("输入错误\n");
}
return 0;
}
其中我给注释掉的地方就是出现问题的地方。
这是作业,真心求大神解救,最好帮我解决我其他不会的。