#include <stdlib.h>
#include <string.h>
int main()
{
char a[20],b[20],canshu1[30],c[20],d[20],canshu2[30],e[20],f[20],canshu3[30];
char TOTAL_STATION[100],STRATEGY[100],DISTANCE[100];
FILE *fp = fopen("dict.dic", "r");
fscanf(fp,"%s",&a);
fscanf(fp,"%s",&b);
fscanf(fp,"%s",&canshu1);
fscanf(fp,"%s",&c);
fscanf(fp,"%s",&d);
fscanf(fp,"%s",&canshu2);
fscanf(fp,"%s",&e);
fscanf(fp,"%s",&f);
fscanf(fp,"%s",&canshu3);
if(a=='TOTAL_STATION')
{
strcpy(canshu1,TOTAL_STATION);
if(b=='STRATEGY')
{
strcpy(canshu2,STRATEGY);
strcpy(canshu3,DISTANCE);
}
else if(b=='DISTANCE')
{
strcpy(canshu3,STRATEGY);
strcpy(canshu2,DISTANCE);
}
}
else if(a=='STRATEGY')
{
strcpy(canshu1,STRATEGY);
if(b=='TOTAL_STATION')
{
strcpy(canshu2,TOTAL_STATION);
strcpy(canshu3,DISTANCE);
}
else if(b=='DISTANCE')
{
strcpy(canshu3,TOTAL_STATION);
strcpy(canshu2,DISTANCE);
}
}
else if(a=='DISTANCE')
{
strcpy(canshu1,DISTANCE);
if(b=='TOTAL_STATION')
{
strcpy(canshu2,TOTAL_STATION);
strcpy(canshu3,STRATEGY);
}
else if(b=='STRATEGY')
{
strcpy(canshu3,TOTAL_STATION);
strcpy(canshu2,STRATEGY);
}
}
printf("%s\n",TOTAL_STATION);
printf("%s\n",STRATEGY);
printf("%s\n",DISTANCE);
fclose(fp);
return 0;
}
字符串比较用 strcmp() 函数,字符串用 “ ” 双引号,修改如下,供参考:
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char a[20],b[20],canshu1[30],c[20],d[20],canshu2[30],e[20],f[20],canshu3[30];
char TOTAL_STATION[100],STRATEGY[100],DISTANCE[100];
FILE *fp = fopen("dict.dic", "r");
fscanf(fp,"%s",&a);
fscanf(fp,"%s",&b);
fscanf(fp,"%s",&canshu1);
fscanf(fp,"%s",&c);
fscanf(fp,"%s",&d);
fscanf(fp,"%s",&canshu2);
fscanf(fp,"%s",&e);
fscanf(fp,"%s",&f);
fscanf(fp,"%s",&canshu3);
if(strcmp(a,"TOTAL_STATION") == 0)//if(a=='TOTAL_STATION')
{
strcpy(canshu1,TOTAL_STATION);
if(strcmp(b,"STRATEGY") == 0) //if(b=='STRATEGY')
{
strcpy(canshu2,STRATEGY);
strcpy(canshu3,DISTANCE);
}
else if(strcmp(b,"DISTANCE") == 0)//else if(b=='DISTANCE')
{
strcpy(canshu3,STRATEGY);
strcpy(canshu2,DISTANCE);
}
}
else if(strcmp(a,"STRATEGY") == 0)//else if(a=='STRATEGY')
{
strcpy(canshu1,STRATEGY);
if(strccmp(b,"TOTAL_STATION") == 0)//if(b=='TOTAL_STATION')
{
strcpy(canshu2,TOTAL_STATION);
strcpy(canshu3,DISTANCE);
}
else if(strcmp(b,"DISTANCE") == 0) // else if(b=='DISTANCE')
{
strcpy(canshu3,TOTAL_STATION);
strcpy(canshu2,DISTANCE);
}
}
else if(strcmp(a,"DISTANCE") == 0) //else if(a=='DISTANCE')
{
strcpy(canshu1,DISTANCE);
if(strcmp(b,"TOTAL_STATION") == 0)//if(b=='TOTAL_STATION')
{
strcpy(canshu2,TOTAL_STATION);
strcpy(canshu3,STRATEGY);
}
else if(strcmp(b,"STRATEGY") == 0)// else if(b=='STRATEGY')
{
strcpy(canshu3,TOTAL_STATION);
strcpy(canshu2,STRATEGY);
}
}
printf("%s\n",TOTAL_STATION);
printf("%s\n",STRATEGY);
printf("%s\n",DISTANCE);
fclose(fp);
return 0;
}