檔案 → 新增專案 → 主控台應用程式 → 撰寫 Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test0929
{
class Program
{
static void Main(string[] args)
{
int steps = 1000; // 積分精準度(越高越精準)
float x, fx, bn, en;
float ans = 0;
Console.WriteLine("Integral(X^2) →");
Console.Write("Float bn = ");
bn = float.Parse(Console.ReadLine());
Console.Write("Float en = ");
en = float.Parse(Console.ReadLine());
float parti = (en - bn) * 1/steps;
for (int i = 0; i < steps; i++)
{
x = bn + parti * i;
fx = x * x;
ans = ans + fx * parti;
}
Console.WriteLine("計算 x^2 之積分,積分範圍 {0} 到 {1}:", en, bn);
Console.WriteLine("ans = {0}", ans);
Console.Read();
}
}
}
沒有留言:
張貼留言