Programming/C
HW 5
hannahchun
2022. 7. 3. 21:09
Problem 1
Flying Characters
1. Compile and run the following code.
* Put Console.c and Console.h into the current directory, and run the following commands:
gcc [file name].c Console.c -D_WINDOWS // on Windows
gcc [file name].c Console.c -D_MAC // on MacOS
2. Complete MoveChar() so that it can run as the video below
skeleton code
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "Console.h"
#define FROM_TOP 0
#define FROM_RIGHT 1
#define FROM_BOTTOM 2
#define FROM_LEFT 3
void MoveChar(char c, int tx, int ty, int screen_width, int screen_height, int dir);
int main()
{
int screen_width = getWindowWidth(); // get width of current console window
int screen_height = getWindowHeight() - 3; // get height of current console window
int cx = screen_width / 2, cy = screen_height / 2; // set initial coordinate by center of the screen
char text[512] = "";
clrscr();
printf("Input a text line: ");
fgets(text, 511, stdin);
int len = strlen(text);
text[--len] = 0;
int sx = cx - len / 2;
for(int i = 0; i < len; i++){
if(isspace(text[i]))
continue;
// move the i-th character of text
int dir = i % 4;
MoveChar(text[i], sx + i, cy, screen_width, screen_height, dir);
// redraw previous characters
gotoxy(sx, cy);
for(int j = 0; j <= i; j++)
putchar(text[j]);
// if ESC is pressed, terminate
if(kbhit() && getch() == 27)
break;
}
gotoxy(1, screen_height);
printf("Bye!\n");
return 0;
}
void MoveChar(char c, int tx, int ty, int screen_width, int screen_height, int dir)
{
int x = 0, y = 0;
int dx = 0, dy = 0;
// TO DO: set initial coordinate (x, y) and movement vector (dx, dy) according to dir
switch(dir % 4){
case FROM_TOP: // from top
// initial coordinate is (tx, 1)
x = tx;
y = 1;
// movement vector of LOWER direction is (0, 1)
dx = 0;
dy = 1;
break;
case FROM_RIGHT: // from right
// TO DO: set initial coordinate and movement vector for movement from right
break;
case FROM_BOTTOM: // from bottom
// TO DO: set initial coordinate and movement vector for movement from bottom
break;
case FROM_LEFT: // from up
// TO DO: set initial coordinate and movement vector for movement from left
break;
}
gotoxy(x, y);
putchar(c);
// TO DO: move character from (x, y) to (tx, ty) using (dx, dy)
while(x != tx || y != ty){
// TO DO: write your code here.
// erase previous coordinate
// update coordinate (x, y) using (dx, dy)
// draw character at the new coordinate
fflush(stdout); // flush output buffer for immediate display
if(dir % 2 == 1)
MySleep(25); // horizontal movement should be faster than vertical movement
else
MySleep(50);
}
}
revised code
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "Console.h"
#define FROM_TOP 0
#define FROM_RIGHT 1
#define FROM_BOTTOM 2
#define FROM_LEFT 3
void MoveChar(char c, int tx, int ty, int screen_width, int screen_height, int dir);
int main()
{
int screen_width = getWindowWidth(); // get width of current console window
int screen_height = getWindowHeight() - 3; // get height of current console window
int cx = screen_width / 2, cy = screen_height / 2; // set initial coordinate by center of the screen
char text[512] = "";
clrscr();
printf("Input a text line: ");
fgets(text, 511, stdin);
int len = strlen(text);
text[--len] = 0;
int sx = cx - len / 2;
for(int i = 0; i < len; i++){
if(isspace(text[i]))
continue;
// move the i-th character of text
int dir = i % 4;
MoveChar(text[i], sx + i, cy, screen_width, screen_height, dir);
// redraw previous characters
gotoxy(sx, cy);
for(int j = 0; j <= i; j++)
putchar(text[j]);
// if ESC is pressed, terminate
if(kbhit() && getch() == 27)
break;
}
gotoxy(1, screen_height);
printf("Bye!\n");
return 0;
}
void MoveChar(char c, int tx, int ty, int screen_width, int screen_height, int dir)
{
int x = 0, y = 0;
int dx = 0, dy = 0;
// TO DO: set initial coordinate (x, y) and movement vector (dx, dy) according to dir
switch(dir % 4){
case FROM_TOP: // from top
// initial coordinate is (tx, 1)
x = tx;
y = 1;
// movement vector of LOWER direction is (0, 1)
dx = 0;
dy = 1;
break;
case FROM_RIGHT: // from right
// TO DO: set initial coordinate and movement vector for movement from right
// initial coordinate is (screen_width, ty)
x = screen_width;
y = ty;
// movement vector of LEFT direction is (-1, 0)
dx = -1;
dy = 0;
break;
case FROM_BOTTOM: // from bottom
// TO DO: set initial coordinate and movement vector for movement from bottom
// initial coordinate is (tx, screen_height)
x = tx;
y = screen_height;
// movement vector of UPPER direction is (0, -1)
dx = 0;
dy = -1;
break;
case FROM_LEFT: // from up
// TO DO: set initial coordinate and movement vector for movement from left
// initial coordinate is (1, ty)
x = 1;
y = ty;
// movement vector of RIGHT direction is (1, 0)
dx = 1;
dy = 0;
break;
}
gotoxy(x, y);
putchar(c);
// TO DO: move character from (x, y) to (tx, ty) using (dx, dy)
while(x != tx || y != ty){
// TO DO: write your code here.
// erase previous coordinate
gotoxy(x,y);
putchar(' ');
// update coordinate (x, y) using (dx, dy)
x+=dx;
y+=dy;
// draw character at the new coordinate
gotoxy(x,y);
putchar(c);
fflush(stdout); // flush output buffer for immediate display
if(dir % 2 == 1)
MySleep(25); // horizontal movement should be faster than vertical movement
else
MySleep(50);
}
}
Problem 2
Rotate text left in a box shape
1. Compile and run the following code.
* Put Console.c and Console.h into the current directory, and run the following commands:
gcc [file name].c Console.c -D_WINDOWS // on Windows
gcc [file name].c Console.c -D_MAC // on MacOS
2. Complete DrawBoxText(), DuplicateText(), and RotateLeft() so that it can run as the video below
skeleton code
#include <stdio.h>
#include <string.h>
#include "Console.h"
void DrawBoxText(char text[], int sx, int sy, int width, int height);
void DuplicateText(char text[], int min_len);
void RotateLeft(char text[]);
int main()
{
int screen_width = getWindowWidth();
int screen_height = getWindowHeight() - 3;
gotoxy(1, 1);
printf("screen size = %d x %d\n", screen_width, screen_height);
char text[512] = "God loves you and has a wonderful plan for your life!";
int box_width = 0, box_height = 0;
clrscr();
printf("Input text: ");
fgets(text, 511, stdin);
text[strlen(text) - 1] = 0;
printf("Input box_width and box_height: ");
scanf("%d %d", &box_width, &box_height);
printf("Press ESC to stop.\n");
if(box_width > screen_width)
box_width = screen_width;
if(box_height > screen_height)
box_height = screen_height;
int sx = (screen_width - box_width) / 2;
int sy = (screen_height - box_height) / 2;
DuplicateText(text, box_width * 2 + box_height * 2 - 4);
while(1){
DrawBoxText(text, sx, sy, box_width, box_height);
RotateLeft(text);
gotoxy(screen_width, 1);
MySleep(100);
if(kbhit() && getch() == 27)
break;
}
gotoxy(1, screen_height);
printf("Bye!\n");
return 0;
}
void DrawBoxText(char text[], int sx, int sy, int width, int height)
{
int i = 0, j = 0;
int n = 0;
int len = strlen(text);
// TO DO: display characters along the top side (this is example)
// display characters along the right side (this is an example.)
for(j = 1; n < len && j < height; j++){
gotoxy(sx + width - 1, sy + j);
putchar(text[n++]);
}
// TO DO: display characters along the bottom side
// TO DO: display characters along the left side
}
void DuplicateText(char text[], int min_len)
{
int len = strlen(text);
if(text[len - 1] != ' ')
text[len++] = ' ';
// TO DO: duplicate text multiple times until the length >= min_len
// ex) when min_len = 25, "hello "(lenth = 6) ==> "hello hello hello hello hello "(length = 30)
// Hint: don't forget to put the null character at the end of the string
}
void RotateLeft(char text[])
{
int len = strlen(text); // the length of text
// TO DO: rotate text one step left
// ex) "Hello " ==> "ello H"
}
revised code
#include <stdio.h>
#include <string.h>
#include "Console.h"
void DrawBoxText(char text[], int sx, int sy, int width, int height);
void DuplicateText(char text[], int min_len);
void RotateLeft(char text[]);
int main()
{
int screen_width = getWindowWidth();
int screen_height = getWindowHeight() - 3;
gotoxy(1, 1);
printf("screen size = %d x %d\n", screen_width, screen_height);
char text[512] = "God loves you and has a wonderful plan for your life!";
int box_width = 0, box_height = 0;
clrscr();
printf("Input text: ");
fgets(text, 511, stdin);
text[strlen(text) - 1] = 0;
printf("Input box_width and box_height: ");
scanf("%d %d", &box_width, &box_height);
printf("Press ESC to stop.\n");
if(box_width > screen_width)
box_width = screen_width;
if(box_height > screen_height)
box_height = screen_height;
int sx = (screen_width - box_width) / 2;
int sy = (screen_height - box_height) / 2;
DuplicateText(text, box_width * 2 + box_height * 2 - 4);
while(1){
DrawBoxText(text, sx, sy, box_width, box_height);
RotateLeft(text);
gotoxy(screen_width, 1);
MySleep(100);
if(kbhit() && getch() == 27)
break;
}
gotoxy(1, screen_height);
printf("Bye!\n");
return 0;
}
void DrawBoxText(char text[], int sx, int sy, int width, int height)
{
int i = 0, j = 0;
int n = 0;
int len = strlen(text);
// TO DO: display characters along the top side (this is example)
for(j = 1; n < len && j < width; j++){
gotoxy(sx + j, sy);
putchar(text[n++]);
}
// display characters along the right side (this is an example.)
for(j = 1; n < len && j < height; j++){
gotoxy(sx + width - 1, sy + j);
putchar(text[n++]);
}
// TO DO: display characters along the bottom side
for(j = width-2; n < len && 0 < j ; j--){
gotoxy(sx + j, sy+height-1);
putchar(text[n++]);
}
// TO DO: display characters along the left side
for(j = height-1; n < len && 0 <= j ; j--){
gotoxy(sx, sy+j);
putchar(text[n++]);
}
}
void DuplicateText(char text[], int min_len)
{
int len = strlen(text);
if(text[len - 1] != ' ')
text[len++] = ' ';
// TO DO: duplicate text multiple times until the length >= min_len
// ex) when min_len = 25, "hello "(lenth = 6) ==> "hello hello hello hello hello "(length = 30)
int cnt=0;
if(min_len%len!=0)
cnt=min_len/len+1;
else
cnt=min_len/len;
int idx=len; // the index where we put the duplicated text
for(int i=0 ; i<cnt ; i++){ // duplicate 'cnt' times
for(int j=0 ; j<len ; j++){ // get each of the letters of 'text'
text[idx++]=text[j]; // append to 'text'
}
}
text[idx]=0; // don't forget to put the null character at the end of the string
}
void RotateLeft(char text[])
{
int len = strlen(text); // the length of text
// TO DO: rotate text one step left
// ex) "Hello " ==> "ello H"
char temp=text[0];
for(int i=0; i<len-1; i++)
text[i]=text[i+1];
text[len-1]=temp;
text[len]=0; // don't forget to put the null character at the end of the string
}