【问题标题】:Cannot find a simple integral with Pascal找不到与 Pascal 的简单积分
【发布时间】:2014-04-10 15:33:15
【问题描述】:

我写了一个程序,用矩形法求解积分 X^2+2

program fourrr;

var    a, b : real; { borders }
       h : real; { increment argument }
       s : real; { approximate value of the integral }
       n : integer; { number of intervals }
       x : real; { argument }
       y : real; { function value at the beginning of interval }
       i : integer;
begin

    write('lower border a: '); read(a);
    write('upper border b: '); read(b);
    write('increment argument h: '); read(h);

     n := (b - a) / (h + 1);
     x := a;
     s := 0;

    i := 1;
    while i <= n do
      begin
        y := x * x + 2;   // function value at the beginning of interval
        s := s + (y * h);
        x := x + h;
      end;

   writeln('Integral value: ', s); 
end.

但是我运行它时无法解决数据类型转换的问题。

所以请帮助我,这非常重要。谢谢

【问题讨论】:

  • 对于初学者,您可能会考虑在循环内更改循环终止条件中的至少一个元素。
  • 到底是什么问题? “数据类型转换问题”是什么意思?代码在哪些方面不适合您?

标签: integration pascal type-conversion freepascal turbo-pascal


【解决方案1】:

(从一开始,对不起我的英语不好)

一开始,你需要输入输出: 程序fourrr(输入,输出);

我看到你做了 n := (b - a) / (h + 1); 那不是 / ,您应该使用“ div ”: n := (b - a) div (h + 1);

我不知道你为什么使用循环,我告诉你我不知道如何解决这个积分,但是,如果它和你一样,你就不需要它。 你可以在循环中改变什么? 使用中频。

{If i<=n THEN Begin y:=x * x + 2; // function value at the beginning of interval s := s + (y * h); x := x + h; end (*without the ; simbol*) Else writeln('ERROR'); (*The ELSE only read for him the writeln('error'), if you want to use more things , use the ELSE begin h:= ... ; alpha:=... If ...(<-for example) end;*)}

【讨论】:

  • “开头”是错误的;程序使用write 提示用户输入每个输入,然后从控制台读取响应。您不需要任何program fourrr(input, output),因为它没有将值作为命令行参数。
  • 您的建议:那不是 / ,您应该使用“ div ”:n := (b - a) div (h + 1); 是错误的(更糟糕的是) 并且会产生另一个错误,因为您不能div 与浮点数据类型一起使用。正确的是n := round((b - a) / (h + 1));
猜你喜欢
  • 1970-01-01
  • 2013-12-03
  • 2016-07-12
  • 1970-01-01
  • 1970-01-01
  • 2014-03-10
  • 2012-03-13
  • 2012-02-23
  • 1970-01-01
相关资源
最近更新 更多