跳到正文

科技·工程

尧哥密码

Chenguoyao44约 32 分钟
原文

这是 2025年11月18日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2025年11月18日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 32 分钟

版本

  1. v2当前
  2. v1

v1.02 在此感谢Anthony2011sfh,感谢他修复bug,为尧哥密码作出巨大贡献

C++
#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", 1.0}, {"ai", 19.00}, {"an", 114.01}, {"ang", 1147.010}, {"ao", 115.01}, 
	{"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.01}, 
	{"bu", 221.01}, {"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", 3815147.00110}, {"chou", 381521.0011}, {"chu", 3821.001}, {"chuan", 3821114.00101}, 
	{"chuang", 38211147.001010}, {"chui", 38219.0010}, {"chun", 382114.0011}, {"chuo", 382115.0011}, 
	{"ci", 39.00}, {"cong", 315147.0110}, {"cou", 31521.011}, {"cu", 321.01}, {"cuan", 321114.0101}, 
	{"cui", 3219.010}, {"cun", 32114.011}, {"cuo", 32115.011}, {"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.001}, {"dong", 415147.0110}, {"dou", 41521.011}, {"du", 421.01},
	{"duan", 421114.0101}, {"dui", 4219.010}, {"dun", 42114.011}, {"duo", 42115.011}, {"e", 5.0},
	{"ei", 59.00}, {"en", 514.01}, {"eng", 5147.010}, {"er", 518.01}, {"fa", 61.00}, {"fan", 6114.001},
	{"fang", 61147.0010}, {"fei", 659.000}, {"fen", 6514.001}, {"feng", 65147.0010}, {"fo", 615.01},
	{"fou", 61521.011}, {"fu", 621.01}, {"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", 715147.0110}, {"gou", 71521.011}, {"gu", 721.01}, {"gua", 7211.010}, {"guai", 72119.0100},
	{"guan", 721114.0101}, {"guang", 7211147.01010}, {"gui", 7219.010}, {"gun", 72114.011}, 
	{"guo", 72115.011}, {"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", 815147.0110},
	{"hou", 81521.011}, {"hu", 821.01}, {"hua", 8211.010}, {"huai", 82119.0100}, {"huan", 821114.0101},
	{"huang", 8211147.01010}, {"hui", 8219.010}, {"hun", 82114.011}, {"huo", 82115.011}, 
	{"ji", 109.10}, {"jia", 1091.100}, {"jian", 109114.1001}, {"jiang", 1091147.10010}, 
	{"jiao", 109115.1001}, {"jie", 1095.100}, {"jin", 10914.101}, {"jing", 109147.1010}, 
	{"jiong", 10915147.10110}, {"jiu", 10921.101}, {"ju", 1021.11}, {"juan", 1021114.1101}, 
	{"jue", 10215.110}, {"jun", 102114.111}, {"ka", 111.10}, {"kai", 1119.100}, {"kan", 11114.101},
	{"kang", 111147.1010}, {"kao", 11115.101}, {"ke", 115.10}, {"ken", 11514.101},
	{"keng", 115147.1010}, {"kong", 1115147.1110}, {"kou", 111521.111}, {"ku", 1121.11}, 
	{"kua", 11211.110}, {"kuai", 112119.1100}, {"kuan", 1121114.1101}, {"kuang", 11211147.11010},
	{"kui", 11219.110}, {"kun", 112114.111}, {"kuo", 112115.111}, {"la", 121.10}, 
	{"lai", 1219.100}, {"lan", 12114.101}, {"lang", 121147.1010}, {"lao", 12115.101}, 
	{"le", 125.10}, {"lei", 1259.100}, {"len", 12514.101}, {"leng", 125147.1010}, {"li", 129.10},
	{"lia", 1291.100}, {"lian", 129114.1001}, {"liang", 1291147.10010}, {"liao", 129115.1001}, 
	{"lie", 1295.100}, {"lin", 12914.101}, {"ling", 129147.1010}, {"liu", 12921.101}, {"long", 1215147.1110}, 
	{"lou", 121521.111}, {"lu", 1221.11}, {"lv", 1222.11}, {"luan", 1221114.1101}, {"lvan", 1222114.1101}, 
	{"lue", 12215.110}, {"lve", 12225.110}, {"lun", 122114.111}, {"luo", 122115.111}, {"ma", 131.10}, 
	{"mai", 1319.100}, {"man", 13114.101}, {"mang", 131147.1010}, {"mao", 13115.101}, {"me", 135.10}, 
	{"mei", 1359.100}, {"men", 13514.101}, {"meng", 135147.1010}, {"mi", 139.10}, {"mian", 139114.1001}, 
	{"miao", 139115.1001}, {"mie", 1395.100}, {"min", 13914.101}, {"ming", 139147.1010}, {"miu", 13921.101}, 
	{"mo", 1315.11}, {"mou", 131521.111}, {"mu", 1321.11}, {"na", 141.10}, {"nai", 1419.100}, 
	{"nan", 14114.101}, {"nang", 141147.1010}, {"nao", 14115.101}, {"ne", 145.10}, {"nei", 1459.100}, 
	{"nen", 14514.101}, {"neng", 145147.1010}, {"ni", 149.10}, {"nian", 149114.1001}, {"niang", 1491147.10010}, 
	{"niao", 149115.1001}, {"nie", 1495.100}, {"nin", 14914.101}, {"ning", 149147.1010}, {"niu", 14921.101}, 
	{"nong", 1415147.1110}, {"nu", 1421.11}, {"nv", 1422.11}, {"nv", 1422.11}, {"nuan", 1421114.1101}, 
	{"nvan", 1422114.1101}, {"nue", 14215.110}, {"nve", 14225.110}, {"nuo", 142115.111}, {"o", 15.1}, 
	{"ou", 1521.11}, {"pa", 161.10}, {"pai", 1619.100}, {"pan", 16114.101}, {"pang", 161147.1010}, 
	{"pao", 16115.101}, {"pei", 1659.100}, {"pen", 16514.101}, {"peng", 165147.1010}, {"pi", 169.10}, 
	{"pian", 169114.1001}, {"piao", 169115.1001}, {"pie", 1695.100}, {"pin", 16914.101}, 
	{"ping", 169147.1010}, {"po", 1615.11}, {"pou", 161521.111}, {"pu", 1621.11}, {"qi", 179.10}, 
	{"qia", 1791.100}, {"qian", 179114.1001}, {"qiang", 1791147.10010}, {"qiao", 179115.1001}, 
	{"qie", 1795.100}, {"qin", 17914.101}, {"qing", 179147.1010}, {"qiong", 17915147.10110}, 
	{"qiu", 17921.101}, {"qu", 1721.11}, {"quan", 1721114.1101}, {"que", 17215.110}, {"qun", 172114.111}, 
	{"ran", 18114.101}, {"rang", 181147.1010}, {"rao", 18115.101}, {"re", 185.10}, {"ren", 18514.101}, 
	{"reng", 185147.1010}, {"ri", 189.10}, {"rong", 1815147.1110}, {"rou", 181521.111}, {"ru", 1821.11}, 
	{"ruan", 1821114.1101}, {"rui", 18219.110}, {"run", 182114.111}, {"ruo", 182115.111}, {"sa", 191.10}, 
	{"sai", 1919.100}, {"san", 19114.101}, {"sang", 191147.1010}, {"sao", 19115.101}, {"se", 195.10}, 
	{"sen", 19514.101}, {"seng", 195147.1010}, {"sha", 1981.100}, {"shai", 19819.1000}, {"shan", 198114.1001}, 
	{"shang", 1981147.10010}, {"shao", 198115.1001}, {"she", 1985.100}, {"shen", 198514.1001}, {"sheng", 1985147.10010}, 
	{"shi", 1989.100}, {"shou", 1981521.1011}, {"shu", 19821.101}, {"shuan", 19821114.10101}, {"shuang", 198211147.101010}, 
	{"shui", 198219.1010}, {"shun", 1982114.1011}, {"shuo", 1982115.1011}, {"si", 199.10}, {"song", 1915147.1110}, 
	{"sou", 191521.111}, {"su", 1921.11}, {"suan", 1921114.1101}, {"sui", 19219.110}, {"sun", 192114.111}, 
	{"suo", 192115.111}, {"ta", 201.10}, {"tai", 2019.100}, {"tan", 20114.101}, {"tang", 201147.1010}, {"tao", 20115.101}, 
	{"te", 205.10}, {"teng", 205147.1010}, {"ti", 209.10}, {"tian", 209114.1001}, {"tiao", 209115.1001}, {"tie", 2095.100}, 
	{"ting", 209147.1010}, {"tong", 2015147.1110}, {"tou", 201521.111}, {"tu", 2021.11}, {"tuan", 2021114.1101}, 
	{"tui", 20219.110}, {"tun", 202114.111}, {"tuo", 202115.111}, {"wa", 231.10}, {"wai", 2319.100}, {"wan", 23114.101}, 
	{"wang", 231147.1010}, {"wei", 2359.100}, {"wen", 23514.101}, {"weng", 235147.1010}, {"wo", 2315.11}, {"wu", 2321.11}, 
	{"xi", 249.10}, {"xia", 2491.100}, {"xian", 249114.1001}, {"xiang", 2491147.10010}, {"xiao", 249115.1001}, 
	{"xie", 2495.100}, {"xin", 24914.101}, {"xing", 249147.1010}, {"xiong", 24915147.10110}, {"xiu", 24921.101}, 
	{"xu", 2421.11}, {"xuan", 2421114.1101}, {"xue", 24215.110}, {"xun", 242114.111}, {"ya", 251.10}, {"yan", 25114.101}, 
	{"yang", 251147.1010}, {"yao", 25115.101}, {"ye", 255.10}, {"yi", 259.10}, {"yin", 25914.101}, {"ying", 259147.1010}, 
	{"yong", 2515147.1110}, {"you", 251521.111}, {"yu", 2521.11}, {"yuan", 2521114.1101}, {"yue", 25215.110}, 
	{"yun", 252114.111}, {"za", 261.10}, {"zai", 2619.100}, {"zan", 26114.101}, {"zang", 261147.1010}, {"zao", 26115.101}, 
	{"ze", 265.10}, {"zen", 26514.101}, {"zeng", 265147.1010}, {"zha", 2681.100}, {"zhai", 26819.1000}, {"zhan", 268114.1001},
	{"zhang", 2681147.10010}, {"zhao", 268115.1001}, {"zhe", 2685.100}, {"zhen", 268514.1001}, {"zheng", 2685147.10010}, 
	{"zhi", 2689.100}, {"zhong", 26815147.10110}, {"zhou", 2681521.1011}, {"zhu", 26821.101}, {"zhuan", 26821114.10101}, 
	{"zhuang", 268211147.101010}, {"zhui", 268219.1010}, {"zhun", 2682114.1011}, {"zhuo", 2682115.1011}, {"zi", 269.10}, 
	{"zong", 2615147.1110}, {"zou", 261521.111}, {"zu", 2621.11}, {"zuan", 2621114.1101}, {"zui", 26219.110}, 
	{"zun", 262114.111}, {"zuo", 262115.111}
};

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;
}