D - Artificial Intelligence?

来源:互联网 发布:网络歌曲牛什么牛 编辑:程序博客网 时间:2024/06/05 00:49


Description

Download as PDF

Physics teachers in high school often think that problems given as text are more demanding than pure computations. After all, the pupils have to read and understand the problem first!

So they don't state a problem like ``U=10V, I=5A, P=?" but rather like ``You have an electrical circuit that contains a battery with a voltage of U=10V and a light-bulb. There's an electrical current of I=5A through the bulb. Which power is generated in the bulb?".

However, half of the pupils just don't pay attention to the text anyway. They just extract from the text what is given: U=10V, I=5A. Then they think: ``Which formulae do I know? Ah yes, P=U*I. Therefore P=10V*5A=500W. Finished."

OK, this doesn't always work, so these pupils are usually not the top scorers in physics tests. But at least this simple algorithm is usually good enough to pass the class. (Sad but true.)

Today we will check if a computer can pass a high school physics test. We will concentrate on the P-U-I type problems first. That means, problems in which two of power, voltage and current are given and the third is wanted.


Your job is to write a program that reads such a text problem and solves it according to the simple algorithm given above.

Input 

The first line of the input file will contain the number of test cases.

Each test case will consist of one line containing exactly two data fields and some additional arbitrary words. A data field will be of the form I=xAU=xV or P=xW, where x is a real number.

Directly before the unit (AV or W) one of the prefixes m (milli), k (kilo) and M (Mega) may also occur. To summarize it: Data fields adhere to the following grammar:

DataField ::= Concept '=' RealNumber [Prefix] UnitConcept   ::= 'P' | 'U' | 'I'Prefix    ::= 'm' | 'k' | 'M'Unit      ::= 'W' | 'V' | 'A'

Additional assertions:

  • The equal sign (`=') will never occur in an other context than within a data field.
  • There is no whitespace (tabs,blanks) inside a data field.
  • Either P and UP and I, or U and I will be given.

Output 

For each test case, print three lines:

  • a line saying ``Problem #k" where k is the number of the test case
  • a line giving the solution (voltage, power or current, dependent on what was given), written without a prefix and with two decimal places as shown in the sample output
  • a blank line

Sample Input 

3If the voltage is U=200V and the current is I=4.5A, which power is generated?A light-bulb yields P=100W and the voltage is U=220V. Compute the current, please.bla bla bla lightning strike I=2A bla bla bla P=2.5MW bla bla voltage?

Sample Output 

Problem #1P=900.00WProblem #2I=0.45AProblem #3U=1250000.00V



Miguel A. Revilla
1999-01-11

// Artificial Intelligence.cpp : 定义控制台应用程序的入口点。
//
#include<stdio.h>#include<string.h>#include<ctype.h>#define MAXN 100+10char s[MAXN];void decode_prefix(char s[],double *d)     //单位转换函数{int i=0;while (isdigit(s[i]) || s[i]=='.') i++;switch(s[i]){case 'm': *d/=1000; break;case 'k': *d*=1000; break;case 'M': *d*=1000000; break;default: break;}}int main(){int i,j,n,is_P,is_I,is_U;double P,I,U;scanf("%d",&n);getchar();             //读取之前输入的字符n,后面fgets函数读入时从流中继续读取一行for(i=0;i<n;i++){is_P=is_I=is_U=0;fgets(s,MAXN,stdin);for(j=0;j<(int)strlen(s);j++){if(s[j]=='P' && s[j+1]=='='){is_P=1;sscanf(&s[j+2],"%lf",&P);   //读取是数字的字符,转换成浮点型decode_prefix(&s[j+2],&P);   //单位转换函数,检查数据后的单位}if(s[j]=='I' && s[j+1]=='='){is_I=1;sscanf(&s[j+2],"%lf",&I);decode_prefix(&s[j+2],&I);}if(s[j]=='U' && s[j+1]=='='){is_U=1;sscanf(&s[j+2],"%lf",&U);decode_prefix(&s[j+2],&U);}}printf("Problem #%d\n",i+1);if(is_P!=1) printf("P=%.2lfW\n",U*I);if(is_I!=1) printf("I=%.2lfA\n",P/U);if(is_U!=1) printf("U=%.2lfV\n",P/I);printf("\n");}return 0;}



相关函数

isdigit

表头文件#include<,ctype,h>定义函数int,isdigit(,char,c)函数说明检查参数c是否为,阿拉伯数字,0到9返回值若参数c为阿拉伯数字,则返回TRUE,否则返回NULL(0)附加说明此为,宏定义,非真正函数范例

/* 找出str字符串中为阿拉伯数字的字符*/

#include<ctype.h>

main()

{

char str[]="123@#FDsP[e?";

int i;

for(i=0;str[i]!=0;i++)

{

if( isdigit (str[i]) )

printf( "%c is an digit character\n",str[i] );

}

}

执行

1 is an digit character

  2 is an digit character

  3 is an digit character

sscanf() - 从一个字符串中读进与指定格式相符的数据。swscanf()- 用于处理宽字符串,和sscanf功能相同。

详情见http://baike.sogou.com/v7600226.htm

0 0