I've attempted a bunch of different iterations of this and they have all failed. Whenever you attempt to save the current value it never saves. When you go to try to use 'GetFromMemory' it instead just displays the current value instead of the saved one.
I've tried multiple iterations of and can't seem to find the problem on why it won't save.
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "Strings.h"
#include "MN.h"
#define LIMIT 32
#define WHAT '?'
#define QUIT 'Q'
#define READ 'R'
#define PRINT 'P'
#define ADD '+'
#define SUBTRACT '-'
#define MULTIPLY '*'
#define DIVIDE '/'
#define WHOLE 'W'
#define NUM 'N'
#define DEM 'D'
#define INVERT 'I'
#define COMPARE 'C'
#define SAVE 'S'
#define GET 'G'
#define MEMORY 'M'
#define PERIOD '.'
void print(MN this) {
printf(" %s",toStringMN(this));
}
void println(MN this) {
print(this);
printf("\n");
}
MN read(char *s) {
MN result;
char *input;
if(Slength(s) < 1) {
printf(" :>"); input = nextLine(LIMIT);
result = newSMN(input);
free(input);
}
else {
result = newSMN(s);
}
return result;
}
int readIndex(char *s) {
int result;
char *input;
int success = 0;
do {
if((success == 0) || (Slength(s) < 1)) {
printf(" :>"); input = nextLine(LIMIT);
success = sscanf(input,"%d",&result);
free(input);
}
else {
success = sscanf(s,"%d",&result);
}
} while(success < 1);
return result;
}
int validIndex(int index) {
int result = 0;
if((index > 0) && (index <= LIMIT)) {
result = index;
}
return result;
}
int validMemory(int index, MN memory[]) {
int result = 0;
if((validIndex(index) == index) && (memory[index] != NULL)) {
result = index;
}
return result;
}
void printWHAT() {
printf(" Valid Commands are:\n");
printf(" %c=List Commands, %c=Quit, %c=Read, %c=Print,\n",
WHAT,QUIT,READ,PRINT);
printf(" %c=Add, %c=Subtract, %c=Multiply, %c=Divide,\n",
ADD,SUBTRACT,MULTIPLY,DIVIDE);
printf(" %c=Invert, %c=Whole, %c=Numerator, %c=Denominator, %c=Compare\n",
INVERT,WHOLE,NUM,DEM,COMPARE);
printf(" %c=SaveToMemory, %c=GetFromMemory, %c=DisplayMemory\n",
SAVE,GET,MEMORY);
}
void nullify(MN array[]) {
for(int i=0; i<LIMIT+1; i++) {
array[i] = NULL;
}
}
int main() {
MN memory[LIMIT+1];
nullify(memory);
MN X = newIMN(1,0,0,1);
MN Y;
memory[0] = newIMN(1,0,0,1);
char *input;
char command;
int toPrint;
do {
toPrint = 1;
printf("::>"); input = nextLine(LIMIT);
command = toupper(input[0]);
if(command == WHAT) {
printWHAT(); toPrint = 0;
}
else if(command == READ) {
X = read(input+1);
}
else if(command == PRINT) {
}
else if(command == ADD) {
Y = read(input+1); addMN(X,Y);
}
else if(command == SUBTRACT) {
Y = read(input+1); subtractMN(X,Y);
}
else if(command == MULTIPLY) {
Y = read(input+1); multiplyMN(X,Y);
}
else if(command == DIVIDE) {
Y = read(input+1); divideMN(X,Y);
}
else if(command == WHOLE) {
toPrint = 0;
printf(" %d\n",wOfMN(X));
}
else if(command == NUM) {
toPrint = 0;
printf(" %d\n",nOfMN(X));
}
else if(command == DEM) {
toPrint = 0;
printf(" %d\n",dOfMN(X));
}
else if(command == INVERT) {
invertMN(X);
}
else if(command == COMPARE) {
toPrint = 0;
Y = read(input+1);
printf(" %d\n",compareToMN(X,Y));
}
else if(command == SAVE) {
memory[validIndex(readIndex(input+1))] = X;
}
}
else if(command == GET) {
X = memory[validMemory(readIndex(input+1),memory)];
}
else if(command == MEMORY) {
for(int i=0; i<=LIMIT; i++) {
if(memory[i] != NULL) {
printf(" %2d: %s\n",i,toStringMN(memory[i]));
}
}
}
else if(command != QUIT) {
toPrint = 0;
printf(" --->UNKNOWN COMMAND\n");
}
if((command != QUIT) && toPrint) {
println(X);
}
free(input);
} while(command != QUIT);
}
There is an extra closing curly brace } in the SAVE command block. It is causing a syntax error and making the program exit prematurely before the GetFromMemory command can be executed. Removing this curly brace should fix the issue. Here is the corrected code for the SAVE block:
scsselse if(command == SAVE) {
memory[validIndex(readIndex(input+1))] = X;
}
else if(command == GET) {
X = memory[validMemory(readIndex(input+1),memory)];
}
else if(command == MEMORY) {
for(int i=1; i<=LIMIT; i++) {
if(memory[i] != NULL) {
printf(" %2d:",i); print(memory[i]);
}
}
toPrint = 0;
}
free(input);
if(toPrint) {
println(X);
}
Additionally, there seems to be a typo in the validMemory function. The condition should check if memory[index] is not equal to NULL, not if it is equal to NULL. Here is the corrected function:
perlint validMemory(int index, MN memory[]) {
int result = 0;
if((validIndex(index) == index) && (memory[index] != NULL)) {
result = index;
}
return result;
}
Comments
Post a Comment