UVAOJ 537字符串处理

注意有小数点的字符串的处理

熟悉字符串操作


#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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值