uva 537

一道关于字符串的简单题,主要就是从字符串中找到两个等号的位置后,进行分析便可。

另外注意前缀所代表的数值大小(k, M, m)

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <sstream>

using namespace std;
int kasenum;

string str;
double power, volt, curr;
int pos[3];
char ch;

double scan(int index)
{
	bool flag = false;
	double res = 0.0, frac = 0.0, ratio = 0.1, multi = 1;

	if ('-' == str[index])
		flag = true;
	else if (str[index] >= '0' && str[index] <= '9')
		res = str[index] - '0';
	index++;

	while (str[index] >= '0' && str[index] <= '9')
	{
		res = res * 10 + str[index] - '0';
		index++;
	}
	if ('.' == str[index])
	{
		index++;
		while (str[index] >= '0' && str[index] <= '9')
		{
			frac = frac + ratio * (str[index] - '0');
			index++;
			ratio *= 0.1;
		}
	}
	res += frac;

	switch (str[index])
	{
	case 'm':
		multi = 1e-3;
		break;
	case 'k':
		multi = 1e+3;
		break;
	case 'M':
		multi = 1e+6;
		break;
	default:
		multi = 1.0;
		break;
	}

	res *= multi;
	return flag ? -res : res;
}


void readInfo()
{
	getline(cin, str);
	pos[0] = str.find('=');
	pos[1] = str.find('=', pos[0] + 1);

	for (int i = 0; i < 2; i++)
	{
		switch (str[pos[i] - 1])
		{
		case 'U':
			volt = scan(pos[i] + 1);
			break;
		case 'I':
			curr = scan(pos[i] + 1);
			break;
		case 'P':
			power = scan(pos[i] + 1);
			break;
		}
	}
}
int main()
{
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	scanf("%d", &kasenum);
	getchar();
	int kase = 0;
	while (kasenum--)
	{
		power = -1;
		volt = -1;
		curr = -1;
		readInfo();
		
		printf("Problem #%d\n", ++kase);
		if (-1 == power)
			printf("P=%.2lfW\n", volt * curr);
		else if (-1 == volt)
			printf("U=%.2lfV\n", power / curr);
		else
			printf("I=%.2lfA\n", power / volt);
		printf("\n");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值