您的位置:首页 > 其它

uva 537

2013-08-15 22:13 246 查看
#include <string>
#include <iostream>
#include <cstdio>

using namespace std;

int cases;
string str;

class elements{
public:
	void set(int pos, string &str){
		type = str[pos];
		pos += 2;
		val = 0.0;						// init val
		while(str[pos] >= '0' && str[pos] <= '9'){
			val = val * 10 + (str[pos] - '0'); 		
			++pos;		
		}
		if(str[pos] == '.'){					//if there is a dot
			++pos;
			double k = 10;
			while(str[pos] >= '0' && str[pos] <= '9'){ 
					val += (str[pos] - '0')/k;
					k *= 10;
					++pos;
			}	
		}
		if(str[pos] == 'm' || str[pos] == 'k' || str[pos] == 'M'){
			resetVal(pos, str);				//reset val
		}	

	}

	void resetVal(const int &pos, string &str){
		switch(str[pos]){
			case 'm': val /= 1000; break;
			case 'k': val *= 1000; break;	
			case 'M': val *= 1000000; break;
			}

	}
	void output(){
		printf("%c=%0.2lf", type, val);
		switch(type){
			case 'U':printf("V"); break;
			case 'I':printf("A"); break;
			case 'P':printf("W"); break;	

		}
		printf("\n");
	}		
	double val;		//the val 
	char type;		//the type
};

elements store[3];

int main()
{
	cin >> cases;
	getchar();
	for(int  i = 1; i <= cases; i++){
			getline(cin, str);
			int index = 0;
			int pos = 0;
			while(index < 2 ){ 
				while(str[pos] != '='){ pos++; }
				store[index++].set(pos - 1, str);
				++pos;
			}
			if(index == 2){	
				for(index = 0; index != 2; index++){
					if(store[index].type == 'P')
						break;
				}
				if(index != 2){
						pos = 1 - index;
					if(store[pos].type == 'U'){
						store[2].type = 'I';
					}else{
						store[2].type = 'U';
					}
					store[2].val = store[index].val/store[pos].val;
				}else{
					store[2].type = 'P';
					store[2].val = store[0].val * store[1].val;
				}
			}
			printf("Problem #%d\n", i);
			store[2].output();
			printf("\n");
		
	}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: