从string中寻找"P="、"U="、"I=",然后记录P、U、I的值,使用istringstream比较方便。
以下是我的代码:
#include<iostream>
#include<sstream>
#include<string>
#include<cstdio>
using namespace std;
int main()
{
/*
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
//*/
int T;
cin>>T;getchar();
for(int case_num=1;case_num<=T;case_num++)
{
string line;
getline(cin,line);
double P(-1.0),U(-1.0),I(-1.0);
istringstream sin(line);
char ch;
while(sin>>ch)
{
if(ch=='P')
{
char t;
sin>>t;
if(t!='=')
continue;
sin>>P;
sin>>t;
if(t=='m')
P/=1000;
else if(t=='k')
P*=1000;
else if(t=='M')
P*=1000000;
}
else if(ch=='U')
{
char t;
sin>>t;
if(t!='=')
continue;
sin>>U;
sin>>t;
if(t=='m')
U/=1000;
else if(t=='k')
U*=1000;
else if(t=='M')
U*=1000000;
}
else if(ch=='I')
{
char t;
sin>>t;
if(t!='=')
continue;
sin>>I;
sin>>t;
if(t=='m')
I/=1000;
else if(t=='k')
I*=1000;
else if(t=='M')
I*=1000000;
}
}
cout<<"Problem #"<<case_num<<endl;
if(P==-1.0)
printf("P=%.2fW\n",U*I);
else if(U==-1.0)
printf("U=%.2fV\n",P/I);
else
printf("I=%.2fA\n",P/U);
printf("\n");
}
return 0;
}
posted on 2011-04-07 12:53
lee1r 阅读(686)
评论(0) 编辑 收藏 引用 所属分类:
题目分类:字符串处理