My codes can compile correctly in VC6.0, but they always have something wrong in TC2.0. I know TC2.0 is C compiler, but I really did't use any C++ codes in my program. Unfortunately,my homework asked us to do it use TC2.0. My god,who can find out the bugs??????
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct info
{
char num[20];
char name[10];
char adress[30];
char sex[5];
struct info *next;
struct info *last;
};
void author();
struct info* input(struct info *);
struct info* search(char *,struct info *);
struct info* del(char *,struct info *);
struct info* deleteMenu(struct info *);
void searchMenu(struct info *fPtr);
void saveData(struct info *);
struct info* loadData(struct info *);
void showAll(struct info *);
void main()
{
author();
struct info *first=0;
int choose;
while(1){
?printf("1:Input\n2:Delete\n3:Search\n4:Quit\n5:Save Data\n6:Load Data\n7:Show All\n\n\n");
?printf("input your choose:"); ?
?scanf("%d",&choose); ? ?
?
?switch(choose){
?case 1:
? first=input(first);
? break;
?case 2:
? first=deleteMenu(first);
? break;
?case 3:
? searchMenu(first);
? break;
?case 4:
? return;
?case 5:
? saveData(first);
? break;
?case 6:
? first=loadData(first);
? ? ?break;
?case 7:
? showAll(first);
? break;
?default:
? printf("Wrong\n\n\n");
? break;
?}
printf("\n\n\n");
}
}
struct info *input(struct info *fPtr)
{
struct info *i;
i=(struct info*)malloc(sizeof(struct info));
printf("input student number:");
scanf("%s",i->num);
printf("input student name:");
scanf("%s",i->name);
printf("input student adress:");
scanf("%s",i->adress);
? ?printf("input sex:");
scanf("%s",i->sex);
(i->next)=fPtr;
if(fPtr!=0){
?(fPtr->last)=i;
}
fPtr=i;
return fPtr;
}
struct info* del(char *s,struct info *fPtr)
{
struct info *r;
r=search(s, fPtr);
if(r==0){
?printf("Wrong\n\n\n");
?return fPtr;
}
else{
?if(r==fPtr){
? fPtr=fPtr->next;
? ? ?free(r);
? printf("OK\n\n\n");
? return fPtr;
?}
?((r->last)->next)=(r->next);
?free(r);
?printf("OK\n\n\n");
?return fPtr;
}
}
struct info *search(char *s,struct info *fPtr)
{
int r;
if(fPtr==0){
?return 0;
}
while(1){
?r=strcmp(fPtr->num, s);
?if(r==0){
? return fPtr;
? ? ? ?}
?r=strcmp(fPtr->name,s);
? ? ? ?if(r==0){
? return fPtr;
? ? ? ?}
?if((fPtr->next)!=0){
? fPtr=(fPtr->next);
?}
?else{
? return 0;
?}
}
}
struct info* deleteMenu(struct info *fPtr)
{
char nn[20];
printf("input the student's Name or Number:");
scanf("%s",nn);
return del(nn,fPtr);
}
void searchMenu(struct info *fPtr)
{
char nn[2];
struct info *r;
printf("input the student's Name or Number:");
scanf("%s",nn);
r=search(nn,fPtr);
if(r==0){
?printf("Wrong\n\n\n");
?return;
}
else{
?printf("Number:%s Name:%s Adress:%s sex:%s\n\n\n",r->num,r->name,r->adress,r->sex);
}
}
void saveData(struct info *fPtr)
{
if(fPtr==0){
?printf("There are nothing to save!\n\n\n");
?return;
}
FILE *fp;
fp=fopen("info.stu","wb+");
? ?while(1){
?fwrite(fPtr,sizeof(struct info),1,fp);
?fPtr=(fPtr->next);
?if(fPtr==0){
? break;
?}
}
printf("Save is OK\n");
fclose(fp);
}
struct info* loadData(struct info *fPtr)
{
struct info *now=0;
struct info *up=0;
FILE *fp;
if((fp=fopen("info.stu","rb"))==NULL){
?printf("can't open file");
?return fPtr;
}
? ?fPtr=(struct info*)malloc(sizeof(struct info));
up=fPtr;
fread(fPtr,sizeof(struct info),1,fp);
if((fPtr->next)==0){
?printf("Load data is OK!\n\n\n");
?return fPtr;
}
while(1){
? ? now=(struct info*)malloc(sizeof(struct info));
? ? ? ?fread(now,sizeof(struct info),1,fp);
?(now->last)=up;
?(up->next)=now;
?up=now;
?if((now->next)==0){
? fclose(fp);
? printf("Load data is OK!\n\n\n");
? return fPtr;
?}
}
}
void showAll(struct info *fPtr)
{
if(fPtr==0){
?printf("There are noting to show!\n\n\n");
?return;
}
while(1){
?printf("Number:%s Name:%s Adress:%s sex:%s\n-----------------------------\n",fPtr->num,fPtr->name,fPtr->adress,fPtr->sex);
?fPtr=fPtr->next;
?if(fPtr==0){
? printf("Show all is OK!\n\n\n");
? return;
?}
}
}
void author()
{
printf("***********************************************\n");
printf("* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *\n");
printf("* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?1.0 ?*\n");
printf("***********************************************\n\n\n");
}
Question
zhwcn
My codes can compile correctly in VC6.0, but they always have something wrong in TC2.0. I know TC2.0 is C compiler, but I really did't use any C++ codes in my program. Unfortunately,my homework asked us to do it use TC2.0. My god,who can find out the bugs??????
#include<stdio.h> #include<stdlib.h> #include<string.h> struct info { char num[20]; char name[10]; char adress[30]; char sex[5]; struct info *next; struct info *last; }; void author(); struct info* input(struct info *); struct info* search(char *,struct info *); struct info* del(char *,struct info *); struct info* deleteMenu(struct info *); void searchMenu(struct info *fPtr); void saveData(struct info *); struct info* loadData(struct info *); void showAll(struct info *); void main() { author(); struct info *first=0; int choose; while(1){ ?printf("1:Input\n2:Delete\n3:Search\n4:Quit\n5:Save Data\n6:Load Data\n7:Show All\n\n\n"); ?printf("input your choose:"); ? ?scanf("%d",&choose); ? ? ? ?switch(choose){ ?case 1: ? first=input(first); ? break; ?case 2: ? first=deleteMenu(first); ? break; ?case 3: ? searchMenu(first); ? break; ?case 4: ? return; ?case 5: ? saveData(first); ? break; ?case 6: ? first=loadData(first); ? ? ?break; ?case 7: ? showAll(first); ? break; ?default: ? printf("Wrong\n\n\n"); ? break; ?} printf("\n\n\n"); } } struct info *input(struct info *fPtr) { struct info *i; i=(struct info*)malloc(sizeof(struct info)); printf("input student number:"); scanf("%s",i->num); printf("input student name:"); scanf("%s",i->name); printf("input student adress:"); scanf("%s",i->adress); ? ?printf("input sex:"); scanf("%s",i->sex); (i->next)=fPtr; if(fPtr!=0){ ?(fPtr->last)=i; } fPtr=i; return fPtr; } struct info* del(char *s,struct info *fPtr) { struct info *r; r=search(s, fPtr); if(r==0){ ?printf("Wrong\n\n\n"); ?return fPtr; } else{ ?if(r==fPtr){ ? fPtr=fPtr->next; ? ? ?free(r); ? printf("OK\n\n\n"); ? return fPtr; ?} ?((r->last)->next)=(r->next); ?free(r); ?printf("OK\n\n\n"); ?return fPtr; } } struct info *search(char *s,struct info *fPtr) { int r; if(fPtr==0){ ?return 0; } while(1){ ?r=strcmp(fPtr->num, s); ?if(r==0){ ? return fPtr; ? ? ? ?} ?r=strcmp(fPtr->name,s); ? ? ? ?if(r==0){ ? return fPtr; ? ? ? ?} ?if((fPtr->next)!=0){ ? fPtr=(fPtr->next); ?} ?else{ ? return 0; ?} } } struct info* deleteMenu(struct info *fPtr) { char nn[20]; printf("input the student's Name or Number:"); scanf("%s",nn); return del(nn,fPtr); } void searchMenu(struct info *fPtr) { char nn[2]; struct info *r; printf("input the student's Name or Number:"); scanf("%s",nn); r=search(nn,fPtr); if(r==0){ ?printf("Wrong\n\n\n"); ?return; } else{ ?printf("Number:%s Name:%s Adress:%s sex:%s\n\n\n",r->num,r->name,r->adress,r->sex); } } void saveData(struct info *fPtr) { if(fPtr==0){ ?printf("There are nothing to save!\n\n\n"); ?return; } FILE *fp; fp=fopen("info.stu","wb+"); ? ?while(1){ ?fwrite(fPtr,sizeof(struct info),1,fp); ?fPtr=(fPtr->next); ?if(fPtr==0){ ? break; ?} } printf("Save is OK\n"); fclose(fp); } struct info* loadData(struct info *fPtr) { struct info *now=0; struct info *up=0; FILE *fp; if((fp=fopen("info.stu","rb"))==NULL){ ?printf("can't open file"); ?return fPtr; } ? ?fPtr=(struct info*)malloc(sizeof(struct info)); up=fPtr; fread(fPtr,sizeof(struct info),1,fp); if((fPtr->next)==0){ ?printf("Load data is OK!\n\n\n"); ?return fPtr; } while(1){ ? ? now=(struct info*)malloc(sizeof(struct info)); ? ? ? ?fread(now,sizeof(struct info),1,fp); ?(now->last)=up; ?(up->next)=now; ?up=now; ?if((now->next)==0){ ? fclose(fp); ? printf("Load data is OK!\n\n\n"); ? return fPtr; ?} } } void showAll(struct info *fPtr) { if(fPtr==0){ ?printf("There are noting to show!\n\n\n"); ?return; } while(1){ ?printf("Number:%s Name:%s Adress:%s sex:%s\n-----------------------------\n",fPtr->num,fPtr->name,fPtr->adress,fPtr->sex); ?fPtr=fPtr->next; ?if(fPtr==0){ ? printf("Show all is OK!\n\n\n"); ? return; ?} } } void author() { printf("***********************************************\n"); printf("* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *\n"); printf("* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?1.0 ?*\n"); printf("***********************************************\n\n\n"); }Link to comment
Share on other sites
4 answers to this question
Recommended Posts