{ "question_id": 768078, "question_title": "如何根据一个值,查找一个对应关系表,获得对应值呢?", "question_content": "

如何根据一个小范围值区间,查找一个对应关系表,获得一个对应值呢\n例如电压值为2000~2026(mv),![图片说明](https://img-ask.csdn.net/upload/201907/03/1562152764_309711.png)\n其对应功率值为-52(dB),如何编程,完成建表,查表,获得结果呢?

", "difficulty": "困难", "answer_id": 827931, "answer_content": "\n```\n/*17点:一一对应版本*/\n#include \n\n#define NG (0xFF)\n\n//data structure\ntypedef struct\n{\n\t\tint voltage;\n\t\tint power;\t\n}stVoltPowerTbl;\n\n//table\nconst stVoltPowerTbl myTable[] = \n{\n\t\t{-19,1083},\n\t\t{-18,1049},\n\t\t{-17,1025},\n\t\t{-16,996},\n\t\t//add your data \n};\n\nint Find(int voltage)\n{\n\t\tint Ret = NG;\n\t\tint i = 0;\n\t\t\n\t\tfor(i=0;i<((sizeof(myTable))/(sizeof(stVoltPowerTbl)));i++)\n\t\t{\n\t\t\t\tif(myTable[i].voltage == voltage)\n\t\t\t\t{\n\t\t\t\t\t\tRet = myTable[i].power;\n\t\t\t\t}\n\t\t}\n\t\treturn Ret;\n}\n\nint main()\n{\n\t\tint Ret = 0;\n\t\t\n\t\tRet = Find(-17);\n\t\t\n\t\tif(Ret != NG)\n\t\t{ \n\t\t\t\tprintf(\"power = %d \\n\",Ret);\n\t\t}\n\t\telse\n\t\t{ \n\t\t\t\tprintf(\"Error Voltage! \\n\");\n\t\t}\n\t\t\n\t\treturn 0;\n}\n```\n\n```\n/*21点:根据范围求对应值版本*/\n#include \n\n#define NG (0xFF)\n\n//data structure\ntypedef struct\n{\n\t\tint power;\t\t\n\t\tint minVoltage;\n\t\tint maxVoltage;\n}stVoltPowerTbl;\n\n//table\nconst stVoltPowerTbl myTable[] = \n{\n\t\t{-55,2080,2094},\n\t\t{-54,2060,2075},\n\t\t{-52,2000,2026},\n\t\t//add your data \n};\n\n\nint Find(int voltage)\n{\n\t\tint Ret = NG;\n\t\tint i = 0;\n\t\t\n\t\tfor(i=0;i<((sizeof(myTable))/(sizeof(stVoltPowerTbl)));i++)\n\t\t{\n\t\t\t\tif((myTable[i].minVoltage < voltage)&&(myTable[i].maxVoltage > voltage))\n\t\t\t\t{\n\t\t\t\t\t\tRet = myTable[i].power;\n\t\t\t\t}\n\t\t}\n\t\treturn Ret;\n}\n\nint main()\n{\n\t\tint Ret = 0;\n\t\t\n\t\tRet = Find(2001);\n\t\t\n\t\tif(Ret != NG)\n\t\t{ \n\t\t\t\tprintf(\"power = %d \\n\",Ret);\n\t\t}\n\t\telse\n\t\t{ \n\t\t\t\tprintf(\"Error Voltage! \\n\");\n\t\t}\n\t\t\n\t\treturn 0;\n}\n```", "tag_name": "c语言", "cpp": "#include \n#define NG (0xFF)\ntypedef struct\n{\n\tint voltage;\n\tint power;\t\n}stVoltPowerTbl;\nconst stVoltPowerTbl myTable[] = \n{\n\t{-19,1083},\n\t{-18,1049},\n\t{-17,1025},\n\t{-16,996},\n};\nint Find(int voltage)\n{\n\tint Ret = NG;\n\tint i = 0;\n\tfor(i=0;i<((sizeof(myTable))/(sizeof(stVoltPowerTbl)));i++)\n\t{\n\t\tif(myTable[i].voltage == voltage)\n\t\t{\n\t\t\tRet = myTable[i].power;\n\t\t}\n\t}\n\treturn Ret;\n}\nint main()\n{\n\tint Ret = 0;\n\tRet = Find(-17);\n\tif(Ret != NG)\n\t{\t\n\t\tprintf(\"power = %d \\n\",Ret);\n\t}\n\telse\n\t{\t\n\t\tprintf(\"Error Voltage! \\n\");\n\t}\n\treturn 0;\n}", "topic_link": "https://bbs.csdn.net/topics/600470255", "status": 0, "keywords": "动态表,表扩张,算法中阶,摊还分析,高级设计和分析技术", "license": "csdn.net", "notebook": { "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/22.ipynb?type=file" }, "notebook_enable": 1 }