新手求教

lzxtc12 2012-12-01 07:52:13
学校给了一个作业是这样的哈!!

"AFAIK" "as far as I know"
"FWIW" "for what it’s worth"
"IME" "in my experience"
“IMO” “in my opinion”
"OT" "off- topic"
"WDYT" "what do you think?"
“WDYMBT” "what do you mean by that?"
"YOYO" "you’re on your own”
每一个简写对应一句话

然后要求output是:

Welcome to the abbreviation help system.

1 - look up an abbreviation
2 - search for words in the description
3 - quit

Your choice: 1(这个部分我做出来了)
Enter command name to look up: FWIW
FWIW – for what it’s worth

Your choice: 2(这个部分我不知道怎么写)
Enter a word to search for: you
The following entries contain the word "you":
WDYT – what do you think?
WDYMBT – what do you mean by that?
YOYO – you’re on your own

Your choice: 1(这个部分是用if else吗??然后是和上面那个choice 1 写在一起的吗??)
Enter command name to look up: FAQ
Sorry, "FAQ" is not in the database.

Your choice: 4
Invalid choice

Your choice: 3
End of program

我写了一个程序,用了两个数组,一个是存那些简写,另一个是用来存放简写相对应的句子,但是上面个choice 2,我不知道要怎么写,写出来总是运行不出来!拜托各位大神可以帮帮我吗??万分感谢。。。
...全文
257 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
14号选手 2012-12-03
  • 打赏
  • 举报
回复
或者这样也行 不用ind++,赋值为i也是一样的,只是前一种就限定成0和1两个数字了,后一种就没有,直接标志出字符串的位置了
#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;
}
            
14号选手 2012-12-03
  • 打赏
  • 举报
回复
引用 24 楼 lzxtc12 的回复:
引用 23 楼 xuchao1229 的回复:引用 22 楼 lzxtc12 的回复:引用 20 楼 xuchao1229 的回复:引用 19 楼 lzxtc12 的回复:引用 18 楼 xuchao1229 的回复:引用 16 楼 lzxtc12 的回复:引用 15 楼 xuchao1229 的回复:引用 14 楼 xuchao1229 的回复:刚才那样有一点小bug ……
不好意思,刚才回复的调试出bug了 又作了修改,把FAQ给扩展了一下
#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;
}
            
14号选手 2012-12-03
  • 打赏
  • 举报
回复
引用 24 楼 lzxtc12 的回复:
引用 23 楼 xuchao1229 的回复:引用 22 楼 lzxtc12 的回复:引用 20 楼 xuchao1229 的回复:引用 19 楼 lzxtc12 的回复:引用 18 楼 xuchao1229 的回复:引用 16 楼 lzxtc12 的回复:引用 15 楼 xuchao1229 的回复:引用 14 楼 xuchao1229 的回复:刚才那样有一点小bug
……

#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;
}



lzxtc12 2012-12-03
  • 打赏
  • 举报
回复
引用 23 楼 xuchao1229 的回复:
引用 22 楼 lzxtc12 的回复:引用 20 楼 xuchao1229 的回复:引用 19 楼 lzxtc12 的回复:引用 18 楼 xuchao1229 的回复:引用 16 楼 lzxtc12 的回复:引用 15 楼 xuchao1229 的回复:引用 14 楼 xuchao1229 的回复:刚才那样有一点小bug 修改了一下 C/C++ code??123……
英国,伯明翰。大哥,还想问个问题。。就是下面这个情况,该怎么编到程序里啊??我试了下,会重复8遍,如果我用else加在if后面 Your choice: 1(这个部分是用if else吗??然后是和上面那个choice 1 写在一起的吗??) Enter command name to look up: FAQ Sorry, "FAQ" is not in the database.
lzxtc12 2012-12-02
  • 打赏
  • 举报
回复
引用 18 楼 xuchao1229 的回复:
引用 16 楼 lzxtc12 的回复:引用 15 楼 xuchao1229 的回复:引用 14 楼 xuchao1229 的回复:刚才那样有一点小bug 修改了一下 C/C++ code??12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849……
哦!谢谢啊,还有那个Y是什么意思啊?
14号选手 2012-12-02
  • 打赏
  • 举报
回复
引用 16 楼 lzxtc12 的回复:
引用 15 楼 xuchao1229 的回复:引用 14 楼 xuchao1229 的回复:刚才那样有一点小bug 修改了一下 C/C++ code??1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859……
如果减1的话,你加个等号就行了
14号选手 2012-12-02
  • 打赏
  • 举报
回复
引用 16 楼 lzxtc12 的回复:
引用 15 楼 xuchao1229 的回复:引用 14 楼 xuchao1229 的回复:刚才那样有一点小bug 修改了一下 C/C++ code??1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859……
这个位置不用减1,我刚看了下,是我写错了,呵呵,不好意思 j是代表列了,也就是i所在行的往后顺延的字母了,你可以在纸上写一下,一行一个字母,i代表行,j就是列,也就是代表每个字母的具体位置了
lzxtc12 2012-12-02
  • 打赏
  • 举报
回复
引用 15 楼 xuchao1229 的回复:
引用 14 楼 xuchao1229 的回复:刚才那样有一点小bug 修改了一下 C/C++ code??1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465#include<st……
我不是很懂下面这个部分,j代表什么啊?还有为什么长度要减去1呢? for(j=0;j<strlen(command[i])-1;j++) if(command[i][j]=='Y')
14号选手 2012-12-02
  • 打赏
  • 举报
回复
引用 22 楼 lzxtc12 的回复:
引用 20 楼 xuchao1229 的回复:引用 19 楼 lzxtc12 的回复:引用 18 楼 xuchao1229 的回复:引用 16 楼 lzxtc12 的回复:引用 15 楼 xuchao1229 的回复:引用 14 楼 xuchao1229 的回复:刚才那样有一点小bug 修改了一下 C/C++ code??12345678910111213141516……
呵呵 你是在国外留学? 哪个学校?
lzxtc12 2012-12-02
  • 打赏
  • 举报
回复
引用 20 楼 xuchao1229 的回复:
引用 19 楼 lzxtc12 的回复:引用 18 楼 xuchao1229 的回复:引用 16 楼 lzxtc12 的回复:引用 15 楼 xuchao1229 的回复:引用 14 楼 xuchao1229 的回复:刚才那样有一点小bug 修改了一下 C/C++ code??1234567891011121314151617181920212223242526272……
不好意思哦!我不在国内,有个时差,所以不能马上的结帖,不过非常感谢你!谢谢
14号选手 2012-12-02
  • 打赏
  • 举报
回复
引用 19 楼 lzxtc12 的回复:
引用 18 楼 xuchao1229 的回复:引用 16 楼 lzxtc12 的回复:引用 15 楼 xuchao1229 的回复:引用 14 楼 xuchao1229 的回复:刚才那样有一点小bug 修改了一下 C/C++ code??123456789101112131415161718192021222324252627282930313233343536373……
问题解决完了结贴啊~~~~
14号选手 2012-12-02
  • 打赏
  • 举报
回复
引用 19 楼 lzxtc12 的回复:
引用 18 楼 xuchao1229 的回复:引用 16 楼 lzxtc12 的回复:引用 15 楼 xuchao1229 的回复:引用 14 楼 xuchao1229 的回复:刚才那样有一点小bug 修改了一下 C/C++ code??123456789101112131415161718192021222324252627282930313233343536373……
你不是要找有you的字符串吗 只要找到有y的就行了,通过缩写去找就行了 后面的break一定要加上去,不然会重复查找的
lzxtc12 2012-12-01
  • 打赏
  • 举报
回复
引用 11 楼 xuchao1229 的回复:
引用 10 楼 lzxtc12 的回复:引用 9 楼 xuchao1229 的回复:引用 8 楼 lzxtc12 的回复:引用 7 楼 xuchao1229 的回复:可以用二维数组来存储字符串 一个存放简写,一个存放简写对应的字符 然后查找,对已简写的就输出全部的,反之也一样 不过这样的效率也是比较低的 我一个choice写出来了,第二个choice我知道是用st……
我写出来了,我试了很多遍也不知道第二个choice该怎么写?
#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 到底哪里错了吗??谢谢你
14号选手 2012-12-01
  • 打赏
  • 举报
回复
引用 10 楼 lzxtc12 的回复:
引用 9 楼 xuchao1229 的回复:引用 8 楼 lzxtc12 的回复:引用 7 楼 xuchao1229 的回复:可以用二维数组来存储字符串 一个存放简写,一个存放简写对应的字符 然后查找,对已简写的就输出全部的,反之也一样 不过这样的效率也是比较低的 我一个choice写出来了,第二个choice我知道是用strstr,但是我卡在它那个条件上了,我用……
已经给你提示了啊 楼上几位说的也很不错的 你自己先修改一下,再贴出来,大家一起帮你看看 这样你写出来之后才是你自己的东西了,不然我们写出来了,你再去看,那样你怎么都不会理解的
lzxtc12 2012-12-01
  • 打赏
  • 举报
回复
引用 9 楼 xuchao1229 的回复:
引用 8 楼 lzxtc12 的回复:引用 7 楼 xuchao1229 的回复:可以用二维数组来存储字符串 一个存放简写,一个存放简写对应的字符 然后查找,对已简写的就输出全部的,反之也一样 不过这样的效率也是比较低的 我一个choice写出来了,第二个choice我知道是用strstr,但是我卡在它那个条件上了,我用了一个for loop,然后我for loo……
那应该是怎么改呢?
14号选手 2012-12-01
  • 打赏
  • 举报
回复
引用 8 楼 lzxtc12 的回复:
引用 7 楼 xuchao1229 的回复:可以用二维数组来存储字符串 一个存放简写,一个存放简写对应的字符 然后查找,对已简写的就输出全部的,反之也一样 不过这样的效率也是比较低的 我一个choice写出来了,第二个choice我知道是用strstr,但是我卡在它那个条件上了,我用了一个for loop,然后我for loop里面加了一个if 我对if的条件是i……
你这里
if((strstr(command[i],b))!=0)
是用以为数组分别存储字符串的? 那这样写的话不对啊,你这里写的是二维数组存储字符串的行指针了
lzxtc12 2012-12-01
  • 打赏
  • 举报
回复
引用 7 楼 xuchao1229 的回复:
可以用二维数组来存储字符串 一个存放简写,一个存放简写对应的字符 然后查找,对已简写的就输出全部的,反之也一样 不过这样的效率也是比较低的
我一个choice写出来了,第二个choice我知道是用strstr,但是我卡在它那个条件上了,我用了一个for loop,然后我for loop里面加了一个if 我对if的条件是if((strstr(command[i],b))!=0),接下来就不知道怎么写了。。你可以教下我吗?谢谢你
14号选手 2012-12-01
  • 打赏
  • 举报
回复
可以用二维数组来存储字符串 一个存放简写,一个存放简写对应的字符 然后查找,对已简写的就输出全部的,反之也一样 不过这样的效率也是比较低的
lzxtc12 2012-12-01
  • 打赏
  • 举报
回复
引用 5 楼 cfjtaishan 的回复:
这个问题有一个很好的解决问题的办法,只不过,这样做很不智能。即,你建立的两个数组,让它们一一对应起来,通过使用strcmp比较,找到第二个字符对应的字符串,然后返回其下标,该下标就是第一个字符数组的对应的字符串。如果没找到,则返回一个负值。 这种方法比较低级,你需要事先将两个字符数组一一对应好。
我一个choice写出来了,第二个choice我知道是用strstr,但是我卡在它那个条件上了,我用了一个for loop,然后我for loop里面加了一个if 我对if的条件是if((strstr(command[i],b))!=0),接下来就不知道怎么写了。。你可以教下我吗?谢谢你
自信男孩 2012-12-01
  • 打赏
  • 举报
回复
这个问题有一个很好的解决问题的办法,只不过,这样做很不智能。即,你建立的两个数组,让它们一一对应起来,通过使用strcmp比较,找到第二个字符对应的字符串,然后返回其下标,该下标就是第一个字符数组的对应的字符串。如果没找到,则返回一个负值。 这种方法比较低级,你需要事先将两个字符数组一一对应好。
加载更多回复(7)

69,370

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧