Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.52 KB | None | 0 0
  1.  
  2. #include <stdio.h>  /* getchar, printf */
  3. #include <stdlib.h> /* NULL */
  4. #include "scanner.h"
  5. #include "recognizeEq.h"
  6. #include <string.h> // Used for strcmp, strlen and strcpy
  7.  
  8.  
  9.  
  10.  
  11. int acceptNumber(List *lp) {
  12.   if (*lp != NULL && (*lp)->tt == Number) {
  13.     *lp = (*lp)->next;
  14.     return 1;
  15.   }
  16.   return 0;
  17. }
  18.  
  19. int acceptIdentifier(List *lp) {
  20.   if (*lp != NULL && (*lp)->tt == Identifier) {
  21.     *lp = (*lp)->next;
  22.     return 1;
  23.   }
  24.   return 0;
  25. }
  26.  
  27. int acceptCharacter(List *lp, char c) {
  28.   if (*lp != NULL && (*lp)->tt == Symbol && ((*lp)->t).symbol == c) {
  29.     *lp = (*lp)->next;
  30.     return 1;
  31.   }
  32.   return 0;
  33. }
  34. ////////////////////////////////////////////////////////////////////////////////
  35.  
  36. int acceptTerm(List *lp){ // function to recognize a term
  37.   if(acceptNumber(lp)){
  38.     if(acceptIdentifier(lp)){
  39.       if(acceptCharacter(lp,'^')){  // it can either have a power or not
  40.         if(acceptNumber(lp)){
  41.           return 1;     // this is the case in which a term has all the possible combinations
  42.         }
  43.         else return 0;
  44.       }
  45.       else return 1;
  46.     }
  47.     else return 1;
  48.   } else {
  49.     if(acceptIdentifier(lp)){   // The case in which the identifier has no number before it
  50.       if(acceptCharacter(lp, '^')){
  51.         if(acceptNumber(lp)){
  52.           return 1;
  53.         }
  54.         else return 0;
  55.       }
  56.       else return 1;
  57.     }
  58.   }
  59.   return 0;   // in this case if it does not go into any of the ifs then it is not a term.
  60. }
  61.  
  62.  
  63. int acceptExpression(List *lp){   // function to recognize an expression, we assume it is an expression until we find a mistake and return 0
  64.   if(acceptCharacter(lp,'-')){    // We can either have a '-' or not
  65.     if(!acceptTerm(lp)){
  66.       return 0;
  67.     }
  68.   } else {
  69.     if(!acceptTerm(lp)){
  70.       return 0;
  71.     }
  72.   }
  73.   while(acceptCharacter(lp, '+') || acceptCharacter(lp, '-')){
  74.     if(!acceptTerm(lp)){
  75.       return 0;
  76.     }
  77.   }
  78.   return 1;
  79. }
  80.  
  81.  
  82.  
  83.  
  84.  
  85. int acceptEquation(List *lp){ // the easy function to determine an equation
  86.   if(!acceptExpression(lp)){
  87.     return 0;
  88.   }
  89.   if(!acceptCharacter(lp,'=')){
  90.     return 0;
  91.   }
  92.   if(!acceptExpression(lp)){
  93.     return 0;
  94.   }
  95.   return 1;
  96. }
  97.  
  98. int checkPower(List *lp){ // our function that computes the highestdegree
  99.   int highestdegree=1;    // we assume that the equation starts with the degree 1
  100.   int degree=0;
  101.   while (*lp!=NULL) {     // we iterate thorugh the list and everytime we find a higher degree we update our variable.
  102.     if (((*lp)->t).symbol == '^') {
  103.       *lp = (*lp)->next;
  104.       degree=(*lp)->t.number;
  105.     }
  106.  
  107.     if(degree>highestdegree){
  108.       highestdegree=degree;
  109.     }
  110.     *lp = (*lp)->next;
  111.   }
  112.   free(*lp);
  113.   return highestdegree; // we output the highest degree.
  114.  
  115. }
  116.  
  117. int checkIdentifier(List *lp){  //our funtion that returns 1 if the equation is in one variable.
  118.   char f[10000]="";             // we initialise the strings empty
  119.   char g[10000]="";
  120.   int validCheck=1;
  121.   int variableCounter=0;        // we don't count all variables now but we can adapt it for the second part of the assignment if we need to
  122.  
  123.   while(*lp!=NULL){             // we iterate through the list and we store the identifiers in 2 strings.
  124.     if (variableCounter == 0 && (*lp)->tt == Identifier) {
  125.       strcpy(f,(*lp)->t.identifier);
  126.       strcpy(g,(*lp)->t.identifier);
  127.       variableCounter++;
  128.  
  129.     } else if(variableCounter>0 && (*lp)->tt == Identifier){    // if we already encountered an identifier we store the new one in only one string
  130.       strcpy(g,(*lp)->t.identifier);
  131.     }
  132.     if(strcmp(f,g)!=0){         // if the strings are not the same it means we have more than one variable so we make validCheck 0
  133.       validCheck=0;
  134.     }
  135.  
  136.  
  137.     if(validCheck==0){          // we use this if to get out of the while loop if we already found 2 different identifiers
  138.       return 0;
  139.     }
  140.     *lp = (*lp)->next;
  141.   }
  142.   if(strlen(f)<1 && strlen(g)<1){   // if we did not find 2 different identifiers we first check to see if the strings did not remain empty
  143.     return 0;
  144.     }
  145.     free(*lp);
  146.   return 1;       // If no different identifiers and the strings are non empty we return 1
  147.  
  148. }
  149.  
  150. float solveEquation(List *lp){
  151.   int identifiercounter,auxcounter,freeterms;
  152.   float finalvalue;
  153.   identifiercounter=0;
  154.   auxcounter=0;
  155.   freeterms=0;
  156.   int powerholder=0;
  157.   while((*lp)->t.symbol != '='){
  158.     if(((*lp)->t).symbol == '-'){
  159.       *lp = (*lp)->next;                              // - 3 x ^ 1  - 3 x + 3 - 3 + x - x =
  160.       if((*lp)->tt == Number){                        // - 3 x ^ 1 = 7
  161.         auxcounter=(*lp)->t.number;
  162.         *lp = (*lp)->next;
  163.         if((*lp)->tt == Identifier){
  164.           *lp = (*lp)->next;
  165.           if((*lp)->t.symbol == '^'){
  166.             *lp = (*lp)->next;
  167.             powerholder=(*lp)->t.number;
  168.             if(powerholder==1){
  169.               identifiercounter=identifiercounter-auxcounter;
  170.               auxcounter=0;
  171.             }
  172.             else if(powerholder==0){
  173.               freeterms=freeterms-auxcounter;
  174.             }
  175.           } else {
  176.           identifiercounter=identifiercounter-auxcounter;
  177.           auxcounter=0;
  178.         }
  179.  
  180.         } else {
  181.           freeterms=freeterms-auxcounter;
  182.           auxcounter=0;
  183.         }
  184.       } else if((*lp)->tt == Identifier){
  185.           identifiercounter=identifiercounter-1;
  186.           *lp = (*lp)->next;
  187.       }
  188.     } else if ((*lp)->tt == Number ){
  189.       auxcounter=(*lp)->t.number;
  190.       *lp = (*lp)->next;
  191.       if((*lp)->tt == Identifier){
  192.         identifiercounter=identifiercounter+auxcounter;
  193.         auxcounter=0;
  194.         *lp = (*lp)->next;
  195.       } else {
  196.         freeterms=freeterms+auxcounter;
  197.         auxcounter=0;
  198.       }
  199.  
  200.     } else if ( (*lp)->tt == Identifier) {
  201.       identifiercounter=identifiercounter+1;
  202.       *lp = (*lp)->next;
  203.     } else if ( ((*lp)->t).symbol == '+' ){
  204.       *lp = (*lp)->next;
  205.     }
  206.  
  207.   } // end brace for while
  208.   *lp = (*lp)->next;
  209.   printf("We ve existed the first while loop\n" );
  210.   printf("identifiercounter after the first while is %d\n",identifiercounter );
  211.   printf("freeterm value after the first while is %d\n",freeterms );
  212.   while(*lp!=NULL){
  213.   //  printf("I got in the second while\n" );
  214.     if(((*lp)->t).symbol == '-'){
  215.       *lp = (*lp)->next;                              // 3 x - 3 x + 3 - 3 + x - x =
  216.       if((*lp)->tt == Number){
  217.         auxcounter=(*lp)->t.number;
  218.         *lp = (*lp)->next;
  219.         if(*lp==NULL){
  220.           freeterms=freeterms-auxcounter;
  221.           auxcounter=0;
  222.           break;
  223.         }
  224.         if((*lp)->tt == Identifier){
  225.           identifiercounter=identifiercounter+auxcounter;
  226.           auxcounter=0;
  227.           *lp = (*lp)->next;
  228.         } else {
  229.           freeterms=freeterms+auxcounter;
  230.           auxcounter=0;
  231.         }
  232.       } else if((*lp)->tt == Identifier){
  233.           identifiercounter=identifiercounter+1;
  234.           *lp = (*lp)->next;
  235.       }
  236.     } else if ((*lp)->tt == Number ){
  237.       auxcounter=(*lp)->t.number;
  238.       *lp = (*lp)->next;
  239.       if(*lp==NULL){
  240.         freeterms=freeterms-auxcounter;
  241.         auxcounter=0;
  242.         break;
  243.       }
  244.       if((*lp)->tt == Identifier){
  245.         identifiercounter=identifiercounter-auxcounter;
  246.         auxcounter=0;
  247.         *lp = (*lp)->next;
  248.       } else {
  249.         freeterms=freeterms-auxcounter;
  250.         auxcounter=0;
  251.       }
  252.  
  253.     } else if ( (*lp)->tt == Identifier) {
  254.       identifiercounter=identifiercounter-1;
  255.       *lp = (*lp)->next;
  256.     } else if ( ((*lp)->t).symbol == '+' ){
  257.       printf("i got to the plus\n" );
  258.       *lp = (*lp)->next;
  259.     }
  260.     printf("I am here at the end\n" );
  261.  
  262.   }
  263.   printf("We ve existed the second while loop\n" );
  264.   printf("freeterm is %d\n",freeterms );
  265.   printf("identifiercounter is %d\n",identifiercounter );
  266.   finalvalue=(float)freeterms/identifiercounter;
  267.   finalvalue=finalvalue*(-1);
  268.   if(finalvalue==-0.000){
  269.     finalvalue=0.000;
  270.   }
  271.   if(identifiercounter==0){
  272.     return 69;
  273.   }
  274.   return finalvalue;
  275.  
  276. }
  277.  
  278.  
  279.  
  280.  
  281. /* The function recognizeEquation tells us if it is an equation or not */
  282. void recognizeEquation() {
  283.   char *ar;
  284.   List tl,tl1,tl2,tl3;    // we created 2 more lists because we did not if we are allowed to use tl
  285.   int idenvar,power;      // this variables hold the values for the identifier and degree
  286.   float solution;
  287.   ar = readInput();
  288.   while (ar[0] != '!') {
  289.     tl = tokenList(ar);
  290.     printf("give an equation: ");
  291.     printList(tl);
  292.     tl1 = tl;
  293.     tl2 = tl1;
  294.     tl3 = tl1;
  295.     idenvar=checkIdentifier(&tl2);  // we use tl2 to check for identifiers
  296.     power=checkPower(&tl3);         // and tl3 to check for powers
  297.  
  298.     if (acceptEquation(&tl1) && tl1 == NULL) {
  299.       if(idenvar==1){               // if it is an equation and it has only one variable
  300.         if(power==1){
  301.           solution=solveEquation(&tl);
  302.           if(solution!=69){
  303.           printf("this is an equation in 1 variable of degree %d\n",power);
  304.           printf("solution: %0.3f",solution);
  305.         } else {printf("this is an equation in 1 variable of degree %d\n",power);
  306.         printf("not solvable");}
  307.         } else {
  308.         printf("this is an equation in 1 variable of degree %d",power);
  309.       }
  310.  
  311.       } else {                       // it is an equation but not in one variable
  312.         printf("this is an equation, but not in 1 variable\n");
  313.       }
  314.  
  315.  
  316.     } else {
  317.       printf("this is not an equation\n");
  318.     }
  319.     printf("\n" );
  320.     free(ar);
  321.     freeTokenList(tl);
  322.     ar = readInput();
  323.   }
  324.   free(ar);
  325.   printf("give an equation: good bye\n");
  326.  
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement