1005 Spell It Right

1005 Spell It Right(20 分)

Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.

Input Specification:

Each input file contains one test case. Each case occupies one line which contains an N (10100​​).

Output Specification:

For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.

Sample Input:

12345

Sample Output:

one five

#include<stdio.h>
#include<iostream>
#include<string>

using namespace std;

int charToInt(char n)
{
    int result;
    switch(n)
    {
    case '0':
        result = 0;
        break;
    case '1':
        result = 1;
        break;
    case '2':
        result = 2;
        break;
    case '3':
        result = 3;
        break;
    case '4':
        result = 4;
        break;
    case '5':
        result = 5;
        break;
    case '6':
        result = 6;
        break;
    case '7':
        result = 7;
        break;
    case '8':
        result = 8;
        break;
    case '9':
        result = 9;
        break;
    default:
        break;
    }

    return result;
}

string intToString(int n)
{
    string result;
    switch(n)
    {
    case 0:
        result = "zero";
        break;
    case 1:
        result = "one";
        break;
    case 2:
        result = "two";
        break;
    case 3:
        result = "three";
        break;
    case 4:
        result = "four";
        break;
    case 5:
        result = "five";
        break;
    case 6:
        result = "six";
        break;
    case 7:
        result = "seven";
        break;
    case 8:
        result = "eight";
        break;
    case 9:
        result = "nine";
        break;
    default:
        break;
    }

    return result;
}

int main()
{
    char N;
    int sum = 0;

    while(cin >> N)
    {
        sum += charToInt(N);
    }
    
    if(sum == 0)
    {
        cout << "zero" <<endl;
        return 0;
    }

    int num[101]; //sum存入num中
    int p = 0;//记录sum的位数
    while(sum != 0)
    {
        num[p] = sum%10;
        sum = sum/10;
        p++;
    }

    for(int i = p-1;i > 0;i--)
    {
        cout << intToString(num[i]) << " ";
    }
    cout << intToString(num[0]) << endl;

    return 0;
}
原文地址:https://www.cnblogs.com/meiqin970126/p/9563327.html