❶ java设计一个简单的计算器,要求根据输入的操作数和运算符(+-*/)计算其值
include<iostream>
float a,b,m=1;
string n;
while(m==1){
cin>>a>>b;
cin>>n;
switch(n){
case `+` : cout<<a<<"+"<<b<<"="<<a+b
<<end1;
break;
case `-` : cout<<a<<"-"<<b<<"="<<a-b
<<end1;
break;
case `*` : cout<<a<<"*"<<b<<"="<<a*b
<<end1;
break;
case `/` : cout<<a<<"/"<<b<<"="<<a/b
<<end1;
break;
case `%` : cout<<a<<"%"<<b<<"="<<a%b
<<end1;
break;
defult: cout<<"输入错误,请检查"
<<end1;
}
cout<<"按1继续,其他键退出:";
cin>>m;
}
❷ java队列和栈在计算器的应用
public class MyStack { String str; StringBuilder expression; static final String operationalCharacters = "+-*/()";//操作符号 static final String number = "012345689";//数字集 public MyStack(String str) { this.str = str; generateExp(); } public void generateExp(){ expression = new StringBuilder(); for(char ch:str.toCharArray()){ if(isNumber(ch) || isOperationalCharacter(ch)){ expression.append(ch); } } } public String getExp(){ return expression.toString(); } public boolean isNumber(char ch){ return number.indexOf(ch) >= 0; } public boolean isOperationalCharacter(char ch){ return operationalCharacters.indexOf(ch) >= 0; } public void showItems(){ System.out.println(expression); } public static void main(String[] args) { MyStack s = new MyStack("d5 ~c+ 2$+a32*10b"); s.showItems(); }}
❸ 用java编写一个简单的计算器类
这个是有界面的,自己改一些就可以了。。
---------------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
class Stack_Float
{
float nums[];
int top;
Stack_Float()
{
nums = new float[50];
top = -1;
}
boolean IsEmpty()
{
if (top == -1)
return true;
else
return false;
}
float Pop_Stack()
{
if (top == -1)
{
return 0;
}
top--;
return nums[top + 1];
}
float GetTop()
{
return nums[top];
}
void Push_Stack(float num)
{
if (top == 49)
return;
top++;
nums[top] = num;
}
}
class Stack_Char
{
char str[];
int top;
Stack_Char()
{
str = new char[50];
top = -1;
}
boolean CanPush(char c)
{
int temp = top;
if (c == '(')
{
while (temp != -1)
{
if (str[temp] == '(')
{
return false;
}
temp--;
}
}
temp = top;
if (c == '[')
{
while (temp != -1)
{
if (str[temp] == '[' || str[temp] == '(')
{
return false;
}
temp--;
}
}
if (c == '{')
{
while (temp != -1)
{
if (str[temp] == '{' || str[temp] == '[' || str[temp] == '(')
{
return false;
}
temp--;
}
}
return true;
}
boolean IsEmpty()
{
if (top == -1)
return true;
else
return false;
}
void Push_Stack(char ch)
{
if (top == 49)
return;
top++;
str[top] = ch;
}
char Pop_Stack()
{
if (top == -1)
return '\0';
top--;
return str[top + 1];
}
char GetTop()
{
if (top == -1)
{
System.out.print("error");
System.exit(0);
}
return str[top];
}
}
public class jisuanqi extends javax.swing.JFrame implements ActionListener
{
JTextField text = new JTextField();
JTextField text1 = new JTextField();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
JButton jButton5 = new JButton();
JButton jButton6 = new JButton();
JButton jButton7 = new JButton();
JButton jButton8 = new JButton();
JButton jButton9 = new JButton();
JButton jButton10 = new JButton();
JButton jButton11 = new JButton();
JButton jButton12 = new JButton();
JButton jButton13 = new JButton();
JButton jButton14 = new JButton();
JButton jButton15 = new JButton();
JButton jButton16 = new JButton();
JButton jButton17 = new JButton();
JButton jButton18 = new JButton();
JButton jButton19 = new JButton();
JButton jButton20 = new JButton();
JButton jButton21 = new JButton();
JButton jButton22 = new JButton();
String show = "";
public jisuanqi()
{
initComponents();
}
char[] TranSmit(char str[])
{
char houzhui[] = new char[50]; // 存放后缀表达式的字符串
int i = 0, j = 0;
char c = str[i];
Stack_Char s = new Stack_Char(); // 存放运算符的栈
while (c != '=') // 对算术表达式扫描未结束时
{
if (c >= '0' && c <= '9')
{
while (c >= '0' && c <= '9')// 数字直接入栈
{
houzhui[j] = c;
j++;
i++;
c = str[i];
}
houzhui[j] = '#';// 用#隔开数字
j++;
}
switch (c) // 扫描到运算符时
{
case '+':
case '-':
case '*':
case '/':
case '(':
case '[':
case '{':
if (s.IsEmpty() == true) // 栈空,直接入栈
{
s.Push_Stack(c);
i++;
c = str[i];
break;
}
if (ComPare(s.GetTop(), c) == -1) {
s.Push_Stack(c); // 入栈
i++;
c = str[i];
break;
}
if (ComPare(s.GetTop(), c) == 1) {
houzhui[j] = s.Pop_Stack();// 出栈元素存入后缀表达式
j++;
break;
}
case ')': // 扫描到 )
while (s.GetTop() != '(') // 未扫描到 ( 时,出栈
{
houzhui[j] = s.Pop_Stack();
j++;
}
s.Pop_Stack(); // '(' 出栈
i++;
c = str[i];
break;
case ']': // 扫描到 ]
while (s.GetTop() != '[') // 未扫描到 [ 时,出栈
{
houzhui[j] = s.Pop_Stack();
j++;
}
s.Pop_Stack(); // '[' 出栈
i++;
c = str[i];
break;
case '}': // 扫描到 }
while (s.GetTop() != '{') // 未扫描到 { 时,出栈
{
houzhui[j] = s.Pop_Stack();
j++;
}
s.Pop_Stack(); // '{' 出栈
i++;
c = str[i];
break;
}
}
while (s.IsEmpty() != true)// 把剩余的运算符直接出栈
{
houzhui[j] = s.Pop_Stack();
j++;
}
houzhui[j] = '=';// 后缀表达式后面加 =
j++;
houzhui[j] = '\0';
j++;
return houzhui;
}
float Count(char str[])
{
Stack_Float s = new Stack_Float();// 定义存放数字的栈
char c = str[0];
int i = 0;
float result = 0, temp, left, right;
while (c != '=') // 未扫描到 = 时
{
if (c >= '0' && c <= '9')// 扫描到数字
{
temp = 0;
while (c != '#')// 未读到分隔符时
{
temp = temp * 10 + c - '0';
i++;
c = str[i];
}
s.Push_Stack(temp);// 进栈
}
switch (c)// 扫描到运算符时
{
case '+':
{
result = s.Pop_Stack() + s.Pop_Stack();// 2个数字出栈相加
s.Push_Stack(result);// 最后得数进栈
break;
}
case '-':
{
right = s.Pop_Stack();// 右操作数出栈
left = s.Pop_Stack();// 左操作数出栈
result = left - right;
s.Push_Stack(result);
break;
}
case '*':
{
result = s.Pop_Stack() * s.Pop_Stack();// 2个数字出栈相乘
s.Push_Stack(result);
break;
}
case '/':
{
right = s.Pop_Stack();// 右操作数出栈
left = s.Pop_Stack();// 左操作数出栈
result = left / right;
s.Push_Stack(result);
break;
}
}
i++;
c = str[i];
}
return result;
}
int ComPare(char a, char b) // 判断运算符的优先级函数
{
int s[][] = {// 栈顶元素高于算术表达式中的元素时, 返回 1,否则返回 -1
{ 1, 1, -1, -1, -1, 1, -1, 1, -1, 1 },
{ 1, 1, -1, -1, -1, 1, -1, 1, -1, 1 },
{ 1, 1, 1, 1, -1, 1, -1, 1, -1, 1 },
{ 1, 1, 1, 1, -1, 1, -1, 1, -1, 1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ 1, 1, 1, 1, -1, 1, -1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ 1, 1, 1, 1, -1, -1, -1, -1, -1, 1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ 1, 1, 1, 1, -1, -1, -1, -1, -1, -1 } };
char x1[] = { '+', '-', '*', '/', '(', ')', '[', ']', '{', '}' };// 栈顶元素
char x2[] = { '+', '-', '*', '/', '(', ')', '[', ']', '{', '}' };// 算术表达式中的元素
int k = 0, m, n = 0;
for (m = 0; m < 10; m++) // 查找2个进行比较的运算符在表中的位置,并返回比较结果
{
for (n = 0; n < 10; n++)
{
if (x1[m] == a && x2[n] == b)
{
k = 1;
break; // 找到比较结果后,跳出循环
}
}
if (k == 1)
break;
}
return s[m][n];// 返回比较结果
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jButton1)
{
show += "1";
text.setText(show);
}
if (e.getSource() == jButton2)
{
show += "2";
text.setText(show);
}
if (e.getSource() == jButton3)
{
show += "3";
text.setText(show);
}
if (e.getSource() == jButton4)
{
show += "4";
text.setText(show);
}
if (e.getSource() == jButton5)
{
show += "5";
text.setText(show);
}
if (e.getSource() == jButton6)
{
show += "6";
text.setText(show);
}
if (e.getSource() == jButton7)
{
show += "7";
text.setText(show);
}
if (e.getSource() == jButton8)
{
show += "8";
text.setText(show);
}
if (e.getSource() == jButton9)
{
show += "9";
text.setText(show);
}
if (e.getSource() == jButton10)
{
show += "0";
text.setText(show);
}
if (e.getSource() == jButton11)
{
show += "+";
text.setText(show);
}
if (e.getSource() == jButton12)
{
show += "-";
text.setText(show);
}
if (e.getSource() == jButton13)
{
show += "*";
text.setText(show);
}
if (e.getSource() == jButton14)
{
show += "/";
text.setText(show);
}
if (e.getSource() == jButton15)
{
show += "(";
text.setText(show);
}
if (e.getSource() == jButton16)
{
show += ")";
text.setText(show);
}
if (e.getSource() == jButton17)
{
show += "[";
text.setText(show);
}
if (e.getSource() == jButton18)
{
show += "]";
text.setText(show);
}
if (e.getSource() == jButton19)
{
show += "{";
text.setText(show);
}
if (e.getSource() == jButton20)
{
show += "}";
text.setText(show);
}
if (e.getSource() == jButton21)
{
show = "";
text.setText("");
text1.setText("");
}
if (e.getSource() == jButton22)
{
show += "=";
text.setText(show);
char str1[] = new char[50];
char str2[] = new char[50];
float result = 0;
str1 = show.toCharArray();
str2 = TranSmit(str1);
result = Count(str2);
text1.setText((new String(str2)).trim());
text.setText("" + result);
show = "";
}
}
private void initComponents()
{
text.setBounds(10, 10, 270, 30);
text1.setBounds(10, 50, 270, 30);
jButton1.setBounds(10, 90, 60, 25);
jButton2.setBounds(80, 90, 60, 25);
jButton3.setBounds(150, 90, 60, 25);
jButton4.setBounds(220, 90, 60, 25);
jButton5.setBounds(10, 120, 60, 25);
jButton6.setBounds(80, 120, 60, 25);
jButton7.setBounds(150, 120, 60, 25);
jButton8.setBounds(220, 120, 60, 25);
jButton9.setBounds(10, 150, 60, 25);
jButton10.setBounds(80, 150, 60, 25);
jButton11.setBounds(150, 150, 60, 25);
jButton12.setBounds(220, 150, 60, 25);
jButton13.setBounds(10, 180, 60, 25);
jButton14.setBounds(80, 180, 60, 25);
jButton15.setBounds(150, 180, 60, 25);
jButton16.setBounds(220, 180, 60, 25);
jButton17.setBounds(150, 210, 60, 25);
jButton18.setBounds(220, 210, 60, 25);
jButton19.setBounds(10, 210, 60, 25);
jButton20.setBounds(80, 210, 60, 25);
jButton21.setBounds(10, 240, 60, 25);
jButton22.setBounds(80, 240, 60, 25);
jButton1.setText("1");
jButton2.setText("2");
jButton3.setText("3");
jButton4.setText("4");
jButton5.setText("5");
jButton6.setText("6");
jButton7.setText("7");
jButton8.setText("8");
jButton9.setText("9");
jButton10.setText("0");
jButton11.setText("+");
jButton12.setText("-");
jButton13.setText("*");
jButton14.setText("/");
jButton15.setText("(");
jButton16.setText(")");
jButton17.setText("[");
jButton18.setText("]");
jButton19.setText("{");
jButton20.setText("}");
jButton21.setText("CE");
jButton22.setText("=");
jButton1.addActionListener(this);
jButton2.addActionListener(this);
jButton3.addActionListener(this);
jButton4.addActionListener(this);
jButton5.addActionListener(this);
jButton6.addActionListener(this);
jButton7.addActionListener(this);
jButton8.addActionListener(this);
jButton9.addActionListener(this);
jButton10.addActionListener(this);
jButton11.addActionListener(this);
jButton12.addActionListener(this);
jButton13.addActionListener(this);
jButton14.addActionListener(this);
jButton15.addActionListener(this);
jButton16.addActionListener(this);
jButton17.addActionListener(this);
jButton18.addActionListener(this);
jButton19.addActionListener(this);
jButton20.addActionListener(this);
jButton21.addActionListener(this);
jButton22.addActionListener(this);
add(text);
add(text1);
add(jButton1);
add(jButton2);
add(jButton3);
add(jButton4);
add(jButton5);
add(jButton6);
add(jButton7);
add(jButton8);
add(jButton9);
add(jButton10);
add(jButton11);
add(jButton12);
add(jButton13);
add(jButton14);
add(jButton15);
add(jButton16);
add(jButton17);
add(jButton18);
add(jButton19);
add(jButton20);
add(jButton21);
add(jButton22);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setBounds(300, 300, 300, 300);
setVisible(true);
}
public static void main(String args[])
{
new jisuanqi();
}
}
❹ JAVA中如何用运算符的优先级来实现一个简单的计算器,说出思路便可,适当请用代码表明
曾经写过,现在忘了代码;思路就是:先定义“+”,“-“等符号,当然如果写的全,还有”()“,”[]“,"{}"这些符号的优先级,记得是写一个枚举类型的;然后,计算的时候用java的栈,通过入栈和出栈来计算的;
不好意思,代码是找不到了
❺ JAVA的基础程序··计算器,出现堆栈溢出的情况,求解···
---------------------------------------------
public JpanelClass (){
//..
panel =new JpanelClass ();
--------------------------------------------
上面是你的代码,你在构造函数里还在调用自己这个构造函数,所以出现StackOverflowError
死循环了
❻ java简易计算器
我这里有一个以前自己写的简易计算器的源代码 只能实现一次运算一次 不能进行复杂的四则连续运算
如果要实现复杂的四则连续运算 需要搞清楚压栈的原理
废话不多说 上图 看懂了再去自己打一遍
我帮人家修改代码从来只给图不给源代码 因为有些人 给了他源代码 复制粘贴一下 就觉得了事了 必须要自己打一遍
❼ 用JAVA编写一个简单的计算器,要求如下
以下是上图计算器的代码:
packageComputer;
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.Container;
importjava.awt.Font;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.util.Stack;
importjavax.swing.JApplet;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
{
/**
*
*/
=1L;
privateJTextFieldtextField=newJTextField("请输入");
Stringoperator="";//操作
Stringinput="";//输入的式子
booleanflag=true;
//booleanflag1=true;
//booleanflag2=true;
publicvoidinit()//覆写Applet里边的init方法
{
ContainerC=getContentPane();
JButtonb[]=newJButton[16];
JPanelpanel=newJPanel();
C.add(textField,BorderLayout.NORTH);
C.add(panel,BorderLayout.CENTER);
panel.setLayout(newGridLayout(4,4,5,5));
Stringname[]={"7","8","9","+","4","5","6","-","1","2","3","*","0","C","=","/"};//设置按钮
for(inti=0;i<16;i++)//添加按钮
{
b[i]=newJButton(name[i]);
b[i].setBackground(newColor(192,192,192));
b[i].setForeground(Color.BLUE);//数字键设置为蓝颜色
if(i%4==3)
b[i].setForeground(Color.RED);
b[i].setFont(newFont("宋体",Font.PLAIN,16));//设置字体格式
panel.add(b[i]);
b[i].addActionListener(this);
}
b[13].setForeground(Color.RED);//非数字键,即运算键设置为红颜色
b[13].setForeground(Color.RED);
}
publicvoidactionPerformed(ActionEvente)
{
intcnt=0;
StringactionCommand=e.getActionCommand();
if(actionCommand.equals("+")||actionCommand.equals("-")||actionCommand.equals("*")||actionCommand.equals("/"))
input+=""+actionCommand+"";//设置输入,把输入的样式改成需要的样子
elseif(actionCommand.equals("C"))
input="";
elseif(actionCommand.equals("="))//当监听到等号时,则处理input
{
input+="="+compute(input);
textField.setText(input);
input="";
cnt=1;
}
else
input+=actionCommand;//数字为了避免多位数的输入不需要加空格
if(cnt==0)
textField.setText(input);
}
privateStringcompute(Stringinput)//即1237的样例
{
Stringstr[];
str=input.split("");
Stack<Double>s=newStack<Double>();
doublem=Double.parseDouble(str[0]);
s.push(m);
for(inti=1;i<str.length;i++)
{
if(i%2==1)
{
if(str[i].compareTo("+")==0)
{
doublehelp=Double.parseDouble(str[i+1]);
s.push(help);
}
if(str[i].compareTo("-")==0)
{
doublehelp=Double.parseDouble(str[i+1]);
s.push(-help);
}
if(str[i].compareTo("*")==0)
{
doublehelp=Double.parseDouble(str[i+1]);
doubleans=s.peek();//取出栈顶元素
s.pop();//消栈
ans*=help;
s.push(ans);
}
if(str[i].compareTo("/")==0)
{
doublehelp=Double.parseDouble(str[i+1]);
doubleans=s.peek();
s.pop();
ans/=help;
s.push(ans);
}
}
}
doubleans=0d;
while(!s.isEmpty())
{
ans+=s.peek();
s.pop();
}
Stringresult=String.valueOf(ans);
returnresult;
}
publicstaticvoidmain(Stringargs[])
{
JFrameframe=newJFrame("Count");
Countapplet=newCount();
frame.getContentPane().add(applet,BorderLayout.CENTER);
applet.init();//applet的init方法
applet.start();//线程开始
frame.setSize(350,400);//设置窗口大小
frame.setVisible(true);//设置窗口可见
}
}
❽ 用Java写的计算器的程序!不需要界面!
用java写的计算器的程序,主要是通过控制台输入,主要方法是使用scanner类来接收用户从键盘输入的一个算式,通过分解算式,存入两个字符串,判断中间的的符号,进行相应计算,如下代码:
System.out.println("-----------------------------------");
System.out.println("请输入一个算术表达式,如:45*23");
Scannerin=newScanner(System.in);//接收用户从键盘输入的字符
Stringstr=in.nextLine();
StringBufferbuffer=newStringBuffer();//保存左侧的数字
StringBufferbuffer1=newStringBuffer();//保存右侧的数字
chart='';//保存运算符
for(inti=0;i<str.length();i++){
if(str.charAt(i)=='+'||str.charAt(i)=='-'
||str.charAt(i)=='*'||str.charAt(i)=='/'){
t=str.charAt(i);//识别是什么运算符
for(intj=i+1;j<str.length();j++){
buffer1.append(str.charAt(j));
}
break;
}else{
buffer.append(str.charAt(i));
}
}
Stringc=buffer.toString();
Stringd=buffer1.toString();
doublea=Double.parseDouble(c);
doubleb=Double.parseDouble(d);
doublesum=0;
if(t=='+'){
sum=a+b;
}
if(t=='-'){
sum=a-b;
}
if(t=='*'){
sum=a*b;
}
if(t=='/'){
sum=a/b;
}
System.out.println("程序运算...");
System.out.println(c+t+d+"="+sum);
System.out.print("-----------------------------------");
运行结果如下:
❾ java中,用布局器实现计算器界面 并实现加减乘除等功能,怎样编程
http://wenku..com/view/5cfcb42d2af90242a895e5f6.html
❿ 用Java编译一个计算器的基本思想是什么
首先你得先画一个界面,先把界面实现出来,用Swing或者AWT;
然后就是书写监听类,每个按钮都得有监听的类,一般是内部类;
加减乘除的监听好写,关键是多位运算和连续运算,得想好是什么时候显示结果,比如说我输入一个数字,再点击按钮时如果是运算按键,那显示不动,如果还是数字键,那就要变成两位,这些考虑清楚了就差不多了,祝你成功