❶ 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;
然後就是書寫監聽類,每個按鈕都得有監聽的類,一般是內部類;
加減乘除的監聽好寫,關鍵是多位運算和連續運算,得想好是什麼時候顯示結果,比如說我輸入一個數字,再點擊按鈕時如果是運算按鍵,那顯示不動,如果還是數字鍵,那就要變成兩位,這些考慮清楚了就差不多了,祝你成功