Skip to content

Instantly share code, notes, and snippets.

@anakaiti
Last active September 15, 2016 07:56
Show Gist options
  • Save anakaiti/8f940eaa2ec18c46ca432489ef5e3dd0 to your computer and use it in GitHub Desktop.
Save anakaiti/8f940eaa2ec18c46ca432489ef5e3dd0 to your computer and use it in GitHub Desktop.
Shape Printer
#include <iostream>
#include <stdlib.h>
char unit='#';
using namespace std;
int putunit(){
cout << unit;
}
int putwhitespace(){
cout << " ";
}
int putnewline(){
cout << endl;
}
int TrianglePrinter (int h){
int offset = h;
for(int i=1;i<=h;i++){
if(i==1){
for(int j=1;j<=offset;j++){
putwhitespace();
}
putunit();
} else if (i==h) {
for(int m=1;m<=offset;m++){
putwhitespace();
}
for(int k=1;k<=(i*2)-1;k++){
putunit();
}
} else {
for(int m=1;m<=offset;m++){
putwhitespace();
}
putunit();
for(int l=1;l<=(i*2)-3;l++){
putwhitespace();
}
putunit();
}
offset--;
putnewline();
}
putnewline();
cout << "Area: " << (h*2h/2 <<endl;
return 0;
}
int SquarePrinter(int w, int h){
for (int i=1;i<=h;i++){
if(i==1 || i==h){
for(int j=1;j<=w;j++){
putunit();
}
putnewline();
} else {
putunit();
for(int j=1;j<=(w-2);j++){
putwhitespace();
}
putunit();
putnewline();
}
}
putnewline();
cout << "Area: " << w*h <<endl;
return 0;
}
int ParallelogramPrinter(int w, int h){
int offset;
offset = h;
for (int i=1;i<=h;i++){
for(int j=1;j<=offset;j++){
putwhitespace();
}
if (i==1 || i==h){
for(int k=1;k<=w;k++){
putunit();
}
} else {
putunit();
for(int k=1;k<=(w-2);k++){
putwhitespace();
}
putunit();
}
offset--;
putnewline();
}
putnewline();
cout << "Area: " << w*h <<endl;
return 0;
}
int DiamondPrinter(int s){
int offset = (s-1)/2;
for (int i=1;i<=s;i++){
int absoffset=abs(offset);
for (int j=1;j<=absoffset;j++){
putwhitespace();
}
putunit();
if (i!=1 && i!=s){
for (int l=1;l<=s-2-(absoffset*2);l++){
putwhitespace();
}
putunit();
}
putnewline();
offset--;
}
putnewline();
cout << "Area: " << (s^2)/2 <<endl;
return 0;
}
int main(){
int menuselect;
int var1;
int var2;
cout << " _ _ _____ _ _ _____ ______ ____ ____ _____ _____ " << endl;
cout << " _| || |_ / ____| | | | /\\ | __ \\| ____| _ \\ / __ \\_ _|/ ____|" << endl;
cout << " |_ __ _| (___ | |__| | / \\ | |__) | |__ | |_) | | | || | | (___ " << endl;
cout << " _| || |_ \\___ \\| __ | / /\\ \\ | ___/| __| | _ <| | | || | \\___ \\ " << endl;
cout << " |_ __ _|____) | | | |/ ____ \\| | | |____| |_) | |__| || |_ ____) |" << endl;
cout << " |_||_| |_____/|_| |_/_/ \\_\\_| |______|____/ \\____/_____|_____/ " << endl;
putnewline();
while (true){
cout << "[1] Tri-angle [2] Rekt-angle [3] Jajar Genjang [4] Diamond" << endl;
cout << "Select Shape: ";
cin >> menuselect;
switch (menuselect){
case 1:
cout << "Input Height: ";
cin >> var1;
TrianglePrinter(var1);
break;
case 2:
cout << "Input Width: ";
cin >> var1;
cout << "Input Height: ";
cin >> var2;
SquarePrinter(var1,var2);
break;
case 3:
cout << "Input Width: ";
cin >> var1;
cout << "Input Height: ";
cin >> var2;
ParallelogramPrinter(var1,var2);
case 4:
while (true){
cout << "Input Width (needs to be odd!): ";
cin >> var1;
if ((var1%2)!=0){
break;
} else {
cout << var1 <<" Is not odd :(" << endl;
}
}
DiamondPrinter(var1);
break;
default:
cout << "Invalid Input!";
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment