uva537 - Artificial Intelligence?

/**********************
 * Author:FISTY
 * DATA:2014-12-2
 * string
 * uva 537
 * ******************/


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <math.h>
#include <ctype.h>
using namespace std;
#define MAX_N 100
double cover(char s, double a){
        if(s == 'k'){
                a *= 1000;
        }else if(s == 'M'){
                a *= 1000000;
        }else{
                if(s == 'm'){
                        a *= 0.001;
                }
        }
        return a;
}       
int main(){
        int n;
        int kcase = 0;
        scanf("%d", &n);
        char c = getchar();
        while(n--){
                char str[MAX_N];//存放原字符串
                char strU[MAX_N], strI[MAX_N],strP[MAX_N];// 存放值
                gets(str);
                
                printf("Problem #%d\n", ++kcase);
                int u = 0,ii = 0,p = 0;
                for(int i = 0;i < strlen(str); i++){
                        if(str[i] == 'U' && str[i+1] == '='){
                                u = 1;
                                int cnt = 0;   //str1的下标
                                char *q = strstr(str, "U="); // 得到U=首地址
                                for(int j = q-str+2; str[j] != 'V'; j++){
                                        strU[cnt++] = str[j]; 
                                }
                                strU[cnt] = '\0';
                                //for(int k = 0;k < strlen(strU); k++)
                                //        putchar(strU[k]);
                        }else if(str[i] == 'I' && str[i+1] == '='){
                                ii = 1;
                                int cnt = 0;   //str1的下标
                                char *q = strstr(str+2, "I="); // 得到U=首地址
                                for(int j = q-str+2; str[j] != 'A'; j++){
                                        strI[cnt++] = str[j];
                                }
                                strI[cnt] = '\0';
                                
                        }else if(str[i] == 'P' && str[i+1] == '='){
                                p = 1;
                                int cnt = 0;   //str1的下标
                                char *q = strstr(str, "P="); // 得到U=首地址
                                for(int j = q-str+2; str[j] != 'W'; j++){
                                        strP[cnt++] = str[j];
                                }
                                strP[cnt] = '\0';
                                
                        }else{
                                continue;
                        }
                }
                if(u && ii){
                        //求P
                        double U,I;
                        int U_len = strlen(strU);
                        int I_len = strlen(strI);
                        //U
                        U = atof(strU);
                        U = cover(strU[U_len-1], U);

                        //I
                        I = atof(strI);
                        I = cover(strI[I_len-1],I);
                        printf("P=%.2lfW\n", U*I);

                }else if(u && p){
                        //求I
                        double P,U;
                        int U_len = strlen(strU);
                        int P_len = strlen(strP);
                        U = atof(strU);
                        U = cover(strU[U_len-1],U);
                        P = atof(strP);
                        P = cover(strP[P_len-1], P);
                        printf("I=%.2lfA\n", P/U);
                }else {
                        if(ii && p){
                        //求U
                        double I,P;
                        int I_len = strlen(strI);
                        int P_len = strlen(strP);
                        I = atof(strI);
                        I = cover(strI[I_len-1], I);
                        P = atof(strP);
                        P = cover(strP[P_len-1], P);
                        printf("U=%.2lfV\n", P/I);

                        }
                }
                printf("\n");
        }
        return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值