MedusaCppStudy 재동

렌덤워크
~cpp 
#include <iostream>
#include <vector>
#include <ctime>
#include <stdexcept>

using namespace std;

const int DIRECTION_ROW[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int DIRECTION_COL[8] = {0, 1, 1, 1, 0, -1, -1, -1};

struct sRoach
{
	int curRow;
	int curCol;
	int count;
};

void showBoard(const vector< vector<int> >& board);
bool isAllBoard(const vector< vector<int> >& board);
void moveRoach(vector< vector<int> >& board, sRoach& roach);
void inputRoachPosition(sRoach& roach, const vector< vector<int> >& board);

int main()
{
	srand((unsigned)time(NULL));
         
	cout<< "Enter board size(ex: row, col): ";
	int rows, cols;
	cin >> rows >> cols;

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

	try
	{
		inputRoachPosition(roach, board);
	}
	catch(domain_error)
	{
		cout << "You must correct roach position!" << endl;
		return 1;
	}
	
	while(!isAllBoard(board))
	{
		moveRoach(board, roach);
	}
	
	showBoard(board);
	cout << "count of roach: " << roach.count << endl;

	return 0;
}

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

bool isAllBoard(const vector< vector<int> >& board)
{
	for(int i = 0; i < board.size(); i++)
	{
		for(int j = 0; j < board[i].size() ; j++)
		{
			if(board[i][j] == 0)
				return false;
		}
	}

	return true;
}

void moveRoach(vector< vector<int> >& board, sRoach& roach)
{
	int dir = rand() % 8;

	roach.curRow += DIRECTION_ROW[dir];
	roach.curCol += DIRECTION_COL[dir];
	
	if((roach.curRow >= 0 && roach.curRow <= board.size() - 1) 
		&& (roach.curCol >= 0 && roach.curCol <= board[0].size() - 1))
	{
		board[roach.curRow][roach.curCol]++;
		roach.count++;
	}
	else
	{
		roach.curRow -= DIRECTION_ROW[dir];
		roach.curCol -= DIRECTION_COL[dir];
	}
}

void inputRoachPosition(sRoach& roach, const vector< vector<int> >& board)
{
	cout << "Enter roach initil position(ex: row, col): ";
	cin >> roach.curRow >> roach.curCol;

	if((roach.curRow < 0 || roach.curRow > board.size())
		|| (roach.curCol < 0 || roach.curCol > board[0].size()))
		throw domain_error("Roach position is worng");
}


단어 빈도수
~cpp 
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

struct sWord
{
	string name;
	int count;
};

bool compare(const sWord& x, const sWord &y);
void showResult(const vector<sWord>& words);
void checkInWords(vector<sWord>& words, const string& x);
void reform(string& x);
void addInWords(vector<sWord>& words, const string& x);

int main()
{
	vector<sWord> words;

	cout << ">> ";
	string x;
	while(cin >> x)
	{
		if(x == ";")
			break;

		reform(x);
		checkInWords(words, x);
	}
	sort(words.begin(), words.end(), compare);
	showResult(words);

	return 0;
}

bool compare(const sWord& x, const sWord &y)
{
	return x.name < y.name;
}

void showResult(const vector<sWord>& words)
{
	for(vector<sWord>::size_type i = 0; i < words.size(); i++)
	{
		cout << words[i].name << "\t" << words[i].count << endl;
	}
	cout << "total words: " << words.size() << endl;
}

void checkInWords(vector<sWord>& words, const string& x)
{
	for(vector<sWord>::size_type i = 0; i < words.size(); i++)
	{
		if(words[i].name == x)
		{
			words[i].count++;
			return;
		}
	}
	addInWords(words, x);
}

void reform(string& x)
{
	for(string::size_type i = 0; i < x.size(); i++)
		strlwr(&x[i]);

	char lastChar = x[x.size() - 1];
	if(lastChar == '.' || lastChar == ',')
		x.resize(x.size() - 1);
}

void addInWords(vector<sWord>& words, const string& x)
{
	sWord newWord;
	newWord.name = x;
	newWord.count = 1;

	words.push_back(newWord);
}

Retrieved from http://wiki.zeropage.org/wiki.php/MedusaCppStudy/재동
last modified 2021-02-07 05:23:45