UVa 537 - Artificial Intelligence?

题目链接:UVa 537 - Artificial Intelligence?

字符串处理,简单题。注意几个函数:

1、strncpy,如果没有把源串全部复制完,需要手动补‘\0’。还有这个函数可以通过源串的指针确定从哪开始复制,这点刚开始没有想到。

2、atof 字符串转化为浮点数,头文件,stdlib.h。

#include <iostream>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <iomanip>

using namespace std;

const int MAX_N = 1024 * 2;
char line[MAX_N];
char a[2][MAX_N / 2];
int T;
double _a[2];
int cnt;
char b[2];
int solve(int i,int len)
{
    int j;
    if(line[i - 1] == 'U')
    {
        b[cnt] = 'U';
        for(j = i + 1;j < len;j++)
        {
            if(line[j] == 'V')
                break;
        }
        if(line[j - 1] == 'm')
        {
            strncpy(a[cnt],line + i + 1,j - i - 1);
            a[cnt][j - i - 1] = '\0';
            _a[cnt] = atof(a[cnt]);
            _a[cnt] = _a[cnt] / 1000.00;
        }
        else if(line[j - 1] == 'k')
        {
            strncpy(a[cnt],line + i + 1,j - i - 1);
            a[cnt][j - i - 1] = '\0';
            _a[cnt] = atof(a[cnt]);
            _a[cnt] = _a[cnt] * 1000.00;
        }
        else if(line[j - 1] == 'M')
        {
            strncpy(a[cnt],line + i + 1,j - i - 1);
            a[cnt][j - i - 1] = '\0';
            _a[cnt] = atof(a[cnt]);
            _a[cnt] = _a[cnt] * 1000000.00;
        }
        else
        {
            strncpy(a[cnt],line + i + 1,j - i);
            a[cnt][j - i] = '\0';
            _a[cnt] = atof(a[cnt]);
        }
    }
        else if(line[i - 1] == 'P')
        {
            b[cnt] = 'P';
            for(j = i + 1;j < len;j++)
            {
                if(line[j] == 'W')
                    break;
            }
            if(line[j - 1] == 'm')
            {
               strncpy(a[cnt],line + i + 1,j - i - 1);
                a[cnt][j - i - 1] = '\0';
                _a[cnt] = atof(a[cnt]);
                _a[cnt] = _a[cnt] / 1000.00;
            }
            else if(line[j - 1] == 'k')
            {
                strncpy(a[cnt],line + i + 1,j - i - 1);
                a[cnt][j - i - 1] = '\0';
                _a[cnt] = atof(a[cnt]);
                _a[cnt] = _a[cnt] * 1000.00;
            }
            else if(line[j - 1] == 'M')
            {
                strncpy(a[cnt],line + i + 1,j - i - 1);
                a[cnt][j - i - 1] = '\0';
                _a[cnt] = atof(a[cnt]);
                _a[cnt] = _a[cnt] * 1000000.00;
            }
            else
            {
                strncpy(a[cnt],line + i + 1,j - i);
                a[cnt][j - i] = '\0';
                _a[cnt] = atof(a[cnt]);
            }
        }
        else if(line[i - 1] == 'I')
        {
            b[cnt] = 'I';
            for(j = i + 1;j < len;j++)
            {
                if(line[j] == 'A')
                    break;
            }
            if(line[j - 1] == 'm')
            {
                strncpy(a[cnt],line + i + 1,j - i - 1);
                a[cnt][j - i - 1] = '\0';
                _a[cnt] = atof(a[cnt]);
                _a[cnt] = _a[cnt] / 1000.00;
            }
            else if(line[j - 1] == 'k')
            {
                strncpy(a[cnt],line + i + 1,j - i - 1);
                a[cnt][j - i - 1] = '\0';
                _a[cnt] = atof(a[cnt]);
                _a[cnt] = _a[cnt] * 1000.00;
            }
            else if(line[j - 1] == 'M')
            {
                strncpy(a[cnt],line + i + 1,j - i - 1);
                a[cnt][j - i - 1] = '\0';
                _a[cnt] = atof(a[cnt]);
                _a[cnt] = _a[cnt] * 1000000.00;
            }
            else
            {
                strncpy(a[cnt],line + i + 1,j - i);
                a[cnt][j - i] = '\0';
                _a[cnt] = atof(a[cnt]);
            }
        }
    cnt++;
    return j;
}
int main()
{
    cin>>T;
    int num = 0;
    double result;
    while(T--)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        cnt = 0;
        getchar();
        gets(line);
        int len = strlen(line);
        int num1,num2;
        num1 = num2 = 0;
        int i;
        for(i = 0;i < len;i++)
        {
            if(line[i] == '=')
            {
                i = solve(i,len);
            }
        }
        cout<<"Problem #"<<++num<<endl;
        if(b[0] == 'P')
        {
            if(b[1] == 'U')
            {
                result = _a[0] / _a[1];
                cout<<"I=";
                cout<<fixed<<setprecision(2)<<result;
                cout<<"A"<<endl;
            }
            else
            {
                result = _a[0] / _a[1];
                cout<<"U=";
                cout<<fixed<<setprecision(2)<<result;
                cout<<"V"<<endl;
            }
        }
        else if(b[0] == 'U')
        {
            if(b[1] == 'P')
            {
                result = _a[1] / _a[0];
                cout<<"I=";
                cout<<fixed<<setprecision(2)<<result;
                cout<<"A"<<endl;
            }
            else
            {
               result = _a[0] * _a[1];
               cout<<"P=";
               cout<<fixed<<setprecision(2)<<result;
               cout<<"W"<<endl;
            }

        }
        else if(b[0] == 'I')
        {
            if(b[1] == 'P')
            {
                result = _a[1] / _a[0];
                cout<<"U=";
                cout<<fixed<<setprecision(2)<<result;
                cout<<"V"<<endl;
            }

            else
            {
                result = _a[0] * _a[1];
                cout<<"P=";
                cout<<fixed<<setprecision(2)<<result;
                cout<<"W"<<endl;
            }

        }
        cout<<endl;
    }
    return 0;
}


你可能感兴趣的:(UVa 537 - Artificial Intelligence?)