Design a C++ program that will draw a series of geometric objects. Each drawing
ID: 3631479 • Letter: D
Question
Design a C++ program that will draw a series of geometric objects. Each drawing must be appropriately labeled. The program must be designed to run in batch mode using Linux redirection. (Do not prompt for input.) The list of objects to be drawn is provided below. If the object code is invalid (not L, R, or T) the program should display an error message that includes the code and states that it was invalid. The program should continue processing data until the end of file is encountered.
The objects the program should be able to draw are:
Line - The data set for drawing a line will consist of 4 values: an object code (L), a direction code will indicate whether the line should be horizontal (H), vertical (V) or diagonal (D), the length of the line will be an integer, and the character used to draw the line. The diagonal line MUST be oriented as shown in the example.
sample data set: L H 10 z
Horizontal Line
zzzzzzzzzz
sample data set: L V 3 P
Vertical Line
P
P
P
sample data set: L D 4 #
Diagonal Line
#
#
#
#
Rectangle - The data set for drawing a rectangle will consist of 5 values: an object code (R ), an integer representing the length (vertical size) of the rectangle, an integer representing the width (horizontal size) of the rectangle, the character used to draw the outside of the rectangle, and the character used to fill the inside of the rectangle.Note: if the length and/or width is < 3, the fill character will not be part of the drawing.
sample data set: R 4 3 $ +
Rectangle
$$$
$+$
$+$
$$$
Triangle - The data set for drawing a triangle will begin with an object code (T) followed by a triangle type (E for equilateral, R for right). If the type is E, an integer representing the height of the triangle, the character used to draw the outside of the triangle, and the character used to fill the inside of the triangle will be given. If the type is R, an integer representing the height of the triangle and a character used to draw the triangle will be given. The drawing MUST be oriented as shown in the examples.
sample data set: T E 4 & #
Equilateral Triangle
&
&#&
&###&
&&&&&&&
sample data set: T R 5 O
Right Triangle
OOOOO
OOOO
OOO
OO
O
Input File Assumptions
The input for the program will be stored in a data file.
Each line of the file will start with a character.
If the character at the start of the line not a valid object code, there WILL NOT be anything else on the line. Only capital letters should be accepted for object codes.
If the character at the start of the line is a valid object code, then the remaining input values will be present, in the correct order, valid, and separated by at least 1 blank space.
Line direction and triangle types will be capital letters. (No error checking is necessary.)
All lengths, widths, and heights in the file will be between 1 and 50, inclusive.
Program Requirements
Display your name, section #, and assignment # before the first object is drawn.
Include a label above each drawing that indicates the type of figure drawn.
Leave at least 1 blank line between each drawing.
The only "error" the program must check for is an invalid object code.
No string type variables or arrays can be used in the program. (Automatic 60% of total point value deduction.)
Sample terminal session:
[lee@bobby keys_fa11]$ more hw07data
L H 13 $
t
L V 3 +
L D 5 @
R 4 13 $ +
&
T R 5 Z
T E 6 O I
[lee@bobby keys_fa11]$ g++ hw07.cpp
[lee@bobby keys_fa11]$ ./a.out < hw07data
Lee Misch Assignment #7 Section #100_
Horizontal Line
$$$$$$$$$$$$$
t is not a valid object code
Vertical Line
+
+
+
Diagonal Line
@
@
@
@
@
Rectangle
$$$$$$$$$$$$$
$+++++++++++$
$+++++++++++$
$$$$$$$$$$$$$
& is not a valid object code
Right Triangle
ZZZZZ
ZZZZ
ZZZ
ZZ
Z
Equilateral Triangle
O
OIO
OIIIO
OIIIIIO
OIIIIIIIO
OOOOOOOOOOO
Explanation / Answer
as per your message - if any problems please message me before rating - thanks
Please rate - thanks
#include <iostream>
using namespace std;
void line();
void horizontal(int,char);
void vertical(int,char);
void diagonal(int,char);
void equilateral();
void right();
void rectangle();
void triangle();
int main()
{char type;
cin>>type;
while(cin)
{switch(type)
{case 'L': line(); break;
case 'R': rectangle(); break;
case 'T': triangle(); break;
default: cout<<type<<" is not a valid object code ";
}
cin>>type;
}
return 0;
}
void horizontal(int n,char c)
{int i;
for(i=0;i<n;i++)
cout<<c;
cout<<endl<<endl;
}
void vertical(int n,char c)
{int i;
for(i=0;i<n;i++)
cout<<c<<endl;
cout<<endl;
}
void diagonal(int n,char c)
{int i,j;
for(i=0;i<n;i++)
{for(j=0;j<i;j++)
cout<<" ";
cout<<c<<endl;
}
cout<<endl;
}
void line()
{char dir,c;
int n,i,j;
cin>>dir>>n>>c;
if(dir=='H')
horizontal(n,c);
else if(dir=='V')
vertical(n,c);
else
diagonal(n,c);
}
void rectangle()
{char code, out,in;
int i,j,length,width;
cin>>length>>width>>out>>in;
for(i=0;i<width;i++)
cout<<out;
cout<<endl;
for(i=0;i<length-2;i++)
{cout<<out;
for(j=0;j<width-2;j++)
cout<<in;
cout<<out<<endl;
}
for(i=0;i<width;i++)
cout<<out;
cout<<endl<<endl;
}
void triangle()
{char code;
cin>>code;
if(code=='E')
equilateral();
else
right();
}
void equilateral()
{int i,j,k,n,l=1;
char out,in;
cin>>n>>out>>in;
l=n-1;
for(i=0;i<l;i++)
cout<<" ";
cout<<out<<endl;
for(i=n-2;i>0;i--)
{l--;
for(k=0;k<l;k++)
cout<<" ";
cout<<out;
for(j=0;j<(n-i-1)*2-1;j++)
cout<<in;
cout<<out<<endl;
}
for(i=0;i<n*2-1;i++)
cout<<out;
cout<<endl<<endl;
}
void right()
{int i,j,n;
char c;
cin>>n>>c;
for(i=0;i<n;i++)
{for(j=0;j<n-i;j++)
cout<<c;
cout<<endl;
}
cout<<endl;
}