当前位置: 代码迷 >> C语言 >> 这个程序有个函数的问题
  详细解决方案

这个程序有个函数的问题

热度:365   发布时间:2007-08-02 16:54:32.0
这个程序有个函数的问题

#include"stdio.h"
struct xinxi
{
char name[20];
char yuanxi[20];
long num;
int xiangmu;
int mingci;
};
struct xinxi a[50];
struct xinxi p;
void shuru()
{
int i;
char c;
for(i=0;i<50;i++)
{
printf("qing shu ru bi sai xiang mu bian hao\n");
scanf("%d",&a[i].xiangmu);
printf("\n name:\n");
scanf("%s",a[i].name);
printf("\n yuanxi \n");
scanf("%s",a[i].yuanxi);
printf("\n xuehao \n");
scanf("%ld",&a[i].num);
printf("\n ming ci \n");
scanf("%d",&a[i].mingci);
printf("\n continue yes or no? xie y|n\n");
scanf("%c",&c);
scanf("%c",&c);
if(c=='y') continue;
if(c=='n') break;
}

}
void chazhao()
{
int i,j;
long b;
printf("qing shu ru chao zhao de xue hao(0=tiaochu)\n");
scanf("%ld",&b);
printf("\n xiang mu \n");
scanf("%d",&j);
while(b!=0)
{
for(i=0;i<50;i++)
{
if(a[i].num==b&&a[i].xiangmu==j)
printf("xing ming :%s\n",a[i].name);
printf("yuan xi :%s\n",a[i].yuanxi);
printf("xiang mu :%d\n",a[i].xiangmu);
printf("xue hao: %ld\n",a[i].num);
printf("ming ci :%d\n",a[i].mingci);
}
printf("\n qing shu ru chao zhao de xue hao(0=tiaochu)\n");
scanf("%ld",&b);
printf("\n xiang mu \n");
scanf("%d",&j);
}

}
void cunchu(int n)
{
int i,j,k,h;
FILE *fw;
fw=fopen("D:\hujian.txt","w");
for(k=1;k<=n;k++)
{
printf("di %d xiang bi sai de ji lu\n",k);
fprintf(fw,"di %d xiang bi sai de ji lu\n",k);
for(h=0;h<50;h++)
{
if(a[h].xiangmu==k)
{
for(i=1;i<50;i++)
for(j=0;j<50-i;j++)
{
if(a[j].mingci>a[j+1].mingci)
{
p=a[j];a[j]=a[j+1];a[j+1]=p;
}
}

for(i=0;i<3;i++)
{
printf("%10s %10s %10ld %10d\n",a[i].name,a[i].yuanxi,a[i].num,a[i].mingci);
fprintf(fw,"%10s %10s %10ld %10d\n",a[i].name,a[i].yuanxi,a[i].num,a[i].mingci);
}

}}}
fclose(fw);
}

void shanchu(int n)
{
long b;
int a1,h,j,i,k;
FILE *fw;
fw=fopen("D:\\hujian.txt","w");
printf("qing shu ru xue hao\n");
scanf("%ld",&b);
printf("\n xiang mu \n");
scanf("%d",&a1);
for(h=0;h<50;h++)
{
if(a[h].num==b&&a[h].xiangmu==a1)
{
for(k=1;k<=n;k++)
{
fprintf(fw,"di %d xiang bi sai de ji lu\n",k);
for(i=1;i<50;i++)
for(j=0;j<50-i;j++)
{
if(a[j].mingci>a[j+1].mingci)
{
p=a[j];a[j]=a[j+1];a[j+1]=p;
}
}
for(i=h;i<50;i++)
a[i+1]=a[i];
for(i=0;i<3;i++)
{
fprintf(fw,"%10s %10s %10ld %10d\n",a[i].name,a[i].yuanxi,a[i].num,a[i].mingci);
}
}

}
}
}
int main()
{
int n,g,i,j,k;
FILE *fw;
fw=fopen("D:\\hujian.txt","w");
printf("*********** ****************** 1.shuru **************************************\n");
printf("*********** ****************** 2.chazhao **************************************\n");
printf("*********** ****************** 3.xianshi\cunchu **************************************\n");
printf("*********** ****************** 4.shanchu **************************************");
printf("\n qing xian shu ru bi sai xiang mu ge shu\n");
scanf("%d",&n);
printf("\nqing shu ru ni de xun ze(1-4)\n");
scanf("%d",&g);
while(g!=0)
{switch(g)
{
case 1:shuru();break;
case 2:chazhao();break;
case 3:cunchu(n);break;
case 4:shanchu(n);break;
default:printf("wrong\n");
}
printf("\nqing shu ru ni de xun ze(1.shuru 2.chazhao 3.cunchu\xianshi 4.shanchu 0==jiesu)\n");
scanf("%d",&g);}
return 0;
}
为什么那个cunchu函数显示不出来信息,,显示的全是0

搜索更多相关的解决方案: 函数  

----------------解决方案--------------------------------------------------------

就把文件和源程序放一起得了

[此贴子已经被作者于2007-8-2 19:00:23编辑过]


----------------解决方案--------------------------------------------------------

#include"stdio.h"
#include"string.h"
struct xinxi
{
char name[20];
char yuanxi[20];
long num;
int xiangmu;
int mingci;
};
struct xinxi a[50];
struct xinxi p;
struct xinxi b[50];
void shuru()
{
int i,j=0;
char c;
for(i=0;i<50;i++)
{
printf("qing shu ru bi sai xiang mu bian hao\n");
scanf("%d",&a[i].xiangmu);
printf("\n name:\n");
scanf("%s",a[i].name);
printf("\n yuanxi \n");
scanf("%s",a[i].yuanxi);
printf("\n xuehao \n");
scanf("%ld",&a[i].num);
printf("\n ming ci \n");
scanf("%d",&a[i].mingci);
printf("\n continue yes or no? xie y|n\n");
scanf("%c",&c);
scanf("%c",&c);
if(c=='y') continue;
if(c=='n') break;
}

}
void chazhao()
{
int i,j;
long b;
printf("qing shu ru chao zhao de xue hao(0=tiaochu)\n");
scanf("%ld",&b);
printf("\n xiang mu \n");
scanf("%d",&j);
while(b!=0)
{
for(i=0;i<50;i++)
{
if(a[i].num==b&&a[i].xiangmu==j)
{ printf("xing ming :%s\n",a[i].name);
printf("yuan xi :%s\n",a[i].yuanxi);
printf("xiang mu :%d\n",a[i].xiangmu);
printf("xue hao: %ld\n",a[i].num);
printf("ming ci :%d\n",a[i].mingci);}
}
printf("\n qing shu ru chao zhao de xue hao(0=tiaochu)\n");
scanf("%ld",&b);
printf("\n xiang mu \n");
scanf("%d",&j);
}

}
void cunchu(int n)
{
int i,j,k,g=0;
FILE *fw;
fw=fopen("E:\\fujian.txt","w");
for(i=0;i<50;i++)
{
if(a[i].xiangmu==k)
{strcpy(b[g].name,a[i].name);
strcpy(b[g].yuanxi,a[i].yuanxi);
b[g].num=a[i].num;
b[g].mingci=a[i].mingci;
g++;}}
for(k=1;k<=n;k++)
{
printf("di %d xiang bi sai de ji lu\n",k);
fprintf(fw,"di %d xiang bi sai de ji lu\n",k);
for(i=0;i<50;i++)
for(j=1;j<50-i;j++)
{
if(b[j].mingci>b[j+1].mingci)
{
p=b[j];
b[j]=b[j+1];
b[j+1]=p;
}}
for(i=0;i<3;i++)
{
printf("%10s %10s %10ld %10d\n",b[i].name,b[i].yuanxi,b[i].num,b[i].mingci);
fprintf(fw,"%10s %10s %10ld %10d\n",b[i].name,b[i].yuanxi,b[i].num,b[i].mingci);}
}

fclose(fw);
}

void shanchu(int n)
{
long b;
int a1,h,j,i,k;
FILE *fw;
fw=fopen("E:\\fujian.txt","w");
printf("qing shu ru xue hao\n");
scanf("%ld",&b);
printf("\n xiang mu \n");
scanf("%d",&a1);
for(h=0;h<50;h++)
{
if(a[h].num==b&&a[h].xiangmu==a1)
{
for(k=1;k<=n;k++)
{
fprintf(fw,"di %d xiang bi sai de ji lu\n",k);
for(i=1;i<50;i++)
for(j=0;j<50-i;j++)
{
if(a[j].mingci>a[j+1].mingci)
{
p=a[j];a[j]=a[j+1];a[j+1]=p;
}
}
for(i=h;i<50;i++)
a[i+1]=a[i];
for(i=0;i<3;i++)
{
fprintf(fw,"%10s %10s %10ld %10d\n",a[i].name,a[i].yuanxi,a[i].num,a[i].mingci);
}
}

}
}
}
int main()
{
int n,g,i,j,k;
FILE *fw;
fw=fopen("E:\\fujian.txt","w");
printf("*********** ****************** 1.shuru **************************************\n");
printf("*********** ****************** 2.chazhao **************************************\n");
printf("*********** ****************** 3.xianshi\cunchu **************************************\n");
printf("*********** ****************** 4.shanchu **************************************");
printf("\n qing xian shu ru bi sai xiang mu ge shu\n");
scanf("%d",&n);
printf("\nqing shu ru ni de xun ze(1-4)\n");
scanf("%d",&g);
while(g!=0)
{switch(g)
{
case 1:shuru();break;
case 2:chazhao();break;
case 3:cunchu(n);break;
case 4:shanchu(n);break;
default:printf("wrong\n");
}
printf("\nqing shu ru ni de xun ze(1.shuru 2.chazhao 3.cunchu\xianshi 4.shanchu 0==jiesu)\n");
scanf("%d",&g);}
fclose(fw);
return 0;
}
我把他改了一下,,但那个cunchu函数还是没有显示,,能帮我改下吗


----------------解决方案--------------------------------------------------------

就改了改输入和存储,其他的有时间接着改吧,你这结构体本身就不是很理想,凑合着用先!
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Message
{
char name[20];
char Department[20];
long num;
int Item;
int Position;
};
struct Message a[50];
struct Message b[50];
struct Message p;
int GameItemTable[50][2];
int GameItemNum;
void ClearStructArr(struct Message* s)
{
for(int i=0;i<50;i++)
{
memset(s[i].name,0,20);
memset(s[i].Department,0,20);
s[i].num=0;
s[i].Item=-1;
s[i].Position=0;
}
}
void ClearGameItemTable()
{
for(int i=0;i<50;i++)
{
GameItemTable[i][0]=-1;
GameItemTable[i][1]=0;
}

GameItemNum=0;
}
void quick_sort(struct Message *item,int left,int right)
{
register int i,j;
struct Message s1,s2;

i=left;
j=right;
s1=item[(left+right)/2];

do
{
while((item[i].Position<s1.Position)&&(i<right))i++;
while((s1.Position<item[j].Position)&&(j>left))j--;

if(i<=j)
{
s2=item[i];
item[i]=item[j];
item[j]=s2;
i++,j--;
}
}while(i<=j);

if(left<j)quick_sort(item,left,j);
if(i<right)quick_sort(item,i,right);

}
void Input()
{
int i,j=0;
char c;
int flag;
FILE *datafp=NULL;

ClearStructArr(a);

printf("1.Load from file...\n");
printf("2.I can Input it...\n");
printf("3 or else.Exit,I donnot want to Input,now!\n");
scanf("%d",&flag);
if(flag==1)
{
if((datafp=fopen("D:\\In.txt","rb"))==NULL)
{
printf("cannot open data file!\n");
return;
}
for(i=0;i<50;i++)
{
fscanf(datafp,"%s %s %ld %d %d",a[i].name,a[i].Department,&a[i].num,&a[i].Item,&a[i].Position);
printf("%s,%s,%ld,%d,%d\n",a[i].name,a[i].Department,a[i].num,a[i].Item,a[i].Position);
}
fclose(datafp);
printf("OK! You have Load it success!\n");
return;

}
else if(flag==2)
{
for(i=0;i<50;i++)
{
printf("Please input the Game Item-number:\n");
scanf("%d",&a[i].Item);
printf("name:\n");
scanf("%s",a[i].name);
printf("Department:\n");
scanf("%s",a[i].Department);
printf("schoolnumber:\n");
scanf("%ld",&a[i].num);
printf("position\n");
scanf("%d",&a[i].Position);
printf("continue yes or no?(Input y or n)\n");
getchar();
scanf("%c",&c);
if(c=='y'||c=='Y') continue;
if(c=='n'||c=='N') break;
}
printf("You have Input all!\n");
printf("Do you want to Save your Input?(Input y or n)\n");
getchar();
scanf("%c",&c);
if(c=='y'||c=='Y')
{
if((datafp=fopen("D:\\In.txt","wb"))==NULL)
{
printf("cannot open data file!\n");
return;
}

for(i=0;i<50;i++)
{
if(a[i].Item!=-1)
{
fprintf(datafp,"%s %s %ld %d %d\n",a[i].name,a[i].Department,a[i].num,a[i].Item,a[i].Position);
}
}
printf("OK!Save it Success!\n");
fclose(datafp);
return;
}
else
{
return;
}
}

else
{
return;
}

}
void FindMessage()
{
int i,j;
long b;
printf("Please input the SchoolNumber you want to Find(while 0 can exit):\n");
scanf("%ld",&b);
printf("Item:\n");
scanf("%d",&j);
while(b!=0)
{
for(i=0;i<50;i++)
{
if(a[i].num==b&&a[i].Item==j)
{ printf("Your Name :%s\n",a[i].name);
printf("Your Department :%s\n",a[i].Department);
printf("Game Item :%d\n",a[i].Item);
printf("Your SchoolNumber: %ld\n",a[i].num);
printf("Your Position :%d\n",a[i].Position);
}
}
printf("Please input the SchoolNumber you want to Find(while 0 can exit):\n");
scanf("%ld",&b);
printf("Item:\n");
scanf("%d",&j);
}

}
void StoreMessage()
{
int k=0;
int knum=0;
int j=0;
int g;
FILE *fw;
if((fw=fopen("D:\\fujian.txt","w"))==NULL)
{
printf("cannot open data file!\n");
return;
}

ClearGameItemTable();

for(int i=0;i<50;i++)
{
if(a[i].Item!=-1)
{
if(GameItemTable[a[i].Item][0]==-1)
{
GameItemTable[a[i].Item][0]=a[i].Item;
}
GameItemTable[a[i].Item][1]++;
}

}
while(1)
{
ClearStructArr(b);
while(GameItemTable[j][0]==-1)j++;
if(j==50)break;
k=j;
knum+=GameItemTable[k][1];
for(i=0,g=0;i<50;i++)
{
if(a[i].Item==k)
{
strcpy(b[g].name,a[i].name);
strcpy(b[g].Department,a[i].Department);
b[g].num=a[i].num;
b[g].Position=a[i].Position;
g++;
}
if(g==GameItemTable[k][1])break;
}

printf("Game N0.%d Records:\n",k);
fprintf(fw,"%d %d\n",k,g);

quick_sort(b,0,g-1);

for(i=0;i<g;i++)
{
printf("%10s %10s %10ld %10d\n",b[i].name,b[i].Department,b[i].num,b[i].Position);
fprintf(fw,"%10s %10s %10ld %10d\n",b[i].name,b[i].Department,b[i].num,b[i].Position);
}

printf("The First-THREE position:\n");
for(i=0;i<3;i++)
{
printf("%10s %10s %10ld %10d\n",b[i].name,b[i].Department,b[i].num,b[i].Position);
}
if(j==49)break;
j++;
}

fclose(fw);
}

void DeleteMessage()
{
long b;
int a1,h,j,i,k;
FILE *fw;
int n;
printf("How many Games you take part in?\n");
scanf("%d",&n);
fw=fopen("D:\\fujian.txt","w");
printf("Input your SchoolNumber:\n");
scanf("%ld",&b);
printf("Game Item: \n");
scanf("%d",&a1);
for(h=0;h<50;h++)
{
if(a[h].num==b&&a[h].Item==a1)
{
for(k=1;k<=n;k++)
{
fprintf(fw,"Game N0.%d Records:\n",k);
for(i=1;i<50;i++)
for(j=0;j<50-i;j++)
{
if(a[j].Position>a[j+1].Position)
{
p=a[j];a[j]=a[j+1];a[j+1]=p;
}
}
for(i=h;i<50;i++)
a[i+1]=a[i];
for(i=0;i<3;i++)
{
fprintf(fw,"%10s %10s %10ld %10d\n",a[i].name,a[i].Department,a[i].num,a[i].Position);
}
}

}
}
}
int main()
{
int g=1;
printf("*********** 1.Input **************************\n");
printf("*********** 2.FindMessage **************************\n");
printf("*********** 3.TypeOut\\StoreMessage **************************\n");
printf("*********** 4.DeleteMessage **************************\n");
while(g)
{
printf("Make your choice,Now!(1.Input 2.FindMessage 3.StoreMessage\\TypeOut 4.DeleteMessage 0==exit)");
scanf("%d",&g);
switch(g)
{
case 1:Input();break;
case 2:FindMessage();break;
case 3:StoreMessage();break;
case 4:DeleteMessage();break;
default:printf("wrong\n");break;
}

}

return 0;
}

[此贴子已经被作者于2007-8-3 10:03:59编辑过]


----------------解决方案--------------------------------------------------------
你就把你的程序要求都写清楚贴出来看看,很难琢磨你程序的内涵,不好改的,
就只能改改语法什么的不能解决实际问题。
你就把结构体,各函数所要完成的功能给介绍下。一些主要的参数也介绍下,我就不知道你那K是什么,取琢磨你真正的意思很费时间。
----------------解决方案--------------------------------------------------------
首先shuru功能是输入 xiangmu name yuanxi num mingci
chazhao功能是按num和xiangmu查找,并显示xiangmu name yuanxi num mingci这些信息
cunchu功能是按xiangmu来存储而且是从第一位开始存储前三位并且还要显示
如:第一项
name yuanxi num mingci
第2项
name yuanxi num mingci.........
shanchu功能是按num和xiangmu来确定删除对象并存储覆盖原文件
----------------解决方案--------------------------------------------------------
yuanxi中文何解?还有那个K是啥东西?存储信息里面的!

[此贴子已经被作者于2007-8-2 19:45:18编辑过]



----------------解决方案--------------------------------------------------------
院系
----------------解决方案--------------------------------------------------------
K是项目
----------------解决方案--------------------------------------------------------
K啊,存储里面的!
----------------解决方案--------------------------------------------------------
  相关解决方案