EDIT: Please note, this assignment was already submitted.
Well for our assignment this time around, we had to write up a BASIC editor. It had to sort lines of code numerically. Each line would never be more than 80 characters long and there would never be more than 100 lines. Seeing this, I decided to use a 2-dimensional Character Array.
Basically, the program had to be able to do the following.
1.) Sort the strings entered into the array (does this automatically)
2.) Save strings into a file with the name specified by the user.
3.) Load a user-specified file into the 2-d char array
4.) Print out all of the lines of basic
5.) exit <This one works!
My teacher has a saying: PAD1 (Principle of Algorithm Design 1) students have a tendency to make programs exponentially more complicated then they have to be.
I'm assuming that this is the case this time around....
# include<stdio.h>
# include<stdlib.h>
# include<string.h>
# define MAX_COLUMNS 80
# define MAX_ROWS 100
void save(char text[MAX_ROWS][MAX_COLUMNS], char filename[100]){
FILE *fout;
int i;
fout=fopen(filename+5, "wt");
for(i=0; i<MAX_ROWS; ++i){
if(text[i][1]>=0 && text[i][1]<=9)
fprintf(fout,"%s",text);
}
fclose(fout);
return;
}
char load(char filename[50]){
int i, j;
FILE *fin=fopen(filename+5, "rt");
char txt[MAX_ROWS][MAX_COLUMNS];
for(j=0; j<MAX_ROWS; ++j){
for(i=0; i<MAX_COLUMNS; ++i){
fscanf(fin,"%c", txt[j][i]);
}
}
fclose(fin);
return 0;
}
int main(void){
FILE *fout, *fin;
char filename[50], spare[MAX_COLUMNS], str[MAX_ROWS][MAX_COLUMNS];
for(int y=0; y<=MAX_ROWS; ++y){
int z=y-1;
fgets(str, sizeof str, stdin);
/*This is the sorter*/
if(str[y][1]<=9 && str[y][1]>=0 ){
if(y>0){
if(strcmp(str[y],str[z])<0){
strcpy(spare,str[z]);
strcpy(str[z],str[y]);
strcpy(str[z],spare);
}
else;
}
}
else if(strncmp(str[y], "save", 4)==0){
char filename[100];
str[y][strlen(str[y])-1]=0;
strcpy(filename,str[y]);
}
else if(strncmp(str[y], "load", 4)==0){
char filename[100];
str[y][strlen(str[y])-1]=0;
strcpy(filename,str[y]);
load(filename);
}
else if(strncmp(str, "list", 4)==0){
for(int y=0; y<MAX_ROWS; ++y){
if(str[y][1]<='9' && str[y][1]>=0)
printf("%s", str[y]);
else;
}
}
else if(strncmp(str, "exit", 4)==0){
exit(0);
}
else;
}
return 0;
}