简单模拟题。代码有点乱,感觉自己实现起来也有点麻烦。要加大力度练习啦!~

CODE:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

const int maxn = 101;
char str[11][20] = {"zero""one""two""three""four""five""six""seven""eight""nine"};
char word[11][11];
char s[maxn];
int cnt;


int cmp(char *s)  //查找 
{
    for(int i = 0 ; i < 10 ; i++)
    {
        if(strcmp(s, str[i]) == 0return i;
    }
    return -1;
}


void init(char *s)         //将数字保存进字符二维数组中 
{
    char save[21];
    int i, j;
    int l = strlen(s);
    int tot = 0;
    cnt = 0;
    for(i = 0 ; i < l; i++)
    {
        if(s[i] == ' ')
        {
            save[tot] = '\0';
            strcpy(word[cnt++], save);
            tot = 0;
        }
        else
        {
            save[tot++] = s[i];
        }
    }
}


int get()
{
    int a = 0, b = 0;
    int t;
    int i = 0;
    while(i < cnt)
    {
        if(cmp(word[i]) == -1break//接收'+' 
        a = a*10 + cmp(word[i++]);
    }
    i++;
    while(i < cnt)
    {
        if(cmp(word[i]) == -1break;  //接收'=' 
        b = b*10 + cmp(word[i++]);

    }
    return a+b;
}


int main()
{
    while(fgets(s, maxn, stdin))       //fgets(s, maxn, stdin);
    {
        memset(word, 0sizeof(word));
        init(s);
        int m = get();
        if(!m) break;
        printf("%d\n", m);
    }
    return 0;

} 

posted on 2012-07-20 12:36  有间博客  阅读(186)  评论(0编辑  收藏  举报