⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 expressiontree.c

📁 this is program on scientfic calculator.
💻 C
字号:
//Calculater..........
//             Jitendra Bansal         y05uc054
//             Lavlesh Garg            y05uc058
//             Nishant Sharma          y05uc068
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
typedef struct node{
                        char ch;
                        float data;
                        struct node* left;
                        struct node* right;
                        struct node* parent;
                        }Node;
char infix[100];
int pref(char);
float ExpressionTree(Node **ptr);
float InorderTreeWalk(Node *ptr);
main()
{
    float f;
    Node *head=NULL;
   // printf("address of head is %d\n",&head);
    printf("Give The Infix Expression::");
    scanf("%s",infix);
  //  Node *head=NULL;
    f=ExpressionTree(&head);
    printf("\nResult of Infix Expression is::%.2f\n",f);
    getch();
}
float ExpressionTree(Node **ptr)
{
    char sign=' ';
    char num[10];
    char math[20]={'\0'};
    int j=0;
    int l=0;
    int i=0;
    float n;
    char c;
    static int k=0;
    int m;
    Node *temp1=NULL;
    Node *temp2=NULL;
    Node *ptr2=NULL;
    Node *ptr1;
    //printf("address of temp2 is %d\n",&temp2);
     //printf("address of temp1 is %d\n",&temp1);
     //printf("address of ptr2 is %d\n",&ptr2);
     //printf("address of ptr1 is %d\n",&ptr1);
       //getch();
   //exit(1);
    //ptr1=*ptr;
     //printf("value of ptr1 is %d\n",ptr1);
      //printf("address of ptr is %d\n",&ptr);
       //printf("value of ptr is %d\n",ptr);

      //getch();

 //   Node *ptr2=NULL;
    while(infix[k]!='\0')
    {
        c=infix[k++];
        if(c=='(')
        {
                n=ExpressionTree(&ptr2);
                printf("\n");
                goto A;
        }
        if(c==')')
               break;
        if(c=='+' || c=='-' || c=='/' || c=='*')
        {
                temp1=malloc(sizeof(Node));
                temp1->ch=c;
               //printf("address of temp1 is%d\n",&temp1);
               //getch();
              //  exit(1);
                if(sign==' ')
                {
                                temp1->left=temp2;
                                temp2->parent=temp1;
                                temp1->right=NULL;
                                temp1->parent=NULL;
                                *ptr=temp1;
                                ptr1=temp1;
                                //printf("value of temp1 %d\n",&temp1);
                               //printf("value otemp2 is %d\n",&temp2);
                                //printf("value of ptr %d\n",ptr);
                                //printf("value of ptr1 %d\n",ptr1);
                                //getch();
                                //exit(1);
                }
                else if(pref(c)>pref(sign))
                {
                                temp1->left=ptr1->right;
                                ptr1->right->parent=temp1;
                                temp1->parent=ptr1;
                                ptr1->right=temp1;
                                temp1->right=NULL;
                                ptr1=temp1;
                }
                else if(pref(c)<pref(sign))
                {
                //printf("parent of ptr1 is %d\n",ptr1->parent);
                //getch();
                            while(ptr1->parent!=NULL)
                                       ptr1=ptr1->parent;
                            ptr1->parent=temp1;
                            temp1->left=ptr1;
                            temp1->right=NULL;
                            temp1->parent=NULL;
                            *ptr=temp1;
                            ptr1=temp1;
                }
                else if(pref(c)==pref(sign))
                {
                         temp1->parent=ptr1->parent;
                         if(ptr1->parent!=NULL)
                                           ptr1->parent->right=temp1;
                         ptr1->parent=temp1;
                         temp1->left=ptr1;
                         temp1->right=NULL;
                         ptr1=temp1;
                         if((*ptr)->parent!=NULL)
                                 *ptr=temp1;
                }
                j=0;
                sign=c;
       }
       else if((c>47 && c<58) || c=='.')
       {
                if(i!=0)
                {
                         math[i]='\0';
                         i=0;
                }
                num[j++]=c;
                if(infix[k]=='+' || infix[k]=='-' || infix[k]=='/' || infix[k]=='*' || infix[k]=='(' || infix[k]==')' || infix[k]=='\0')
                {
                      num[j]='\0';
                      n=atof(num);
A:                    temp2=malloc(sizeof(Node));
                    // printf("value of temp2 is %d\n",&temp2);
                     // getch();
                   // exit(1);
                      if(math[0]!='\0')
                      {
                                if(!strcmp(math,"sin"))
                                          n=sin(n*3.14/180);
                                else if(!strcmp(math,"cos"))
                                          n=cos(n*3.14/180);
                                else if(!strcmp(math,"tan"))
                                          n=tan(n*3.14/180);
                                else if(!strcmp(math,"cosec"))
                                          n=1/sin(n*3.14/180);
                                else if(!strcmp(math,"cot"))
                                          n=1/tan(n*3.14/180);
                                else if(!strcmp(math,"sec"))
                                          n=1/cos(n*3.14/180);
                                else if(!strcmp(math,"log"))
                                          n=log10(n);
                                math[0]='\0';
                                i=0;
                      }
                      temp2->data=n;
                      temp2->ch=' ';
                      temp2->left=NULL;
                      temp2->right=NULL;
                      temp2->parent=ptr1;
                      if(ptr1==NULL)
                     // {
                     // printf("value of temp2 at start is %d\n",&temp2);
                             *ptr=temp2;
                     // printf("value of ptr at start is %d\n",ptr);
                     // getch();
                     // exit(1);
                     // }
                      if(temp1!=NULL)
                      {
                                if(temp1->left==NULL)
                                       temp1->left=temp2;
                                else
                                       temp1->right=temp2;
                      }
                }
        }
        else
               math[i++]=c;
    }
    return(InorderTreeWalk((*ptr)));
}
int pref(char c)
{
    if(c=='+' || c=='-')
        return 1;
    if(c=='*' || c=='/')
        return 2;
    if(c=='(' || c==')')
        return 3;
    return 0;

}
float InorderTreeWalk(Node *ptr)
{
    float left=0;
    float right=0;
    if(ptr!=NULL)
    {
        left=InorderTreeWalk(ptr->left);
        if(ptr->ch==' ')
                printf("%0.2f ",ptr->data);
        else
                printf("%c ",ptr->ch);
        right=InorderTreeWalk(ptr->right);
        if(ptr->left==NULL && ptr->right==NULL)
                return ptr->data;
        else if(ptr->ch=='+')
                return left+right;
        else if(ptr->ch=='-')
                return left-right;
        else if(ptr->ch=='*')
                return left*right;
        else if(ptr->ch=='/')
                return left/right;
    }
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -