您的位置:首页 > 其它

UVAOJ 537字符串处理

2013-09-05 20:17 411 查看
注意有小数点的字符串的处理

熟悉字符串操作

#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <iomanip>
using namespace std;

double getNum(string k){
int find = k.find(".");
double res =0, xs =0;
int cnt = (find==-1)?k.size():find;
int xnt = (find==-1)?0:(k.size()-find);
// cout<<"k:"<<k<<"find:"<<find<<endl;
for(int i=0; i<cnt;i++)
res = res*10+(k[i] - '0');
for(int j=1; j<xnt;j++)
xs = xs*10+(k[find+j] - '0');
return res + xs* pow(10.0, -1.0*(k.size()-1-find));
}

double isP(string str, int k){
double fg = 0;
int i;
string num;
for(i=k; i<str.size();i++){
if(str[i] == 'A' || str[i] == 'V' || str[i] == 'W')	break;
if(isdigit(str[i])||str[i]=='.')
num = num + str[i];
}
char c = str[i-1];
if(c=='m')			fg = 0.001;
else if(c=='k')		fg = 1000;
else if(c=='M')		fg = 1000000;
else fg = 1;
return fg*getNum(num);
}

void pro(string str){
int p = str.find("P=");
int q = str.find("U=");
int r = str.find("I=");
double pi = (p!=-1)?isP(str, p):0;
double qi = (q!=-1)?isP(str, q):0;
double ri = (r!=-1)?isP(str, r):0;
//	cout<<pi<<" "<<qi<<" "<<ri<<endl;
if(p!=-1 && q!=-1){
printf("I=%.2fA\n", pi/qi);
}else if(p!=-1 && r!=-1){
printf("U=%.2fV\n", pi/ri);
}else if(q!=-1 && r!=-1){
printf("P=%.2fW\n", qi*ri);
}
}

int main(){
//freopen("537.txt", "r", stdin);
vector<string> re;
int totl, k=0;
string res;
cin>>totl;
cin.ignore();

while(k++ < totl){
getline(cin, res);
re.push_back(res);
}
for(int i=0; i<totl;i++){
cout<<"Problem #"<<(i+1)<<endl;
pro(re[i]);
cout<<endl;
}
//fclose(stdin);
//freopen("CON", "r", stdin);

//	system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: