1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
#include <iostream>
using namespace std;
void Right(int height, char mark);
void Isosceles(int height, char mark);
void Left(int height, char mark);
void drawBar(int length, char mark);
void Menu(int height, char mark, int selection);
/*************************************************
* Name: Menu() *
* Type: void *
* Parameters: none *
* Purpose: creates a menu that prompts the user
for a height and triangle type
then calls the apropriate function and passes the height parameter
*************************************************/
void Menu(int height, char mark, int selection)
{
cout << "What is the height of the triangle? ";
cin >> height;
cout << endl;
cout << "What character do you want to use?" ;
cin >> mark;
cout << endl;
cout << "What type of triangle would you like to draw? (Enter a number)" << endl;
cout << "1) Right Aligned" << endl << "2) Isosceles" << endl << "3) Left Aligned" << endl;
cin >> selection;
if (selection <= 3) {
switch(selection)
{
case 1:
Right(height,mark);
break;
case 2:
Isosceles(height,mark);
break;
case 3:
Left(height,mark);
break;
}
}
else if (selection > 3)
{ cout << "The number you selected was invalid. Please try again." ; }
}
/*************************************************
* Name: Left *
* Type: void *
* Parameters: int height – height of triangle *
* height must be >=0 *
* *
* Purpose: creates a left-aligned triangle *
based on the height parameter *
*************************************************/
void Left(int height, char mark)
{
for (int i = 1; i <= height; i++)
{
drawBar(i,mark);
}
}
/*************************************************
* Name:Isosceles *
* Type: void *
* Parameters: int height – height of triangle *
* height must be >=0 *
* *
* Purpose: creates a center-aligned triangle *
based on the height parameter *
*************************************************/
void Isosceles(int height, char mark)
{
for (int i = 1; i <= height; i++)
{
for (int j = 0; j < height - i; j++)
{
cout << " ";
}
drawBar((i * 2) - 1,mark);
}
}
/*************************************************
* Name: Right *
* Type: void *
* Parameters: int height – height of triangle *
* height must be >=0 *
* *
* Purpose: creates a right-aligned triangle *
based on the height parameter *
*************************************************/
void Right(int height, char mark)
{
for (int i = 1; i <= height; i++)
{
for (int j = 1; j <= height - i; j++)
{
cout << " ";
}
drawBar(i, mark);
}
}
/*************************************************
* Name: drawBar *
* Type: void *
* Parameters: int length – the length of the bar *
* length must be >=0 *
* char mark – the symbol for drawing *
* Purpose: Displays a bar of length specified *
* using the specified in mark to draw *
* it, followed by a newline. *
*************************************************/
void drawBar(int length, char mark)
{
for(int i=0; i<length; i++)
cout<<mark;
cout<<endl;
}
int main()
{
int height=0;
char mark = 0;
int selection=0;
do{
Menu(height,mark,selection);
system("pause");
system("cls");
}
while (selection != 0);
}
|