一个简单的计算器

这是一个简单的计算器程序,使用Java Swing实现。具备基本的数学运算功能,并提供复制、粘贴等辅助功能。

java最后一次实验内容。。。


import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.io.IOException;
public class computer implements ActionListener {
 private JFrame frame=new JFrame("简单计算器");
 private Panel p=new Panel();
 private Panel p1=new Panel();
 private Panel p2=new Panel();
 private Panel p3=new Panel();
 private MenuBar mb=new MenuBar();
 private Button button[]=new Button[21];
 private Button button1[]=new Button[4];
 private Button button2[]=new Button[5];
 private JTextField tf=new JTextField();
 private TextField tf1=new TextField();
 //定义操作数和操作符
 private String str1,str2,opr;
 private boolean flag=true;
 //定义中间缓存变量
 private double result, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
 private Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
 
public void com( )
{

 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.getContentPane().setLayout(new BorderLayout(5,5));
 frame.setBackground(Color.LIGHT_GRAY);
 frame.setLocation(300,200);
 //设置又对齐
 tf.setHorizontalAlignment(SwingConstants.RIGHT);
 
 p.setLayout(new BorderLayout(3,3));
 p.setBackground(Color.lightGray);
 p1.setLayout(new GridLayout(4,5));
 p2.setLayout(new FlowLayout(FlowLayout.LEFT,5,0));
 p3.setLayout(new GridLayout(4,1));
 frame.setMenuBar(mb);
 Menu m1 = new Menu("编辑(E)");
 Menu m2 = new Menu("查看(V)");
 Menu m3 = new Menu("帮助(H)");
    mb.add(m1);
    mb.add(m2);
    mb.add(m3);
    MenuItem mi1 = new MenuItem("复制(C)",new MenuShortcut(KeyEvent.VK_C));
 MenuItem mi2 = new MenuItem("粘贴(V)",new MenuShortcut(KeyEvent.VK_V));
 MenuItem mi3 = new MenuItem("退出");
 MenuItem mi4 = new MenuItem("科学型(S)");
 MenuItem mi5 = new MenuItem("数字分组(T)");
 MenuItem mi6 = new MenuItem("关于计算器");
 MenuItem mi7 = new MenuItem("帮助主题"); 
    mi1.addActionListener(this);
    mi2.addActionListener(this);
    mi3.addActionListener(this);
    mi4.addActionListener(this);
    mi5.addActionListener(this);
    mi6.addActionListener(this);
    mi7.addActionListener(this);
   
    //添加
    m1.add(mi1);
    m1.add(mi2);
    m1.addSeparator();
    m1.add(mi3);
   
    m2.add(mi4);
    m2.add(mi5);
   
    m3.add(mi6);
    m3.add(mi7);
   
    //定义Backspace,CE,C,""
    for(int i=1;i<4;i++)
    {
     button1[i]=new Button();
     button1[i].setFont(new Font("仿宋",0,16));
     button1[i].setBackground(Color.lightGray);
     button1[i].setForeground(Color.red);
    }
    button1[1].setLabel("Backspace");
    button1[2].setLabel("CE");
    button1[3].setLabel("  C  ");
    tf1.setEnabled(false);
    p2.add(tf1);
    for(int i=1;i<4;i++)
    {
     p2.add(button1[i]);
     button1[i].addActionListener(this);
    }
    for(int i=1;i<21;i++)
    {
     button[i]=new Button();
     button[i].setFont(new Font("仿宋",0,16));
     button[i].setBackground(Color.LIGHT_GRAY);
     button[i].setForeground(Color.BLUE);
    }
    button[1].setLabel("7");
    button[2].setLabel("8");
    button[3].setLabel("9");
    button[4].setLabel("/");
    button[5].setLabel("sqrt");
    button[6].setLabel("4");
    button[7].setLabel("5");
    button[8].setLabel("6");
    button[9].setLabel("*");
    button[10].setLabel("%");
    button[11].setLabel("1");
    button[12].setLabel("2");
    button[13].setLabel("3");
    button[14].setLabel("-");
    button[15].setLabel("1/x");
    button[16].setLabel("0");
    button[17].setLabel("+/-");
    button[18].setLabel(".");
    button[19].setLabel("+");
    button[20].setLabel("=");
    for(int i=1;i<21;i++)
    {
     p1.add(button[i]);
     button[i].addActionListener(this);
     button[i].setBackground(Color.LIGHT_GRAY);
     button[i].setForeground(Color.BLUE);
    }
    for(int i=4;i<21;i=i+5)
    {
     button[i].setForeground(Color.RED);
    }
    button[20].setForeground(Color.RED);
   
    for(int i=1;i<5;i++)
    {
     button2[i]=new Button();
     button2[i].setFont(new Font("仿宋",0,16));
     button2[i].setBackground(Color.lightGray);
     button2[i].setForeground(Color.red);
    }
   
    button2[1].setLabel("MC");
    button2[2].setLabel("MR");
    button2[3].setLabel("MS");
    button2[4].setLabel("M+");
   
    for(int i=1;i<5;i++)
    {
     p3.add(button2[i]);
     button2[i].addActionListener(this);
    }
    frame.getContentPane().add(tf,"North");
    p.add(p1,"Center");
    p.add(p2,"North");
    p.add(p3,"West");
    frame.getContentPane().add(p,"Center");
   
 frame.pack();
 frame.setResizable(false);
 frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
 str1=e.getActionCommand();
 String str3=tf.getText();
 if(str1=="1"|str1=="2"|str1=="3"|str1=="4"|str1=="5"|str1=="6"|str1=="7"|str1=="8"|str1=="9"|str1=="0")
 {
  if(flag)
  {
   tf.setText(str3+str1);
  }
  else
  {
   tf.setText(str1);
   flag=true;
  }
 }
 else if(str1=="+"|str1=="-"|str1=="*"|str1=="/")
 {
  result=Double.parseDouble(tf.getText());
  flag=false;
  opr=str1;
 }
 else if(str1=="=")
 {
  String strtmp;
  String tempstr="";
  String str0;
  int loc;
  boolean ok;
  tmp1=Double.parseDouble(tf.getText());
  if(opr=="+")result+=tmp1;
  if(opr=="-")result-=tmp1;
  if(opr=="*")result*=tmp1;
  if(opr=="/")result/=tmp1;
  strtmp=Double.toString(result);
  loc=strtmp.indexOf(".");
  str0=strtmp.substring(loc+1);
  if(Double.parseDouble(str0)==0.0)
  {
   tf.setText(strtmp.substring(0,loc));
  }
  else tf.setText(Double.toString(result));
  opr=" ";
  flag=false;
 }
 else if(str1=="sqrt")
 {
  tmp2=Double.parseDouble(tf.getText());
  if(tmp2>0)
  {
   tmp2=Math.sqrt(Double.parseDouble(tf.getText()));
   tf.setText(Double.toString(tmp2));
   flag=false;
  }
 }
 else if(str1=="%")
 {
  tmp3=Double.parseDouble(tf.getText())/100;
  if(opr == "*")
  {
  result = result*(result*tmp3);
  tf.setText(Double.toString(result));
  }
  else if(opr == "+")
  {
  result = result+result*tmp3;
  tf.setText(Double.toString(result));
  }
  else if(opr == "-")
  {
  result = result-result*tmp3;
  tf.setText(Double.toString(result));
  }
  else if(opr == "/")
  {
  result = result/(result*tmp3);
  tf.setText(Double.toString(result));
  }
  else
  {
  tf.setText("0.0");
  }
  flag = false;
  opr = "";
 }
 else if(str1=="1/x")
 {
  tmp4=Double.parseDouble(tf.getText());
  result=1.0/tmp4;
  tf.setText(Double.toString(result));
  flag=false;
 }
 else if(str1=="+/-")
 {
  tmp5=-1*Double.parseDouble(tf.getText());
  tf.setText(Double.toString(tmp5));
 }
 else if(str1==".")
 {
  if(str3.indexOf('.')==-1)
  {
       tf.setText(str3+str1);
  }
 }
 else if(str1=="  C  ")
 {
  tf.setText("0.0");
  flag=false;
 }
 else if(str1=="CE")
 {
  tf.setText("0");
  flag=false;
 }
 else if(str1=="Backspace")
 {
  tf.setText(str3.substring(0,(str3.length()-1)));
 }
 else if(str1=="MC")
 {
  str2 ="";
  tf1.setText("");
 }
 else if(str1=="MS")
 {
  String before = tf.getText();
  tmp7 = Double.parseDouble(before);
  str2 = Double.toString(tmp7);
  tf1.setText(" M");
  flag = false;
 }
 else if(str1=="M+")
 {
  tmp7 = tmp7 + Double.parseDouble(tf.getText());
  str2 = Double.toString(tmp7);
  flag = false;
 }
 else if(str1=="复制(C)")
 {
  String text = tf.getText();
  StringSelection selection = new StringSelection(text);
  cb.setContents(selection, null);
 }
 else if(str1=="粘贴(V)")
 {
  Transferable contents = cb.getContents(this);
  DataFlavor flavor = DataFlavor.stringFlavor;
  try{
  String text = (String)contents.getTransferData(flavor);
  if (!text.equals(null))
  {
  tf.setText(text);
  }
  else
  {
    tf.setText("0.0");
  }
   }
  catch(UnsupportedFlavorException ef)
  {
  }
  catch(IOException ex)
  {
  }
 }
 else if(str1=="数字分组(T)")
 {
  String str4=tf.getText();
  for(int i=1;(3*i)<str4.length();i++)
  {
   
  }
 }
 else if(str1=="关于计算器")
 {
  aboutcomputer d;
  d=new aboutcomputer(frame);
    d.display();
 }
 else if(str1=="帮助主题")
 {
  helpcomputer d;
  d=new helpcomputer(frame);
  d.display();
 }
 else if(str1=="退出")
 {
  System.exit(1);
 }
}
class pressKeyboard extends KeyAdapter
{
 private JTextField tf;
 public pressKeyboard(JTextField tf)
 {
  this.tf=tf;
 }
 public void KeyTyped(KeyEvent e)
 {
  String s1=tf.getText();
  char ch=e.getKeyChar();
  if(ch=='1'|ch=='2'|ch=='3'|ch=='4'|ch=='5'|ch=='6'|ch=='7'|ch=='8'|ch=='9'|ch=='0')
  {
   if(flag)
   {
    tf.setText(s1+ch);
   }
   else
   {
    tf.setText(ch+"");
    flag=true;
   }
  }
  else if(ch=='+'|ch=='-'|ch=='*'|ch=='/')
  {
   result=Double.parseDouble(tf.getText());
   flag=false;
   opr=ch+"";
  }
 }
}
public static void main(String[]args)
{
 computer computer1=new computer();
 computer1.com();
}
}


关于

aboutcomputer.java

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class aboutcomputer extends Dialog implements ActionListener {
 private Label l;
 private Label ll;
 private Button b;
 public TextArea ta;
 private Panel p1;
 private Panel p2;
 private Panel p3;
 private String title = "关于计算器";
 
 public aboutcomputer(JFrame f){
  super(f,true);
  this.setTitle(title);
  l = new Label("简单计算器");
  l.setForeground(Color.blue);
  l.setFont(new Font("Arial",Font.PLAIN,24));
  ll = new Label("java最后一次实验!");
  ll.setForeground(Color.blue);
  ll.setFont(new Font("Arial",Font.PLAIN,24));
  ll.setBackground(Color.BLUE);
  b = new Button(" 确定 ");
  b.addActionListener(this); //(new ButtonHandler());
  ta = new TextArea("",30,50,TextArea.SCROLLBARS_VERTICAL_ONLY);
  ta.setText(ll.getText());
  ta.setBackground(Color.lightGray);
  ta.setEditable(false);
  ta.enable(false);
  p1 = new Panel();
  p2 = new Panel();
  p3 = new Panel();
  p2.setLayout(new BorderLayout(5,5));
  p1.setBackground(Color.lightGray);
  p2.setBackground(new Color(130,254,240,200));
  p3.setBackground(Color.lightGray);
  p1.add(l);
  p2.add(ta);
  p3.add(b);
  this.add(p1,"North");
  this.add(p2,"Center");
  this.add(p3,"South");
  this.setLocation(f.getX() + f.getWidth(), f.getY());
  this.addWindowListener(new WindowHandler());
  f.pack();
  f.setResizable(false);
  f.setVisible(true);
 }
 public void actionPerformed(ActionEvent e)
 {
  {
   dispose();
  }
 }
 public void display(){
  this.setSize(350,350);
  this.setResizable(false);
  this.setVisible(true);
 }
 private class WindowHandler extends WindowAdapter{
  public void windowClosing(WindowEvent e){
  dispose();
//  System.exit(1);
  }
 }
 
}

helpcomputer.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class helpcomputer extends aboutcomputer implements ActionListener{
 private String str;
 private Button bt;
 public helpcomputer(JFrame f)
 {
  super(f);
  this.setTitle("简单计算器帮助文档!");
  str="呵呵,还没有帮助文档!";
  ta.setText(str);
  bt=new Button("确定");
 }
}

标题SpringBoot智能在线预约挂号系统研究AI更换标题第1章引言介绍智能在线预约挂号系统的研究背景、意义、国内外研究现状及论文创新点。1.1研究背景与意义阐述智能在线预约挂号系统对提升医疗服务效率的重要性。1.2国内外研究现状分析国内外智能在线预约挂号系统的研究与应用情况。1.3研究方法及创新点概述本文采用的技术路线、研究方法及主要创新点。第2章相关理论总结智能在线预约挂号系统相关理论,包括系统架构、开发技术等。2.1系统架构设计理论介绍系统架构设计的基本原则和常用方法。2.2SpringBoot开发框架理论阐述SpringBoot框架的特点、优势及其在系统开发中的应用。2.3数据库设计与管理理论介绍数据库设计原则、数据模型及数据库管理系统。2.4网络安全与数据保护理论讨论网络安全威胁、数据保护技术及其在系统中的应用。第3章SpringBoot智能在线预约挂号系统设计详细介绍系统的设计方案,包括功能模块划分、数据库设计等。3.1系统功能模块设计划分系统功能模块,如用户管理、挂号管理、医生排班等。3.2数据库设计与实现设计数据库表结构,确定字段类型、主键及外键关系。3.3用户界面设计设计用户友好的界面,提升用户体验。3.4系统安全设计阐述系统安全策略,包括用户认证、数据加密等。第4章系统实现与测试介绍系统的实现过程,包括编码、测试及优化等。4.1系统编码实现采用SpringBoot框架进行系统编码实现。4.2系统测试方法介绍系统测试的方法、步骤及测试用例设计。4.3系统性能测试与分析对系统进行性能测试,分析测试结果并提出优化建议。4.4系统优化与改进根据测试结果对系统进行优化和改进,提升系统性能。第5章研究结果呈现系统实现后的效果,包括功能实现、性能提升等。5.1系统功能实现效果展示系统各功能模块的实现效果,如挂号成功界面等。5.2系统性能提升效果对比优化前后的系统性能
在金融行业中,对信用风险的判断是核心环节之一,其结果对机构的信贷政策和风险控制策略有直接影响。本文将围绕如何借助机器学习方法,尤其是Sklearn工具包,建立用于判断信用状况的预测系统。文中将涵盖逻辑回归、支持向量机等常见方法,并通过实际操作流程进行说明。 一、机器学习基本概念 机器学习属于人工智能的子领域,其基本理念是通过数据自动学习规律,而非依赖人工设定规则。在信贷分析中,该技术可用于挖掘历史数据中的潜在规律,进而对未来的信用表现进行预测。 二、Sklearn工具包概述 Sklearn(Scikit-learn)是Python语言中广泛使用的机器学习模块,提供多种数据处理和建模功能。它简化了数据清洗、特征提取、模型构建、验证与优化等流程,是数据科学项目中的常用工具。 三、逻辑回归模型 逻辑回归是一种常用于分类任务的线性模型,特别适用于二类问题。在信用评估中,该模型可用于判断借款人是否可能违约。其通过逻辑函数将输出映射为0到1之间的概率值,从而表示违约的可能性。 四、支持向量机模型 支持向量机是一种用于监督学习的算法,适用于数据维度高、样本量小的情况。在信用分析中,该方法能够通过寻找最佳分割面,区分违约与非违约客户。通过选用不同核函数,可应对复杂的非线性关系,提升预测精度。 五、数据预处理步骤 在建模前,需对原始数据进行清理与转换,包括处理缺失值、识别异常点、标准化数值、筛选有效特征等。对于信用评分,常见的输入变量包括收入水平、负债比例、信用历史记录、职业稳定性等。预处理有助于减少噪声干扰,增强模型的适应性。 六、模型构建与验证 借助Sklearn,可以将数据集划分为训练集和测试集,并通过交叉验证调整参数以提升模型性能。常用评估指标包括准确率、召回率、F1值以及AUC-ROC曲线。在处理不平衡数据时,更应关注模型的召回率与特异性。 七、集成学习方法 为提升模型预测能力,可采用集成策略,如结合多个模型的预测结果。这有助于降低单一模型的偏差与方差,增强整体预测的稳定性与准确性。 综上,基于机器学习的信用评估系统可通过Sklearn中的多种算法,结合合理的数据处理与模型优化,实现对借款人信用状况的精准判断。在实际应用中,需持续调整模型以适应市场变化,保障预测结果的长期有效性。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值