MouhcineFD

CALCULATOR

Nov 26th, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.83 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. #define lengthOfString 100
  5.  
  6. typedef struct operand_s operand_s;
  7. struct operand_s
  8. {
  9.     double theOperand;
  10.     char theOperator;
  11.     int theDegree;
  12.     operand_s *nextOperand;
  13. };
  14.  
  15. void intialization_operands(operand_s *myOperands, int nbrOperands)
  16. {
  17.     for (int i = 0; i < nbrOperands; i++)
  18.     {
  19.         operand_s *newOperand = malloc(sizeof(*newOperand));
  20.         newOperand->theOperand = 0;
  21.         newOperand->theOperator = ' ';
  22.         newOperand->theDegree = 0;
  23.         newOperand->nextOperand = myOperands->nextOperand;
  24.         myOperands->nextOperand = newOperand;
  25.     }
  26. }
  27.  
  28. int nmbrOfDigInOper(char *myOperation)
  29. {
  30.     int theNumbre = 0;
  31.     for (int i = 0; myOperation[i] != '\0'; i++)
  32.     {
  33.         if (myOperation[i] >= 48 && myOperation[i] <= 57)
  34.         {
  35.             theNumbre++;
  36.             while (myOperation[i] >= 48 && myOperation[i] <= 57 || myOperation[i] == '.')
  37.             {
  38.                 i++;
  39.             }
  40.         }
  41.     }
  42.     return theNumbre;
  43. }
  44.  
  45. int first_treatment(char *myOperation, operand_s *myOperands)
  46. {
  47.     int degree = 0, largDegree = 0;
  48.  
  49.     operand_s *temporaryOp = myOperands;
  50.  
  51.     for (int i = 0; myOperation[i] != '\0'; i++)
  52.     {
  53.         if (myOperation[i] == '(')
  54.         {
  55.             degree += 2;
  56.             if (degree > largDegree)
  57.                 largDegree = degree;
  58.         }
  59.         else if (myOperation[i] == ')')
  60.         {
  61.             degree -= 2;
  62.             temporaryOp->theDegree -= 2;
  63.         }
  64.         else if (myOperation[i] >= 48 && myOperation[i] <= 57)
  65.         {
  66.             temporaryOp->theDegree = degree;
  67.             temporaryOp->theOperand = strtod(&myOperation[i], NULL);
  68.             if (myOperation[i - 2] == '(')
  69.                 temporaryOp->theOperand *= -1;
  70.             while (myOperation[i] >= 48 && myOperation[i] <= 57 || myOperation[i] == '.')
  71.             {
  72.                 i++;
  73.             }
  74.             i--;
  75.  
  76.         }
  77.         else if (myOperation[i] == '*' || myOperation[i] == '/')
  78.         {
  79.             temporaryOp->theDegree++;
  80.             if (temporaryOp->theDegree > largDegree)
  81.                 largDegree = temporaryOp->theDegree;
  82.             temporaryOp->theOperator = myOperation[i];
  83.             temporaryOp = temporaryOp->nextOperand;
  84.         }
  85.         else if (myOperation[i] == '+' || myOperation[i] == '-')
  86.         {
  87.             if (myOperation[i - 1] != '(')
  88.             {
  89.                 temporaryOp->theOperator = myOperation[i];
  90.                 temporaryOp = temporaryOp->nextOperand;
  91.             }
  92.         }
  93.  
  94.     }
  95.     return largDegree;
  96. }
  97.  
  98. void second_treatment(operand_s *myOperands, int largDegree)
  99. {
  100.     operand_s *temporaryOp = myOperands, *operandADelet = NULL;
  101.     while (temporaryOp->nextOperand != NULL)
  102.     {
  103.         if (temporaryOp->theDegree == largDegree)
  104.         {
  105.             switch (temporaryOp->theOperator)
  106.             {
  107.             case '*':
  108.                 temporaryOp->theOperand *= temporaryOp->nextOperand->theOperand;
  109.                 break;
  110.             case '/':
  111.                 temporaryOp->theOperand /= temporaryOp->nextOperand->theOperand;
  112.                 break;
  113.             case '+':
  114.                 temporaryOp->theOperand += temporaryOp->nextOperand->theOperand;
  115.                 break;
  116.             case '-':
  117.                 temporaryOp->theOperand -= temporaryOp->nextOperand->theOperand;
  118.                 break;
  119.             }
  120.             temporaryOp->theOperator = temporaryOp->nextOperand->theOperator;
  121.             temporaryOp->theDegree = temporaryOp->nextOperand->theDegree;
  122.             operandADelet = temporaryOp->nextOperand;
  123.             temporaryOp->nextOperand = temporaryOp->nextOperand->nextOperand;
  124.             free(operandADelet);
  125.         }
  126.         else if (temporaryOp->theDegree != largDegree)
  127.             temporaryOp = temporaryOp->nextOperand;
  128.     }
  129. }
  130.  
  131. int main()
  132. {
  133.     int nmbrOfDig, largDegree;
  134.     char theOperation[lengthOfString] = { ' ' };
  135.     operand_s *theOperands = malloc(sizeof(*theOperands));
  136.  
  137.     theOperands->theOperand = 0;
  138.     theOperands->theOperator = ' ';
  139.     theOperands->theDegree = 0;
  140.     theOperands->nextOperand = NULL;
  141.  
  142.     scanf("%s", theOperation);
  143.  
  144.     nmbrOfDig = nmbrOfDigInOper((char*)&theOperation);
  145.     intialization_operands(theOperands, nmbrOfDig - 1);
  146.     largDegree = first_treatment((char*)&theOperation, theOperands);
  147.  
  148.     while (largDegree >= 0)
  149.     {
  150.         second_treatment(theOperands, largDegree);
  151.         largDegree--;
  152.     }
  153.  
  154.     printf("%s = %lf\n", theOperation, theOperands->theOperand);
  155.  
  156.     free(theOperands);
  157.     return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment