| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1181 人关注过本帖
标题:[求助]求助啊~~~~求了
只看楼主 加入收藏
Rank: 1
等 级:新手上路
帖 子:255
专家分:0
注 册:2005-4-25
收藏
 问题点数:0 回复次数:12 
[求助]求助啊~~~~求了
求助
现有己下几个己赋值的变量
int asdflj=23,lsjf=33,dsllie=234;

这几个变量的名字并没有任何联系

我想得到这样的结果:
当我从键盘输入asdflj或lsjf或dsllie则输出相应变量名的值

不用switch一个一个去判断的办法 有没有别的办法?
期待帮助......
搜索更多相关主题的帖子: 变量 asdflj dsllie lsjf 
2005-05-27 21:39
ChenMo
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:3
帖 子:481
专家分:10
注 册:2004-4-8
收藏
得分:0 
我的想法是:建一个 KeyWord 表应该足以解决这个问题。
个人认为建表存在的好处是程序能进行扩展。
使用结构体便能胜任。

欢迎加入 MVC 技术讨论群(新群:90093426)
2005-05-28 01:10
ChenMo
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:3
帖 子:481
专家分:10
注 册:2004-4-8
收藏
得分:0 
其实不用 switch 是有可能的,
用 strcmp 函数去检验字符串是否相等也不失为一个好办法。

欢迎加入 MVC 技术讨论群(新群:90093426)
2005-05-28 01:13
ChenMo
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:3
帖 子:481
专家分:10
注 册:2004-4-8
收藏
得分:0 

这样吧,我把代码写上,您可以参考一下。 为了测试,我对您的题目进行了如下修改: 增加了两个字符串,且它们的值由1到5排列。 本人不保证程序会发生异常或运行期错误。 #include "stdio.h" #include "conio.h" #include "string.h"

#define MAXBUF 128 /* 字符串最大缓冲区*/ #define MAXTABLE 5 /* 表大小*/ typedef struct KeyWordTag /* 表类型*/ { char KeyName[MAXBUF]; /* 字符串存储区*/ int Value; /* 对应的值存储区*/ }KeyWord;

int main(void) { KeyWord kwTab[MAXTABLE] = /* KeyWord Table */ { {"asdflj",1}, /* 初始化表 */ {"lsjf",2}, {"dsllie",3}, {"huen",4}, {"bccn",5} }; KeyWord *pkwTab = &kwTab; /* 指向由结构体构建的类型 KeyWord 的指针 */ char Buf_InStr[MAXBUF]; /*输入字符串的缓冲区*/ char *pInStr = Buf_InStr; /* 指向输入字符串的指针 */ int T; /* 循环辅助作用的临时变量 */

puts("\nInput a string."); scanf("%s",pInStr); /* 获得输入字符串 */

for(T = 0;T < MAXTABLE;T++) { if(!strcmp((pkwTab+T) -> KeyName,pInStr)) /* 找到匹配的字符串,输出其相应的值 */ { /* 输出结果 */ printf("\nFindout this string <%s> successfully!!!\nIt's value is: %d", pInStr, (pkwTab+T) -> Value); puts("\n\nPress any key to exit....\n"); getch(); return 0; /* 并不继续查找是否尚有相同的字符串 */ } }

printf("Fail: Unable to findout this string <%s>.\n", pInStr); /*查找失败*/ puts("\nPress any key to exit....\n"); getch(); return 0; }


欢迎加入 MVC 技术讨论群(新群:90093426)
2005-05-28 02:25
ChenMo
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:3
帖 子:481
专家分:10
注 册:2004-4-8
收藏
得分:0 
注释应该算非常详尽,此处附上测试结果:

第一次测试,输入表中存在的正确字符串 "huen",运行过程:正确运行
图片附件: 游客没有浏览图片的权限,请 登录注册


第二次测试,输入表中存在的正确字符串 "bccn",运行过程:正确运行
图片附件: 游客没有浏览图片的权限,请 登录注册


第三次测试,输入表中不存在的错误字符串 "temp",运行过程:正确运行
图片附件: 游客没有浏览图片的权限,请 登录注册


[此贴子已经被作者于2005-5-28 2:32:43编辑过]



欢迎加入 MVC 技术讨论群(新群:90093426)
2005-05-28 02:29
Rank: 1
等 级:新手上路
帖 子:255
专家分:0
注 册:2005-4-25
收藏
得分:0 
晕 哈哈 看来我意思没说清楚 先谢谢 我这就看

[此贴子已经被作者于2005-5-28 2:30:40编辑过]



/bbs/showimg.asp?BoardID=5&filename=2005-4/2005427111228529.jpg" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=5&filename=2005-4/2005427111228529.jpg');}" onmousewheel="return imgzoom(this);" alt="" /> 欢迎加入C语言QQ群698156 我们都是菜鸟乃至新手 坚信有一天定能展翅高飞 因为有着努力的决心 衷心盼望你的到来 让我们一起进步
2005-05-28 02:30
Rank: 1
等 级:新手上路
帖 子:255
专家分:0
注 册:2005-4-25
收藏
得分:0 
其实我的意思呢是 有没有可以偷赖的办法
比如结构里的段位不能用数组表示吧 必须是
struct {
unsigned a:1;
unsigned b:1;
unsigned c:1;
unsigned d:1;};
当段位数量稍多如十几个时 去查看段位的数值 (比如输入5,想要查看第5个段位的值)
如果一个一个地判断就要写十几个case挺麻烦
所以我就在想 有没有办法不用写这么多语句 并且只需要输入a就能直接查看到a段位的值就好了

/bbs/showimg.asp?BoardID=5&filename=2005-4/2005427111228529.jpg" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=5&filename=2005-4/2005427111228529.jpg');}" onmousewheel="return imgzoom(this);" alt="" /> 欢迎加入C语言QQ群698156 我们都是菜鸟乃至新手 坚信有一天定能展翅高飞 因为有着努力的决心 衷心盼望你的到来 让我们一起进步
2005-05-28 02:38
ChenMo
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:3
帖 子:481
专家分:10
注 册:2004-4-8
收藏
得分:0 
原来如此,我尚未想到什么好办法。
要定位我想多数情况下多要经过搜索的,
计算机可不如人眼,一看——就是它——不用找了,如果是的话那计算机可真神了。。。

偷懒?……
那吃饭时不勺碗不夹菜你怎么吃呢?食物如果会自动飞进嘴里那该有多好啊(好像植物人是这样“灌”的)……

欢迎加入 MVC 技术讨论群(新群:90093426)
2005-05-28 03:27
Rank: 1
等 级:新手上路
帖 子:255
专家分:0
注 册:2005-4-25
收藏
得分:0 
希望偷赖 只是希望

/bbs/showimg.asp?BoardID=5&filename=2005-4/2005427111228529.jpg" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=5&filename=2005-4/2005427111228529.jpg');}" onmousewheel="return imgzoom(this);" alt="" /> 欢迎加入C语言QQ群698156 我们都是菜鸟乃至新手 坚信有一天定能展翅高飞 因为有着努力的决心 衷心盼望你的到来 让我们一起进步
2005-05-28 03:56
Rank: 1
等 级:新手上路
帖 子:255
专家分:0
注 册:2005-4-25
收藏
得分:0 
厉害啊  注释写得这么清楚 我根本不知道如何写注释

/bbs/showimg.asp?BoardID=5&filename=2005-4/2005427111228529.jpg" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=5&filename=2005-4/2005427111228529.jpg');}" onmousewheel="return imgzoom(this);" alt="" /> 欢迎加入C语言QQ群698156 我们都是菜鸟乃至新手 坚信有一天定能展翅高飞 因为有着努力的决心 衷心盼望你的到来 让我们一起进步
2005-05-28 04:01
快速回复:[求助]求助啊~~~~求了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013348 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved