독도 광고 모금 캠페인

특별한딸기이야기

블로그 이미지
딸기다운 사이버 공간을 만들고 싶어하는 특별한 딸기의 블로그입니다.
by 특별한녀석
  • 9,675Total hit
  • 4Today hit
  • 25Yesterday hit

최근에 달린 레몬펜 쪽지

Statistics Graph

'2008/05/22'에 해당되는 글 2건

  1. 2008/05/22
    자바 코드(병합 프로그램)
  2. 2008/05/22
    자바 코드(숫자 바꾸기)

두 개의 배열을 합하는 프로그램

사용자로부터 두 개의 배열을 입력받아 하나의 배열로 만든다.

모든 배열을 정렬되어야 한다.

public class Run
{
 // 메인 함수
 public static void main(String[] args)
 {
  IntArray a, b, c;
  Sort s = new Sort();
  Add add = new Add();
 
  a = new IntArray();
  b = new IntArray();
 
  a.UserInput();
  s.BubbleSort(a);
  a.PrintArray();
 
  b.UserInput();
  s.BubbleSort(b);
  b.PrintArray();
 
  c = add.ArrayAdd(a, b);
  s.BubbleSort(c);
  c.PrintArray();
 }
}

import java.util.*;

public class IntArray
 {
  int[] number;
  Scanner scan;
 
  // 생성자
  // 키보드 입력을 위해 Scanner 객체 생성
  public IntArray()
  {
   scan = new Scanner(System.in);
  }
 
  // 반환값 : 배열의 크기
  // 배열의 크기를 반환한다.
  public int GetArrayLength()
  {
   return number.length;
  }
 
  // 입력값 : 배열위치
  // 반환값 : 배열위치에 저장된 정수값
  // 지정 위치에 저장된 값을 반환한다
  public int GetArrayValue(int position)
  {
   return number[position];
  }
 
  // 입력값 : 배열의 크기
  // 배열을 입력값에 맞는 크기로 설정한다.
  public void SetArrayLength(int length)
  {
   number = new int[length];
  }
 
  // 입력값 : 배열의 위치, 저장될 값
  // 배열의 위치에 저장될 값을 저장한다.
  public void SetArrayValue(int position, int value)
  {
   number[position] = value;
  }
 
  // 사용자로 부터 배열의 크기와 각 배열의 값을 받아온다.
  public void UserInput()
  {
   int temp, temp_1;
   
   System.out.print("배열의 크기를 입력하세요 : ");
   temp = scan.nextInt();
   this.SetArrayLength(temp);
   
   for(temp_1 = 0; temp_1 < this.GetArrayLength(); temp_1++)
   {
    System.out.print(temp_1 + "번째 값을 입력하세요 : ");
    temp = scan.nextInt();
    this.SetArrayValue(temp_1, temp);
   }
  }
 
  // 배열에 저장된 값을 출력한다
  public void PrintArray()
  {
   int temp;
   
   System.out.println("저장된 배열의 값");
   
   for(temp = 0; temp < this.GetArrayLength(); temp++)
   {
    System.out.println((temp + 1) + "번째 값 : " + this.GetArrayValue(temp));
   }
  }
 }

public class Sort
{
 // 입력값 : 정렬할 IntArray 포인터
 // 버블 정렬을 통해 배열을 정렬한다.
 public void BubbleSort(IntArray a)
 {
  int temp, temp_1, temp_2;
 
  for(temp = 0; temp < a.GetArrayLength() - 1; temp++)
  {
   for(temp_1 = 0; temp_1 < a.GetArrayLength() - temp - 1; temp_1++)
   {
    if(a.GetArrayValue(temp_1) > a.GetArrayValue(temp_1 + 1))
    {
     temp_2 = a.GetArrayValue(temp_1 + 1);
     a.SetArrayValue(temp_1 + 1, a.GetArrayValue(temp_1));
     a.SetArrayValue(temp_1, temp_2);
    }
   }
  }
 }
}

public class Add
{
 // 입력값 : 합칠 IntArray 포인터 2개
 // 반환값 : 합쳐진 IntArray 포인터
 // 두개의 IntArray를 합친다.
 public IntArray ArrayAdd(IntArray a, IntArray b)
 {
  IntArray result = new IntArray();
  int temp;
 
  result.SetArrayLength(a.GetArrayLength() + b.GetArrayLength());
 
  for(temp = 0; temp < a.GetArrayLength(); temp++)
  {
   result.SetArrayValue(temp, a.GetArrayValue(temp));
  }
 
  for(temp = 0; temp < b.GetArrayLength(); temp++)
  {
   result.SetArrayValue(temp + a.GetArrayLength(), b.GetArrayValue(temp));
  }
 
  return result;
 }
}

'딸기 공부방 > JAVA' 카테고리의 다른 글

scjp 1.4 지료  (0) 2008/07/03
자바 코드(병합 프로그램_선택 정렬 추가)  (0) 2008/05/24
자바 코드(병합 프로그램)  (0) 2008/05/22
자바 코드(숫자 바꾸기)  (0) 2008/05/22
TRACKBACK 1 AND COMMENT 0

// 숫자를 입력받아 반대로 출력하는 프로그램
// 예 : 123456 -> 654321
package com.tistory.special0strawberry;

import java.util.*;

public class Integer_Print
{
 private int x;
 private int division;
 private int[] result;
 private Scanner scan;
 
 // 생성자
 // 내용 : 변수의 초기화
 public Integer_Print()
 {
  setX(0);
  setDivision(1);
  scan = new Scanner(System.in);
 }
 
 // 입력값 : 배열의 크기
 // 내용 : result 배열의 크기를 정한다.
 public void Set_Result_Length(int s)
 {
  result = new int[s];
 }
 
 // 입력값 : 배열 위치의 값, 배열의 위치
 // 내용 : 배열의 위치에 맞는 값을 넣는다.
 public void Set_Result_Position(int n, int p)
 {
  result[n] = p;
 }
 
 // 반환값 : 배열의 크기
 // 내용 : 배열의 크기를 반환한다.
 public int Get_Result_Length()
 {
  return result.length;
 }
 
 // 입력값 : 배열의 위치
 // 반환값 : result 배열의 위치의 값
 // 내용 : 배열 중에서 위치에 맞는 값을 반환한다.
 public int Get_Result_Position(int p)
 {
  return result[p];
 }
 
 // 입력값 : 정수형 변수 i
 // 내용 : x의 값을 i로 정한다.
 public void setX(int i)
 {
  x = i;
 }
 
 // 반환값 : 정수형 값
 // 내용 : x의 값을 반환한다.

 // 반환값 : x의 값
 // 내용 : x의 현재값을 반환한다.
 public int getX()
 {
  return x;
 }
 
 // 입력값 : 정수형 변수 d
 // 내용 : division의 값을 d로 정한다.
 
 // 입력값 : 정수값
 // 내용 : division의 값을 정수값으로 정한다.
 public void setDivision(int d)
 {
  division = d;
 }
 
 // 반환값 : 정수형 값
 // 내용 : division의 값을 반환한다.
 
 // 반환값 : division의 값
 // 내용 : division의 값을 반환한다.
 public int getDivision()
 {
  return division;
 }
 
 // 내용 : 정수 값을 키보드로 부터 받아온다.
 public int Scan()
 {
  int result;
  System.out.print("정수의 값을 입력하세요 : ");
  result = scan.nextInt();
 
  return result;
 }
 
 // 내용 : division의 값을 계산한다.
 public void Cal_Division()
 {
  int temp;
  int temp_1;
 
  temp = getX();
  temp_1 = getDivision();
 
  while(true)
  {
   if((temp / temp_1) == 0)
   {
    break;
   }
   else
   {
    temp_1 *= 10;
   }
  }
 
  setDivision(temp_1);
 }
 
 // 내용 : 값이 들어갈 배열의 크기를 계산한다.
 public void Cal_Result_Length()
 {
  Set_Result_Length((int)Math.log10(getDivision()) + 1);
 }
 
 // 내용 : 값을 배열에 집어 넣는다.
 public void Input_Result()
 {
  int temp, x_value, input_value, number, cal_number;
 
  x_value = getX();
  number = 0;
  cal_number = 0;
   
  for(temp = Get_Result_Length() - 1; temp > 0; temp--)
  {
   // 배열에 들어갈 값 계산
   input_value = x_value % 10;
   x_value /= 10;
   
   // 정수값 계산
   cal_number *= 10;
   cal_number += input_value;
   
   Set_Result_Position(number, input_value);
   Set_Result_Position(this.Get_Result_Length() - 1, cal_number);
   number++;
  }
 }
 
 // 내용 : result배열의 값을 화면에 출력한다.
 public void Print_Result()
 {
  int length;
 
  System.out.println("number : " + this.getX());
  System.out.println("result : " + this.Get_Result_Position(this.Get_Result_Length() -1));
  for(length = 0; length < this.Get_Result_Length() - 1; length++)
  {
   System.out.println(length + " : " + this.Get_Result_Position(length));
  }
 }
 
 // 내용 : 프로그램을 시작한다.
 public void Start()
 {
  setX(Scan());
  Cal_Division();
  Cal_Result_Length();
  Input_Result();
  Print_Result();
 }
}

'딸기 공부방 > JAVA' 카테고리의 다른 글

scjp 1.4 지료  (0) 2008/07/03
자바 코드(병합 프로그램_선택 정렬 추가)  (0) 2008/05/24
자바 코드(병합 프로그램)  (0) 2008/05/22
자바 코드(숫자 바꾸기)  (0) 2008/05/22
TRACKBACK 0 AND COMMENT 0

ARTICLE CATEGORY

딸기 이야기 (254)
딸기의 혼잣말 (55)
딸기 호감 사이트 (2)
딸기의 사진 (16)
딸기 리뷰 (15)
딸기 공부방 (113)
딸기 자료실 (52)

CALENDAR

«   2008/05   »
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

ARCHIVE