|
发表于 2016-1-2 14:10:19
|
显示全部楼层
这样可以吗
- #include<stdio.h>
- #include <string.h>
- typedef struct
- {
- char string [10];
- char value;
- } TAB;
- void dispose_string (TAB tab[], //需要tab的地址
- char *a, char *b, //a b 字符串的地址
- int *x, int *y); //x y 为返回的对应的数字
- int main()
- {
- TAB tab[10] = {{ "zero", 0 }, { "one", 1 }, { "two", 2 },{ "three", 3 } ,{ "four", 4 } ,{ "five", 5 } ,{ "six", 6 } ,{ "seven", 7 } ,{ "eight", 8 } ,{ "nine", 9 } };
- char a[10], b[10]; //加数和被加数
- int x = 0, y = 0; //转换后的加数和被加数
- printf ("请输入:");
- scanf ("%s %s", &a, &b);
- //这里应该检查a b的值
- dispose_string (tab, a, b, &x, &y);
- printf ("%s和%s的和为:%d\n", a, b, x + y);
- return(0);
- }
- void dispose_string(TAB tab[], //需要tab的地址
- char *a, char *b, //a b 字符串的地址
- int *x, int *y) //x y 为返回的对应的数字
- {
- int i, k;
- for (i = 0; i < 10; i++)
- {
- if (strcmp (tab[i].string,a) == 0)
- {
- *x = tab[i].value;
- }
- }//应该处理一下没有找到的情况
- for (k = 0; k < 10; k++)
- {
- if (strcmp(tab[k].string, b) == 0)
- {
- *y = tab[k].value;
- }
- }
- }
复制代码 |
|