Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <malloc.h>
- #include <locale.h>
- #define pi 3.14159265358979323846
- #define STACK_OVERFLOW -100
- #define STACK_UNDERFLOW -101
- #define NMAX 100
- typedef long double Dig;
- typedef char Char;
- char str[100];
- char str1[100];
- bool exit1=false;
- bool exit2=false;
- bool exit6=false;
- int quantity=0;
- int prior=0;
- int stack_amount=0;
- typedef struct number
- {
- Dig valueNum;
- struct number *nextNum;
- } number_t;
- typedef struct sign
- {
- Char valueOper;
- struct sign *nextOper;
- } oper_t;
- void push_num(number_t **headNum, Dig value)
- {
- number_t *tmp = (struct number*)malloc(sizeof(number_t));
- if (tmp == NULL) {
- exit(STACK_OVERFLOW);
- }
- tmp->nextNum = *headNum;
- tmp->valueNum = value;
- *headNum = tmp;
- }
- void push_oper(oper_t **headOper, Char value)
- {
- oper_t *tmp = (struct sign*)malloc(sizeof(oper_t));
- if (tmp == NULL) {
- exit(STACK_OVERFLOW);
- }
- stack_amount++;
- tmp->nextOper = *headOper;
- tmp->valueOper = value;
- *headOper = tmp;
- }
- Dig pop_num(number_t **headNum)
- {
- number_t *out;
- Dig value;
- if (*headNum == NULL)
- {
- exit(STACK_UNDERFLOW);
- }
- out = *headNum;
- *headNum = (*headNum)->nextNum;
- value = out->valueNum;
- free(out);
- return value;
- }
- Char pop_oper(oper_t **headOper)
- {
- oper_t *out;
- Char value;
- if (*headOper == NULL)
- {
- exit(STACK_UNDERFLOW);
- }
- out = *headOper;
- *headOper = (*headOper)->nextOper;
- value = out->valueOper;
- stack_amount--;
- free(out);
- return value;
- }
- void PrintStack(const oper_t* headOper)
- {
- while (headOper)
- {
- printf("%c ", headOper->valueOper);
- headOper = headOper->nextOper;
- }
- }
- size_t GetSize_num(const number_t *headNum) {
- size_t size = 0;
- while (headNum) {
- size++;
- headNum = headNum->nextNum;
- }
- return size;
- }
- size_t GetSize_oper(const oper_t *headOper) {
- size_t size = 0;
- while (headOper) {
- size++;
- headOper = headOper->nextOper;
- }
- return size;
- }
- void summa()
- {
- }
- void raznost()
- {
- }
- void mult()
- {
- }
- void del()
- {
- }
- void factorial()
- {
- }
- void power()
- {
- }
- void sinus()
- {
- }
- void cosinus()
- {
- }
- void tang()
- {
- }
- void ctang()
- {
- }
- void arcsin()
- {
- }
- void arccosinus()
- {
- }
- void arctang()
- {
- }
- void arcctang()
- {
- }
- int priority(char c)
- {
- if (c=='(')
- {
- //printf("^");
- prior= 1;
- }
- if (c=='+' || c=='-')
- {
- //printf("+-");
- prior= 2;
- }
- if (c=='*' || c=='/')
- {
- //printf("*");
- prior= 3;
- }
- if (c=='^' || c== 's' ||)
- {
- //printf("^");
- prior= 4;
- }
- return prior;
- }
- void treatment(struct number **headNum, struct sign **headOper)
- {
- int elem=0;
- int skobki=0;
- //push_oper(headOper, '*');
- int exit4=0;
- int number_1=0;
- char in, out, out1;
- int j=0;
- int i=0;
- for (int i=0; i<quantity-1; i++)
- {
- if (str[i]=='!')
- {
- str1[j]=str[i];
- number_1=0;
- }
- if (str[i]=='.')
- {
- str1[j]=str[i];
- }
- if (str[i]=='0' || str[i]=='1' || str[i]=='2' || str[i]=='3' || str[i]=='4' || str[i]=='5' || str[i]=='6' || str[i]=='7' ||str[i]=='8' || str[i]=='9')
- {
- if (number_1==0)
- {
- str1[j]='|';
- j++;
- }
- str1[j]=str[i];
- number_1=1;
- }
- if (str[i]=='*' || str[i]=='/' || str[i]=='+' || str[i]=='-' || str[i]=='^')
- {
- if (stack_amount!=0)
- {
- out=pop_oper(headOper);
- //int x=priority(out);
- //printf("%d", x);
- out1=out;
- while (priority(out)>=priority(str[i]))
- {
- str1[j]=out;
- j++;
- exit4++;
- // stack_amount--;
- //pop_oper(headOper);
- /*if (stack_amount==0)
- {
- break;
- }*/
- //printf("(priority(out)=%d ", priority(out));
- // printf("(priority(str[i])=%d \n", priority(str[i]));
- if (stack_amount>0)
- {
- out=pop_oper(headOper);
- if (out=='(')
- {
- push_oper(headOper, out);
- }
- //stack_amount--;
- }
- else
- {
- break;
- }
- }
- if (exit4==0)
- {
- push_oper(headOper, out1);
- //stack_amount++;
- }
- }
- push_oper(headOper, str[i]);
- // stack_amount++;
- number_1=0;
- exit4=0;
- j--;
- }
- if (str[i]=='(')
- {
- push_oper(headOper, str[i]);
- // stack_amount++;
- j--;
- number_1=0;
- }
- if (str[i]==')')
- {
- while(!exit6)
- {
- out=pop_oper(headOper);
- if (out=='(')
- {
- // stack_amount--;
- exit6=true;
- break;
- }
- str1[j]=out;
- // stack_amount--;
- j++;
- }
- j--;
- number_1=0;
- }
- if (str[i]=='s' && str[i+1]=='i' && str[i+2]=='n')
- {
- }
- printf("%d\n", stack_amount);
- PrintStack(*headOper);
- printf("\n");
- j++;
- exit6=false;
- }
- //j--;
- printf("stack amount=%d\n", stack_amount);
- while (stack_amount!=0)
- {
- out=pop_oper(headOper);
- str1[j]=out;
- j++;
- //stack_amount--;
- }
- for (int i=0; i<j+2; i++)
- {
- printf("%c", str1[i]);
- }
- printf("\n");
- printf("%d\n", stack_amount);
- //PrintStack(*headOper);
- //push_oper(headOper, str[coord]);
- /*for(int i=0; i<quantity; i++)
- {
- printf("%c", exp[i]);
- }*/
- }
- int main()
- {
- struct number *headNum = NULL;
- struct sign *headOper = NULL;
- setlocale(LC_ALL, "Rus");
- printf("Введите выражение: \n");
- int i=0;
- while (!exit1)
- {
- scanf("%c", &str[i]);
- if (str[i]=='=')
- {
- exit1=true;
- }
- i++;
- }
- quantity=i;
- printf("\n");
- for(int i=0; i<quantity; i++)
- {
- str1[i]=0;
- }
- treatment(&headNum, &headOper);
- /*for (int i=0; i<quantity; i++)
- {
- printf("%c", str[i]);
- }*/
- //printf("lol");
- _getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement