Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #define lengthOfString 100
- typedef struct operand_s operand_s;
- struct operand_s
- {
- double theOperand;
- char theOperator;
- int theDegree;
- operand_s *nextOperand;
- };
- void intialization_operands(operand_s *myOperands, int nbrOperands)
- {
- for (int i = 0; i < nbrOperands; i++)
- {
- operand_s *newOperand = malloc(sizeof(*newOperand));
- newOperand->theOperand = 0;
- newOperand->theOperator = ' ';
- newOperand->theDegree = 0;
- newOperand->nextOperand = myOperands->nextOperand;
- myOperands->nextOperand = newOperand;
- }
- }
- int nmbrOfDigInOper(char *myOperation)
- {
- int theNumbre = 0;
- for (int i = 0; myOperation[i] != '\0'; i++)
- {
- if (myOperation[i] >= 48 && myOperation[i] <= 57)
- {
- theNumbre++;
- while (myOperation[i] >= 48 && myOperation[i] <= 57 || myOperation[i] == '.')
- {
- i++;
- }
- }
- }
- return theNumbre;
- }
- int first_treatment(char *myOperation, operand_s *myOperands)
- {
- int degree = 0, largDegree = 0;
- operand_s *temporaryOp = myOperands;
- for (int i = 0; myOperation[i] != '\0'; i++)
- {
- if (myOperation[i] == '(')
- {
- degree += 2;
- if (degree > largDegree)
- largDegree = degree;
- }
- else if (myOperation[i] == ')')
- {
- degree -= 2;
- temporaryOp->theDegree -= 2;
- }
- else if (myOperation[i] >= 48 && myOperation[i] <= 57)
- {
- temporaryOp->theDegree = degree;
- temporaryOp->theOperand = strtod(&myOperation[i], NULL);
- if (myOperation[i - 2] == '(')
- temporaryOp->theOperand *= -1;
- while (myOperation[i] >= 48 && myOperation[i] <= 57 || myOperation[i] == '.')
- {
- i++;
- }
- i--;
- }
- else if (myOperation[i] == '*' || myOperation[i] == '/')
- {
- temporaryOp->theDegree++;
- if (temporaryOp->theDegree > largDegree)
- largDegree = temporaryOp->theDegree;
- temporaryOp->theOperator = myOperation[i];
- temporaryOp = temporaryOp->nextOperand;
- }
- else if (myOperation[i] == '+' || myOperation[i] == '-')
- {
- if (myOperation[i - 1] != '(')
- {
- temporaryOp->theOperator = myOperation[i];
- temporaryOp = temporaryOp->nextOperand;
- }
- }
- }
- return largDegree;
- }
- void second_treatment(operand_s *myOperands, int largDegree)
- {
- operand_s *temporaryOp = myOperands, *operandADelet = NULL;
- while (temporaryOp->nextOperand != NULL)
- {
- if (temporaryOp->theDegree == largDegree)
- {
- switch (temporaryOp->theOperator)
- {
- case '*':
- temporaryOp->theOperand *= temporaryOp->nextOperand->theOperand;
- break;
- case '/':
- temporaryOp->theOperand /= temporaryOp->nextOperand->theOperand;
- break;
- case '+':
- temporaryOp->theOperand += temporaryOp->nextOperand->theOperand;
- break;
- case '-':
- temporaryOp->theOperand -= temporaryOp->nextOperand->theOperand;
- break;
- }
- temporaryOp->theOperator = temporaryOp->nextOperand->theOperator;
- temporaryOp->theDegree = temporaryOp->nextOperand->theDegree;
- operandADelet = temporaryOp->nextOperand;
- temporaryOp->nextOperand = temporaryOp->nextOperand->nextOperand;
- free(operandADelet);
- }
- else if (temporaryOp->theDegree != largDegree)
- temporaryOp = temporaryOp->nextOperand;
- }
- }
- int main()
- {
- int nmbrOfDig, largDegree;
- char theOperation[lengthOfString] = { ' ' };
- operand_s *theOperands = malloc(sizeof(*theOperands));
- theOperands->theOperand = 0;
- theOperands->theOperator = ' ';
- theOperands->theDegree = 0;
- theOperands->nextOperand = NULL;
- scanf("%s", theOperation);
- nmbrOfDig = nmbrOfDigInOper((char*)&theOperation);
- intialization_operands(theOperands, nmbrOfDig - 1);
- largDegree = first_treatment((char*)&theOperation, theOperands);
- while (largDegree >= 0)
- {
- second_treatment(theOperands, largDegree);
- largDegree--;
- }
- printf("%s = %lf\n", theOperation, theOperands->theOperand);
- free(theOperands);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment