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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
#pragma warning (disable:4996)
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define BLANK ' '
#define STAR '*'
/* Function prototypes
*/
// Function to prompt to obtain width
// Returns an integer width as read in using scanf
int getWidth(void);
// Function to draw top of a odd valued diamond
// Uses width to compute
void drawOddtop(int, char);
// Function to draw top of a even valued diamond
// Uses width to compute
void drawEventop(int, char);
// Function to draw bottom of a odd valued diamond
// Uses width to compute
void drawOddbottom(int, char);
// Function to draw bottom of a even valued diamond
// Uses width to compute
void drawEvenbottom(int, char);
// Function to display diamond
// Uses width and characters to display
void drawNChars(int, char);
int main()
{
int w;
char ch;
printf("Welcome to the Diamond Generator!\n");
w = getWidth();
printf("Width=%d\n\n", w);
drawOddtop(w, STAR);
drawEventop(w, STAR);
drawOddbottom(w, STAR);
drawEvenbottom(w, STAR);
printf("\nEnter any character to continue");
scanf("%c%c", &ch, &ch);
if ( w % 2 == 0 )
{
drawEventop(w, STAR);
drawEvenbottom(w, STAR);
}
else if ( w % 2 == 1 )
{
drawOddtop(w, STAR);
drawOddbottom(w, STAR);
}
else
{
printf ("Invalid.\n");
return (-1); //return non-zero on error
} // end multiple alternative if
return (0);
} // end Main
int getWidth()
{
int w;
int counter;
printf("Please enter a width with an odd number between 4 and 60\n");
scanf("%d", &w);
counter = 1;
while (w %2 == 0 || w < 4 || w > 60)
{
scanf("%d", &w);
counter = counter +1; ++counter;
if(counter == 5)
{
exit (0);
}
}// end while
return (w);
}
void drawOddtop(int w, char ch) //Function for diamond
{
int nlb; // represents numberof leading blanks
int ns;// represents number of star characters to print in each line
nlb = (w/2)-2;
ns = 1;
for (nlb=(w/2)-2; nlb>=0; --nlb)
{
for ( ns=1; ns>=0; --ns)
{
drawNChars(nlb,STAR);
drawNChars(ns,STAR);
drawNChars(1, '\n');
}
// end ns loop
} // end nlb loop
}
void drawEventop(int w, char ch) //Function for diamond
{
int nlb; // represents numberof leading blanks
int ns;// represents number of star characters to print in each line
nlb = (w/2)-2;
ns = 2;
for (nlb=(w/2)-2; nlb>=0; --nlb)
{
for ( ns=1; ns>=0; --ns)
{
drawNChars(nlb,STAR);
drawNChars(ns,STAR);
drawNChars(1, '\n');
}
// end ns loop
} // end nlb loop
}
void drawOddbottom(int w, char ch) //Function for diamond
{
int nlb; // represents numberof leading blanks
int ns;// represents number of star characters to print in each line
nlb = (w/2)-2;
ns = 1;
for (nlb=(w/2)-2; nlb>=0; --nlb)
{
for ( ns=1; ns>=0; --ns)
{
drawNChars(nlb,STAR);
drawNChars(ns,STAR);
drawNChars(1, '\n');
}
// end ns loop
} // end nlb loop
}
void drawEvenbottom(int w, char ch) //Function for diamond
{
int nlb; // represents numberof leading blanks
int ns;// represents number of star characters to print in each line
nlb = (w/2)-2;
ns = 2;
for (nlb=(w/2)-2; nlb>=0; --nlb)
{
for ( ns=1; ns>=0; --ns)
{
drawNChars(nlb,STAR);
drawNChars(ns,STAR);
drawNChars(1, '\n');
}
// end ns loop
} // end nlb loop
}
// Function to display a character (ch) n times on one line
void drawNChars(int n, char ch)
{
int k;
if (n < 0)
{
printf("\nIn drawNChars, n < 0. Invalid. Terminating program.\n");
exit(0);
} //end if
for (k = 1; k <= n; ++k)
{
printf("%c", ch);
} // end for
} // end drawNChars
|