建立参数对应关系的小程序

来源:互联网 发布:黑桐谷嫂的淘宝店 编辑:程序博客网 时间:2024/04/28 17:29
/* * testparameter.c * *  Created on: 2013-7-10 *      Author: Administrator */#include <stdlib.h>#include <string.h>#include <stdio.h>#include "testparameter.h"static const struct paramater_table paramaters_table[] = { { "a", "apple" }, {"b", "boy" }, { "c", "cat" }, { "d", "dog" } };void usage(void) {fprintf(stderr, "usage: testparameter name\n");exit(0);}void search_parameter(char * fixed_name) {int i = 0;int ret_cmp = 0;    for (i = (sizeof(paramaters_table) / sizeof(paramaters_table[0])) - 1; i >= 0; i--){printf("**%d,%s**\n",i,paramaters_table[i].trname);ret_cmp = strcasecmp(fixed_name, paramaters_table[i].trname);if (ret_cmp == 0) {printf("get the name %s\n", paramaters_table[i].wifiname);return;}}}int main(int argc, char **argv) {if (argc != 2) {usage();}search_parameter(argv[1]);}
/* * testparameter.h * *  Created on: 2013-7-10 *      Author: Administrator */#ifndef TESTPARAMETER_H_#define TESTPARAMETER_H_/*tr and wifi parameter table*/struct paramater_table {const char *trname;const char *wifiname;};#endif /* TESTPARAMETER_H_ */

编译: gcc -o test testparameter.c testparameter.h

执行 : ./test 字符串参数