Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define COLS 80
#define ROWS 23
/* Function prototypes */
int init(char);
int display();
int drawPt(char, int, int);
int drawRect(char, int, int, int, int);
int drawLine(char, int, int, int, int);
int fill(char, int, int);
int saveFile(char* filename);
char buffer [80];
FILE *input, *output;
/* Global variables */
char screen[COLS][ROWS];
int main(void){
input= fopen("finalinput.txt", "r");
int x1, x2, y1, y2;
char *cmd, *outChar;
while ( fgets(buffer, 80, input) != NULL){
/*making sure all coords are initialized*/
x1 = 0;
x2 = 0;
y1 = 0;
y2 = 0;
/*grabbing a line and splitting it into peices*/
cmd=strtok(buffer,",");
outChar= strtok(NULL, ",");
x1= atoi(strtok(NULL, ","));
y1= atoi( strtok(NULL, ","));
x2= atoi( strtok(NULL, ","));
y2= atoi(strtok(NULL, ","));
/*taking the line and sending it to the
function it needs to be sent to*/
switch (cmd[0]){
case 'I':
init( outChar[0]);
break;
case 'D':
display();
break;
case 'R':
drawRect(outChar[0],x1, y1, x2, y2);
break;
case 'P':
drawPt(outChar[0],x1, y1);
break;
case 'L':
drawLine(outChar[0],x1, y1, x2, y2);
break;
case 'F':
fill(outChar[0],x1, y1);
break;
case 'S':
saveFile(outChar);
break;
default:
break;
}
}
system("PAUSE");
return 0;
}
/**************************************************************/
/* init: Clears the screen by assigning all pixels the passed */
/* character. Receives a character to use in clearing the */
/* screen. */
/* Success returns a 0. */
/**************************************************************/
int init(char p) {
int x, y;
for (x = 0; x < COLS; x++) {
for (y = 0; y < ROWS; y++) {
screen[x][y] = p;
}
}
return 0;
}
/**************************************************************/
/* display: Prints the screen matrix to the console. */
/* Success returns a 0. */
/**************************************************************/
int display() {
int x, y;
/*The following line just puts a border above the screen */
for (x = 0; x < COLS; x++)
putchar('_');
/*Print out the screen matrix to standard output */
for (y = ROWS - 1; y >= 0; y--) {
for (x = 0; x < COLS; x++) {
putchar(screen[x][y]);
}
}
/*The following line just puts a border below the screen. */
for (x = 0; x < COLS; x++)
putchar('_');
return 0;
}
/**************************************************************/
/* drawPt: draws a point containing a border character using */
/* coordinates x,y. */
/* Returns -1 if the coordinates are out of bounds and 0 for */
/* success. */
/**************************************************************/
int drawPt(char b, int x, int y) {
/*Test for out of bounds */
if (x < 0 || x >= COLS) return -1;
if (y < 0 || y >= COLS) return -1;
/*Otherwise, draw the point */
screen[x][y] = (char) b;
return 0;
}
/**************************************************************/
/* drawRect: draws a rectangle with a border character using */
/* coordinates x1,y1 and x2,y2 to form opposite corners. */
/* Returns -1 if the coordinates are out of bounds and 0 for */
/* success. */
/**************************************************************/
int drawRect(char b, int x1, int y1, int x2, int y2) {
/*Test for out of bounds*/
if (x1 < 0 || x1 >= COLS) return -1;
if (x2 < 0 || x2 >= COLS) return -1;
if (y1 < 0 || y1 >= ROWS) return -1;
if (y2 < 0 || y2 >= ROWS) return -1;
int c = abs(x1 - x2); /* temporary counter variables */
int r = abs(y1 - y2); /* temporary counter variables */
int x = x1 >= x2 ? x2 : x1; /* Set x to the lowest coord. */
int y = y1 >= y2 ? y2 : y1; /* Set y to the lowest coord. */
while (c >= 0) { /* Draw the horizontal lines. */
screen[x + c][y1] = b;
screen[x + c][y2] = b;
c--;
}
while (r >= 0) { /* Draw the vertical lines. */
screen[x1][y + r] = b;
screen[x2][y + r] = b;
r--;
}
return 0;
}
/**************************************************************/
/* drawLine: draws a line with a border character using */
/* coordinates x1,y1 and x2,y2 to form opposite ends . */
/* A line can be expressed in the form y = mx + b */
/* Returns -1 if the coordinates are out of bounds and 0 for */
/* success. */
/**************************************************************/
int drawLine(char line_char, int x1, int y1, int x2, int y2){
int i, j, x, y, xlow, ylow, xhigh, yhigh, z;
double m=0;
/*making sure coords given are inside perimeters*/
if (x1 < 0 || x1 >= COLS) return -1;
if (x2 < 0 || x2 >= COLS) return -1;
if (y1 < 0 || y1 >= ROWS) return -1;
if (y2 < 0 || y2 >= ROWS) return -1;
/*declaring xlow, xhigh, ylow, and yhigh*/
if (x1>x2){
xlow=x2;
xhigh=x1;
}
else{
xlow=x1;
xhigh=x2;
}
if (y1>y2){
ylow=y2;
yhigh=y1;
}
else{
ylow=y1;
yhigh=y2;
}
/*draws horizontal and vertical lines*/
if ((x1-x2)==0){
for(j=ylow; j<=yhigh; j++) {
screen[x1][j]=line_char;
}
return 0;
}
if ((y1-y2)==0){
for(i=xlow; i<=xhigh; i++){
screen[i][y1]=line_char;
}
return 0;
}
/*finds slope*/
m=(double)(y2-y1)/(x2-x1);
/*draws sloped lines*/
if (fabs(m)>1){
for (j=ylow; j<=yhigh; j++){
x= (int) (((1/m)*(j-y1))+x1+0.5);
screen[x][j]=line_char;
}
return 0;
}
if (fabs(m)<=1){
for (i=xlow; i<=xhigh; i++){
y= (int) ((m*(i-x1))+y1+0.5);
screen[i][y]=line_char;
}
return 0;
}
return 0;
}
/**************************************************************/
/* fill: fills an area with a supplied fill character, */
/* replacing all empty spaces (those pixels with a space */
/* character in a horizontal or vertical direction. */
/* This function uses recursion. */
/* Returns -1 if the coordinates are out of bounds, 1 if the */
/* coordinates are not a space and 0 for success. */
/**************************************************************/
int fill(char fill_char, int x, int y) {
/*out of bounds test*/
if (x < 0 || x >= COLS) return -1;
if (y < 0 || y >= ROWS) return -1;
/*filling all around coord given*/
if(screen[x][y]==' '){
screen[x][y]=fill_char;
fill(fill_char,x-1,y);
fill(fill_char,x+1,y);
fill(fill_char,x,y-1);
fill(fill_char,x,y+1);
return 0;
}
else return 1;
}
/**************************************************************/
/* saveFile: Prints the screen matrix to a file. */
/* Success returns a 0. */
/**************************************************************/
int saveFile(char *filename){
return 0;
}
this is the whole code but the only problem is with the save function at the very bottom