Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.45 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;
  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){
  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.               auxcounter=0;
  175.             }
  176.           }
  177.           identifiercounter=identifiercounter-auxcounter;
  178.           auxcounter=0;
  179.         } else {
  180.           freeterms=freeterms-auxcounter;
  181.           auxcounter=0;
  182.         }
  183.       } else if((*lp)->tt == Identifier){
  184.           identifiercounter=identifiercounter-1;
  185.       }
  186.     } else if ((*lp)->tt == Number ){
  187.       auxcounter=(*lp)->t.number;
  188.       *lp = (*lp)->next;
  189.       if((*lp)->tt == Identifier){
  190.         identifiercounter=identifiercounter+auxcounter;
  191.         auxcounter=0;
  192.         *lp = (*lp)->next;
  193.       } else {
  194.         freeterms=freeterms+auxcounter;
  195.         auxcounter=0;
  196.       }
  197.  
  198.     } else if ( (*lp)->tt == Identifier) {
  199.       identifiercounter=identifiercounter+1;
  200.       *lp = (*lp)->next;
  201.     } else if ( ((*lp)->t).symbol == '+' ){
  202.       *lp = (*lp)->next;
  203.     }
  204.  
  205.   } // end brace for while
  206.   *lp = (*lp)->next;
  207.   printf("We ve existed the first while loop\n" );
  208.   printf("identifiercounter after the first while is %d\n",identifiercounter );
  209.   printf("freeterm value after the first while is %d\n",freeterms );
  210.   while(*lp!=NULL){
  211.     printf("I got in the second while\n" );
  212.     if(((*lp)->t).symbol == '-'){
  213.       *lp = (*lp)->next;                              // 3 x - 3 x + 3 - 3 + x - x =
  214.       if((*lp)->tt == Number){
  215.         auxcounter=(*lp)->t.number;
  216.         *lp = (*lp)->next;
  217.         if(*lp==NULL){
  218.           freeterms=freeterms-auxcounter;
  219.           auxcounter=0;
  220.           break;
  221.         }
  222.         if((*lp)->tt == Identifier){
  223.           identifiercounter=identifiercounter+auxcounter;
  224.           auxcounter=0;
  225.           *lp = (*lp)->next;
  226.         } else {
  227.           freeterms=freeterms+auxcounter;
  228.           auxcounter=0;
  229.         }
  230.       } else if((*lp)->tt == Identifier){
  231.           identifiercounter=identifiercounter+1;
  232.           *lp = (*lp)->next;
  233.       }
  234.     } else if ((*lp)->tt == Number ){
  235.       auxcounter=(*lp)->t.number;
  236.       *lp = (*lp)->next;
  237.       if(*lp==NULL){
  238.         freeterms=freeterms-auxcounter;
  239.         auxcounter=0;
  240.         break;
  241.       }
  242.       if((*lp)->tt == Identifier){
  243.         identifiercounter=identifiercounter-auxcounter;
  244.         auxcounter=0;
  245.         *lp = (*lp)->next;
  246.       } else {
  247.         freeterms=freeterms-auxcounter;
  248.         auxcounter=0;
  249.       }
  250.  
  251.     } else if ( (*lp)->tt == Identifier) {
  252.       identifiercounter=identifiercounter-1;
  253.       *lp = (*lp)->next;
  254.     } else if ( ((*lp)->t).symbol == '+' ){
  255.       printf("i got to the plus\n" );
  256.       *lp = (*lp)->next;
  257.     }
  258.    printf("I am here at the end\n" );
  259.  
  260.   }
  261.   printf("We ve existed the second while loop\n" );
  262.   printf("freeterm is %d\n",freeterms );
  263.   printf("identifiercounter is %d\n",identifiercounter );
  264.   finalvalue=(float)freeterms/identifiercounter;
  265.   finalvalue=finalvalue*(-1);
  266.   if(finalvalue==-0.000){
  267.     finalvalue=0.000;
  268.   }
  269.   if(identifiercounter==0){
  270.     return 69;
  271.   }
  272.   return finalvalue;
  273.  
  274. }
  275.  
  276.  
  277.  
  278.  
  279. /* The function recognizeEquation tells us if it is an equation or not */
  280. void recognizeEquation() {
  281.   char *ar;
  282.   List tl,tl1,tl2,tl3;    // we created 2 more lists because we did not if we are allowed to use tl
  283.   int idenvar,power;      // this variables hold the values for the identifier and degree
  284.   float solution;
  285.   ar = readInput();
  286.   while (ar[0] != '!') {
  287.     tl = tokenList(ar);
  288.     printf("give an equation: ");
  289.     printList(tl);
  290.     tl1 = tl;
  291.     tl2 = tl1;
  292.     tl3 = tl1;
  293.     idenvar=checkIdentifier(&tl2);  // we use tl2 to check for identifiers
  294.     power=checkPower(&tl3);         // and tl3 to check for powers
  295.  
  296.     if (acceptEquation(&tl1) && tl1 == NULL) {
  297.       if(idenvar==1){               // if it is an equation and it has only one variable
  298.         if(power==1){
  299.           solution=solveEquation(&tl);
  300.           if(solution!=69){
  301.           printf("this is an equation in 1 variable of degree %d\n",power);
  302.           printf("solution: %0.3f",solution);
  303.         } else {printf("this is an equation in 1 variable of degree %d\n",power);
  304.         printf("not solvable");}
  305.         } else {
  306.         printf("this is an equation in 1 variable of degree %d",power);
  307.       }
  308.  
  309.       } else {                       // it is an equation but not in one variable
  310.         printf("this is an equation, but not in 1 variable\n");
  311.       }
  312.  
  313.  
  314.     } else {
  315.       printf("this is not an equation\n");
  316.     }
  317.     printf("\n" );
  318.     free(ar);
  319.     freeTokenList(tl);
  320.     ar = readInput();
  321.   }
  322.   free(ar);
  323.   printf("give an equation: good bye\n");
  324.  
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement