Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define stacksize 100
- #define TYPE int
- typedef struct
- {
- int top;
- char items[stacksize];
- } stack;
- void initialize (stack *s)
- {
- s->top=0;
- }
- void push (stack*s,char value)
- {
- s->items[s->top++]=value;
- }
- int pop (stack*s)
- {
- return s->items[--s->top];
- }
- int isfull(stack*s)
- {
- return s->top<stacksize? 0:1;
- }
- int isempty(stack *s)
- {
- return s->top==0? 1:0;
- }
- void error()
- {
- printf("error");
- exit(-1);
- }
- typedef struct {
- TYPE data;
- char data2;
- struct Node *next;
- } Node;
- Node * newNode(TYPE value,char b[]) {
- Node *n=malloc(sizeof(Node));
- n->data=value;
- n->data2=b;
- n->next=NULL;
- return n;
- }
- typedef struct {
- Node *head;
- Node *tail;
- } LinkedList;
- void initLinkedList(LinkedList *ll){ll->head=NULL;ll->tail=NULL;}
- void printLinkedList(Node *n){
- printf("[");
- while (n) {printf("%d ",n->data);n=(Node *)n->next;}
- printf("]\n");
- }
- void addHead(LinkedList *ll,TYPE value,char b[]) {
- Node *n=newNode(value, b);
- n->next=ll->head;
- ll->head=n;
- if(!ll->tail) ll->tail=n; //list was empty
- }
- void addTail(LinkedList *ll,TYPE value,char b[]) {
- Node *n=newNode(value,b);Node *tail=ll->tail;
- ll->tail=n;
- if (tail) tail->next=n;
- else ll->head=n; // TAIL is NULL so is head!
- }
- TYPE deleteHead(LinkedList *ll){
- Node *first=(Node*)ll->head;
- if (first){
- TYPE value=first->data;
- ll->head=(Node *)first->next;
- free(first);
- if (!ll->head) ll->tail=NULL;
- return value;
- }
- return -1 ;/* ??? this is BAD */
- }
- Node * search(Node *n,TYPE value,char b[]) {
- if (!n) return NULL;
- if (n->data==value&&n->data2==b) return n;
- else return search(n->next,value,b);
- }
- char* topost(char a[])
- {
- int i;
- char y;
- stack m;
- initialize(&m);
- char b[200];
- int j=0;
- for (i=0; i<strlen(a); i++)
- {
- if (a[i]>='0'&&a[i]<='9')
- {
- printf("%c",a[i]);
- b[j++]=a[i];
- }
- else
- {
- switch (a[i])
- {
- case'(':
- push(&m,a[i]);
- break;
- case '+':
- case '-':
- case '*':
- case '/':
- push(&m,a[i]);
- break;
- case ')':
- while (!isempty(&m))
- {
- int c=pop(&m);
- if (c=='(')
- break;
- else
- {
- printf("%c",c);
- b[j++]=c;
- }
- }
- break;
- }
- }
- }
- }
- int top(char a[])
- {
- int i;
- int j=0;
- int c;
- char b[200];
- stack m;
- for (i=0; i<strlen(a); i++)
- {
- if (a[i]>='0'&&a[i]<='9')
- {
- printf("%c",a[i]);
- b[j++]=a[i];
- }
- else
- {
- switch (a[i])
- {
- case'(':
- push(&m,a[i]);
- break;
- case '+':
- case '-':
- case '*':
- case '/':
- push(&m,a[i]);
- break;
- case ')':
- while (!isempty(&m))
- {
- c=pop(&m);
- if (c=='(')
- break;
- else
- {
- printf("%c",c);
- b[j++]=c;
- }
- }
- }
- }
- }
- }
- int eval(char a[])
- {
- stack h;
- initialize(&h);
- char *postExp=a;
- char ch, *pt=postExp;
- while (ch=*pt++)
- {
- if (ch==' ') continue;
- if (ch<='9' && ch>='0') push(&h,ch-'0');
- else
- {
- int e,f;
- if (isempty(&h)) error();
- else e=pop(&h);
- if (isempty(&h)) error();
- else f=pop(&h);
- switch(ch)
- {
- case '+':
- push(&h,e+f);
- break;
- case '-':
- push(&h,e-f);
- break;
- case '*':
- push(&h,e*f);
- break;
- case '/':
- push(&h,e/f);
- break;
- default:
- error();
- } // switch
- }
- }
- int z=pop(&h);
- printf("=%d",z);
- return z;
- }
- void putinmemory(char b[])
- {
- LinkedList *s;
- int v=eval(b);
- initLinkedList(s);
- Node*n=newNode(v,b);
- addTail(s,v,b);
- }
- int main()
- {
- stack m;
- char c;
- char p[90];
- initialize(&m);
- char a[100];
- char b[100];
- LinkedList ll;
- initLinkedList(&ll);
- while (1)
- {
- gets(a);
- topost(a);
- eval(a);
- putinmemory(a);
- }
- /* if (a[i]==' ')
- continue;
- if(a[i]>='0'&&a[i]<='9')printf("%c",a[i]);
- if (a[i]==')'){
- if(isempty(&s)){
- printf("error");
- }
- else{
- printf("%c",pop(&s));
- }
- }
- else{
- push(&s,a[i]);
- }
- }*/
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement