Program c Segitiga Alphabet

#include <stdio.h>
#include <windows.h>

#define MAX 25
#define ALPHA 26
#define HORIZONTAL 0
#define VERTICAL 1

void FillBuffer(char*, int, int);
void DisplayBuffer(const char*, int, int);
void FlipBuffer(char*, int, int, int);
void Delay(void);

int main(int argc, char *argv[]) {
char buffer[MAX][MAX];
int max;
printf(“-> Tinggi : “);
scanf(“%d”,&max);
if((max > 0) && (max <= 25)) {
FillBuffer((char*)buffer,max,max);
printf(“-> Pertama :\n”);
DisplayBuffer((char*)buffer,max,max);
FlipBuffer((char*)buffer,max,max,HORIZONTAL);
printf(“\n-> Kedua :\n”);
DisplayBuffer((char*)buffer,max,max);
} else {
printf(“\n-> I am Sorry!\n”);
}
system(“PAUSE”);
return 0;
}

void FillBuffer(char* buffer, int rows, int cols) {
int row, col, idx = 0;
memset(buffer,’ ‘,rows*cols);
for(row = 0; row < rows; ++row) {
for(col = 0; col < row+1; ++col) {
*(buffer+cols*row+col) = ‘a’+idx;
idx = (idx+1)%ALPHA;
}
}
}

void DisplayBuffer(const char* buffer, int rows, int cols) {
int row, col;
for(row = 0; row < rows; ++row) {
for(col = 0; col < cols; ++col) {
printf(“%2c”,*(buffer+cols*row+col));
Delay();
}
printf(“\n”);
}
}

void FlipBuffer(char* buffer, int rows, int cols, int direction) {
switch(direction) {
case HORIZONTAL : {
int row;
for(row = 0; row < rows; ++row) {
int begin = 0, end = cols-1;
while(begin < end) {
int idx = cols*row;
char tmp = *(buffer+idx+begin);
*(buffer+idx+begin) = *(buffer+idx+end);
*(buffer+idx+end) = tmp;
++begin;
–end;
}
}
} break;

case VERTICAL : {
int col;
for(col = 0; col < cols; ++col) {
int begin = 0, end = rows-1;
while(begin < end) {
char tmp = *(buffer+rows*+col+begin);
*(buffer+rows*+col+begin) = *(buffer+rows*+col+end);
*(buffer+rows*+col+end) = tmp;
++begin;
–end;
}
}
} break;

default :
break;
}
}

void Delay(void) {
int i = 0;
while(i < 10000000) {
++i;
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: