Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define NUMBER 1
- #define OFF 0
- #define ON 1
- int getop(char[]); //processes data into a number that is pushed onto a stack or an operand
- void push(double); //pushes a number onto a stack
- double pop(void); //pops the top number in the stack
- double atofx(char[]); //converts the string op[] into a number
- int getline(char[], int); //gets a line. characters followed by \n \0
- int index = 0; //index of the stack of numbers
- double values[MAXARRAYSIZE]; //stack of numbers
- char ungets_string[MAXARRAYSIZE]; //an array used to push back an entire line of numbers and operands onto the input
- int ungets_index = 0; //index of the ungets array
- char getlinearray[MAXARRAYSIZE]; //the array getline stores a line within
- int ix = 0; //the index of the array that stores the information obtained whilst getline runs
- main()
- {
- int operator; //variable for holding a number popped from the stack of numbers values[index]
- double operator2; //second variable for holding a number popped from the stack of numbers values[index]
- double temp; //variable for holding a value temporarily
- double temp2; //variable for holding a value temporarily
- char op[MAXARRAYSIZE]; //array used to store numbers that may be negative and floating-point. Used with atof to produce a number that is pushed onto values[index]
- double a = 0; /* variables for holding values and a flag to indicate if they have been defined*/
- int a_def = NO;
- double b = 0;
- int b_def = NO;
- double x = 0;
- int x_def = NO;
- double y = 0;
- int y_def = NO;
- char undef = 0; //variable that holds the character of the variable being undefined a, b, x, y so it can be redefined as a new value
- double mrp = 0.0; //most recently printed value
- int k = 0;
- char temparray[MAXARRAYSIZE];
- int placeholder = 0;
- start:
- getline(getlinearray, MAXARRAYSIZE); //gets a line and then the program goes to process the line using getop() and switch cases. all of it is commented
- ungets: //this label
- while ((operator = getop(op)) != EOF)
- {
- switch (operator) {
- case 'a':
- if (a_def == NO)
- {
- if (index > 0)
- a = pop(), a_def = YES;
- else
- a = 0;
- }
- else
- push(a);
- break;
- case 'b':
- if (b_def == NO)
- {
- if (index > 0)
- b = pop(), b_def = YES;
- else
- b = 0;
- }
- else
- push(b);
- break;
- case 'x':
- if (x_def == NO)
- {
- if (index > 0)
- x = pop(), x_def = YES;
- else
- x = 0;
- }
- else
- push(x);
- break;
- case 'y':
- if (y_def == NO)
- {
- if (index > 0)
- y = pop(), y_def = YES;
- else
- y = 0;
- }
- else
- push(y);
- break;
- case 'z': //undefine a variable using za zb zx zy. Then the next time the variable is used it either becomes the topmost value in the stack or 0 if the stack is empty
- undef = getchar();
- switch (undef) {
- case 'a':
- a_def = NO;
- break;
- case 'b':
- b_def = NO;
- break;
- case 'x':
- x_def = NO;
- break;
- case 'y':
- y_def = NO;
- break;
- default: printf("Error: The character after z is supposed to be a variable. The variable are a, b, x, and y. The correct syntax to undefine a variable is za, zb, zx, and zy.\n");
- break;
- }
- break;
- case NUMBER: //stores the number that is stored in the array op into the stack values[index] using the function push() and atof()
- push(atof(op));
- break;
- case '+': /* basic operations */
- push(pop() + pop());
- break;
- case '*':
- push(pop() * pop());
- break;
- case '-':
- operator2 = pop();
- push(pop() - operator2);
- break;
- case '/':
- operator2 = pop();
- if (operator2 != 0.0)
- push(pop() / operator2);
- else
- printf("Error: Operator 2 is 0. Cannot divide by 0.\n");
- break;
- case '%':
- operator2 = pop();
- if (operator2 != 0)
- push((int)pop() % (int)operator2);
- else
- printf("Error: Operator 2 is 0. Cannot divide by 0.\n");
- break;
- case 'm': //prints mrp
- printf("%f\n", mrp);
- break;
- case 'p': //prints topmost value of the stack values[index]. useful if you want to define a variable
- temp = pop();
- push(temp);
- printf("%f", temp);
- break;
- case 'd': //duplicated the topmost value in the stack values[index]
- temp = pop();
- push(temp);
- push(temp);
- break;
- case 's': //switches the topmost 2 values in the stack values[index]
- temp = pop();
- temp2 = pop();
- push(temp);
- push(temp2);
- break;
- case 'c': //clears the stack values[index]
- values[0] = '\0';
- index = 0;
- break;
- case 'q': //sin(x)
- push(sin(pop()));
- break;
- case 'w': //cos(x)
- push(cos(pop()));
- break;
- case 'e': //power(x, y)
- operator2 = pop();
- push(pow(pop(), operator2));
- break;
- case 'u': //ungets: 'u': places every character that has been read before 'u' into the array getlinearray[ix] being processed in getop() and creating evaluations of the switch cases
- //Example: 1 2 + u 1 \n should store 1 store 2 add 1 and 2 storing 3 then store 1 store 2 add 1 and 2 storing three store 1 then \n print out 1 and values 3 and 3 should be in values[index]
- //if two 'u' are in the array getlinearray() then this is a label for the operation ungets 'u' to repeat and replace the character 'u' with every character before it. Example: 1 2 u 3 u 1 \n should store 1 store 2 store 1 store 2 store 3 store 1 store 2 store 1 store 2 store 3 store 1 \n print 1
- //and values[index] should be 1 2 1 2 3 1 2 1 2 3
- moreungets:
- k = 0;
- placeholder = ix - 1; //placeholder = ix - 1 is where 'u' is.
- printf("%s\n", ungets_string); //prints out the ungets_string so I see what string is placed onto getlinearray[] processed by getop()
- while (getlinearray[placeholder] != '\0') //stores everything after 'u' into temp
- {
- temparray[k] = getlinearray[placeholder];
- k++, placeholder++;
- }
- temparray[k] = '\0';
- while (ungets_index >= 0) //inserts ungets_string[] into getlinearray[] beginning where 'u' was stored
- {
- getlinearray[placeholder] = ungets_string[ungets_index];
- placeholder++, ungets_index--;
- }
- ungets_string[0] = '\0', ungets_index = 0; //resets ungets_string[] for next use 'u'
- k = 0;
- while (temparray[k] != '\0') //restores the rest of the array getlinearray[] before the insertion of ungets_string[]
- {
- getlinearray[placeholder] = temparray[k];
- k++, placeholder++;
- }
- getlinearray[placeholder++] = '\0';
- ix = 0;
- while (getlinearray[ix] != '\0') //checks getlinerarray[] for any more 'u'
- {
- if (getlinearray[ix] == 'u') //IF 'u' is found repeat
- {
- ix++;
- goto moreungets;
- }
- else //else store everything into ungets_string for the next time 'u' may be read. //ungets_string[] can be cleared with 'r'.
- {
- ungets_string[ungets_index] = getlinearray[ix];
- ix++, ungets_index++;
- }
- }
- goto ungets;
- break;
- case 'r': //clears ungets
- ungets_string[0] = '\0', ungets_index = 0;
- break;
- case '\n': //prints the topmost value of the stack values[index], clears getlinearray[] so a new line can be read and processed and saves mrp.
- printf("\t%.8f\n", mrp = pop());
- getlinearray[0] = '\0', ix = 0;
- goto start;
- break;
- case '!': //terminates
- goto terminate;
- default: //default: error
- printf("Error: The program read a character using getchar() that was not a digit or a character with a switch case defined. The character is %s.\n", op);
- break;
- }
- }
- terminate:
- return 0;
- }
- void push(double OperandValue)
- {
- if (index < MAXARRAYSIZE) //IF THERE IS SPACE IN THE STACK values[index] STORE THE VALUE SENT TO THE FUNCTION else error
- values[index++] = OperandValue;
- else
- printf("Error: Stack full. Cannot push operand value %g.\n", OperandValue);
- }
- double pop(void)
- {
- if (index > 0) //IF THERE IS A VALUE IN values[index] POP/RETURN THE VALUE TO THE FUNCTION CALL pop() else error
- return values[--index];
- else
- {
- printf("Error: There are 0 operands stored in the stack containing numbers.\n");
- return 0.0;
- }
- }
- int getop(char s[]) //processes getline(getlinearray[], MAXARRAYSIZE) and ungets
- {
- int j = 0;
- while (getlinearray[ix] == ' ' || getlinearray[ix] == '\t' || getlinearray[ix] == '\0') //skips white space
- ix++;
- if (getlinearray[ix] != '\n' && getlinearray[ix] != 'u') //save character or number into op "s[]" and ungets_string for ungets
- s[j++] = ungets_string[ungets_index++] = getlinearray[ix];
- else if (getlinearray[ix] == '\n' || getlinearray[ix] == 'u') //don't save 'u' and '\n' into ungets
- ungets_string[ungets_index++] = ' ';
- if (!isdigit(getlinearray[ix]) && getlinearray[ix] != '.' && getlinearray[ix] != '-') //return character
- {
- ungets_string[ungets_index++] = ' ';
- return getlinearray[ix++];
- }
- if (getlinearray[ix] == '-' && !isdigit(getlinearray[ix + 1])) //return '-'
- {
- ungets_string[ungets_index++] = ' ';
- return getlinearray[ix++];
- }
- else if (getlinearray[ix] == '-' && isdigit(getlinearray[ix+1])) //negative number
- {
- ix++;
- s[j] = getlinearray[ix]; //save digit
- ungets_string[ungets_index] = getlinearray[ix];
- j++, ungets_index++;
- }
- if (isdigit(getlinearray[ix + 1])) //save number
- {
- ix++;
- ungets_string[ungets_index] = s[j] = getlinearray[ix];
- ix++, j++, ungets_index++;
- while (isdigit(getlinearray[ix]))
- {
- ungets_string[ungets_index] = s[j] = getlinearray[ix];
- ix++, j++, ungets_index++;
- }
- }
- if (getlinearray[ix] == '.') //save fractional part of number
- {
- ix++;
- ungets_string[ungets_index++] = s[j++] = getlinearray[ix];
- ix++;
- while (isdigit(getlinearray[ix]))
- {
- ungets_string[ungets_index] = s[j] = getlinearray[ix];
- ix++, j++, ungets_index++;
- }
- }
- ix++;
- s[j] = '\0';
- ungets_string[ungets_index++] = ' ';
- return NUMBER; //return to getop() and store op[] into values[index] using atof()
- }
- double atofx(char s[]) //converts a string to a floating point number
- {
- int i = 0;
- int sign = 1;
- int val = 0;
- int power = 1;
- int e = 0;
- double exp = 0;
- if (s[i] == '-')
- {
- sign = -1;
- i++;
- }
- else if (s[i] == '+')
- i++;
- while (isdigit(s[i]))
- val = val * 10 + (s[i++] - '0');
- if (s[i] == '.')
- {
- i++;
- while (isdigit(s[i]))
- {
- val = val * 10 + (s[i++] - '0');
- power *= 10;
- }
- }
- val = sign * val / power;
- if (s[i] == 'e' || s[i] == 'E')
- {
- i++;
- if (s[i] == '-')
- {
- i++;
- while (isdigit(s[i]))
- e = e * 10 + (s[i] - '0');
- while (e-- > 0)
- val /= 10;
- return val;
- }
- else if (s[i] == '+')
- {
- i++;
- while (isdigit(s[i]))
- e = e * 10 + (s[i] - '0');
- while (e-- > 0)
- val *= 10;
- return val;
- }
- }
- else
- return val;
- }
- int getline(char s[], int lim) //stores characters into an array, ends with '\n' '\0'
- {
- int c;
- int i = 0;
- while (--lim > 0 && (c = getchar()) != EOF && c != '\n')
- s[i++] = c;
- if (c == '\n')
- s[i++] = c;
- s[i] = '\0';
- return i;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement