UVa 537 - Artificial Intelligence?

 题意很好看懂 ,直接看输入输出就了然了。Very Good!

写个函数读数据,再分三种情况输出。

 

/**
 * Author: Gneveek
 * Data: 2011-10-5~6
 * Descripition: UVa 537 - Artificial Intelligence?
 */ 
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define N 512
double getdata(char line[], char const ch);
int main()
{
	//freopen("C:\\in.txt","r",stdin);
	int n,i;
	char line[N];
	
	double P,U,I;
	scanf("%d",&n);
	getchar();
	for(i=0; i<n; i++)
	{
		gets(line);	
		printf("Problem #%d\n",i+1);
		P = U = I = 0;
		
		P = getdata(line, 'P');
		U = getdata(line, 'U');
		I = getdata(line, 'I');
		
		if(!P){
			printf("P=%.2lfW\n\n",U*I);
			continue;
		}			
		if(!U){
			printf("U=%.2lfV\n\n",P/I);
			continue;
		}			
		if(!I)
			printf("I=%.2lfA\n\n",P/U);		
	} 
	return 0;
}

double getdata(char line[], char const ch)
{
	char *p;
	bool flag = false;
	double value = 0;
	while(p = strchr(line,ch))
	{
		if(*(p + 1) != '=')
			line++;
		else{
			flag = true;
			break;
		}
			
	}
	if(flag)
	{
		p += 2;
		value = atof(p);
		while(!isalpha(*++p));
		if(*p == 'k')			
			value *= 1000;
		else if(*p == 'm')
			value /= 1000;
		else if(*p == 'M')
			value *= 1000000;	
	}
	return value;
}



你可能感兴趣的:(c)