PAT-A 1005 Spell It Right (20)

#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;

int main()
{
    char c;
    int sum=0;
    while(scanf("%c",&c))
    {
        if(c=='\n')
        {
            break;
        }
        sum+=c-'0';
    }
    if(sum==0)
    {
        cout<<"zero";
    }
    else
    {
        stack<int> a;
        while(sum!=0)
        {
            a.push(sum%10);
            sum/=10;
        }
        int t,flag=0;
        while(!a.empty())
        {
            if(flag)
            {
                cout<<" ";
            }
            flag=1;
            t=a.top();
            a.pop();
            switch(t)
            {
            case 0:
                cout<<"zero";
                break;
            case 1:
                cout<<"one";
                break;
            case 2:
                cout<<"two";
                break;
            case 3:
                cout<<"three";
                break;
            case 4:
                cout<<"four";
                break;
            case 5:
                cout<<"five";
                break;
            case 6:
                cout<<"six";
                break;
            case 7:
                cout<<"seven";
                break;
            case 8:
                cout<<"eight";
                break;
            case 9:
                cout<<"nine";
                break;
            }
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章