JAVA课程设计-模拟科学计算器_第1页
JAVA课程设计-模拟科学计算器_第2页
JAVA课程设计-模拟科学计算器_第3页
JAVA课程设计-模拟科学计算器_第4页
JAVA课程设计-模拟科学计算器_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、课程设计报告课程:模拟科学计算器 学号: 姓名: 班级: 教师: 时间:10.12.1011.01.12 计算机科学与技术系课程设计指导书姓 名学 号班 级课程名称JAVA课程性质专业必修课设计时间2021年12 月10日2021年01月12日设计名称模拟科学计算器设计目的稳固所学理论知识,提高程序设计能力的重要实践环节。综合应用Java根底知识和根本方法。设计要求界面模拟Windows中的计算器程序。实现根本数学运算、函数等功能:加、减、乘、除、阶乘、正弦、余弦和指数运算。实现要点:添加相关组件并进行按钮事件处理。要求提交Application和Applet两个版本的程序。设计思路与设计过程

2、设计Application和Applet两个版本的程序,对Application运用继承JFrame设计,类的设计构造方法利用等。而Applet那么继承Applet,在浏览器上运行。Application过程:1. 菜单栏设计.2. 文本域,即为计算器的屏幕显示区域3. 初始化操作4. 统一设置按钮的的使用方式5. 计算器的根底操作+ - × ÷6. 运算符号的事件监听7. 去除按钮的事件监听8. 数字输入的事件监听9. 小数点的事件监听10. main方法Applet过程:1. init()方法:完成初始化操作;2. 事件统一监听;3. 科学计算;4. 建立Calucat

3、or.html;5. 浏览器运行;方案与进度12.10-12.13 复习Java程序开发的相关操作,了解“计算器相关知识;12.14-12.17 熟悉题目并理解,及找寻相关资料;12.18-12.20 根据题目要求进行需求分析设计;12.21-12.25 对Application进行总体程序设计;12.26-12.30 对Applet进行相关各功能的程序设计;12.31-01.05 对程序进行细节完善;01.06-01.10 进行调试运行并完成资料归档。任课教师意 见备 注设计名称:模拟科学计算器日期:2021年01月05日 设计内容:Application和Applet两个版本的程序,实现根

4、本数学运算、函数等功能:加、减、乘、除、阶乘、正弦、余弦和指数运算。对Application运用继承JFrame设计,类的设计构造方法利用等。而Applet那么继承Applet,在浏览器上运行。设计目的与要求:界面模拟Windows中的计算器程序。实现根本数学运算、函数等功能:加、减、乘、除、阶乘、正弦、余弦和指数运算。实现要点:添加相关组件并进行按钮事件处理。要求提交Application和Applet两个版本的程序。设计环境或器材、原理与说明:设计环境或器材: 硬件:计算机 软件:MyEclipse 集成开发环境 Java JCreator Pro;设计原理说明:运用Application

5、和Applet两个版本的程序。设计过程步骤或程序代码(可以加页):Application程序代码:import java.awt.*;import java.awt.event.*;import java.text.DecimalFormat;import javax.swing.*;public class Calucator extends JFrame private JTextField tf;private JPanel panel1, panel2, panel3, panel4;private JMenuBar myBar;private JMenu menu1, menu2,

6、menu3;private JMenuItem editItem1, editItem2, help1, help2, help3;private JRadioButtonMenuItem seeItem1, seeItem2;/单项选择框private JCheckBoxMenuItem seeItem3;/复选框private ButtonGroup bgb;private String back;private boolean IfResult = true, flag = false;private String oper = "="private double r

7、esult = 0;private Num numActionListener;private DecimalFormat df;public Calucator()super("科学计算器");/设置标题栏df = new DecimalFormat("#.#");/保存四位小数this.setLayout(new BorderLayout(10, 5);panel1 = new JPanel(new GridLayout(1, 3, 10, 10);panel2 = new JPanel(new GridLayout(5, 6, 5, 5);/5行6

8、列panel3 = new JPanel(new GridLayout(5, 1, 5, 5);panel4 = new JPanel(new BorderLayout(5, 5);/* * 菜单栏 */myBar = new JMenuBar();menu1 = new JMenu("编辑(E)");menu2 = new JMenu("查看(V)");menu3 = new JMenu("帮助(H)");, 12);menu2.setFont(new Font("宋体", Font.PLAIN, 12);men

9、u3.setFont(new Font("宋体", Font.PLAIN, 12);/* * 编辑栏 */editItem1 = new JMenuItem("复制(C) Ctrl+C");editItem2 = new JMenuItem("粘贴(P) Ctrl+V");editItem1.setFont(new Font("宋体",Font.PLAIN,12);editItem2.setFont(new Font("宋体",Font.PLAIN,12);/* * 查看栏 */seeItem1

10、 = new JRadioButtonMenuItem("科学型(T)");seeItem2 = new JRadioButtonMenuItem("标准型(S)");seeItem3 = new JCheckBoxMenuItem("数字分组(I)");seeItem1.setFont(new Font("宋体",Font.PLAIN,12);seeItem2.setFont(new Font("宋体",Font.PLAIN,12);seeItem3.setFont(new Font(&quo

11、t;宋体",Font.PLAIN,12);/* * 帮助栏 */help1 = new JMenuItem("帮助主题(H)");help2 = new JMenuItem("关于计算器(A)");help1.setFont(new Font("宋体",Font.PLAIN,12);help2.setFont(new Font("宋体",Font.PLAIN,12);bgb = new ButtonGroup();/选项组menu1.add(editItem1);menu1.add(editItem2);

12、menu2.add(seeItem1);menu2.add(seeItem2);menu2.addSeparator();/添加一条分割线menu2.add(seeItem3);dd(help1);menu3.addSeparator();/添加一条分割线menu3.add(help2);myBar.add(menu1);myBar.add(menu2);myBar.add(menu3);this.setJMenuBar(myBar);numActionListener = new Num();/实现数字监听/* * 文本域,即为计算器的屏幕显示区域 */tf = new JTextField

13、();tf.setEditable(false);/文本区域不可编辑tf.setBackground(Color.white);/文本区域的背景色tf.setHorizontalAlignment(JTextField.RIGHT);/文字右对齐tf.setText("0");tf.setBorder(BorderFactory.createLoweredBevelBorder();init();/对计算器进行初始化/* * 初始化操作 * 添加按钮 */private void init()addButton(panel1, "Backspace",

14、new Clear(), Color.red);addButton(panel1, "CE", new Clear(), Color.red);addButton(panel1, "C", new Clear(), Color.red);addButton(panel2, "1/x", new Signs(), Color.magenta);addButton(panel2, "log", new Signs(), Color.magenta);addButton(panel2, "7", nu

15、mActionListener, Color.blue);addButton(panel2, "8", numActionListener, Color.blue);addButton(panel2, "9", numActionListener, Color.blue);addButton(panel2, "÷", new Signs(), Color.red);addButton(panel2, "n!", new Signs(), Color.magenta);addButton(panel2, &

16、quot;sqrt", new Signs(), Color.magenta);addButton(panel2, "4", numActionListener, Color.blue);addButton(panel2, "5", numActionListener, Color.blue);addButton(panel2, "6", numActionListener, Color.blue);addButton(panel2, "×", new Signs(), Color.red);a

17、ddButton(panel2, "sin", new Signs(), Color.magenta);addButton(panel2, "x2", new Signs(), Color.magenta);addButton(panel2, "1", numActionListener, Color.blue);addButton(panel2, "2", numActionListener, Color.blue);addButton(panel2, "3", numActionListen

18、er, Color.blue);addButton(panel2, "-", new Signs(), Color.red);addButton(panel2, "cos", new Signs(), Color.magenta);addButton(panel2, "x3", new Signs(), Color.magenta);addButton(panel2, "0", numActionListener, Color.blue);addButton(panel2, "-/+", new

19、 Clear(), Color.blue);addButton(panel2, ".", new Dot(), Color.blue);addButton(panel2, "+", new Signs(), Color.red);addButton(panel2, "tan", new Signs(), Color.magenta);addButton(panel2, "%", new Signs(), Color.magenta);addButton(panel2, "", numAction

20、Listener, Color.orange);addButton(panel2, "e", numActionListener, Color.orange);addButton(panel2, "", new Signs(), Color.orange);addButton(panel2, "=", new Signs(), Color.red);JButton btns = new JButton("计算器");btns.setBorder(BorderFactory.createLoweredBevelBor

21、der();btns.setEnabled(false);/按钮不可操作btns.setPreferredSize(new Dimension(20, 20);panel3.add(btns);/参加按钮addButton(panel3, "MC", null, Color.red);addButton(panel3, "MR", null, Color.red);addButton(panel3, "MS", null, Color.red);addButton(panel3, "M+", null, Color

22、.red);panel4.add(panel1, BorderLayout.NORTH);panel4.add(panel2, BorderLayout.CENTER);this.add(tf, BorderLayout.NORTH);this.add(panel3, BorderLayout.WEST);this.add(panel4);pack();this.setResizable(false);/窗口不可改变大小this.setLocation(300, 200);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/* * 统一设置

23、按钮的的使用方式 * param panel * param name * param action * param color */private void addButton(JPanel panel, String name, ActionListener action, Color color)JButton bt = new JButton(name);panel.add(bt);/在面板上增加按钮bt.setForeground(color);/设置前景字体颜色bt.addActionListener(action);/增加监听事件/* * 计算器的根底操作+ - × &

24、#247; * param x */private void getResult (double x)if(oper = "+")result += x;else if(oper = "-")result -= x;else if(oper = "×")result *= x;else if(oper = "÷")result /= x;else if(oper = "=")result = x;tf.setText(df.format(result);/* * 运算符号的事

25、件监听 */class Signs implements ActionListenerpublic void actionPerformed(ActionEvent e) /* * 用ActionEvent对象的getActionCommand()方法 * 取得与引发事件对象相关的字符串 */String str = e.getActionCommand();/* sqrt求平方根 */if(str.equals("sqrt")double i = Double.parseDouble(tf.getText();if(i>=0)/* * String.valueOf(

26、) 转换为字符串 * df.format() 按要求保存四位小数 * Math.sqrt() 求算数平方根 */tf.setText(String.valueOf(df.format(Math.sqrt(i);elsetf.setText("负数不能开平方根");/* log求常用对数 */else if(str.equals("log")double i = Double.parseDouble(tf.getText();if(i>0)tf.setText(String.valueOf(df.format(Math.log(i);elsetf.s

27、etText("负数不能求对数");/* %求百分比 */else if(str.equals("%")(df.format(Double.parseDouble(tf.getText() / 100);/* 1/x求倒数 */else if(str.equals("1/x")if(Double.parseDouble(tf.getText() = 0)tf.setText("除数不能为零");elsetf.setText(df.format(1 / Double.parseDouble(tf.getText();

28、/* sin求正弦函数 */else if(str.equals("sin")ouble(tf.getText();tf.setText(String.valueOf(df.format(Math.sin(i);/* cos求余弦函数 */else if(str.equals("cos")double i = Double.parseDouble(tf.getText();tf.setText(String.valueOf(df.format(Math.cos(i);/* tan求正切函数 */else if(str.equals("tan&q

29、uot;)double i = Double.parseDouble(tf.getText();tf.setText(String.valueOf(df.format(Math.tan(i);/* n!求阶乘 */else if(str.equals("n!")double i = Double.parseDouble(tf.getText();if(i%2=0)|(i%2=1)/判断为整数放进行阶乘操作int j = (int)i;/强制类型转换int result=1;for(int k=1;k<=j;k+)result *= k;tf.setText(Strin

30、g.valueOf(result);elsetf.setText("无法进行阶乘");/* x2求平方 */else if(str.equals("x2")double i = Double.parseDouble(tf.getText();tf.setText(String.valueOf(df.format(i*i);/* x3求立方 */else if(str.equals("x3")double i = Double.parseDouble(tf.getText();(i*i*i);/* 角度转换 */* * 将角度值转换成弧

31、度值,方便三角函数的计算 */else if(str.equals("")double i = Double.parseDouble(tf.getText();tf.setText(String.valueOf(i/180*Math.PI);elseif(flag)IfResult = false;if(IfResult)oper = str;else getResult(Double.parseDouble(tf.getText();oper = str;IfResult = true;/* * 去除按钮的事件监听 */class Clear implements Act

32、ionListenerpublic void actionPerformed(ActionEvent e) /* * 用ActionEvent对象的getActionCommand()方法 * 取得与引发事件对象相关的字符串 */String str = e.getActionCommand();if(str = "C")tf.setText("0");IfResult = true;result = 0;else if(str = "-/+")double i = 0 - Double.parseDouble(tf.getText(

33、).trim();tf.setText(df.format(i);else if(str = "Backspace")if(Double.parseDouble(tf.getText() > 0)if(tf.getText().length() > 1)tf.setText(tf.getText().substring(0, tf.getText().length() - 1);/使用退格删除最后一位字符elsetf.setText("0");IfResult = true;elseif(tf.getText().length() >

34、2)tf.setText(tf.getText().substring(0, tf.getText().length() - 1);elsetf.setText("0");IfResult = true;else if(str = "CE")tf.setText("0");IfResult = true;/* * 数字输入的事件监听 */class Num implements ActionListenerpublic void actionPerformed(ActionEvent e) String str = e.getActi

35、onCommand();if(IfResult)tf.setText("");IfResult = false;if(str="")tf.setText(String.valueOf(Math.PI);else if(str="e")tf.setText(String.valueOf(Math.E);elsetf.setText(tf.getText().trim() + str);if(tf.getText().equals("0")tf.setText("0");IfResult = tru

36、e;flag = true;/* * 小数点的事件监听 */class Dot implements ActionListenerpublic void actionPerformed(ActionEvent e) IfResult = false;if(tf.getText().trim().indexOf(".") = -1)tf.setText(tf.getText() + ".");/* * main方法 */public static void main(String args) new Calucator().setVisible(true)

37、;Applet程序代码:import java.util.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.applet.*;public class Calucator extends JApplet implements ActionListenerboolean i=true;private JButton num0=new JButton("0");private JButton num1=new JButton("1");private JB

38、utton num2=new JButton("2");private JButton num3=new JButton("3");private JButton num4=new JButton("4");private JButton num5=new JButton("5");private JButton num6=new JButton("6");private JButton num7=new JButton("7");private JButton num8=n

39、ew JButton("8");private JButton num9=new JButton("9");private JButton zuok=new JButton("(");private JButton youk=new JButton(")");private JButton dian=new JButton(".");private JButton NULL=new JButton("N");private JButton plu=new JButton(&q

40、uot;+");private JButton min=new JButton("-");private JButton mul=new JButton("x");private JButton div=new JButton("/");private JButton equ=new JButton("=");private JButton cle=new JButton("C");/去除private JTextField space=new JTextField(30);publi

41、c void init()JPanel text=new JPanel();text.setLayout(new FlowLayout();text.add(space);JPanel buttons=new JPanel();buttons.setLayout(new GridLayout(5,4);buttons.add(num9);buttons.add(num8);buttons.add(num7);buttons.add(plu);buttons.add(num6);buttons.add(num5);buttons.add(num4);buttons.add(min);button

42、s.add(num3);buttons.add(num2);buttons.add(num1);buttons.add(mul);buttons.add(num0);buttons.add(cle);buttons.add(equ);buttons.add(div);buttons.add(zuok);buttons.add(youk);buttons.add(dian);buttons.add(NULL);(num9).addActionListener(this);(num8).addActionListener(this);(num7).addActionListener(this);(

43、num6).addActionListener(this);(num5).addActionListener(this);(num4).addActionListener(this);(num3).addActionListener(this);(num2).addActionListener(this);(num1).addActionListener(this);(num0).addActionListener(this);(plu).addActionListener(this);(min).addActionListener(this);(mul).addActionListener(

44、this);(div).addActionListener(this);(equ).addActionListener(this);(cle).addActionListener(this);(zuok).addActionListener(this);(youk).addActionListener(this);(dian).addActionListener(this);setLayout(new BorderLayout();add("North",text);add("South",buttons);space.setText("0&q

45、uot;);public void actionPerformed(ActionEvent e)if(e.getSource()=num9)if(i=true)space.setText("9");i=false;else space.setText(space.getText()+'9');if(e.getSource()=num8)if(i=true)space.setText("8");i=false;else space.setText(space.getText()+'8');if(e.getSource()=n

46、um7)if(i=true)space.setText("7");i=false;else space.setText(space.getText()+'7');if(e.getSource()=num6)if(i=true)space.setText("6");i=false;else space.setText(space.getText()+'6');if(e.getSource()=num5)if(i=true)space.setText("5");i=false;else space.setT

47、ext(space.getText()+'5');if(e.getSource()=num4)if(i=true)space.setText("4");i=false;else space.setText(space.getText()+'4');if(e.getSource()=num3)if(i=true)space.setText("3");i=false;else space.setText(space.getText()+'3');if(e.getSource()=num2)if(i=true)s

48、pace.setText("2");i=false;else space.setText(space.getText()+'2');if(e.getSource()=num1)if(i=true)space.setText("1");i=false;else space.setText(space.getText()+'1');if(e.getSource()=num0)if(i=true)space.setText("0");i=false;else space.setText(space.getTe

49、xt()+'0');if(e.getSource()=zuok)if(i=true)space.setText("(");i=false;else space.setText(space.getText()+'('); if(e.getSource()=youk)if(i=false)space.setText(space.getText()+')');if(e.getSource()=dian)if(i=false)space.setText(space.getText()+'.');if(e.getSour

50、ce()=plu)space.setText(space.getText()+'+');i=false;if(e.getSource()=min)space.setText(space.getText()+'-');i=false;if(e.getSource()=mul)space.setText(space.getText()+'*');i=false;if(e.getSource()=div)space.setText(space.getText()+'/');i=false;if(e.getSource()=equ)spa

51、ce.setText(String.valueOf(Calculator(space.getText();i=true;if(e.getSource()=cle)space.setText("0");i=true;public double Calculator(String f)/科学计算int i=0,j=0,k;char c;StringBuffer s=new StringBuffer();s.append(f);s.append('=');String formula=s.toString();char anArray;anArray=new ch

52、ar50;Stack<Character> mystack=new Stack<Character>();while(formula.charAt(i)!='=')c=formula.charAt(i);switch(c)case '(': mystack.push(new Character(c);i+;break;case ')':while(mystack.peek().charValue()!='(')anArrayj+=mystack.pop().charValue();mystack.pop()

53、;i+;break;case '+':case '-':while(!mystack.empty()&&mystack.peek().charValue()!='(')anArrayj+=mystack.pop().charValue();mystack.push(new Character(c);i+;break;case '*':case '/':while(!mystack.empty()&&(mystack.peek().charValue()='*'|mys

54、tack.peek().charValue()='/')anArrayj+=mystack.pop().charValue();mystack.push(new Character(c);i+;break;case' ': i+;break;default: while(c>='0'&&c<='9')|c='.')anArrayj+=c;i+;c=formula.charAt(i);anArrayj+='#'break;while(!(mystack.empty()anA

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论