U E D R , A S I H C RSS

Medusa Cpp Study/석우

MedusaCppStudy 석우 숙제

  • 정사각형,정삼각형

~cpp 
#include <iostream> 
 
using std::cout; 
using std::endl; 
using std::cin; 
 
int square(); 
int triangle(); 
 
int main() 
{ 
    square(); 
 
    cout << endl << endl;

    triangle(); 
 
    return 0; 
} 
 
//정사각형 그리기 
int square() 
{
    cout << "정사각형 한변의 길이를 입력하세요: "; 
	
    int length;
    cin >> length;
 
    for (int rows = 0 ; rows < length ; rows++) 
    { 
 
        for (int cols = 0 ; cols < length ; cols++) 
        { 
            if (rows == 0 || rows == length - 1 || cols == 0 || cols == length - 1) 
            { 
                cout << "* "; 
            } 
            else 
            { 
                cout << "  "; 
            } 
        } 
        cout << endl; 
    } 
 
    return 0; 
} 
 
//정삼각형 그리기 
int triangle() 
{
    cout << "정삼각형 한변의 길이를 입력하세요: "; 
 
    int side;
    cin >> side; 
 
    for (int r = 0 ; r < side ; r++) 
    { 
        for (int c = 0 ; c < 2 * side ; c++) 
        { 
            if (c == side - r - 1 || c == side + r - 1 || (r == side - 1 && c % 2 == 0)) 
            { 
                cout << "*"; 
            } 
            else 
            { 
                cout << " "; 
            } 
        } 
        cout << endl; 
    } 
 
    return 0; 
}
  • 가장 큰 수 4개

~cpp 
#include <algorithm>
#include <iostream>
#include <vector>

using std::cin;                 using std::sort;
using std::cout;                using std::vector;
using std::endl;                

int maxnumber();

int main()
{
    maxnumber();

    return 0;
}

//가장 큰 숫자 4개표시하기
int maxnumber()
{
    cout << "숫자들을 입력하세요: ";

    int number;
    vector<int> numbers;

    while (cin >> number)
    {
        numbers.push_back(number);
    }

    sort(numbers.begin(),numbers.end());

    if (numbers.size() >= 4)
    {
        for (int i = 4 ; i > 0 ; i--)
        {
            cout << numbers[numbers.size() - i] << "  ";
        }
    }
    else
    {
        for (int j = numbers.size() ; j > 0 ; j--)
        {
            cout << numbers[numbers.size() - j] << "  ";
        }
    }

    cout << endl;

    return 0;
}


  • 가장 긴 string과 가장 짧은 string 길이 출력

~cpp 
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using std::cin;                 using std::sort;
using std::cout;                using std::string;
using std::endl;                using std::vector;

int stringlength();

int main()
{
    stringlength();

    return 0;
}

//가장 긴 stirng과 가장 짧은 string의 길이 구하기
int stringlength()
{
    cout << "문자를 입력하세요: ";

    string word;
    vector<int> length;

    while (cin >> word)
    {
        if (word == ";")
        {
            break;
        }
        else
        {
            length.push_back(word.size());
        }
    }

    sort(length.begin(), length.end());

    cout << "가장 짧은 string: " << length[0] << endl;
    cout << "가장 긴   string: " << length[length.size() - 1] << endl;

    return 0;
}
  • 마방진

~cpp 
#include <iostream> 
#include <vector> 
 
using namespace std; 

int Input();
void Printnumber(vector< vector<int> >& board);
void Showboard(const vector< vector<int> >& board);
 
int main() 
{ 
        cout << "숫자를 입력하세요: "; 
 
        int size = Input();
 
        vector< vector<int> > board(size); 
        for (int i = 0 ; i < size ; i++) 
                board[i].resize(size); 
         
        Printnumber(board);
        Showboard(board); 
 
        return 0; 
}

int Input()
{
	int size;
	cin >> size;
	
	return size;
}

void Printnumber(vector< vector<int> >& board)
{
        int r = 0 , c = board.size() / 2; 
        board[r][c] = 1; 
        for (int count = 2 ; count < board.size() * board.size() + 1 ; count++) 
        { 
                r--; 
                c++; 
                 
                if (r < 0 && c > board.size() - 1) 
                { 
                        r = 1; 
                        c = board.size() - 1; 
                } 
                else 
                { 
                        if (r < 0) 
                                r = board.size() - 1; 
                        if (c > board.size() -1) 
                                c = 0; 
                } 
                 
                if (board[r][c] != 0) 
                { 
                        r += 2; 
                        c--; 
                } 
 
                board[r][c] = count; 
        }
}

void Showboard(const vector< vector<int> >& board) 
{ 
        for(int row = 0; row < board.size(); row++)  
        {  
                for(int col = 0; col < board.size(); col++)  
                {  
                        cout << board[row][col] << "\t";  
                }  
                cout << endl;  
        } 
         
} 
  • Randomwalk

~cpp 
#include <ctime>
#include <iostream>
#include <vector>

using namespace std;

struct  Roachs
{
	int row;
	int col;
	int count;
};

void Input(int& rows, int& cols, Roachs& roach);
void move(vector< vector<int> >& board, Roachs& roach);
void print(const vector< vector<int> >& board, const Roachs& roach);

int main()
{
	srand((unsigned)time(NULL)); 
	
	int rows, cols;
	Roachs roach;

	Input(rows, cols, roach);

	vector< vector<int> > board(rows);
	for (int i = 0 ; i < rows ; i++)
		board[i].resize(cols);

	move(board, roach);
	
	print(board,roach);

	return 0;
}

void Input(int& rows, int& cols, Roachs& roach)
{
	cout << "보드의 사이즈를 입력하세요(가로, 세로): ";
	cin >> rows >> cols;

	cout << "바퀴벌레의 처음 좌표를 입력하세요(가로, 세로): ";
	cin >> roach.row >> roach.col;
	
}

void move(vector< vector<int> >& board, Roachs& roach)
{
	roach.count = 0;
	board[roach.row][roach.col]++;
	
	for (int i = 0 ; i < board.size() ; i++)
	{
		for (int j = 0 ; j < board[0].size() ; j++)
		{
			while (board[i][j] == 0)
			{
				int r[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
				int c[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
				
				int direction = rand() % 8;
				
				roach.row += r[direction];
				roach.col += c[direction];
				
				if ((roach.row >=0 && roach.row <= board.size() - 1) 
					&& (roach.col >=0 && roach.col <= board[0].size() - 1))
				{
					board[roach.row][roach.col]++;
					roach.count++;
				}
				else
				{
					roach.row -= r[direction];
					roach.col -= c[direction];
				}
			}
		}
	}
}

void print(const vector< vector<int> >& board, const Roachs& roach)
{
	for (int i = 0 ; i < board.size(); i++)
	{
		for (int j = 0 ; j < board[0].size() ; j++)
		{
			cout << board[i][j] << "\t";
		}
		cout << endl;
	}

	cout << "총 움직인 횟수는 " << roach.count << "입니다." << endl;
}
  • string 길이 출력

~cpp 
#include <algorithm> 
#include <IOSTREAM> 
#include <STRING> 
#include <VECTOR> 
 
using namespace std; 
 
struct Words 
{ 
        string name; 
        int count; 
}; 
 
void calculation(vector<Words>& sentence, Words& word); 
void deleteperiod(Words& word);
void changeinsmall(Words& word);
bool compare(const Words& x, const Words& y); 
void Print(const vector<Words>& sentence); 
 
int main() 
{ 
        cout << "어디 한번 문장을 입력해보게나(마지막엔 ';'를 입력해야하네) " << endl << ">>"; 
        Words word; 
        vector<Words> sentence; 
 
        calculation(sentence, word); 
        sort(sentence.begin(), sentence.end(), compare); 
        Print(sentence); 
 
        return 0; 
} 
 
void calculation(vector<Words>& sentence, Words& word) 
{ 
        word.count = 1; 
        while (cin >> word.name) 
        { 
                if (word.name == ";")  
                { 
                        break; 
                } 
 
                deleteperiod(word); 
	        changeinsmall(word);

                sentence.push_back(word); 
 
                for (int i = 0 ; i < sentence.size() - 1 ; i++) 
                { 
                        if (sentence[i].name == word.name)  
                        { 
                                sentence[i].count++; 
                                sentence.pop_back(); 
                        } 
                } 
        } 
} 
 
void deleteperiod(Words& word) 
{ 
        if (word.name[word.name.size() - 1] == '.') 
        { 
                word.name.resize(word.name.size() - 1); 
        } 
}

void changeinsmall(Words& word)
{
	for(string::size_type i = 0; i < word.name.size(); i++) 
		strlwr(&word.name[i]); 
}
 
bool compare(const Words& x, const Words& y) 
{ 
        return x.name < y.name; 
} 
 
void Print(const vector<Words>& sentence) 
{ 
        for (int j = 0 ; j < sentence.size() ; j++) 
        { 
                cout << sentence[j].name << "\t" << sentence[j].count << endl; 
        } 
        cout << endl << "총 단어수: " << sentence.size() << endl; 
}
  • Vending machine

~cpp 
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>

using namespace std;

struct Drinks
{
	string name;
	int price;
	int amount;
};

const string drinks[] = {"sprite", "cold tea", "hot tea", "tejava", "cold milk", "hot milk"};
const price[] = {400, 500, 500, 500, 600, 600};

void initialize(Drinks& d, vector<Drinks>& vec);
void Command(const string& command, int& won, vector<Drinks>& vec);
void put(int& won);
void choose(int& won, vector<Drinks>& vec);
void vend(int& won, vector<Drinks>& vec, const vector<Drinks>::size_type& i);
void draw(const int& won);
string Input();

int main()
{
	Drinks d;
	string command;
	int won = 0;
	vector<Drinks> vec;

	initialize(d, vec);

	while(cout << ">>", cin >> command)
	{
		if (command == "exit")
			break;
		
		else
		{
			try
			{
				Command(command, won, vec);			
			}
			catch (domain_error e)
			{
				cout << e.what() << endl;
			}
		}
	}	

	return 0;
}

void initialize(Drinks& d, vector<Drinks>& vec)
{
	vec.resize(6);
	for (vector<Drinks>::size_type i = 0 ; i < vec.size() ; i++)
	{
		vec[i].name = drinks[i];
		vec[i].price = price[i];
		vec[i].amount = 3;
	}
}

void Command(const string& command, int& won, vector<Drinks>& vec)
{

	if (command == "put")
		put(won);

	else if (command == "choose")
		choose(won, vec);
	
	else if (command == "draw")
		draw(won);
	
	else
		throw domain_error("incorrect command!");

}

void put(int& won)
{
	int money;

	cin >> money;
	if (money == 50 || money == 100 || money == 500 || money == 1000)
		won += money;
	else
		throw domain_error("incorrect money!");

	cout << "Money: " << won << endl;
}

void choose(int& won, vector<Drinks>& vec)
{
	string drink = Input();

	for (vector<Drinks>::size_type i = 0 ; i < vec.size() ; i++)
	{
		if (drink == vec[i].name)
		{
			if (vec[i].amount != 0)
			{
				if (won >= vec[i].price)
				{
					vend(won, vec, i);
					return;
				}

				else
					throw domain_error("Not enough money");
			}

			else 
				throw domain_error("Run out of " + vec[i].name + "!");
		}
		if (i == 5 && drink != vec[i].name)
			throw domain_error("We didn't have the drink.");
	}
}

void draw(const int& won)
{
	cout << "Change: " << won << endl; 
}

string Input()
{
	string t, d;

	cin >> t;

	if (t == "cold" || t == "hot")
	{
		cin >> d;

		if (d == "tea" || d == "milk")
		{
			t += " " + d;
			return t;
		}
		else
			throw domain_error("We don't have the drink.");
	}
	else
		return t;
}

void vend(int& won, vector<Drinks>& vec, const vector<Drinks>::size_type& i)
{
	won -= vec[i].price;
	vec[i].amount--;
	
	cout << "come out " << vec[i].name << "\t" << "rest number: "
		<< vec[i].amount << endl << "money: " << won << endl;
}

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:45
Processing time 0.0199 sec