Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 8th, 2012  |  syntax: Delphi  |  size: 1.37 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. {$o-,r+,q+}
  2.  
  3. program Project1;
  4.  
  5. {$APPTYPE CONSOLE}
  6.  
  7. uses
  8.   SysUtils;
  9.  
  10. var
  11.   kol, i, z, pos: integer;
  12.   s: string;
  13.   find: boolean;
  14.  
  15. procedure calc(s: string; cnt, last: integer);
  16. var
  17.   o1, o2: string;
  18.   k, x, y, i, err: integer;
  19. begin
  20.   if (cnt = kol + 1) then
  21.     exit;
  22.   k := -1;
  23.   err := -1;
  24.   if (cnt <> 1) then begin
  25.     i := last + 1;
  26.     while (s[i] <> '+')and (s[i] <> '-') and (i < length(s)) do
  27.       i := i + 1;
  28.     if (i = length(s)) then
  29.       pos := i
  30.     else
  31.       pos := i - 1;
  32.   end;
  33.   if (cnt = 1) then begin
  34.     for i := last to pos do
  35.       if (s[i] = '+') or (s[i] = '-') then
  36.         k := i;
  37.   end else
  38.     k := last;
  39.   if (cnt = 1) then begin
  40.     o1 := copy(s, 1, k - 1);
  41.     val(o1, x, err);
  42.   end else
  43.     x := z;
  44.   o2 := copy(s, k + 1, pos - k);
  45.   val(o2, y, err);
  46.   case s[k] of
  47.     '+': z := x + y;
  48.     '-': z := x - y;
  49.   end;
  50.   calc(s, cnt + 1, pos + 1);
  51. end;
  52.  
  53. begin
  54.   assign(input, 'inp.in');
  55.   assign(output, 'outp.out');
  56.   reset(input);
  57.   rewrite(output);
  58.  
  59.   readln(s);
  60.   kol := 0;
  61.   z := 0;
  62.   pos := length(s);
  63.   find := false;
  64.   for i := 1 to length(s) do begin
  65.     if (s[i] = '+') or (s[i] = '-') then
  66.       kol := kol + 1;
  67.     if (kol = 2) and (not find) then begin
  68.       pos := i - 1;
  69.       find := true;
  70.     end;
  71.   end;
  72.  
  73.   calc(s, 1, 1);
  74.  
  75.   write(z);
  76.  
  77.   close(input);
  78.   close(output);
  79. end.