尧哥密码v1.01 来自豆包 因为我没有实力
#include <iostream>
#include <cstring>
#include <iomanip>
#include <cmath>
#include <cstdlib>
using namespace std;
// 1. 字母序数获取(A=1~Z=26,首字母和后续字母通用)
int getLetterOrder(char c) {
char upperC = toupper(c);
if (upperC >= 'A' && upperC <= 'Z') {
return upperC - 'A' + 1;
}
return -1; // 非法字符
}
// 2. 计算单个字母对应的小数位(两位数→1,一位数→0)
int getDecimalBit(int order) {
return (order >= 10 && order <= 26) ? 1 : 0;
}
// 3. 核心函数:拼音 → 编码(严格匹配规则,兼容旧编译器)
bool pinyinToCode(const char* pinyin, double& code) {
int len = strlen(pinyin);
if (len <= 0) {
cout << "错误:拼音不能为空!" << endl;
return false;
}
// 整数部分:首字母序数 + 后续字母序数(两位数拆分为十位+个位拼接)
long long integerPart = getLetterOrder(pinyin[0]);
for (int i = 1; i < len; i++) {
int order = getLetterOrder(pinyin[i]);
if (order == -1) {
cout << "错误:拼音包含非法字符 '" << pinyin[i] << "'!" << endl;
return false;
}
// 两位数拆分为十位和个位拼接(如 O=15 → 1 和 5)
if (order >= 10) {
integerPart = integerPart * 10 + (order / 10);
}
integerPart = integerPart * 10 + (order % 10);
}
// 小数部分:每个字母→1位(两位数=1,一位数=0),按字母顺序拼接
double decimalPart = 0.0;
for (int i = 0; i < len; i++) {
int order = getLetterOrder(pinyin[i]);
decimalPart = decimalPart * 10 + getDecimalBit(order);
}
// 替换 pow 函数(旧编译器可能不兼容)
double pow10 = 1.0;
for (int i = 0; i < len; i++) {
pow10 *= 10.0;
}
decimalPart /= pow10; // 转换为对应小数位数
code = integerPart + decimalPart;
return true;
}
// 完整拼音-编码映射表(263个标准汉语拼音,GBK编码)
struct PinyinCodeMap {
const char* pinyin;
double code;
};
PinyinCodeMap fullPinyinCodeTable[] = {
// A组
{"a", 1.00}, {"ai", 19.00}, {"an", 114.01}, {"ang", 1147.010}, {"ao", 115.01},
// B组
{"ba", 21.00}, {"bai", 219.000}, {"ban", 2114.001}, {"bang", 21147.0010}, {"bao", 2115.001},
{"bei", 259.000}, {"ben", 2514.001}, {"beng", 25147.0010}, {"bi", 29.00}, {"bian", 29114.0001},
{"biao", 29115.0001}, {"bie", 295.000}, {"bin", 2914.001}, {"bing", 29147.0010}, {"bo", 215.001},
{"bu", 221.010},
// C组
{"ca", 31.00}, {"cai", 319.000}, {"can", 3114.001}, {"cang", 31147.0010}, {"cao", 3115.001},
{"ce", 35.00}, {"cen", 3514.001}, {"ceng", 35147.0010}, {"cha", 381.000}, {"chai", 3819.0000},
{"chan", 38114.0001}, {"chang", 381147.00010}, {"chao", 38115.0001}, {"che", 385.000}, {"chen", 38514.0001},
{"cheng", 385147.00010}, {"chi", 389.000}, {"chong", 381147.00110}, {"chou", 381155.00110}, {"chu", 3821.0010},
{"chuan", 382114.00101}, {"chuang", 3821147.001010}, {"chui", 38219.00100}, {"chun", 382114.00101}, {"chuo", 382115.00101},
{"ci", 39.00}, {"cong", 31147.00110}, {"cou", 31155.00110}, {"cu", 321.010}, {"cuan", 32114.01001},
{"cui", 3219.01000}, {"cun", 32114.01001}, {"cuo", 32115.01001},
// D组
{"da", 41.00}, {"dai", 419.000}, {"dan", 4114.001}, {"dang", 41147.0010}, {"dao", 4115.001},
{"de", 45.00}, {"den", 4514.001}, {"deng", 45147.0010}, {"di", 49.00}, {"dian", 49114.0001},
{"diao", 49115.0001}, {"die", 495.000}, {"ding", 49147.0010}, {"diu", 4921.0010}, {"dong", 41147.00110},
{"dou", 41155.00110}, {"du", 421.010}, {"duan", 42114.01001}, {"dui", 4219.01000}, {"dun", 42114.01001},
{"duo", 42115.01001},
// E组
{"e", 5.00}, {"ei", 59.00}, {"en", 514.01}, {"eng", 5147.010}, {"er", 518.01},
// F组
{"fa", 61.00}, {"fan", 6114.001}, {"fang", 61147.0010}, {"fei", 659.000}, {"fen", 6514.001},
{"feng", 65147.0010}, {"fo", 615.001}, {"fou", 6155.0010}, {"fu", 621.010},
// G组
{"ga", 71.00}, {"gai", 719.000}, {"gan", 7114.001}, {"gang", 71147.0010}, {"gao", 7115.001},
{"ge", 75.00}, {"gen", 7514.001}, {"geng", 75147.0010}, {"gong", 71147.00110}, {"gou", 71155.00110},
{"gu", 721.010}, {"gua", 7211.0100}, {"guai", 72119.01000}, {"guan", 72114.01001}, {"guang", 721147.010010},
{"gui", 7219.01000}, {"gun", 72114.01001}, {"guo", 72115.01001},
// H组(示例核心)
{"ha", 81.00}, {"hai", 819.000}, {"han", 8114.001}, {"hang", 81147.0010}, {"hao", 8115.001},
{"he", 85.00}, {"hen", 8514.001}, {"heng", 85147.0010}, {"hong", 81147.00110}, {"hou", 81155.00110},
{"hu", 821.010}, {"hua", 8211.0100}, {"huai", 82119.01000}, {"huan", 82114.01001}, {"huang", 821147.010010},
{"hui", 8219.01000}, {"hun", 82114.01001}, {"huo", 82115.01001},
// J组
{"ji", 109.10}, {"jia", 1091.100}, {"jian", 109114.1001}, {"jiang", 1091147.10010}, {"jiao", 109115.1001},
{"jie", 1095.100}, {"jin", 10914.1001}, {"jing", 109147.10010}, {"jiong", 101147.11010}, {"jiu", 101155.11010},
{"ju", 1021.110}, {"juan", 102114.11001}, {"jue", 102115.11001}, {"jun", 102114.11001},
// K组
{"ka", 111.10}, {"kai", 1119.1000}, {"kan", 11114.1001}, {"kang", 111147.10010}, {"kao", 11115.1001},
{"ke", 115.10}, {"ken", 11514.1001}, {"keng", 115147.10010}, {"kong", 111147.11010}, {"kou", 111155.11010},
{"ku", 1121.110}, {"kua", 11211.1100}, {"kuai", 112119.11000}, {"kuan", 112114.11001}, {"kuang", 1121147.110010},
{"kui", 11219.11000}, {"kun", 112114.11001}, {"kuo", 112115.11001},
// L组
{"la", 121.10}, {"lai", 1219.1000}, {"lan", 12114.1001}, {"lang", 121147.10010}, {"lao", 12115.1001},
{"le", 125.10}, {"lei", 1259.1000}, {"len", 12514.1001}, {"leng", 125147.10010}, {"li", 129.10},
{"lia", 1291.100}, {"lian", 129114.1001}, {"liang", 1291147.10010}, {"liao", 129115.1001}, {"lie", 1295.100},
{"lin", 12914.1001}, {"ling", 129147.10010}, {"liu", 121155.11010}, {"long", 121147.11010}, {"lou", 121155.11010},
{"lu", 1221.110}, {"lv", 1222.110}, {"luan", 122114.11001}, {"lüan", 122214.11001}, {"lue", 122115.11001},
{"lüe", 122215.11001}, {"lun", 122114.11001}, {"luo", 122115.11001},
// M组
{"ma", 131.10}, {"mai", 1319.1000}, {"man", 13114.1001}, {"mang", 131147.10010}, {"mao", 13115.1001},
{"me", 135.10}, {"mei", 1359.1000}, {"men", 13514.1001}, {"meng", 135147.10010}, {"mi", 139.10},
{"mian", 139114.1001}, {"miao", 139115.1001}, {"mie", 1395.100}, {"min", 13914.1001}, {"ming", 139147.10010},
{"miu", 13921.1010}, {"mo", 1315.1001}, {"mou", 13155.1010}, {"mu", 1321.110},
// N组(示例核心)
{"na", 141.10}, {"nai", 1419.1000}, {"nan", 14114.1001}, {"nang", 141147.10010}, {"nao", 14115.1001},
{"ne", 145.10}, {"nei", 1459.1000}, {"nen", 14514.1001}, {"neng", 145147.10010}, {"ni", 149.10},
{"nian", 149114.1001}, {"niang", 1491147.10010}, {"niao", 149115.1001}, {"nie", 1495.100}, {"nin", 14914.1001},
{"ning", 149147.10010}, {"niu", 141155.11010}, {"nong", 141147.11010}, {"nu", 1421.110}, {"nv", 1422.110},
{"nü", 1422.110}, {"nuan", 142114.11001}, {"nüan", 142214.11001}, {"nue", 142115.11001}, {"nüe", 142215.11001},
{"nuo", 142115.11001},
// O组
{"o", 15.00}, {"ou", 1521.110},
// P组
{"pa", 161.10}, {"pai", 1619.1000}, {"pan", 16114.1001}, {"pang", 161147.10010}, {"pao", 16115.1001},
{"pei", 1659.1000}, {"pen", 16514.1001}, {"peng", 165147.10010}, {"pi", 169.10}, {"pian", 169114.1001},
{"piao", 169115.1001}, {"pie", 1695.100}, {"pin", 16914.1001}, {"ping", 169147.10010}, {"po", 1615.1001},
{"pou", 16155.1010}, {"pu", 1621.110},
// Q组
{"qi", 179.10}, {"qia", 1791.100}, {"qian", 179114.1001}, {"qiang", 1791147.10010}, {"qiao", 179115.1001},
{"qie", 1795.100}, {"qin", 17914.1001}, {"qing", 179147.10010}, {"qiong", 171147.11010}, {"qiu", 171155.11010},
{"qu", 1721.110}, {"quan", 172114.11001}, {"que", 172115.11001}, {"qun", 172114.11001},
// R组
{"ran", 18114.1001}, {"rang", 181147.10010}, {"rao", 18115.1001}, {"re", 185.10}, {"ren", 18514.1001},
{"reng", 185147.10010}, {"ri", 189.10}, {"rong", 181147.11010}, {"rou", 181155.11010}, {"ru", 1821.110},
{"ruan", 182114.11001}, {"rui", 18219.11000}, {"run", 182114.11001}, {"ruo", 182115.11001},
// S组
{"sa", 191.10}, {"sai", 1919.1000}, {"san", 19114.1001}, {"sang", 191147.10010}, {"sao", 19115.1001},
{"se", 195.10}, {"sen", 19514.1001}, {"seng", 195147.10010}, {"sha", 1981.1000}, {"shai", 19819.10000},
{"shan", 198114.10001}, {"shang", 1981147.100010}, {"shao", 198115.10001}, {"she", 1985.1000}, {"shen", 198514.10001},
{"sheng", 1985147.100010}, {"shi", 1989.1000}, {"shou", 1981155.100110}, {"shu", 19821.10010}, {"shuan", 1982114.100101},
{"shuang", 19821147.1001010}, {"shui", 198219.100100}, {"shun", 1982114.100101}, {"shuo", 1982115.100101}, {"si", 199.10},
{"song", 191147.11010}, {"sou", 191155.11010}, {"su", 1921.110}, {"suan", 192114.11001}, {"sui", 19219.11000},
{"sun", 192114.11001}, {"suo", 192115.11001},
// T组
{"ta", 201.10}, {"tai", 2019.1000}, {"tan", 20114.1001}, {"tang", 201147.10010}, {"tao", 20115.1001},
{"te", 205.10}, {"teng", 205147.10010}, {"ti", 209.10}, {"tian", 209114.1001}, {"tiao", 209115.1001},
{"tie", 2095.100}, {"ting", 209147.10010}, {"tong", 201147.11010}, {"tou", 201155.11010}, {"tu", 2021.110},
{"tuan", 202114.11001}, {"tui", 20219.11000}, {"tun", 202114.11001}, {"tuo", 202115.11001},
// W组
{"wa", 231.10}, {"wai", 2319.1000}, {"wan", 23114.1001}, {"wang", 231147.10010}, {"wei", 2359.1000},
{"wen", 23514.1001}, {"weng", 235147.10010}, {"wo", 2315.1001}, {"wu", 2321.110},
// X组
{"xi", 249.10}, {"xia", 2491.100}, {"xian", 249114.1001}, {"xiang", 2491147.10010}, {"xiao", 249115.1001},
{"xie", 2495.100}, {"xin", 24914.1001}, {"xing", 249147.10010}, {"xiong", 241147.11010}, {"xiu", 241155.11010},
{"xu", 2421.110}, {"xuan", 242114.11001}, {"xue", 242115.11001}, {"xun", 242114.11001},
// Y组
{"ya", 251.10}, {"yan", 25114.1001}, {"yang", 251147.10010}, {"yao", 25115.1001}, {"ye", 255.10},
{"yi", 259.10}, {"yin", 2514.1001}, {"ying", 25147.10010}, {"yong", 251147.11010}, {"you", 251155.11010},
{"yu", 2521.110}, {"yuan", 252114.11001}, {"yue", 252115.11001}, {"yun", 252114.11001},
// Z组
{"za", 261.10}, {"zai", 2619.1000}, {"zan", 26114.1001}, {"zang", 261147.10010}, {"zao", 26115.1001},
{"ze", 265.10}, {"zen", 26514.1001}, {"zeng", 265147.10010}, {"zha", 2681.1000}, {"zhai", 26819.10000},
{"zhan", 268114.10001}, {"zhang", 2681147.100010}, {"zhao", 268115.10001}, {"zhe", 2685.1000}, {"zhen", 268514.10001},
{"zheng", 2685147.100010}, {"zhi", 2689.1000}, {"zhong", 261147.11010}, {"zhou", 261155.11010}, {"zhu", 2621.110},
{"zhuan", 262114.11001}, {"zhuang", 2621147.110010}, {"zhui", 26219.11000}, {"zhun", 262114.11001}, {"zhuo", 262115.11001},
{"zi", 269.10}, {"zong", 261147.11010}, {"zou", 261155.11010}, {"zu", 2621.110}, {"zuan", 262114.11001},
{"zui", 26219.11000}, {"zun", 262114.11001}, {"zuo", 262115.11001}
};
const int CODE_TABLE_LEN = sizeof(fullPinyinCodeTable) / sizeof(fullPinyinCodeTable[0]);
// 4. 编码 → 拼音(反向查询)
const char* codeToPinyin(double code) {
for (int i = 0; i < CODE_TABLE_LEN; i++) {
if (fabs(code - fullPinyinCodeTable[i].code) < 1e-6) {
return fullPinyinCodeTable[i].pinyin;
}
}
return "未找到对应拼音";
}
// 5. 批量打印所有拼音编码
void printAllCodes() {
cout << "==================== 所有拼音编码表(共" << CODE_TABLE_LEN << "个)====================" << endl;
cout << setw(10) << "拼音" << setw(20) << "编码" << endl;
cout << "----------------------------------------" << endl;
for (int i = 0; i < CODE_TABLE_LEN; i++) {
int decimalDigits = strlen(fullPinyinCodeTable[i].pinyin);
cout << setw(10) << fullPinyinCodeTable[i].pinyin
<< setw(20) << fixed << setprecision(decimalDigits)
<< fullPinyinCodeTable[i].code << endl;
}
cout << "======================================================================" << endl;
}
// 6. 句子编码:拼音句子(空格分隔)→ 编码句子(/分隔)
// 适配 Dev-C++:用字符数组手动分割,避免 strtok 线程安全问题
void sentenceToCode() {
// 清除缓冲区(兼容 Dev-C++ 输入逻辑)
while (cin.get() != '\n');
char sentence[1024];
cout << "请输入拼音句子(拼音之间用空格分隔,如:ni hao ma):";
cin.getline(sentence, sizeof(sentence));
cout << "句子编码结果:";
int len = strlen(sentence);
int start = 0;
bool first = true;
for (int i = 0; i <= len; i++) {
// 遇到空格或结束符,分割拼音
if (sentence[i] == ' ' || sentence[i] == '\0') {
if (i > start) { // 避免连续空格
if (!first) {
cout << "/";
}
first = false;
// 提取单个拼音
char pinyin[20];
int pinyinLen = i - start;
strncpy(pinyin, sentence + start, pinyinLen);
pinyin[pinyinLen] = '\0'; // 手动添加结束符
double code;
if (pinyinToCode(pinyin, code)) {
int decimalDigits = strlen(pinyin);
cout << fixed << setprecision(decimalDigits) << code;
} else {
cout << "[错误拼音:" << pinyin << "]";
}
}
start = i + 1;
}
}
cout << endl;
}
// 7. 句子解码:编码句子(/分隔)→ 拼音句子(空格分隔)
// 适配 Dev-C++:用字符数组手动分割
void codeToSentence() {
// 清除缓冲区(兼容 Dev-C++ 输入逻辑)
while (cin.get() != '\n');
char codeStr[1024];
cout << "请输入编码句子(编码之间用/分隔,如:149.10/8115.001/1321.110):";
cin.getline(codeStr, sizeof(codeStr));
cout << "句子解码结果:";
int len = strlen(codeStr);
int start = 0;
bool first = true;
for (int i = 0; i <= len; i++) {
// 遇到/或结束符,分割编码
if (codeStr[i] == '/' || codeStr[i] == '\0') {
if (i > start) { // 避免连续/
if (!first) {
cout << " ";
}
first = false;
// 提取单个编码字符串
char codeChar[50];
int codeLen = i - start;
strncpy(codeChar, codeStr + start, codeLen);
codeChar[codeLen] = '\0'; // 手动添加结束符
// 转换为double(兼容旧编译器)
double code = atof(codeChar);
const char* pinyin = codeToPinyin(code);
cout << pinyin;
}
start = i + 1;
}
}
cout << endl;
}
int main() {
// 切换控制台为GBK编码(解决中文乱码)
system("chcp 936");
cout << "==================== 拼音编码转换器(Dev-C++ 5.9.2 专用)====================" << endl;
cout << "编码规则(100% 匹配示例 ni=149.10、hao=8115.001):" << endl;
cout << "1. 字母序数:A=1~Z=26(首字母和后续字母通用)" << endl;
cout << "2. 整数部分:首字母序数 + 后续字母序数(两位数拆分为十位+个位拼接)" << endl;
cout << " - ni:N=14(1+4)+ I=9 → 149" << endl;
cout << "3. 小数部分:每个字母序数→1位(两位数=1,一位数=0),按字母顺序拼接" << endl;
cout << " - ni:N=14(1)+ I=9(0)→ 0.10" << endl;
cout << "句子功能说明:" << endl;
cout << " - 拼音句子输入:拼音之间用空格分隔(如:wo ai zhong guo)" << endl;
cout << " - 编码句子输入:编码之间用/分隔(如:621.110/19.00/261147.11010/721.110)" << endl;
cout << "======================================================================" << endl << endl;
int choice;
while (true) {
cout << "请选择功能:" << endl;
cout << "1. 拼音查询编码" << endl;
cout << "2. 编码查询拼音" << endl;
cout << "3. 拼音句子→编码句子" << endl;
cout << "4. 编码句子→拼音句子" << endl;
cout << "5. 打印所有拼音编码" << endl;
cout << "6. 退出" << endl;
cout << "输入选择(1-6):";
cin >> choice;
switch (choice) {
case 1: {
char inputPinyin[20];
cout << "请输入拼音(如:ni、hao、zhong):";
cin >> inputPinyin;
double code;
if (pinyinToCode(inputPinyin, code)) {
int decimalDigits = strlen(inputPinyin);
cout << "拼音 '" << inputPinyin << "' 的编码:"
<< fixed << setprecision(decimalDigits) << code << endl;
}
break;
}
case 2: {
double inputCode;
cout << "请输入编码(如:149.10、8115.001):";
cin >> inputCode;
cout << "编码 " << fixed << setprecision(5) << inputCode
<< " 对应拼音:" << codeToPinyin(inputCode) << endl;
break;
}
case 3: {
sentenceToCode();
break;
}
case 4: {
codeToSentence();
break;
}
case 5: {
printAllCodes();
break;
}
case 6: {
cout << "感谢使用,再见!" << endl;
system("pause");
return 0;
}
default: {
cout << "输入错误,请选择1-6!" << endl;
break;
}
}
cout << endl << "----------------------------------------" << endl << endl;
}
system("pause");
return 0;
}快用Dev-C++5.9.2试试