• Помогите дописать программу в Паскаль. Задача ниже. Не знаю как сделать ее со скобками.

    Напишите программу, которая вычисляет выражение, состоящее из трех чисел и двух знаков (допускаются знаки «+», «–», «*» и «/») и круглых скобок. Выражение вводится как символьная строка, все числа целые. Операция «/» выполняется как целочисленное деление (div). Пример: Введите выражение: 2*(3+45)+4 Ответ: 100

    Вот что у меня есть: ( я сделала, чтобы вычисляло выражение их 3 чисел и двух знаков, но не получается сделать так, чтобы учитывались круглые скобки )
    var s, s1, s2, s3, s4: string;
    i, a, b, c, d, e, f: integer;
    begin
    Writeln('Введите строку: ');
    Readln(s);
    for i := 1 to length(s) do
    begin
    if (s[i] = '+') or (s[i] = '-') or (s[i] = '*') or (s[i] = '/') then
    begin
    s1 := copy(s, 1, i-1);
    s2 := copy(s, i+1, length(s));
    c := i;
    end;
    end;
    for i := 1 to length(s1) do
    begin
    if (s1[i] = '+') or (s1[i] = '-') or (s1[i] = '*') or (s1[i] = '/') then
    begin
    s3 := copy(s1, 1, i-1);
    s4 := copy(s1, i+1, length(s1));
    e := i;
    end;
    end;
    Val(s3, a, d);
    Val(s4, b, d);
    Val(s2, f, d);
    if (s[e] = '/') and (s[c] = '/') then Writeln((a div b) div f);
    if (s[e] = '/') and (s[c] = '*') then Writeln((a div b) * f);
    if (s[e] = '+') and (s[c] = '*') then Writeln(a + (b * f));
    if (s[e] = '*') and (s[c] = '*') then Writeln(a * b * f);
    if (s[e] = '+') and (s[c] = '/') then Writeln(a + (b div f));
    if (s[e] = '*') and (s[c] = '+') then Writeln((a * b) + f);
    if (s[e] = '/') and (s[c] = '+') then Writeln((a div b) + f);
    if (s[e] = '*') and (s[c] = '/') then Writeln((a * b) div f);
    if (s[e] = '+') and (s[c] = '+') then Writeln(a + b + f);
    if (s[e] = '-') and (s[c] = '-') then Writeln(a - b - f);
    if (s[e] = '+') and (s[c] = '-') then Writeln(a + b - f);
    if (s[e] = '-') and (s[c] = '+') then Writeln(a - b + f);
    if (s[e] = '*') and (s[c] = '-') then Writeln((a * b) - f);
    if (s[e] = '/') and (s[c] = '-') then Writeln((a div b) - f);
    if (s[e] = '-') and (s[c] = '*') then Writeln(a- (b * f));
    if (s[e] = '-') and (s[c] = '/') then Writeln(a - (b div f));
    end.

Ответы 1

  • var

     s, s1, s2, s3, s4, tmp1, tmp2: string;

     i, a, b, c, d, e, f: integer;

    begin

     Writeln('Введите строку: ');

     Readln(s);

     //---------------------------

     for var k := 1 to length(s) do  

     begin

       if (s[k] = '(') then

       begin

         var j := k + 1;

         while (s[j].IsDigit) do

         begin

           tmp1 := tmp1 + s[j];

           j := j + 1;

         end;

         var ch := s[j];

         j := j + 1;

         while (s[j].IsDigit) do

         begin

           tmp2 := tmp2 + s[j];

           j := j + 1;

         end;

         case ch of

           '+': begin s := s.Remove(k - 1, j - k + 1); s := s.Insert(k - 1, inttostr(tmp1.ToInteger + tmp2.ToInteger)); end;

           '-': begin s := s.Remove(k - 1, j - k + 1); s := s.Insert(k - 1, inttostr(tmp1.ToInteger - tmp2.ToInteger)); end;

           '*': begin s := s.Remove(k - 1, j - k + 1); s := s.Insert(k - 1, inttostr(tmp1.ToInteger * tmp2.ToInteger)); end;

           '/': begin s := s.Remove(k - 1, j - k + 1); s := s.Insert(k - 1, inttostr(tmp1.ToInteger div tmp2.ToInteger)); end;

         end;

         break;

       end;

     end;

     //---------------------------

     for i := 1 to length(s) do

     begin

       if (s[i] = '+') or (s[i] = '-') or (s[i] = '*') or (s[i] = '/') then

       begin

         s1 := copy(s, 1, i - 1);

         s2 := copy(s, i + 1, length(s));

         c := i;

       end;

     end;

     for i := 1 to length(s1) do

     begin

       if (s1[i] = '+') or (s1[i] = '-') or (s1[i] = '*') or (s1[i] = '/') then

       begin

         s3 := copy(s1, 1, i - 1);

         s4 := copy(s1, i + 1, length(s1));

         e := i;

       end;

     end;

     Val(s3, a, d);

     Val(s4, b, d);

     Val(s2, f, d);

     if (s[e] = '/') and (s[c] = '/') then Writeln((a div b) div f);

     if (s[e] = '/') and (s[c] = '*') then Writeln((a div b) * f);

     if (s[e] = '+') and (s[c] = '*') then Writeln(a + (b * f));

     if (s[e] = '*') and (s[c] = '*') then Writeln(a * b * f);

     if (s[e] = '+') and (s[c] = '/') then Writeln(a + (b div f));

     if (s[e] = '*') and (s[c] = '+') then Writeln((a * b) + f);

     if (s[e] = '/') and (s[c] = '+') then Writeln((a div b) + f);

     if (s[e] = '*') and (s[c] = '/') then Writeln((a * b) div f);

     if (s[e] = '+') and (s[c] = '+') then Writeln(a + b + f);

     if (s[e] = '-') and (s[c] = '-') then Writeln(a - b - f);

     if (s[e] = '+') and (s[c] = '-') then Writeln(a + b - f);

     if (s[e] = '-') and (s[c] = '+') then Writeln(a - b + f);

     if (s[e] = '*') and (s[c] = '-') then Writeln((a * b) - f);

     if (s[e] = '/') and (s[c] = '-') then Writeln((a div b) - f);

     if (s[e] = '-') and (s[c] = '*') then Writeln(a - (b * f));

     if (s[e] = '-') and (s[c] = '/') then Writeln(a - (b div f));

    end.

  • Добавить свой ответ

Войти через Google

или

Забыли пароль?

У меня нет аккаунта, я хочу Зарегистрироваться

How much to ban the user?
1 hour 1 day 100 years