• poj2121


    字符串处理,还是用string类比较容易,先将字符串用million和thousand切分成三段,然后用同样的方法处理这三段即可。

    注意输入数据结尾有多余空行

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

    string name[] =
    {
    "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
    int id[] =
    {
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, 40, 50, 60, 70, 80, 90 };

    int getid(string st)
    {
    for (int i = 0; i < 27; i++)
    if (st == name[i])
    return id[i];
    return -1;
    }

    int toint(string st)
    {
    if (st == "zero")
    return 0;
    int ret = 0;
    int pos;
    if ((pos = st.find("hundred")) != string::npos)
    {
    ret
    += getid(st.substr(0, pos - 1)) * 100;
    if (st.length() >= pos + 8)
    st.erase(
    0, pos + 8);
    else
    st.erase(
    0, pos + 7);
    }
    if ((pos = st.find(" ")) != string::npos)
    {
    ret
    += getid(st.substr(0, pos));
    if (st.length() >= pos + 1)
    st.erase(
    0, pos + 1);
    else
    st.erase(
    0, pos);
    }
    if (st.length() > 0)
    ret
    += getid(st);
    return ret;
    }

    int main()
    {
    //freopen("D:\\t.txt", "r", stdin);
    string st;
    while (getline(cin, st) && st != "")
    {
    int ans = 0;
    int pos;
    int ne = 1;
    if (st.substr(0, 8) == "negative")
    {
    ne
    = -1;
    st.erase(
    0, 9);
    }
    if ((pos = st.find("million")) != string::npos)
    {
    ans
    += 1000000 * toint(st.substr(0, pos - 1));
    if (st.length() >= pos + 8)
    st.erase(
    0, pos + 8);
    else
    st.erase(
    0, pos + 7);
    }
    if ((pos = st.find("thousand")) != string::npos)
    {
    ans
    += 1000 * toint(st.substr(0, pos - 1));
    if (st.length() >= pos + 9)
    st.erase(
    0, pos + 9);
    else
    st.erase(
    0, pos + 8);
    }
    if (st.length() > 0)
    ans
    += toint(st);
    ans
    *= ne;
    printf(
    "%d\n", ans);
    }
    return 0;
    }
  • 相关阅读:
    uploadify上传文件代码
    事务处理拼接sql语句对数据库的操作.异常回滚
    Scrum【转】
    Redis
    mybatis
    Spring MVC
    IOC的理解(转载)
    spring IOC与AOP
    git
    python基础2
  • 原文地址:https://www.cnblogs.com/rainydays/p/2033411.html
Copyright © 2020-2023  润新知