5820. 【NOIP提高A组模拟2018.8.16】 非法输入

Description

在算法竞赛中,题目一般保证了输入数据的合法性。然而在工程开发中,我们往往不期望程 序得到的输入都是合法的。
D 君正忙着向校内 OJ 添加题目,在写了第 233 个 val.cpp 之后,她的头脑中涌现出了这样 的想法。于是她决定出一道不需要写 val.cpp 的题。
输入两个整数,你需要做的就是输出她们的和。
 

Input

第一行一个正整数 T。
接下来 T 行,每行代表一组数据。
一组合法的数据包含由一个空格隔开的两个合法的十进制整数,行首和行尾不包含任何多余 的字符。
一个合法的十进制整数要么是 0,要么由一个可选的负号,一个 1 到 9 之间的数字,和若干 个 0 到 9 之间的数字顺序连接而成,并且这两个数字均在区间 [−2^31 , 2^31) 之内。
一组不合法的数据是一个不匹配以上规则的字符串。
 

Output

对于每组数据,如果该数据是合法的请输出一行一个整数代表答案,否则请输出 Input Error。
 

Solution

这题还能怎么讲啊,按照Input的要求做就好了,注意前导零。

 

代码(巨丑无比)

 

  1 var
  2   TT:longint;
  3   s:ansistring;
  4 procedure main;
  5 var
  6   a,b:int64;
  7   i,l:longint;
  8   bo1,bo2:boolean;
  9 begin
 10   l:=length(s);
 11   if l<=2 then
 12     begin
 13       writeln('Input Error'); exit;
 14     end;
 15   bo1:=false; a:=0;
 16   if s[1]='-' then bo1:=true else
 17     if (ord(s[1])>=48) and (ord(s[1])<=57) then a:=ord(s[1])-48 else
 18       begin
 19         writeln('Input Error'); exit;
 20       end;
 21   if (bo1) and (s[2]='0') then
 22     begin
 23       writeln('Input Error'); exit;
 24     end;
 25   if l>=2 then
 26   if (not bo1) and (a=0) and (s[2]<>' ') then
 27     begin
 28       writeln('Input Error'); exit;
 29     end;
 30   for i:=2 to l do
 31     if s[i]<>' ' then
 32       begin
 33         if (ord(s[i])>=48) and (ord(s[i])<=57) then
 34           a:=a*10+(ord(s[i])-48) else
 35           begin
 36             writeln('Input Error'); exit;
 37           end;
 38         if bo1 then
 39           begin
 40             if a>maxlongint+1 then
 41               begin
 42                 writeln('Input Error'); exit;
 43               end;
 44           end else
 45           begin
 46             if a>maxlongint then
 47               begin
 48                 writeln('Input Error'); exit;
 49               end;
 50           end;
 51       end else break;
 52   if (bo1) and (i=2) then
 53     begin
 54       writeln('Input Error'); exit;
 55     end;
 56   if i+1<=l then
 57     if s[i+1]=' ' then
 58       begin
 59         writeln('Input Error'); exit;
 60       end;
 61   delete(s,1,i);
 62   l:=length(s);
 63   if l=0 then
 64     begin
 65       writeln('Input Error'); exit;
 66     end;
 67   bo2:=false; b:=0;
 68   if s[1]='-' then bo2:=true else
 69     if (ord(s[1])>=48) and (ord(s[1])<=57) then b:=ord(s[1])-48 else
 70       begin
 71         writeln('Input Error'); exit;
 72       end;
 73   if l>=2 then
 74   if (not bo2) and (b=0) and (s[2]<>' ') then
 75     begin
 76       writeln('Input Error'); exit;
 77     end;
 78   if (bo2) and (s[2]='0') then
 79     begin
 80       writeln('Input Error'); exit;
 81     end;
 82   for i:=2 to l do
 83     if s[i]<>' ' then
 84       begin
 85         if (ord(s[i])>=48) and (ord(s[i])<=57) then
 86           b:=b*10+(ord(s[i])-48) else
 87           begin
 88             writeln('Input Error'); exit;
 89           end;
 90         if bo2 then
 91           begin
 92             if b>maxlongint+1 then
 93               begin
 94                 writeln('Input Error'); exit;
 95               end;
 96           end else
 97           begin
 98             if b>maxlongint then
 99               begin
100                 writeln('Input Error'); exit;
101               end;
102           end;
103       end else break;
104   if (bo2) and (l=1) then
105     begin
106       writeln('Input Error'); exit;
107     end;
108   if i<=l then
109   if (ord(s[i])<48) or (ord(s[i])>57) then
110       begin
111         writeln('Input Error'); exit;
112       end;
113   if bo1 then a:=-a;
114   if bo2 then b:=-b;
115   writeln(a+b);
116 end;
117 
118 begin
119   assign(input,'aplusb1.in');
120   assign(output,'aplusb.out');
121   reset(input);
122   rewrite(output);  
123   readln(TT);
124   while TT>0 do
125     begin
126       readln(s);
127       main;
128       dec(TT);
129     end;
130   close(input);
131   close(output);
132 end.

 

 

 

posted @ 2018-08-16 19:44  猪都哭了  阅读(169)  评论(0编辑  收藏  举报