Skip to content

Instantly share code, notes, and snippets.

@kkoziarski
Last active December 28, 2016 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkoziarski/ec07c505097a6b6fec16441dc26cc69c to your computer and use it in GitHub Desktop.
Save kkoziarski/ec07c505097a6b6fec16441dc26cc69c to your computer and use it in GitHub Desktop.
XmasTree C++
// 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;
}
*
***
*****
*******
*********
***********
#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;
}
}
#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