编译原理第一次作业
来源:互联网 发布:天下3鬼墨女捏脸数据 编辑:程序博客网 时间:2024/05/21 17:02
Calculating the Value of π
Calculate the value of π from the infinite series (Gregory–Leibniz series):
π = 4 – 4/3 + 4/5 – 4/7 +4/9 – 4/11 + …
a) Design and code a main method to print a table that shows the value of π for the first 15 terms of this series (display the value with 12 places after the decimal point:%12.10f).
Example of Table for first 12 terms
# Terms Calculated Pi
1 4.0000000000
2 2.6666666667
… …
12 3.0584027659
b) Then design and add code statements to the main method to print a second table that shows the calculated value ofπusing this series for up to 1,000,000 terms using a table interval of 50,000.
Add a third column showing the percentage error from Math.PI: (Math.PI - pi) / Math.PI * 100.0 Example of Table_for 1 Million terms
Hint: Use an if statement and the remainder operator%to print only every 50,000 calculations.
# Terms Calculated Pi Error(%)
50000 3.1415726536 0.000637%
100000 3.1415826536 0.000318%
… … …
1000000 3.1415916536 0.000032%
c) Based on the code in a) and/or b) above, create a separate method calledcalcPithat accepts the number of terms as a parameter andreturnsthe value calculated for Pi. Select the appropriate return type forcalcPi. Thepublic static calcPimethod must not print out anything or request input from the user.
d) Print the same table as in b) above by adding code to main to call thecalcPimethod using an appropriate loop. You now should be printing 3 separate tables for parts a), b) and c).
e) Write a method public static int calcTargetPercent(double accuracyPercent)that accepts a single parameter specifying the accuracy as a percentage of Math.PI, and returns the number of terms required to reach the required accuracy. Test your method for a value of0.10%.
f) Optional: Determine using code how many terms of this series must be used before you first get value ofπaccurate to 5 decimal places i.e. 3.14159? Create a loop to stop automatically when this value is reached and then display the value and the error relative to Math.PI.
Notes
i. Name the class CalculatePi which should initially include themainmethod.
ii. Ensure that you add an appropriate file header comment, with your name/id. Pay attention to the code formatting.
iii. Copy the output tables generated and include as a comment after the file header comment, to illustrate what the program does.
第一题:
按照公式直接写,注意进制转换就好
public class CalculatePi {public static void main(String args[]) {double pi=0;for(int i=1;i<=15;i++)//计算前十五位pi{if(i%2!=0) {pi+=((double)4/(2*i-1));//double转换进制}elsepi-=((double)4/(2*i-1));System.out.printf("%d %12.10f\n",i,pi);}}}
第二题:
public class CalculatePi {public static void main(String args[]) {double pi=0,pro;for(int i=1;i<=1000000;i++)//计算前十五位pi{if(i%2!=0) {pi+=((double)4/(2*i-1));//double转换进制}elsepi-=((double)4/(2*i-1));if(i%50000==0) {//判断是否满足50000pro=(Math.PI - pi)/Math.PI*100.0;System.out.printf("%d %12.10f %f%%\n",i,pi,pro);}}}}
第三题:
import java.util.*;public class CalculatePi {public static void main(String args[]) {Scanner in=new Scanner(System.in);//输入第几位int i=in.nextInt();double sum=calcPi(i);System.out.printf("%d %12.10f\n",i,sum);}public static double calcPi(int k) {//calcPi方法返回第k个对应的pidouble pi=0;double[] ppi=new double[16];for(int i=1;i<=15;i++)//计算前十五位pi{if(i%2!=0) {pi+=((double)4/(2*i-1));//double转换进制}elsepi-=((double)4/(2*i-1));ppi[i]=pi;}return ppi[k];}}
第四题
import java.util.*;public class CalculatePi {public static void main(String args[]) {for(int i=1;i<=15;i++) {System.out.printf("%d %12.10f\n",i,calcPi(i));//第一道题}System.out.println();double error;for(int i=50000;i<=1000000;i=i+50000) {//第二道题error=(Math.PI - calcPi(i)) / Math.PI * 100.0;System.out.printf("%d %12.10f %f%%\n",i,calcPi(i),error);}System.out.println();Scanner in=new Scanner(System.in); //第三道题int i=in.nextInt();System.out.printf("%d %12.10f\n",i,calcPi(i));}public static double calcPi(int k) {//clacPi方法返回第k个对应的pi值double pi=0;for(int i=1;i<=k;i++) {if(i%2!=0)pi+=(4/(2.0*i-1));elsepi-=(4/(2.0*i-1));}return pi;}}
第五题:
import java.util.*;public class CalculatePi {public static void main(String args[]) {System.out.println(calcTargetPercent(0.1));}public static int calcTargetPercent(double accuracyPercent) {//设置精度double pi=0,error;for(int i=1;;i++) {if(i%2!=0)pi+=4/(2.0*i-1);elsepi+=4/(2.0*i-1);error=(Math.PI - pi) / Math.PI * 100.0;if(error<=accuracyPercent)return i;}}}
第六题:
import java.util.*;public class CalculatePi {public static void main(String args[]) {double pi=0,error;int num;for(int i=1;;i++) {if(i%2!=0)pi+=4/(2.0*i-1);elsepi-=4/(2.0*i-1);error=(Math.PI - pi) / Math.PI * 100.0;num=(int)(pi*100000);//使pi值存在int型num中 并放大100000倍好计算if(num==314159) {System.out.printf("%20.18f %f\n",pi,error);break;}}}}
用Java做编译原理作业,最开始建立了一个HelloWorld项目,然后今天在建立一个作业项目发现运行不了,总是运行第一个HellWorld文件,然后上网查了半天,把第一个项目删掉都不管用,最后才发现java主函数public static void main(String args[])中的static 没有写,尴了个大尬!!
- 编译原理第一次作业
- 编译原理第一次作业
- 武汉大学编译原理第一次作业
- 数据库原理第一次作业
- 计算机组成原理第一次作业
- 编译原理作业小结
- 编译原理作业有感
- RFID原理与技术课程第一次作业
- 武汉大学编译原理第二次作业
- 武汉大学编译原理第三次作业
- 编译原理作业1,2
- 编译原理作业3,4
- 西瓜的编译原理作业
- 编译原理作业1、2
- 【编译原理】第三章作业
- 【编译原理】第四章作业
- 编译原理作业3、4
- 第一次作业
- 如何判断Linux是32位还是64位
- 【opencv3】 Mat类用法总结
- 百度地图之添加覆盖物
- P2939 [USACO09FEB]改造路Revamping Trails(迪杰斯特拉+堆优化)
- activity显隐跳转及值传递
- 编译原理第一次作业
- 记录FastDFS+Nginx 安装与配置过程
- Spark源码解读--任务生成和提交过程
- 百度页面(动画+形变)
- 绘制图片步骤
- python网络爬虫学习笔记之之正则表达式
- NOIP复赛复习(十三)图论算法巩固与提高
- k-d tree算法
- 控件消息的截获