Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- #include <string.h>
- #include <ctype.h>
- #define SIZE 100
- char* POST_IN_INFIC(char*);
- int calculator(char*);
- void Infic_in_Post(char* s);
- struct list_t {
- char data;
- list_t *next;
- list_t *prev;
- };
- list_t *head, *tail;
- int n;
- void Push(char);
- void Pop(void);
- void Del(void);
- void Init(void);
- void Show(void);
- bool Empty(void);
- void Find(char,char);
- enum CHOICE { // Выбор пользователя
- QUIT = 0,
- INFICINPOST,
- CALCULATOR,
- OTHER
- };
- CHOICE DoMenu(void);
- int main()
- {
- freopen("err.txt", "w", stderr);
- head=NULL;
- char* s;
- s=(char*)malloc(100*sizeof(char));
- int choice = -1;
- while (choice != QUIT) {
- choice = DoMenu();
- switch(choice) {
- case INFICINPOST:
- Init();
- scanf("%s", s);
- Infic_in_Post(s);
- break;
- case CALCULATOR:
- scanf("%s", s);
- calculator(s);
- default:
- break;
- }
- }
- return 0;
- }
- CHOICE DoMenu(void)
- {
- CHOICE choice;
- printf( "\nMenu\n"
- "\t%d quit\n"
- "\t%d infic->post\n"
- "\t%d calculator\n"
- "Enter new line: ",
- QUIT, INFICINPOST, CALCULATOR);
- scanf("%d", (int *)&choice);
- switch (choice) {
- case QUIT:
- case INFICINPOST:
- case CALCULATOR:
- return choice;
- default:
- return OTHER;
- }
- return OTHER;
- }
- int PriorityComp(char a, char b)
- {
- switch (a) {
- case '(': case ')':
- a = 0;
- break;
- case '+': case '-':
- a = 1;
- break;
- case '*': case '/':
- a = 2;
- break;
- default:
- a = 3;
- break;
- }
- switch (b) {
- case '(': case ')':
- b = 0;
- break;
- case '+': case '-':
- b = 1;
- break;
- case '*': case '/':
- b = 2;
- break;
- default:
- b = 3;
- break;
- }
- return a - b;
- }
- void Infic_in_Post(char* s)
- { list_t *p;
- int j;
- unsigned int len = strlen(s);
- Init();
- for (unsigned int i = 0; i < len; i++) {
- std::cerr << i << ") " << s[i] << std::endl;
- if (s[i] >= '0' && s[i] <= '9') {
- printf("%c", s[i]);
- if (i == len - 1 || (i < len - 1 && (s[i + 1] < '0' || s[i + 1] > '9'))) printf(" ");
- }
- else if (s[i] == '(') {
- Push(s[i]);
- } else if (s[i] == ')') {
- while (!Empty() && head->data != '(')
- Pop();
- Pop();
- } else if (s[i]=='^') {
- Push(s[i]); j = i+1;
- while (j < len && s[j]<='9' && s[j]>='0') {
- printf("%c", s[j]); j++;}
- i = j - 1;
- Pop();
- }
- else if (s[i]==' ')
- printf(" ");
- else {
- if (Empty()) Push(s[i]);
- else {
- while (head!=NULL)
- if (PriorityComp(head->data, s[i]) >= 0) Pop(); else break;
- Push(s[i]);
- }
- }
- /*else if (s[i]=='+' || s[i]=='-') {
- Find('*','/');
- Find ('+', '-');
- Push (s[i]);
- } else if (s[i]=='*' || s[i]=='/') {
- Find ('*', '/');
- Push (s[i]);
- } */
- }
- while (!Empty()) Pop();
- Del();
- }
- int calculator(char* s)
- {
- int j;
- unsigned int len = strlen(s);
- head->data=-1;
- head->next->data=-1;
- for (unsigned int i=0; i<len; i++) {
- if (s[i]=='+') head->data=(int)head->data+(int)head->next->data;
- else if (s[i]=='-') head->data=(int)head->data-(int)head->next->data;
- else if (s[i]=='*') head->data=(int)head->data*(int)head->next->data;
- else if (s[i]=='/') head->data=(int)head->data/(int)head->next->data;
- else if (s[i]=='^') head->data=(int)head->data^(int)head->next->data;
- else if (s[i]<='9' && s[i]>='0') {
- j=i--;
- if (s[j]==' ') {
- if (head->data!=(-1))
- head->next->data=(int)s[i];
- else head->data=(int)s[i];
- } else if (head->next->data!=-1)
- head->data=head->data*10+(int)s[i];
- else
- head->next->data=head->next->data*10+(int)s[i];
- }
- }
- return head->data;
- }
- bool Empty(void)
- {
- if (n == 0)
- return true;
- else
- return false;
- }
- void Push(char d)
- {
- list_t *ins;
- ins = (list_t*)malloc(sizeof(list_t));
- ins->data = d;
- ins->prev = NULL;
- if (Empty()) {
- ins->next = NULL;
- } else {
- ins->next = head;
- }
- ++n; head = ins;
- }
- void Pop()
- {
- char d;
- if (head == NULL)
- return;
- if (n == 1) {
- d = head->data;
- //free(head);
- head = NULL;
- if (d != '(' && d != ')')
- printf("%c ", d);
- } else {
- d = head->data;
- head = head->next;
- //free(head->prev);
- head->prev = NULL;
- if (d != '(' && d != ')')
- printf("%c ", d);
- }
- --n;
- }
- void Init(void)
- {
- head = NULL;
- n = 0;
- }
- void Del(void)
- {
- return;
- list_t *p;
- if (n == 1)
- printf("%c", head->data);
- free(head);
- for (p = head->next; p != NULL; p = p->next)
- {printf("%c", p->prev->data);
- free(p->prev);}
- head = NULL;
- n = 0;
- }
- void Show(void)
- {
- list_t *p;
- if (Empty()) {
- fprintf(stderr, "Steck is empty!\n");
- return;
- }
- for (p = head; p != NULL; p = p->next)
- printf("%c\t", p->data);
- printf("\n");
- }
- void Find (char a, char b)
- {
- list_t *p;
- p = head;
- while (p!=NULL)
- {
- if (p->data==a || p->data==b)
- printf("%c", p->data);
- p=p->next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement