java·Î ¸µÅ©µå ¸®½ºÆ®¸¦ ½á¼ °è»ê±â Äڵ带 ¸¸µé¾î º¸¾Ò´Âµ¥ °á°ú°ªÀÌ ³ª¿ÀÁú ¾Ê½À´Ï´Ù. ¹öÆ° ´·¯µµ ¿¡·¯´Â ¶ßÁö ¾Ê´Âµ¥ tf¿¡ °á°ú°ªÀº 0.0À¸·Î¸¸ ³ª¿É´Ï´Ù. µð¹ö±ë »ç¿ëÇغ¸·Á ÇßÁö¸¸ variables â¿¡ ¾Æ¹«°Íµµ ¶ßÁö ¾Ê¾Æ¼ È®ÀÎ ÇÒ¼ö°¡ ¾ø³×¿ä ¤Ð
¿Ö r °ª¿¡ °á°ú°¡ ¾Èµé¾î °¡´ÂÁö ´äº¯ÁÖ½Ã¸é °¨»çÇÏ°Ú½À´Ï´Ù.
package cal;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Queue;
class calc implements ActionListener{
JFrame frame;
JTextField tfN;
JTextField tf;
JPanel panel1;
JPanel panelNorth;
GridLayout grid;
JButton Jbu;
static String line = "";
static String num = "";
static int i = 0;
static float r = 0;
static float[] x = new float[30];
static String[] y = new String[30];
static LinkedList c_1 = new LinkedList();
static LinkedList c_2 = new LinkedList();
public calc() {
frame = new JFrame("°è»ê±â");
panel1 = new JPanel();
panelNorth = new JPanel();
tf = new JTextField("0");
tfN = new JTextField("");
}
public void gui() {
tf.setHorizontalAlignment(JTextField.RIGHT);
tf.setEditable(false);
tfN.setHorizontalAlignment(JTextField.RIGHT);
tfN.setEditable(false);
panelNorth.setLayout(new BorderLayout());
panelNorth.add(BorderLayout.NORTH,tfN);
panelNorth.add(BorderLayout.CENTER,tf);
panel1.setLayout(new GridLayout(4,3,6,6));
panel1.setBackground(new Color(222,232,244));
String[] text = {"7","8","9","/","4","5","6","*","1","2","3","-","0","C","+","="};
for(int i=0; i< text.length;i++) {<br />
Jbu = new JButton(text[i]);
Jbu.addActionListener(this);
panel1.add(Jbu);
}
frame.add(BorderLayout.NORTH,panelNorth);
frame.add(BorderLayout.CENTER,panel1);
frame.setResizable(false);
frame.setSize(220, 310);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
switch(str) {
case "C" :
tfN.setText("");
tf.setText("0");
line = "";
num ="";
eraser();
break;
case "1" : case "2" : case "3" : case "4" : case "5" :
case "6" : case "7" : case "8" : case "9" : case "0" :
tfN.setText(add(str));
break;
case "+" : case "-" : case "*" : case "/" :
tfN.setText(add_s(str));
break;
case "=" :
while(c_2.size() == 0) {
//for(int i =0; i
if(c_2.get(i)=="*" || c_2.get(i)=="/"){
r = c(i);
}
else{
i++;
}
}
while(c_2.size() == 0) {
//for(i =0; i
r = c(0);
}
tf.setText(String.valueOf(r));
break;
}
}
public static String add(String a) {
line = line + a;
num = num+a;
return line;
}
public static String add_s(String a) {
c_1.addLast(Float.parseFloat(num));
num ="";
line = line + a;
c_2.addLast(a);
return line;
}
public static float c(int a) {
if(c_2.size()== 0) {
return r;
}
else {
String str = (String) c_2.get(a);
float n_1 = (float) c_1.get(a);
float n_2 = (float) c_1.get(a+1);
switch(str) {
case "+" :
r = n_1 + n_2;
break;
case "-" :
r = n_1 - n_2;
break;
case "*" :
r = n_1 * n_2;
break;
case "/" :
r = n_1 / n_2;
break;
}
c_1.remove(a);
c_1.remove(a);
c_2.remove(a);
c_1.add(a, r);
return r;
}
}
public void eraser() {
while(c_1.size() ==0) {
//for(int z = 0;z
c_1.remove(0);
c_2.remove(0);
}
}
public static void main(String args[]) {
calc cal = new calc();
cal.gui();
}
}
---------------------------------------------------------
¾Æ·¡´Â linkedlist Ŭ·¡½º ÀÔ´Ï´Ù.
package cal;
public class LinkedList {
private Node head;
private Node tail;
private int size =0;
private class Node{
private Object data;
private Node next;
public Node(Object input) {
this.data = input;
this.next = null;
}
public String toString(){
return String.valueOf(this.data);
}
}
public void addFirst(Object input){
Node newNode = new Node(input);
newNode.next = head;
head = newNode;
size++;
if(head.next == null){
tail = head;
}
}
public void addLast(Object input){
Node newNode = new Node(input);
if(size == 0){
addFirst(input);
} else {
tail.next = newNode;
tail = newNode;
size++;
}
}
public void add(int k, Object input){
if(k == 0){
addFirst(input);
} else {
Node temp1 = node(k-1);
Node temp2 = temp1.next;
Node newNode = new Node(input);
temp1.next = newNode;
newNode.next = temp2;
size++;
if(newNode.next == null){
tail = newNode;
}
}
}
public Object removeFirst(){
Node temp = head;
head = temp.next;
Object returnData = temp.data;
temp = null;
size--;
return returnData;
}
Node node(int index) {
Node x = head;
for (int i = 0; i < index; i++)<br />
x = x.next;
return x;
}
public Object remove(int k){
if(k == 0)
return removeFirst();
Node temp = node(k-1);
Node todoDeleted = temp.next;
temp.next = temp.next.next;
Object returnData = todoDeleted.data;
if(todoDeleted == tail){
tail = temp;
}
todoDeleted = null;
size--;
return returnData;
}
public int indexOf(Object data){
Node temp = head;
int index = 0;
while(temp.data != data){
temp = temp.next;
index++;
if(temp == null)
return -1;
}
return index;
}
public Object get(int k){
Node temp = node(k);
return temp.data;
}
public int size() {
return size;
}
} | |
|