2016年6月2日 星期四

C#-examination-2

題目要求:
  • button1: 做積分
  • steps數目10000
  • show出結果與執行時間(耗時)
##ReadMore##

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace examination_2
{
    public partial class exam2 : Form
    {

        Stopwatch stopWatch = new Stopwatch();

        public exam2()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Start Watch
            stopWatch.Reset();
            stopWatch.Start();

            // Integration
            int steps = 10000;
            double ans = 0;
            double x, fx;
            double en = 4 * Math.PI;
            double bn = 0;
            double parti = (en - bn) * 1 / steps;

            for (int i = 0; i < steps; i++)
            {
                x = bn + parti * i;
                fx = x * x * x * Math.Sin(2 * x) * Math.Cos(x);
                ans = ans + fx * parti;
            }

            // Stop Watch
            stopWatch.Stop();

            // Show timesapn and Answer
            TimeSpan ts = stopWatch.Elapsed;
            textBox1.Text = ts.ToString();
            textBox2.Text = ans.ToString();
            
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

沒有留言:

張貼留言