자바 이클립스 계산기 - jaba ikeullibseu gyesangi

  • 상세정보
  • 자료후기 (0)
  • 자료문의 (0)
  • 판매자정보

소개글

JAVA Eclipse로 구현한 Windows용 계산기 구현 보고서
기본 사칙연산, 루트기능의 계산기의 소스분석및
구현 내용을 작성하였다.

목차

1. 프로젝트 제목
2. 프로젝트 기간
3. 프로젝트 목표
3. 프로젝트 목표
4. 주요 관련 기술
5. 수행 내용
6. GUI 및 기능 구현 화면
7. 개선 사항
8. 참고 문서
9. 팀원 프로젝트 활동 사진

본문내용

1. 프로젝트 제목 : 자바를 이용한 계산기 프로젝트
2. 프로젝트 기간 : 4월 24일 ~ 5월 1일
3. 프로젝트 목표 : 컴퓨터에서 일반적으로 사용되고 있는 계산기를 자바 이클립스로 기능 및 GUI 구현
3. 프로젝트 목표
- 컴퓨터에서 일반적으로 사용되고 있는 계산기를 수업시간에 배운 자바 관련 기술(Eclipse)을 토대로 팀을 만들어 상호간의 토의 및 연구를 수행하고 Calculator의 기본 연산기능(+, -, *, /) 외에 수업시간에 배운 것을 활용하여 팀 개성에 맞는 기능 추가 및 GUI 구현을 목표로 함

4. 주요 관련 기술
import java.util.*;

public class Calc {
public static double eval(String exp){
StringTokenizer st = new StringTokenizer(exp);
Stack stack = new Stack();

while(st.hasMoreTokens()){
String tok = st.nextToken();
if(opType(tok) > 0){
String s1 = (String)stack.pop();
String s2 = (String)stack.pop();
double v1 = Double.parseDouble(s1);
double v2 = Double.parseDouble(s2);
double value = 0;
switch (opType(tok)) {
case 1: // +
value = v2 + v1;
break;
case 2: // -
value = v2 - v1;
break;
case 3: // *
value = v2 * v1;
break;
case 4: // /
value = v2 / v1;
break;

}
stack.push(String.valueOf(value));
} else {
stack.push(tok);
}
}
String s = (String) stack.pop();
double result = Double.parseDouble(s);
return result;
}

private static int opType(String op){
op = op.trim();

참고 자료

[1] 초보자를 위한 자바 2, 홍릉과학출판사
[2] http://pllab.kw.ac.kr/j2seAPI/api/index.html : java API Site

태그

이 자료와 함께 구매한 자료

 * 계산식이 나오지 않고 동작하는 옛날 계산기 방식의 단순한 프로그램을 AWT로 GUI를 구성하여 만들었습니다. 


코드)

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

//Button Calculator
public class Calculator extends Frame implements ActionListener {

	private static final long serialVersionUID = 1L;

	// 멤버 필드
	private String[] sustr = { "7", "8", "9", "4", "5", "6", "1", "2", "3", "0", "+/-", "." };
	private Button[] subt = new Button[sustr.length];
	private String[] funstr = { "BackSpace", "CE", "C" };
	private Button[] funbt = new Button[funstr.length];
	private String[] operstr = { "+", "-", "*", "/" };
	private Button[] operbt = new Button[operstr.length];
	private Button equbt = new Button("=");
	private Label disp = new Label("0.", Label.RIGHT);
	
	private boolean first = true; // true 처음, false 처음이 아님. -> 숫자 입력하기 위한 멤버필드
	private boolean jumcheck = false; //false면 소수점을 누르지  않음, true 소수점을 누름.  
	private char operator = '+'; //연산자
	private double result = 0;
	
	// 생성자
	public Calculator() {
		super("Calculator");
		buildGUI();
		setEvent();
	}

	// 메서드
	@Override
	public void actionPerformed(ActionEvent e) {
		
		// 숫자 입력 로직
		for(int i = 0; i<subt.length-2 ; i++) {
			if(e.getSource()== subt[i]) {
				
				//처음으로 눌렀음.
				if(first) {
					// 0 연속으로 누르는 것 방지
					if(disp.getText().equals("0.") && subt[i].getLabel().equals("0")) return;
					disp.setText(subt[i].getLabel()+".");
					first = false; //다음 번엔 처음이 아니게 한다.
				} 
				
				//처음이 아닌경우 
				else {
					String tmp = disp.getText();
					if(jumcheck) { //"." 을 누른 적이 있다.
						disp.setText(tmp + subt[i].getLabel());
					} else { // "."을 누른적이 없다. 
						tmp = tmp.substring(0, tmp.length()-1); //마지막 점을 뻄.
						disp.setText(tmp + subt[i].getLabel() + ".");
					}
				}
				return;
			}
		} //end subt
		
		
		// 부호 처리 로직
		if(e.getSource() == subt[subt.length-2]) {
			String tmp = disp.getText();
			if(tmp.equals("0.")) return; //처음 0일때  +/- 의미 x
			if(tmp.charAt(0) == '-') {
				disp.setText(tmp.substring(1));
			} else {
				disp.setText('-' + tmp);
			}
		}
		
		// 소수점 처리  로직 
		if(e.getSource()== subt[subt.length-1]) { 
			first = false;
			jumcheck = true;
			return;
		}
		
		// 연산자 처리  로직
		for(int i = 0 ; i<operbt.length ; i++) {
			if(e.getSource() == operbt[i]) {
				calc();//계산
				operator = operbt[i].getLabel().charAt(0);
				first = true;
				jumcheck = false;
			}
		}
		
		// '=' 처리로직 
		if(e.getSource() == equbt) {
			calc();
			first = true;
			jumcheck = false;
			operator = '+';
			result = 0;
			return;
		}
		
		// "backSpace" 처리로직 
		if(e.getSource() == funbt[0]) {
			String tmp = disp.getText(); 
			if(tmp.equals("0.")) return; 
			
			//2. 5. 인 경우..
			if(tmp.length() == 2) {
				disp.setText("0.");
				first = true;
				jumcheck = false;
				return;
			}
			//-4.등등 인경우.
			if(tmp.length() == 3 && tmp.charAt(0) =='-') {
				disp.setText("0.");
				first = true;
				jumcheck = false;
				return;
			}
			
			// + 인경우 ex)2.3
			if(tmp.length() == 3 && tmp.charAt(0) =='0') {
				disp.setText("0.");
				first = true;
				jumcheck = false;
				return;
			}
			
			if(tmp.charAt(tmp.length()-1)== '.') {
				tmp = tmp.substring(0,tmp.length() -2);
				disp.setText(tmp  +".");
			} else {
				disp.setText(tmp.substring(0,tmp.length() -1));
			}
			return;
		}
		
		// "CE" 처리로직 
		if(e.getSource() == funbt[1]) {
			disp.setText("0.");
			first = true;
			jumcheck = false;
			result = 0;
			return;		
		}
		
		// "C" 처리로직 
		if(e.getSource() == funbt[2]) {
			disp.setText("0.");
			first = true;
			jumcheck = false;
			operator = '+';
			result = 0;
			return;
		}
	}
	
	public void calc() {
		double value =Double.parseDouble(disp.getText());
		switch(operator){
			case '+' : result = result + value; break;
			case '-' : result = result - value; break;
			case '*' : result = result * value; break;
			case '/' : result = result / value; break;
		}
		
		//소수점 처리
		double val = result - (int)result;
		if(val > 0) {
			disp.setText(String.valueOf(result));
		} else {
			disp.setText(String.valueOf((int)result)+".");
		}
		
	}
	
	//이벤트 연결
	public void setEvent() {
		for(int i = 0 ; i<subt.length; i++) 
			subt[i].addActionListener(this);
		
		for(int i = 0 ; i<funbt.length; i++) 
			funbt[i].addActionListener(this);
		
		for(int i = 0 ;i<operbt.length; i++) 
			operbt[i].addActionListener(this);
		
		equbt.addActionListener(this);
		
		//익명클래스로 이벤트 달기
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}
	
	// 화면  구성
	public void buildGUI() {
		setBackground(Color.cyan);
		add("North", new Label()); //여백
		add("South", new Label()); //여백
		add("West", new Label()); //여백
		add("East", new Label()); //여백
		
		//기본 BorderLayout
		
		Panel mainPanel = new Panel(new BorderLayout(3,3));
			disp.setBackground(Color.white);
			disp.setFont(new Font("Serif", Font.BOLD,15));
			mainPanel.add("North", disp); 
			
			Panel cPanel = new Panel(new GridLayout(1,2,3,3));
				Panel cleftPanel = new Panel(new GridLayout(4,3,3,3));
					for(int i = 0 ;i<subt.length;i++) {
						subt[i] = new Button(sustr[i]); //버튼 초기화
						subt[i].setFont(new Font("Serif", Font.BOLD,15));
						cleftPanel.add(subt[i]);
					}
				cPanel.add(cleftPanel);
				
				Panel crightPanel = new Panel(new GridLayout(4,1,3,3));
					Panel cr1Panel =new Panel(new GridLayout(1,3,3,3));
						for(int i = 0 ; i<funbt.length; i++) {
							funbt[i] = new Button(funstr[i]);
							funbt[i].setFont(new Font("Serif", Font.BOLD,15));
							cr1Panel.add(funbt[i]);
						}
					crightPanel.add(cr1Panel);
					
					Panel cr2Panel =new Panel(new GridLayout(1,3,3,3));
					Panel cr3Panel =new Panel(new GridLayout(1,3,3,3));
						for(int i = 0 ; i<operstr.length ;i++) {
							operbt[i] = new Button(operstr[i]);
							operbt[i].setFont(new Font("Serif", Font.BOLD,15));
							if(i<2) cr2Panel.add(operbt[i]);
							else cr3Panel.add(operbt[i]);
						}
						
					crightPanel.add(cr2Panel);
					crightPanel.add(cr3Panel);
					
					equbt.setFont(new Font("Serif", Font.BOLD,15));
					crightPanel.add(equbt);
					
				cPanel.add(crightPanel);
			mainPanel.add("Center",cPanel);
		
		add("Center", mainPanel);
		
		pack();
		
		Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension my = getSize();
		setLocation(scr.width / 2 - my.width / 2, scr.height / 2 - my.height / 2);
		
		setResizable(false);
		setVisible(true);
	}
	
	public static void main(String[] args) {
		new Calculator();
	}

}
자바 이클립스 계산기 - jaba ikeullibseu gyesangi
실행화면

계산기.zip

0.02MB