c语言编写函数 输入一个整数 输出对应的字符串,该如何处理
c语言编写函数 输入一个整数 输出对应的字符串
例如: ‘it is ok!’=1 当输入为1时,输出为it is ok!
------解决方案--------------------
如果整数是从1开始连续的,搞个字符指针数组就好了
------解决方案--------------------
用c++的map<string,int> simap;
------解决方案--------------------
------解决方案--------------------
嗯,如果不是的话,用switch也行
例如: ‘it is ok!’=1 当输入为1时,输出为it is ok!
------解决方案--------------------
如果整数是从1开始连续的,搞个字符指针数组就好了
------解决方案--------------------
用c++的map<string,int> simap;
------解决方案--------------------
#include <stdio.h>
#include <string.h>
char s[3][20]={
"It is error!",
"It is ok!",
"It may ok.",
};
int main() {
int i;
while (1) {
printf("Input a number(0..2):");
fflush(stdout);
rewind(stdin);
if (1==scanf("%d",&i)) {
if (0<=i && i<=2) {
printf("%s\n",s[i]);
continue;
}
}
printf("Format error!\n");
break;
}
return 0;
}
//Input a number(0..2):0
//It is error!
//Input a number(0..2):1
//It is ok!
//Input a number(0..2):2
//It may ok.
//Input a number(0..2):3
//Format error!
//
------解决方案--------------------
嗯,如果不是的话,用switch也行