uva 537 Artificial Intelligence?

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid;=8&page;=show_problem&problem;=478 题意:给出一段文字。。其中包含了 P,U,I(功率,电压,电流)中的两个。。求第三个。 思路:字符串处理。。第一次用vim复制整段代码。。命令模式下按v,然后光标扫过的区域都会选中,按y就就复制到剪贴板了。。 所以虽然代码写了300行但只有100行是需要写的。。200行复制改下就好== WA了两次。。一次是因为I写成了小写。。另一次是因为多组数据记得初始化多次。

  1/* ***********************************************
  2Author :111qqz
  3Created Time :2016年01月22日 星期五 03时25分16秒
  4File Name :code/uva/537.cpp
  5************************************************ */
  6
  7#include <cstdio>
  8#include <cstring>
  9#include <iostream>
 10#include <algorithm>
 11#include <vector>
 12#include <queue>
 13#include <set>
 14#include <map>
 15#include <string>
 16#include <cmath>
 17#include <cstdlib>
 18#include <ctime>
 19#define fst first
 20#define sec second
 21#define lson l,m,rt<<1
 22#define rson m+1,r,rt<<1|1
 23#define ms(a,x) memset(a,x,sizeof(a))
 24typedef long long LL;
 25#define MP make_pair
 26using namespace std;
 27const double eps = 1E-8;
 28const int dx4[4]={1,0,0,-1};
 29const int dy4[4]={0,-1,1,0};
 30const int inf = 0x3f3f3f3f;
 31string str;
 32char cstr[20];
 33double i,p,u;
 34int pu,pp;
 35int pi;
 36int pv,pw,pa;
 37int beishu;
 38string tmp;
 39int main()
 40{
 41	#ifndef  ONLINE_JUDGE 
 42	freopen("code/in.txt","r",stdin);
 43  #endif
 44	int T;
 45	int cas = 0;
 46	cin>>T;
 47	getchar();
 48	while (T--)
 49	{
 50	    beishu = 0;  //多组数据。。。忘记每次都初始化==
 51	    printf("Problem #%d\n",++cas);
 52	    getline(cin,str);
 53//	    cout<<"str:"<<str<<endl;
 54	    pu=str.find("U=");
 55	    pi=str.find("I=");
 56	    pp=str.find("P=");
 57//	    cout<<"pi:"<<pi<<" pu:"<<pu<<" pp:"<<pp<<endl;
 58	    if (pp==-1)
 59	    {
 60		pv=str.find('V',pu+1);
 61		pa=str.find('A',pi+1);
 62
 63		if (str[pv-1]=='m'||str[pv-1]=='k'||str[pv-1]=='M')
 64		{
 65		    if (str[pv-1]=='m')
 66		    {    
 67			beishu-=3;
 68			tmp = str.substr(pu+2,pv-pu-3);
 69		    }
 70		    if (str[pv-1]=='k')
 71		    {
 72			beishu+=3;
 73			tmp = str.substr(pu+2,pv-pu-3);
 74		    }
 75		    if (str[pv-1]=='M')
 76		    {
 77			beishu+=6;
 78			tmp = str.substr(pu+2,pv-pu-3);
 79		    }
 80		}
 81		else
 82		{
 83		    tmp = str.substr(pu+2,pv-pu-2);
 84		}
 85
 86		strcpy(cstr,tmp.c_str());
 87		sscanf(cstr,"%lf",&u);
 88//		cout<<"u:"<<u<<endl;
 89		if (str[pa-1]=='m'||str[pa-1]=='k'||str[pa-1]=='M')
 90		{
 91		    if (str[pa-1]=='m')
 92		    {
 93			beishu-=3;
 94			tmp = str.substr(pi+2,pa-pi-3);
 95		    }
 96		    if (str[pa-1]=='k')
 97		    {
 98			beishu+=3;
 99			tmp = str.substr(pi+2,pa-pi-3);
100		    }
101		    if (str[pa-1]=='M')
102		    {
103			beishu+=6;
104			tmp = str.substr(pi+2,pa-pi-3);
105		    }
106		}
107		else
108		{
109		    tmp = str.substr(pi+2,pa-pi-2);
110		}
111		strcpy(cstr,tmp.c_str());
112		sscanf(cstr,"%lf",&i);
113//		cout<<"i:"<<i<<endl;
114
115		p = 1.0;
116		if (beishu<0)
117		{
118		    for ( int i = 1 ; i <= abs(beishu) ; i++)
119		    {
120			p = p*0.1;
121		    }
122		}
123		else
124		{
125		    for ( int i = 1 ; i <= beishu ; i++)
126		    {
127			p = p * 10;
128		    }
129		}
130		p = p*u*i;
131		printf("P=%.2fW\n",p);
132	    }
133	    if (pi==-1)
134	    {
135		pv = str.find('V',pu+1);
136		pw = str.find("W",pp+1);
137		if (str[pv-1]=='m'||str[pv-1]=='k'||str[pv-1]=='M')
138		{
139		    if (str[pv-1]=='m')
140		    {    
141			beishu+=3;
142			tmp = str.substr(pu+2,pv-pu-3);
143		    }
144		    if (str[pv-1]=='k')
145		    {
146			beishu-=3;
147			tmp = str.substr(pu+2,pv-pu-3);
148		    }
149		    if (str[pv-1]=='M')
150		    {
151			beishu-=6;
152			tmp = str.substr(pu+2,pv-pu-3);
153		    }
154		}
155		else
156		{
157		    tmp = str.substr(pu+2,pv-pu-2);
158		}
159
160		strcpy(cstr,tmp.c_str());
161		sscanf(cstr,"%lf",&u);
162//		cout<<"u:"<<u<<endl;
163
164		if (str[pw-1]=='m'||str[pw-1]=='k'||str[pw-1]=='M')
165		{
166		    if (str[pw-1]=='m')
167		    {
168			beishu-=3;
169			tmp = str.substr(pp+2,pw-pp-3);
170		    }
171		     if (str[pw-1]=='k')
172		    {
173			beishu+=3;
174			tmp = str.substr(pp+2,pw-pp-3);
175		    }
176		    if (str[pw-1]=='M')
177		    {
178			beishu+=6;
179			tmp = str.substr(pp+2,pw-pp-3);
180		    }
181
182		}
183		else
184		{
185		    tmp = str.substr(pp+2,pw-pp-2);
186		}
187
188		strcpy(cstr,tmp.c_str());
189		sscanf(cstr,"%lf",&p);
190//		cout<<"p:"<<p<<endl;
191
192		i=1.0;
193		if (beishu<0)
194		{
195		    for ( int i = 1 ; i <=abs(beishu) ; i++)
196		    {
197			i=i*0.1;
198		    }
199		}
200		else
201		{
202		    for ( int i = 1 ; i <= beishu ; i++)
203		    {
204			i=i*10;
205		    }
206		}
207		i = i*p/u;
208		printf("I=%.2fA\n",i);	
209	    }
210
211	    if (pu==-1)
212	    {
213		pa = str.find('A',pi);
214		pw = str.find('W',pp);
215		if (str[pa-1]=='m'||str[pa-1]=='k'||str[pa-1]=='M')
216		{
217		    if (str[pa-1]=='m')
218		    {
219			beishu+=3;
220			tmp = str.substr(pi+2,pa-pi-3);
221		    }
222		    if (str[pa-1]=='k')
223		    {
224			beishu-=3;
225			tmp = str.substr(pi+2,pa-pi-3);
226		    }
227		    if (str[pa-1]=='M')
228		    {
229			beishu-=6;
230			tmp = str.substr(pi+2,pa-pi-3);
231		    }
232		}
233		else
234		{
235		    tmp = str.substr(pi+2,pa-pi-2);
236		}
237		strcpy(cstr,tmp.c_str());
238		sscanf(cstr,"%lf",&i);
239//		cout<<"i:"<<i<<endl;
240
241		if (str[pw-1]=='m'||str[pw-1]=='k'||str[pw-1]=='M')
242		{
243		    if (str[pw-1]=='m')
244		    {
245			beishu-=3;
246			tmp = str.substr(pp+2,pw-pp-3);
247		    }
248		     if (str[pw-1]=='k')
249		    {
250			beishu+=3;
251			tmp = str.substr(pp+2,pw-pp-3);
252		    }
253		    if (str[pw-1]=='M')
254		    {
255			beishu+=6;
256			tmp = str.substr(pp+2,pw-pp-3);
257		    }
258
259		}
260		else
261		{
262		    tmp = str.substr(pp+2,pw-pp-2);
263		}
264
265		strcpy(cstr,tmp.c_str());
266		sscanf(cstr,"%lf",&p);
267//		cout<<"p:"<<p<<endl;
268
269
270		u = 1;
271		if (beishu<0)
272		{
273		    for ( int i = 1 ; i <=abs(beishu) ; i++)
274		    {
275			u *=0.1;
276		    }
277		}
278		else
279		{
280		    for ( int i = 1 ; i <= beishu ; i++)
281		    {
282			u *=10;
283		    }
284		}
285		u = u*p/i;
286		printf("U=%.2fV\n",u);
287
288
289	    }
290	    printf("\n");
291
292
293	}
294
295  #ifndef ONLINE_JUDGE  
296  fclose(stdin);
297  #endif
298    return 0;
299}