Last active
December 28, 2016 19:30
-
-
Save kkoziarski/ec07c505097a6b6fec16441dc26cc69c to your computer and use it in GitHub Desktop.
XmasTree C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CPPXmasTree.cpp : Defines the entry point for the console application. | |
// | |
#include "stdafx.h" | |
#include <iostream> | |
#include <ostream> | |
#include <string> | |
#include "XmasTree.h" | |
inline void printHelloWorld(); | |
int main(int argc, char* argv[]) | |
{ | |
//printHelloWorld(); | |
std::wcout << L"Enter odd number (tree's bottom length): "; | |
std::wstring userInput; | |
std::wcin >> userInput; | |
int length = std::stoi(userInput); | |
XmasTree tree = XmasTree(); | |
tree.drawTree(length); | |
system("pause"); | |
return 0; | |
} | |
void printHelloWorld() | |
{ | |
std::wstring helloWorld; | |
helloWorld = L"hello, it's me"; | |
std::wcout << L"Please input an string and then press Enter: "; | |
std::wstring noun; | |
std::wcin >> noun; | |
std::wcout << L"You entered '" << noun << L"'." << std::endl; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* | |
*** | |
***** | |
******* | |
********* | |
*********** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
#include <iostream> | |
#include "XmasTree.h" | |
XmasTree::XmasTree() : m_length(7) | |
{ | |
} | |
XmasTree::XmasTree(int length) : m_length(length) | |
{ | |
} | |
XmasTree::~XmasTree() | |
{ | |
} | |
void XmasTree::setLength(int length) | |
{ | |
m_length = length; | |
} | |
void XmasTree::drawTree(int length) | |
{ | |
std::wcout << L"Three length is: " << length << L" " << std::endl; | |
std::wcout << L"Loops tree:" << std::endl; | |
std::wcout << std::endl; | |
loopsTree(length); | |
std::wcout << L"Recursive tree:" << std::endl; | |
recursiveTree(length); | |
} | |
void XmasTree::drawTree() | |
{ | |
drawTree(m_length); | |
} | |
void XmasTree::loopsTree(int length) | |
{ | |
int half = length / 2; | |
for (int row = 0; row < half + 1; row++) | |
{ | |
print((half - row), L" "); | |
print((row * 2 + 1), L"*"); | |
std::wcout << std::endl; | |
} | |
} | |
void XmasTree::recursiveTree(int length, int stars) | |
{ | |
int howManyStars = stars; | |
int howManySpaces = (length - stars) / 2; | |
print(howManySpaces, L" "); | |
print(howManyStars, L"*"); | |
std::wcout << std::endl; | |
if (stars < length) | |
{ | |
recursiveTree(length, stars + 2); | |
} | |
} | |
void XmasTree::print(int howMany, const wchar_t * chars) | |
{ | |
for (int i = 0; i < howMany; i++) | |
{ | |
std::wcout << chars; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
class XmasTree | |
{ | |
public: | |
XmasTree(); | |
XmasTree(int length); | |
~XmasTree(); | |
void setLength(int length); | |
void drawTree(int length); | |
void drawTree(); | |
private: | |
void loopsTree(int length); | |
void recursiveTree(int length, int stars = 1); | |
void print(int howMany, const wchar_t* chars); | |
int m_length; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment