Skip to content

Instantly share code, notes, and snippets.

@nicksinch
Created May 8, 2019 06:09
Show Gist options
  • Select an option

  • Save nicksinch/38fb3282493291ff3694e1cc9aef5746 to your computer and use it in GitHub Desktop.

Select an option

Save nicksinch/38fb3282493291ff3694e1cc9aef5746 to your computer and use it in GitHub Desktop.
#include "Logo.h"
#include <string>
#include <iostream>
Logo::Logo(unsigned n):num(n),numDashes(n),numStars(n)
{
char dash = '-';
char star = '*';
unsigned totalLines = num + 1; //Total lines of the logo are always n + 1
/*
We see that every line of the first part of the logo
consists of 3 times n*dashes and 2 times n*stars,
so since the whole line consists of two parts( two Ms ), we multiply
this by two and on every line there will always be 2 * 3 times n * dashes and
2 * 2 times n * stars, so we always have to print 6 times dashes and 4 times stars
*/
//start of first part
unsigned wholeLineDashes = 6;
unsigned wholeLineStars = 4;
unsigned innerDashes = n; //on the first half(part) the pattern follows that inner dashes always start n
/*
We break our logo into two parts:
1st part is the first (totalLines / 2) lines
2nd part is the other half
*/
for (unsigned i = 0; i < totalLines / 2; i++)
{
while(wholeLineDashes > 0)
{
if (wholeLineStars != 3 && wholeLineStars != 1)
{
printNTimes(dash, numDashes);
wholeLineDashes--;
}
if (wholeLineDashes != 3 && wholeLineStars > 0) //we check if we have reached the middle of the line ( the end of first M )
{
printNTimes(star, numStars);
wholeLineStars--;
if (wholeLineStars == 3 || wholeLineStars == 1) //we print the inner dashes always after 1st stars and after 3rd stars
{
printNTimes(dash, innerDashes);
wholeLineDashes--;
}
}
}
numStars += 2; //the pattern of M says that on the first half we increment stars by 2
numDashes -= 1; // decrement dashes by 1
innerDashes -= 2; //and decrement inner dashes by 2
wholeLineDashes = 6; //reseting value
wholeLineStars = 4; //reseting value
std::cout << std::endl;
}
//end of first part
//start of second part
//setting the initial values that the pattern follows for the second half (part)
numStars -= 2; //resetting to the initial value of numStars
wholeLineDashes = 8;
wholeLineStars = 6;
innerDashes = 1; //in the patters on the second half , inner dashes always start with 1
for (unsigned i = 0; i < totalLines / 2; i++)
{
unsigned twoMs = 2;//we want the next loop(while) to execute two times, one time for each M in the logo,
// and since there are two M's , we initialize this variable with 2
while (twoMs)
{
//This is the pattern for the second part of the logo(lines after totalLines / 2 lines) for the first M
//and this while loop should iterate 2 times since there are two identical M's in the logo
printNTimes(dash, numDashes);
printNTimes(star, n);
printNTimes(dash, innerDashes);
printNTimes(star, numStars);
printNTimes(dash, innerDashes);
printNTimes(star, n);
printNTimes(dash, numDashes);
twoMs--;
}
numDashes--;
innerDashes += 2;
numStars -= 2;
std::cout << std::endl;
}
}
void Logo::printNTimes(char c, int n)
{
// character c will be printed n times
std::cout << std::string(n, c);
}
#pragma once
class Logo
{
private:
unsigned num; //This is initialized to N the user enters
unsigned numDashes;
unsigned numStars;
public:
Logo(unsigned); //constructor
void printNTimes(char,int); //prints a charater N times. We will use this to print dashes and stars N times
};
#include <iostream>
#include "Logo.h"
using namespace std;
int main()
{
cout << "Enter N: " << endl;
unsigned n = 0;
cin >> n;
while (n <= 2 || n >= 10000 || n % 2 == 0)
{
cout << "Incorrect digit!\nN must be at least 3, no bigger than 10 000 and an odd number. Re enter N: " << endl;
cin >> n;
}
Logo l(n);
system("PAUSE");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment