Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

EcologicalBinPacking/강희경: Difference between revisions

From ZeroWiki
imported>Unknown
No edit summary
 
(Repair batch-0002 pages from live compare)
 
Line 4: Line 4:
  using namespace std;
  using namespace std;
   
   
  int bottle[9];
  int bottle[9];
  int noMove[6];
  int noMove[6];
  int movedBottle;
  int movedBottle;
   
   
Line 22: Line 22:
  for(int i = 0; i < 9; i++)
  for(int i = 0; i < 9; i++)
  {
  {
  cin >> bottle[i];
  cin >> bottle[i];
  totalBottle += bottle[i];
  totalBottle += bottle[i];
  cin.get();
  cin.get();
  }
  }
Line 31: Line 31:
  void setNotMove()
  void setNotMove()
  {
  {
  noMove[0] = bottle[0] + bottle[4] + bottle[8];
  noMove[0] = bottle[0] + bottle[4] + bottle[8];
  noMove[1] = bottle[0] + bottle[5] + bottle[7];
  noMove[1] = bottle[0] + bottle[5] + bottle[7];
  noMove[2] = bottle[1] + bottle[3] + bottle[8];
  noMove[2] = bottle[1] + bottle[3] + bottle[8];
  noMove[3] = bottle[1] + bottle[5] + bottle[6];
  noMove[3] = bottle[1] + bottle[5] + bottle[6];
  noMove[4] = bottle[2] + bottle[3] + bottle[7];
  noMove[4] = bottle[2] + bottle[3] + bottle[7];
  noMove[5] = bottle[2] + bottle[4] + bottle[6];
  noMove[5] = bottle[2] + bottle[4] + bottle[6];
  }
  }
   
   
Line 47: Line 47:
  for(int i = 0; i < 6; i++)
  for(int i = 0; i < 6; i++)
  {
  {
  if(noMove[i] > MAX)
  if(noMove[i] > MAX)
  {
  {
  MAX = noMove[i];
  MAX = noMove[i];
  minimum = i;
  minimum = i;
  }
  }
Line 107: Line 107:
  int* input()  
  int* input()  
  {
  {
     int* slots = new int[NumberOfSlots + 1];
     int* slots = new int[NumberOfSlots + 1];
     int totalBottle = 0;  
     int totalBottle = 0;  
     for(int i = 0; i < NumberOfSlots; i++)  
     for(int i = 0; i < NumberOfSlots; i++)  
     {  
     {  
         if(!(cin >> slots[i])){
         if(!(cin >> slots[i])){
             isCorrectInput = false;
             isCorrectInput = false;
         }
         }
         totalBottle += slots[i];   
         totalBottle += slots[i];   
     }
     }
     char temp;
     char temp;
Line 120: Line 120:
     if(temp != '\n')
     if(temp != '\n')
         isCorrectInput = false;
         isCorrectInput = false;
     slots[9] = totalBottle;
     slots[9] = totalBottle;
     return slots;  
     return slots;  
  }  
  }  
Line 126: Line 126:
  int* setNotMove(int *aSlots)  
  int* setNotMove(int *aSlots)  
  {  
  {  
     int* noMove = new int[NumberOfCases];
     int* noMove = new int[NumberOfCases];
     noMove[0] = aSlots[0] + aSlots[4] + aSlots[8];  
     noMove[0] = aSlots[0] + aSlots[4] + aSlots[8];  
     noMove[1] = aSlots[0] + aSlots[5] + aSlots[7];  
     noMove[1] = aSlots[0] + aSlots[5] + aSlots[7];  
     noMove[2] = aSlots[1] + aSlots[3] + aSlots[8];  
     noMove[2] = aSlots[1] + aSlots[3] + aSlots[8];  
     noMove[3] = aSlots[1] + aSlots[5] + aSlots[6];  
     noMove[3] = aSlots[1] + aSlots[5] + aSlots[6];  
     noMove[4] = aSlots[2] + aSlots[3] + aSlots[7];  
     noMove[4] = aSlots[2] + aSlots[3] + aSlots[7];  
     noMove[5] = aSlots[2] + aSlots[4] + aSlots[6];
     noMove[5] = aSlots[2] + aSlots[4] + aSlots[6];
     return noMove;
     return noMove;
  }  
  }  
Line 149: Line 149:
     for(int i = 0; i < NumberOfCases; i++)  
     for(int i = 0; i < NumberOfCases; i++)  
     {  
     {  
         if(pNoMove[i] > MAX)  
         if(pNoMove[i] > MAX)  
         {  
         {  
    MAX = pNoMove[i];  
    MAX = pNoMove[i];  
             minimumCase = i;  
             minimumCase = i;  
         }  
         }  
     }  
     }  
     aSlots[9] -= MAX;
     aSlots[9] -= MAX;
     int *resultInformation = new int[NumberOfResultInformations];
     int *resultInformation = new int[NumberOfResultInformations];
     resultInformation[0] = minimumCase;
     resultInformation[0] = minimumCase;
     resultInformation[1] = aSlots[9];
     resultInformation[1] = aSlots[9];
     delete aSlots;
     delete aSlots;
     delete pNoMove;
     delete pNoMove;
Line 168: Line 168:
     if(isCorrectInput)
     if(isCorrectInput)
     {
     {
         string colorsCombination[] = {"BGC", "BCG", "GBC", "GCB", "CBG", "CGB"};
         string colorsCombination[] = {"BGC", "BCG", "GBC", "GCB", "CBG", "CGB"};
         cout << colorsCombination[aInformation[0]] << aInformation[1] << endl;
         cout << colorsCombination[aInformation[0]] << aInformation[1] << endl;
     }
     }
     else
     else
Line 190: Line 190:
  if(isCorrectInput)
  if(isCorrectInput)
  {
  {
  string colorsCombination[] = {"BGC", "BCG", "GBC", "GCB", "CBG", "CGB"};
  string colorsCombination[] = {"BGC", "BCG", "GBC", "GCB", "CBG", "CGB"};
  cout << colorsCombination[aInformation[0]] << aInformation[1] << endl;
  cout << colorsCombination[aInformation[0]] << aInformation[1] << endl;
  }
  }
  else
  else
Line 202: Line 202:
  오~ 굿 어드바이스요 --[[강희경]]
  오~ 굿 어드바이스요 --[[강희경]]
----
----

Latest revision as of 00:16, 27 March 2026

리팩토링 전

#include<iostream>
using namespace std;

int bottle[9];
int noMove[6];
int movedBottle;

int input();
void setNotMove();
int minimumMove();
void output(int noMove);
void main()
{   
	output(minimumMove());
}

int input()
{
	int totalBottle = 0;
	for(int i = 0; i < 9; i++)
	{
		cin >> bottle[i];
		totalBottle += bottle[i];
		cin.get();
	}
	return totalBottle;
}

void setNotMove()
{
	noMove[0] = bottle[0] + bottle[4] + bottle[8];
	noMove[1] = bottle[0] + bottle[5] + bottle[7];
	noMove[2] = bottle[1] + bottle[3] + bottle[8];
	noMove[3] = bottle[1] + bottle[5] + bottle[6];
	noMove[4] = bottle[2] + bottle[3] + bottle[7];
	noMove[5] = bottle[2] + bottle[4] + bottle[6];
}

int minimumMove()
{
	int MAX = 0;
	movedBottle = input();
	int minimum;
	setNotMove();
	for(int i = 0; i < 6; i++)
	{
		if(noMove[i] > MAX)
		{
			MAX = noMove[i];
			minimum = i;
		}
	}
    movedBottle -= MAX; 
    cout << endl;
	return minimum;
}

void output(int noMove)
{
	switch(noMove)
	{
		case 0:
			cout << "BGC ";
			break;
		case 1:
			cout << "BCG ";
			break;
		case 2:
			cout << "GBC ";
			break;
		case 3:
			cout << "GCB ";
			break;
		case 4:
			cout << "CBG ";
			break;
		case 5:
			cout << "CGB ";
	}
	cout << movedBottle << endl;
}

리팩토링 후

#include<iostream>
#include<string>
using namespace std; 

#define NumberOfSlots 9
#define NumberOfCases 6
#define NumberOfResultInformations 2
 
int* input(); 
int* setNotMove(int *aSlots); 
int* minimumMove(int *aSlots); 
void output(int *aInformation); 

bool isCorrectInput;

void main() 
{
    isCorrectInput = true;
    output(minimumMove(input())); 
} 
 
int* input() 
{
    int* slots = new int[NumberOfSlots + 1];
    int totalBottle = 0; 
    for(int i = 0; i < NumberOfSlots; i++) 
    { 
        if(!(cin >> slots[i])){
            isCorrectInput = false;
        }
        totalBottle += slots[i];  
    }
    char temp;
    cin.get(temp);
    if(temp != '\n')
        isCorrectInput = false;
    slots[9] = totalBottle;
    return slots; 
} 
 
int* setNotMove(int *aSlots) 
{ 
    int* noMove = new int[NumberOfCases];
    noMove[0] = aSlots[0] + aSlots[4] + aSlots[8]; 
    noMove[1] = aSlots[0] + aSlots[5] + aSlots[7]; 
    noMove[2] = aSlots[1] + aSlots[3] + aSlots[8]; 
    noMove[3] = aSlots[1] + aSlots[5] + aSlots[6]; 
    noMove[4] = aSlots[2] + aSlots[3] + aSlots[7]; 
    noMove[5] = aSlots[2] + aSlots[4] + aSlots[6];
    return noMove;
} 
 
int* minimumMove(int *aSlots) 
{	
    if(!isCorrectInput){
        delete aSlots;
        int *temp = new int;
        return temp;
    }
	
    int MAX = 0;  
    int minimumCase; 
    int* pNoMove = setNotMove(aSlots); 
    for(int i = 0; i < NumberOfCases; i++) 
    { 
        if(pNoMove[i] > MAX) 
        { 
	   MAX = pNoMove[i]; 
            minimumCase = i; 
        } 
    } 
    aSlots[9] -= MAX;
    int *resultInformation = new int[NumberOfResultInformations];
    resultInformation[0] = minimumCase;
    resultInformation[1] = aSlots[9];
    delete aSlots;
    delete pNoMove;
    return resultInformation; 
} 
 
void output(int *aInformation) 
{
    if(isCorrectInput)
    {
        string colorsCombination[] = {"BGC", "BCG", "GBC", "GCB", "CBG", "CGB"};
        cout << colorsCombination[aInformation[0]] << aInformation[1] << endl;
    }
    else
    {
        cout << "잘못된 입력으로 프로그램을 종료합니다.\n";
    }
    delete aInformation;
}

리팩토링의 목적과 결과

*전역변수 남발로 인한 공간(memory)낭비 없애기
*예외처리
*불분명한 변수명 변경(가독성)
*속도의 개선이 조금이나마 이루어짐 

import <string>

void output(int *aInformation) 
{
	if(isCorrectInput)
	{
		string colorsCombination[] = {"BGC", "BCG", "GBC", "GCB", "CBG", "CGB"};
		cout << colorsCombination[aInformation[0]] << aInformation[1] << endl;
	}
	else
	{
		cout << "잘못된 입력으로 프로그램을 종료합니다.\n";
	}
	delete aInformation;
}

output() 함수도 이런식으로 리펙토링할 수 있다. --재동

오~ 굿 어드바이스요 --강희경