Advertisement

将整数翻译成英文(C++)

阅读量:

将整数翻译成英文(C++)

复制代码
    #include <iostream>
    #include <string.h>
    using namespace std;
    static char *num1[] = {"",        "one",       "two",      "three",
                       "four",    "five",      "six",      "seven",
                       "eight",   "nine",      "ten",      "eleven",
                       "twelve",  "thirteen",  "fourteen", "fifteen",
                       "sixteen", "seventeen", "eighteen", "nineteen"};
    static char *num10[] = {"",      "",      "twenty",  "thirty", "forty",
                        "fifty", "sixty", "seventy", "eighty", "ninety"};
    static char *num100[] = {"", "thousand", "millon", "billoon", "tirllion"};
    class robot {
      private:
    char name[20];
    char type[20];
    
      public:
    robot() {
        strcpy(name, "xxx");
        strcpy(type, "xxx");
    }
    void set(char n[], char t[]) {
        strcpy(name, n);
        strcpy(type, t);
    }
    void out(int a); //小于1000
    void tran_int(int n, int k);
    ~robot(){};
    };
    
    void robot::out(int a) {
    int b = a % 100;
    if (a / 100 != 0) {
        cout << num1[a / 100] << " hundred "; //百位
        if (b != 0)
            cout << "and ";
    }
    if (b < 20)
        cout << num1[b]; // 20以内
    else {
        cout << num10[b / 10]; //大于20的十位
        if (b % 10 != 0)
            cout << num1[b % 10]; //个位
    }
    }
    void robot::tran_int(int n, int k) {  //大于1000的数
    if (n / 1000 != 0) {
        tran_int(n / 1000, ++k );  //递归调用 k记录该适用num100中哪个数量级
    }
    out(n % 1000);  //每三位调用一次out
    cout << " " << num100[k] << ",";
    }
    int main() {
    int n;
    cout << "shuru n:" << endl;
    cin >> n;
    if (n < 2147483647) {
        cout << n << endl;
        robot brown;
        brown.tran_int(n, 0);
    } else
        cout << "数字太大无法翻译" << endl;
    system("pause");
    return 0;
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码

全部评论 (0)

还没有任何评论哟~