69,370
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<string.h>
int main (void)
{
char command[8][7]={"AFAIK","FWIW","IME","IMO","OT","WDYT","WDYMBT","YOYO"};
char description[8][25]={"as far as i know","for what its worth","in my experiecen","in my opinion","off-topic","what do you think?","what do you mean bt that","you are in your own"};
char x[7],b[25];
int choice,i,ind=0,ind1,j;
printf("Welcome to the abbreviation help system\n");
printf("1=look up an abbreviation\n");
printf("2=search for words in the description\n");
printf("3=quit\n");
printf("Your choice:\n");
//建议就用while就行了
while(scanf("%d",&choice)==1)
{
//吸收缓冲区的回车符
getchar();
switch(choice)
{
case 1:
printf("Enter command name to look up: ");
gets(x);
for(i=0;i<8;i++)
{
if(strcmp(command[i],x)==0)
{
//应该改成一个标志位
ind=i;
printf("%s: %s\n",x,description[i]);
printf("Find it!\n");
ind=0;
//找到了就可以直接退出
break;
}
if(i==7&&ind==0)
printf("Sorry, %s is not in the database.\n",x);
// if(ind!=i)
// {
// printf("Sorry, %s is not in the database.\n",x);
// break;
// }
}
break;
case 2:
printf("Enter a word to search for: ");
scanf("%s",b);
//这里只要查询首字母就行了
printf("The following entries contanin the word is short for %c:\n",b[0]);
for(i=0;i<8;i++)
{
//这里有问题,你是要寻找字符串里存在字母y就可以了,遍历一下就行了
for(i=0;i<8;i++)
for(j=0;j<strlen(command[i])-1;j++)
if(command[i][j]=='Y')
{
printf("%s\n",description[i]);
//发现仙童字母就跳出,不在遍历,不然会再次输出该字符串
break;
}
}
break;
case 3:
printf("End of program\n");
break;
default:
printf("Invalid operator\n");
break;
}
if(choice==3)
break;
printf("Your choice:\n");
}
return 0;
}
#include<stdio.h>
#include<string.h>
int main (void)
{
char command[8][7]={"AFAIK","FWIW","IME","IMO","OT","WDYT","WDYMBT","YOYO"};
char description[8][25]={"as far as i know","for what its worth","in my experiecen","in my opinion","off-topic","what do you think?","what do you mean bt that","you are in your own"};
char x[7],b[25];
int choice,i,ind=0,ind1,j;
printf("Welcome to the abbreviation help system\n");
printf("1=look up an abbreviation\n");
printf("2=search for words in the description\n");
printf("3=quit\n");
printf("Your choice:\n");
//建议就用while就行了
while(scanf("%d",&choice)==1)
{
//吸收缓冲区的回车符
getchar();
switch(choice)
{
case 1:
printf("Enter command name to look up: ");
gets(x);
for(i=0;i<8;i++)
{
if(strcmp(command[i],x)==0)
{
//应该改成一个标志位
ind++;
printf("%s: %s\n",x,description[i]);
printf("Find it!\n");
ind=0;
//找到了就可以直接退出
break;
}
if(i==7&&ind==0)
printf("Sorry, %s is not in the database.\n",x);
// if(ind!=i)
// {
// printf("Sorry, %s is not in the database.\n",x);
// break;
// }
}
break;
case 2:
printf("Enter a word to search for: ");
scanf("%s",b);
//这里只要查询首字母就行了
printf("The following entries contanin the word is short for %c:\n",b[0]);
for(i=0;i<8;i++)
{
//这里有问题,你是要寻找字符串里存在字母y就可以了,遍历一下就行了
for(i=0;i<8;i++)
for(j=0;j<strlen(command[i])-1;j++)
if(command[i][j]=='Y')
{
printf("%s\n",description[i]);
//发现仙童字母就跳出,不在遍历,不然会再次输出该字符串
break;
}
}
break;
case 3:
printf("End of program\n");
break;
default:
printf("Invalid operator\n");
break;
}
if(choice==3)
break;
printf("Your choice:\n");
}
return 0;
}
#include<stdio.h>
#include<string.h>
int main (void)
{
char command[8][7]={"AFAIK","FWIW","IME","IMO","OT","WDYT","WDYMBT","YOYO"};
char description[8][25]={"as far as i know","for what its worth","in my experiecen","in my opinion","off-topic","what do you think?","what do you mean bt that","you are in your own"};
char x[7],b[25];
int choice,i,ind=0,ind1,j;
printf("Welcome to the abbreviation help system\n");
printf("1=look up an abbreviation\n");
printf("2=search for words in the description\n");
printf("3=quit\n");
printf("Your choice:\n");
//建议就用while就行了
while(scanf("%d",&choice)==1)
{
switch(choice)
{
case 1:
printf("Enter command name to look up: ");
scanf("%s",x);
for(i=0;i<8;i++)
{
if(strcmp(command[i],x)==0)
{
//应该改成一个标志位
ind = 1;
printf("%s: %s\n",x,description[i]);
//找到了就可以直接退出
break;
}
if(ind!=1)
{
printf("Sorry, \"FAQ\" is not in the database.\n");
break;
}
}
break;
case 2:
printf("Enter a word to search for: ");
scanf("%s",b);
//这里只要查询首字母就行了
printf("The following entries contanin the word is short for %c:\n",b[0]);
for(i=0;i<8;i++)
{
//这里有问题,你是要寻找字符串里存在字母y就可以了,遍历一下就行了
for(i=0;i<8;i++)
for(j=0;j<strlen(command[i])-1;j++)
if(command[i][j]=='Y')
{
printf("%s\n",description[i]);
//发现仙童字母就跳出,不在遍历,不然会再次输出该字符串
break;
}
}
break;
case 3:
printf("End of program\n");
break;
default:
printf("Invalid operator\n");
break;
}
if(choice==3)
break;
printf("Your choice:\n");
}
return 0;
}
#include<stdio.h>
#include<string.h>
int main (void)
{
char command[8][7]={"AFAIK","FWIW","IME","IMO","OT","WDYT","WDYMBT","YOYO"};
char description[8][25]={"as far as i know","for what its worth","in my experiecen","in my opinion","off-topic","what do you think?","what do you mean bt that","you are in your own"};
char x[7],b[25];
int choice,i,ind,ind1;
printf("Welcome to the abbreviation help system");
printf("1=look up an abbreviation");
printf("2=search for words in the description");
printf("3=quit");
do
{
printf("Your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter command name to look up: ");
scanf("%s",x);
for(i=0;i<8;i++)
{
if(strcmp(command[i],x)==0)
{
ind = i;
printf("%s: %s\n",x,description[ind]);
}
}
break;
case 2:
printf("Enter a word to search for: ");
scanf("%s",b);
printf("The following entries contanin the word %s:\n",b);
for(i=0;i<8;i++)
{
if((strstr(command[i],b))!=0)
printf("%s: %s",command[i],description[ind1]);
}
break;
case 3:
printf("End of program\n");
break;
default:
printf("Invalid operator\n");
break;
}
}while(choice!=3);
return 0;
}
这是我写的,能帮我看下case 2 到底哪里错了吗??谢谢你if((strstr(command[i],b))!=0)
是用以为数组分别存储字符串的?
那这样写的话不对啊,你这里写的是二维数组存储字符串的行指针了