Artificial Intelligence 字符串截取

Description

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=xA, U=xV or P=xW, where x is a real number.

Directly before the unit (A, V 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] Unit
Concept ::= '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 U, P 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

3
If 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 #1
P=900.00W

Problem #2
I=0.45A

Problem #3
U=1250000.00V

思路1:判定 '='好位置,截取后面字符串,把字符串转换。记得截取所得串末尾加 '/0'


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<stdio.h>
#include<string.h>
#include<math.h>
double  prim( char  a[])
{
     double  sum=0;
     int  len1,i,j;
     len1= strlen (a);
     for (i=0;i<len1;i++)
     if (a[i]== '.' )
     break ;
     for (j=0;j<i;j++)
     sum+=( pow (10,i-j-1)*(a[j]- '0' ));
     for (i++,j=1;i<len1;i++)
     sum+= pow (0.1,j++)*(a[i]- '0' );
     return  sum;
}
int  main()
{
     int  n,k,len,i,j,m;
     double  dy,dl,gl,x;
     char  str[1000],str1[100];
     scanf ( "%d" ,&n);
     getchar ();
     for (k=1;k<=n;k++)
     {
         dy=0;dl=0;gl=0;
         gets (str);
         len= strlen (str);
         for (i=0;i<len;i++)
         {
             if (str[i]== '=' )
             {
                  x=1; //x=1技巧
                 for (j=i+1,m=0;str[j]>= '0' &&str[j]<= '9' ||str[j]== '.' ;j++)
                 str1[m++]=str[j];
                 str1[m]= '\0' ; //  注意'\0'
                 switch (str[j])
                 {
                     case  'm' :x=0.001; break ;
                     case  'k' :x=1000; break ;
                     case  'M' :x=1000000; break ;
                 }
                 switch (str[i-1])
                 {
                     case  'P' :gl=x*prim(str1); break ; //体现x=1妙处
                     case  'I' :dl=x*prim(str1); break ;
                     case  'U' :dy=x*prim(str1); break ;
                 }
                 i=j;    
             }
         }
         if (gl==0)
     printf ( "Problem #%d\nP=%0.2lfW\n\n" ,k,dy*dl);
     else
     if (dl==0)
     printf ( "Problem #%d\nI=%0.2lfA\n\n" ,k,gl/dy);
     else
     if (dy==0)
     printf ( "Problem #%d\nU=%0.2lfV\n\n" ,k,gl/dl);           
     }
     return  0;
}
思路2:直接利用getchar()读入字符,当读到'='号时候,按照浮点型读入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<stdio.h>
char  str[2000];
int  main()
{
     int  n,k,j;
     float  u,i,p,prem,mid;
     char  ch;
     str[0]= 'q' ;
     scanf ( "%d" ,&n);
     getchar ();
     for (k=1;k<=n;k++)
     {
         u=0;i=0;p=0;j=1;
     while ((str[j]= getchar ())&&str[j]!= '\n' )
         {
             j++;
             if (str[j-1]== '=' )
             {
                 prem=1;
                 scanf ( "%f %c" ,&mid,&ch);
                 switch (ch)
                 {
                     case  'm' : prem=0.001; break ;
                     case  'k' : prem=1000;  break ;
                     case  'M' : prem=1000000; break ;
                 }
                 switch (str[j-2])
                 {
                     case  'U' : u=mid*prem; break ;
                     case  'I' : i=mid*prem; break ;
                     case  'P' : p=mid*prem; break ;
                 }
            
         }
     if (p==0)
     printf ( "Problem #%d\nP=%0.2fW\n\n" ,k,u*i);
     else
     if (i==0)
     printf ( "Problem #%d\nI=%0.2fA\n\n" ,k,p/u);
     else
     if (u==0)
     printf ( "Problem #%d\nU=%0.2fV\n\n" ,k,p/i);    
     }
     return  0;
}

人工智能和LISP(LISt Processing)是紧密相关的两个概念。LISP是一种编程语言,最早于1958年由美国计算机科学家约翰·麦卡锡(John McCarthy)设计并开发。正是基于LISP语言的特性,人工智能领域得到了巨大的推动和发展。 LISP的设计使其非常适用于处理符号化的数据和执行复杂的逻辑运算。这使得LISP成为早期人工智能研究中的首选编程语言。研究人员利用LISP的高度灵活性和表达能力,构建了许多能够模拟人类智能的系统。 LISP为人工智能的发展提供了强大的工具和理论基础。其中最著名的例子是LISP语言中的列表(list)数据结构,它提供了一种方便的方式来存储和操作符号化的知识。这在构建专家系统和知识表示方面起到了重要的作用。 另一个与LISP和人工智能密切相关的概念是LISP的元编程能力。通过LISP的元编程功能,研究人员可以在运行时修改和扩展程序逻辑,这对于开发智能系统的学习和自适应能力非常有帮助。例如,利用LISP的元编程能力,人工智能研究人员能够实现基于案例推理的系统,通过不断添加新的规则和示例,使系统能够从经验中学习。 总之,LISP语言为人工智能的发展做出了巨大贡献。它提供了强大的符号化处理能力和元编程能力,使研究人员能够构建智能系统并实现学习和自适应。至今,LISP仍然在人工智能研究中扮演着重要的角色,它仍然是研究人员用于开发智能系统的一种首选语言。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值