用户操作
[留言]  [发消息]  [加为好友] 
订阅我的博客
XML聚合    FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
L_H_Wilson的公告
文章分类
    Program Languages
    存档

    原创  两个求 π 的程序 收藏

     

    ---------------------------------------------

    C++ Version

    ---------------------------------------------

     

    #include <iostream>
    #include <stdlib.h>

    using namespace std;
    //double func(double&)const;   Isn't needed;
    class Pi
    {
     public:
      friend double func(Pi&)throw(double);
      Pi(int n):interval(n){};
      double loop(double)const;
     private:
      int interval;
    };
    double Pi::loop(double x)const
    {
     return 4/(1+x*x);
    }
    double func(Pi& p)throw(double)
    {
     double h=1/static_cast<double>(p.interval);
     //cout<<h<<endl;
     static double presum,sum;
     for(int i=1;i<p.interval;i++)
     {
      double xi=i*h;
      presum+=p.loop(xi);
     }
      sum=h/2*(p.loop(0.0)+2*presum+p.loop(1.0));//if "double loop(double&)const;",cannot be used like this!
      //cout<<"sum="<<sum<<"\n";;
      if(sum<=3.14)
       throw sum;
      else
       return sum;

    }

    int main(int argc, char *argv[])
    {
      Pi p(100);
      try
      {
      cout<<"pi="<<func(p)<<endl;
      }
      catch(double s)
      {
       cout<<"Error!"<<endl;
       cout<<"pi="<<s<<" is not accurate!"<<endl;
      }
     
      system("PAUSE"); 
      return 0;
    }

     

    ----------------------------------------------

    Java Version

    -----------------------------------------------

    package Exc;
    import java.util.Scanner;

    public class Pi
    {

     public static void main(String[] args)
     {
     double b = 1;//int b=1,see h;
     double a = 0;
     double sum = 0;
     double p=0;
     Scanner sc = new Scanner(System.in);
     System.out.println("Input the number n of intervals.");
     int n = sc.nextInt();
     
     double h=(b-a)/n;
     System.out.println("h=" + h);
     for(int i=1;i < n;i++)
        {
     
      double xi=a + i*h;
     
       sum=sum+2*f(xi);
      
      
      }
      try
      {
       
          p=summary(sum,h,a,b);
        
      }
      catch (ErrorException e)
      {
       System.out.println("caught: " + e);
       System.out.println("message: " + e.getMessage());
       System.out.println("stack: " + e.getStackTrace());
       return ;
      }
      finally
      {
       System.out.println("pi=" + p);
      }


     }

     static double summary(double s,double h,double a,double b) throws ErrorException
     {

          s =h / 2 * (s + f(a) + f(b));
      if (s < 3.1415926)
       throw new ErrorException("pi = " + s +" is not accurate.");
      else
       return s;
     
     }
     static class ErrorException extends Exception
     {
      ErrorException( String  what) { super(what); }
     
     }

     static double f(double x)
     {
      return 4/(1+x*x);
     
     }


    }

    发表于 @ 2008年08月05日 18:18:00 | 评论( loading... ) | 编辑| 举报| 收藏

    旧一篇:主流计算机编程语言之父---集锦

    • 发表评论
    • 评论内容:
    •  
    Copyright © L_H_Wilson
    Powered by CSDN Blog