UVaOJ 537

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

void printResult(int num,double value,char temp,char temp1)
{
	printf("Problem #%d\n",num);
	printf("%c=%.2f%c\n\n",temp,value,temp1);
} 
char text[1000];
int num;
int sign[3];//0表示P 1表示I 2表示U  位数表示存在的数值 
double value[3];
char charToValue[20]; 
 
int main()
{
	memset(text,0,sizeof(text));
	memset(sign,0,sizeof(sign));
	memset(value,0,sizeof(value));
	memset(charToValue,0,sizeof(charToValue));

	int checkValue = 0;
	int tmpSign = 0; 
	double result = 0;
	char temp1;
	char temp2; 
	int count = 1; 
		
		scanf("%d",&num); 
		while(num)
		{
			getchar();
			fgets(text,1000,stdin);
			int j = 0;
			memset(sign,0,sizeof(sign)); 
			for(int i = 0;i<strlen(text);i++)
			{	
				if((text[i]=='U'||text[i]=='I'||text[i]=='P')&&text[i+1]=='=')
				{
					checkValue++;
					if(text[i]=='U')
					{
						sign[2] = 1;
						tmpSign = 2;
					}
					
					if(text[i]=='I')
					{
						sign[1] = 1;
						tmpSign = 1;
					}
					
					if(text[i]=='P')
					{
						sign[0] = 1;
						tmpSign = 0;
					}
					
					for(int k=i+2;k<strlen(text);k++)
					{
						if(isdigit(text[k])||text[k]=='.')
							charToValue[j++] = text[k];
						else
						{
							charToValue[j] = '\0';
							j = 0;
							value[tmpSign] = atof(charToValue);
							
							if(text[k]=='m')
							{
								value[tmpSign] = value[tmpSign]*0.001f; 
							}
							if(text[k]=='k')
							{
								value[tmpSign] = value[tmpSign]*1000.0f; 
							}
							if(text[k]=='M')
							{
								value[tmpSign] = value[tmpSign]*1000000.0f;
							}
							i = k;
							break; 
						}
					} 
					if(checkValue>=2)
						{
							checkValue = 0;
							break; 
						}
				}
				
			} 
			for(int m =0;m<3;m++)
			{
				if(sign[m]==0)
				{
					if(m==0)
					{
						result = value[1]*value[2]; 
						temp1='P';
						temp2='W';
					}
					if(m==1)
					{
						result = value[0]/value[2];
						temp1='I';
						temp2='A';
					}
					if(m==2)
					{
						result = value[0]/value[1];
						temp1='U';
						temp2='V';
					}
					break;
				}
			}
			printResult(count,result,temp1,temp2);
			num--; 
			count ++;
		}	
	return 0;
}
YOYOYO 又AC了~

猜你喜欢

转载自blog.csdn.net/yao_jianlun/article/details/8791103
今日推荐