程序在VC6.0下能正确运行,在TC下却运行错误。
请大家帮帮我,程序是在64位VC写的,但是要交作业了,老师要求是TC,1 我想问问程序错在哪里?
2 此程序后面读取文件中学生信息时候会有乱码出现,应该怎么操作?
# include "stdio.h"
# include "stdlib.h"
# include "string.h"
struct node{
int number;
char name[20];
char type;
int age;
int math;
int english;
int Clanguage;
int structure;
int sports;
int total;
int average;
}student[30];
struct node temp;
void menu()
{
system("cls");
printf("\t\t** Welcome to use the Program! **\t\n");
printf("\t\t* * * * * * * * * * * * * * * * *\t\n");
printf("\t\t******** * ** * ********\t\n");
printf("\t\t******** [1] input ********\t\n");
printf("\t\t******** [2] find ********\t\n");
printf("\t\t******** [3] insert ********\t\n");
printf("\t\t******** [4] delete ********\t\n");
printf("\t\t******** [5] print ********\t\n");
printf("\t\t******** [6] save ********\t\n");
printf("\t\t******** [7] read ********\t\n");
printf("\t\t******** [8] exit ********\t\n");
printf("\t\t******** * ** * ********\t\n");
printf("\t\t* * * * * * * * * * * * * * * * *\t\n");
printf("\t\t choose(1-8):");
}
void input(int c)
{
int i;
for(i=1;i<4;i++)
{
printf("please input %d student's number:",i);
scanf("%d",&student[i].number);
printf("please input %d student's name:",i);
scanf("%s",student[i].name);
getchar();
printf("please input %d student's age:",i);
scanf("%d",&student[i].age);
flushall();
printf("please input %d student's type:",i);
scanf("%c",&student[i].type);
flushall();
printf("please input %d student's math:",i);
scanf("%d",&student[i].math);
printf("please input %d student's english:",i);
scanf("%d",&student[i].english);
printf("please input %d student's Clanguage:",i);
scanf("%d",&student[i].Clanguage);
printf("please input %d student's structure:",i);
scanf("%d",&student[i].structure);
printf("please input %d student's sports:",i);
scanf("%d",&student[i].sports);
student[i].total=student[i].math+student[i].english+student[i].Clanguage+student[i].structure+student[i].sports;
student[i].average=student[i].total/5;
}
}
void find(int c)
{
int n,m,i,j,l;
char k[20];
system("cls");
printf("number find 1:\n");
printf("name find 2:\n");
printf("please choose(1-2):");
scanf("%d",&n);
if (n==1)
{
printf("you choose number find\n");
printf("please input number:");
scanf("%d",&m);
for(i=1;i<=c;i++)
{
if(student[i].number==m)
{
printf("************************************\n");
printf("number:%d\n",student[i].number);
printf("name:%s\n",student[i].name);
printf("age:%d\n",student[i].age);
printf("type:%c\n",student[i].type);
printf("math:%d\n",student[i].math);
printf("english:%d\n",student[i].english);
printf("Clanguage:%d\n",student[i].Clanguage);
printf("structure:%d\n",student[i].structure);
printf("sports:%d\n",student[i].sports);
printf("total:%d\n",student[i].total);
printf("average:%d\n",student[i].average);
printf("************************************\n");
getchar();
}
}
}
else
if (n==2)
{
getchar();
printf("you choose name find");
printf("please input name:");
fgets(k,20,stdin);
l=strlen(k);
k[l-1]='\0';
for(j=1;j<=c;j++)
{
if(strcmp(k,student[j].name)==0)
{
printf("*********************************\n");
printf("number:%d\n",student[j].number);
printf("name:%s\n",student[j].name);
printf("age:%d\n",student[j].age);
printf("type:%c\n",student[j].type);
printf("math:%d\n",student[j].math);
printf("english:%d\n",student[j].english);
printf("Clanguage:%d\n",student[j].Clanguage);
printf("structure:%d\n",student[j].structure);
printf("sports:%d\n",student[j].sports);
printf("total:%d\n",student[j].total);
printf("average:%d\n",student[j].average);
printf("*********************************\n");
}
}
}
else
{
getchar();
}
system("pause");
}
void insert(int c)
{
int i,j;
system("cls");
printf("please input student's number:");
scanf("%d",&temp.number);
printf("please input student's name:");
scanf("%s",temp.name);
printf("please input student's age:");
scanf("%d",&temp.age);
flushall();
printf("please input student's type:");
scanf("%c",&temp.type);
flushall();
printf("please input student's math:");
scanf("%d",&temp.math);
printf("please input student's english:");
scanf("%d",&temp.english);
printf("please input student's Clanguage:");
scanf("%d",&temp.Clanguage);
printf("please input student's structure:");
scanf("%d",&temp.structure);
printf("please input student's sports:");
scanf("%d",&temp.sports);
temp.total=temp.math+temp.english+temp.Clanguage+temp.structure+temp.sports;
temp.average=temp.total/5;
for (i=0;i<c;i++)
{
if (temp.number<student[i].number)
{
for (j=c;j>i;j--)
{
student[j].number=student[j-1].number;
strcpy(student[j].name,student[j-1].name);
student[j].age=student[j-1].age;
student[j].type=student[j-1].type;
student[j].math=student[j-1].math;
student[j].english=student[j-1].english;
student[j].Clanguage=student[j-1].Clanguage;
student[j].structure=student[j-1].structure;
student[j].sports=student[j-1].sports;
student[j].total=student[j-1].total;
student[j].average=student[j-1].average;
}
student[j].number=temp.number;
strcpy(student[j].name,temp.name);
student[j].age=temp.age;
student[j].type=temp.type;
student[j].math=temp.math;
student[j].english=temp.english;
student[j].Clanguage=temp.Clanguage;
student[j].structure=temp.structure;
student[j].sports=temp.sports;
student[j].total=temp.total;
student[j].average=temp.average;
break;
}
}
if(i>=c)
{
student[i].number=temp.number;
strcpy(student[j].name,temp.name);
student[i].age=temp.age;
student[i].type=temp.type;
student[i].math=temp.math;
student[i].english=temp.english;
student[i].Clanguage=temp.Clanguage;
student[i].structure=temp.structure;
student[i].sports=temp.sports;
student[i].total=temp.total;
student[i].average=temp.average;
}
}
void deletel(int c)
{
int i,j,n,k;
char name[20];
printf("number delete(1):\n");
printf("name delete(2):\n");
scanf("%d",&k);
if(k==1)
{
printf("please input number:\n");
scanf("%d",&n);
for(i=0;i<c;i++)
{
if(student[i].number==n)
{
c=c-1;
for(j=i;j<c;j++)
{
student[j].number=student[j+1].number;
strcpy(student[j].name,student[j+1].name);
student[j].age=student[j+1].age;
student[j].type=student[j+1].type;
student[j].structure=student[j+1].structure;
student[j].english=student[j+1].english;
student[j].Clanguage=student[j+1].Clanguage;
student[j].structure=student[j+1].structure;
student[j].sports=student[j+1].sports;
student[j].total=student[j+1].total;
student[j].average=student[j+1].average;
}
}
}
}
else
if(k==2)
{
getchar();
printf("please input student name:\n");
for(i=0;i<c;i++)
{
if(strcmp(name,student[i].name)==0)
{
for(j=i;j<c;j++)
{
student[j].number=student[j+1].number;
strcpy(student[j].name,student[j+1].name);
student[j].type=student[j+1].type;
student[j].age=student[j+1].age;
student[j].structure=student[j+1].structure;
student[j].english=student[j+1].english;
student[j].Clanguage=student[j+1].Clanguage;
student[j].structure=student[j+1].structure;
student[j].sports=student[j+1].sports;
student[j].total=student[j+1].total;
student[j].average=student[j+1].average;
}
}
}
}
else
{
getchar();
}
}
void print(int c)
{
int i;
system("cls");
for(i=1;i<=c;i++)
{
printf("***************************************\n");
printf("number:%d\n",student[i].number);
printf("name:%s\n",student[i].name);
printf("age:%d\n",student[i].age);
printf("type:%c\n",student[i].type);
printf("math grade:%d\n",student[i].math);
printf("english grade:%d\n",student[i].english);
printf("Clanguage grade:%d\n",student[i].Clanguage);
printf("structure grade:%d\n",student[i].structure);
printf("sports grade:%d\n",student[i].sports);
printf("total:%d\n",student[i].total);
printf("average:%d\n",student[i].average);
printf("***************************************\n");
}
printf("Press any key to continue .................\n");
getchar();
getchar();
}
void save(int c)
{
int i;
FILE *fp;
if((fp=fopen("studentdate.txt","a+"))==NULL)
{
printf("File open error! \n");
exit(0);
}
for(i=1;i<=c;i++)
fprintf(fp,"%d %s %c %d %d %d %d %d %d %d %d\n",student[i].number,student[i].name,student[i].type,student[i].age,student[i].math,student[i].english,student[i].Clanguage,student[i].structure,student[i].sports,student[i].total,student[i].average);
if(fclose(fp))
{
printf("Can not close the file! \n");
exit(0);
}
system("pause");
}
void read(int c)
{
FILE *fp;
int i;
if((fp=fopen("studentdate.txt","a+"))==NULL)
{
printf("File open error! \n");
exit(0);
}
printf("number name type age math english Clanguage tructure sports total average \n");
for(i=1;i<=c;i++)
{
fseek(fp,i*sizeof(struct node),0);
fread(&student[i],sizeof(struct node),3,fp);
printf("%d\t%s\t%c\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",student[i].number,student[i].name,student[i].type,student[i].age,student[i].math,student[i].english,student[i].Clanguage,student[i].structure,student[i].sports,student[i].total,student[i].average);
getchar();
}
if(fclose(fp))
{
printf("Can not close the file! \n");
exit(0);
}
system("pause");
}
void error()
{
printf("Error!!!\n");
printf("please choose (1-8):\n");
printf("press Enter to continue....\n");
getchar();
getchar();
}
int main()
{
int n,c=0;
while(1)
{
menu();
printf("please input your choose:");
scanf("%d",&n);
switch(n)
{
case 1:input(c);c=c+3;break;
case 2:find(c);break;
case 3:insert(c);++c;break;
case 4:deletel(c);--c;break;
case 5:print(c); break;
case 6:save(c);break;
case 7:read(c);break;
case 8:break;
default:error();break;
}
}
}