Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define ll long long
- #include <stdio.h>
- #include <mm_malloc.h>
- #include <memory.h>
- #include <stdbool.h>
- #include <ctype.h>
- typedef struct {
- int* arrayOfIntegers;
- int size;
- } stack;
- stack* createStack(size_t size) {
- stack *newStack = calloc(1, sizeof(stack));
- newStack -> arrayOfIntegers = (int*)calloc(size, sizeof(int));
- newStack -> size = 0;
- return newStack;
- }
- //Add the element to stack Time: O(1)
- void push(stack* stack, int value) {
- }
- //Returns the element on the peek of stack Time: O(1)
- int peek(stack* stack) {
- }
- //Delete the peek element from stack Time: O(1)
- void pop(stack* stack) {
- }
- char* getLine() {
- }
- //Splitting expression to "Tokens" which can be numbers or symbols of operations Time: O(length)
- char** splitBySpaces(char* expression, int *quantityOfTokens) {
- }
- //All of the methods below are calculating an operation for two peek operands in stack Time: O(1)
- void operationAdd(stack* stack) {
- }
- void operationSubtract(stack* stack) {
- }
- void operationMultiply(stack* stack) {
- }
- void operationDivide(stack* stack) {
- }
- //Checking a token if it's a number Time: O(length)
- bool isNumber(char* token) {
- for (size_t i = 0; i < strlen(token); i++) {
- if (!isdigit(token[i])) {
- return false;
- }
- }
- return true;
- }
- int main() {
- stack* stackOfNumbers = createStack(1000);
- char* expression = (char*)calloc(1001, sizeof(char));
- if (!expression) {
- printf("No memory?!");
- exit(1);
- } else {
- expression = getLine();
- }
- int quantityOfTokens = 0;
- char** splittedString = splitBySpaces(expression, &quantityOfTokens);
- for (int i = 0; i < quantityOfTokens; i++) {
- if (strcmp(splittedString[i], "+") == 0) {
- operationAdd(stackOfNumbers);
- } else if (strcmp(splittedString[i], "-") == 0) {
- operationSubtract(stackOfNumbers);
- } else if (strcmp(splittedString[i], "*") == 0) {
- operationMultiply(stackOfNumbers);
- } else if (strcmp(splittedString[i], "/") == 0) {
- operationDivide(stackOfNumbers);
- } else if (isNumber(splittedString[i])){
- push(stackOfNumbers, );
- } else {
- printf("bad input");
- exit(1);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement