View Code of Problem 16

# include <iostream>
# include <cstdio>
# include <cstdlib>
# include <cstring>
using namespace std;

char a[1005];
int main()
{
    string s;
    string ss[11] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
    int t, n, i1, i2, i3, i4, i5, i6, i7, i8, i9, i0, i, len;
    int l, r;
    cin >> t;
    getchar();
    while(t--)
    {
        cin >> n;
        getchar();
        getline(cin, s);
        i0 = s.find("zero");
        while(i0 != string::npos)
        {
            s.replace(i0, 4, "0", 1);i0 = s.find("zero");
        }
        i1 = s.find("one");
        while(i1 != string::npos)
        {
            s.replace(i1, 3, "1", 1);i1 = s.find("one");
        }
        i2 = s.find("two");
        while(i2 != string::npos)
        {
            s.replace(i2, 3, "2", 1);i2 = s.find("two");
        }
        i3 = s.find("three");
        while(i3 != string::npos)
        {
            s.replace(i3, 5, "3", 1);i3 = s.find("three");
        }
        i4 = s.find("four");
        while(i4 != string::npos)
        {
            s.replace(i4, 4, "4", 1);i4 = s.find("four");
        }
        i5 = s.find("five");
        while(i5 != string::npos)
        {
            s.replace(i5, 4, "5", 1);i5 = s.find("five");
        }
        i6 = s.find("six");
        while(i6 != string::npos)
        {
            s.replace(i6, 3, "6", 1);i6 = s.find("six");
        }
        i7 = s.find("seven");
        while(i7 != string::npos)
        {
            s.replace(i7, 5, "7", 1);i7 = s.find("seven");
        }
        i8 = s.find("eight");
        while(i8 != string::npos)
        {
            s.replace(i8, 5, "8", 1);i8 = s.find("eight");
        }
        i9 = s.find("nine");
        while(i9 != string::npos)
        {
            s.replace(i9, 4, "9", 1);i9 = s.find("nine");
        }
        int ik = s.find(" ");
        while(ik != string::npos)
        {
            s.erase(s.begin() + ik);
            ik = s.find(" ");
        }
        s.copy(a, s.size(), 0);
        sscanf(a, "%d+%d", &l,&r);

        int sum = l + r;
        memset(a, 0, sizeof(a));
        sprintf(a, "%d",sum);
        if(n)
        {
            len = strlen(a);
            for(i=0; i<len-1; ++i)
            {
                cout << ss[a[i]-48] << " ";
            }
            cout << ss[a[i]-48] << endl;
        }
        else
            puts(a);

    }
    return 0;
}

Double click to view unformatted code.


Back to problem 16