Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> /* getchar, printf */
- #include <stdlib.h> /* NULL */
- #include "scanner.h"
- #include "recognizeEq.h"
- #include <string.h> // Used for strcmp, strlen and strcpy
- int acceptNumber(List *lp) {
- if (*lp != NULL && (*lp)->tt == Number) {
- *lp = (*lp)->next;
- return 1;
- }
- return 0;
- }
- int acceptIdentifier(List *lp) {
- if (*lp != NULL && (*lp)->tt == Identifier) {
- *lp = (*lp)->next;
- return 1;
- }
- return 0;
- }
- int acceptCharacter(List *lp, char c) {
- if (*lp != NULL && (*lp)->tt == Symbol && ((*lp)->t).symbol == c) {
- *lp = (*lp)->next;
- return 1;
- }
- return 0;
- }
- ////////////////////////////////////////////////////////////////////////////////
- int acceptTerm(List *lp){ // function to recognize a term
- if(acceptNumber(lp)){
- if(acceptIdentifier(lp)){
- if(acceptCharacter(lp,'^')){ // it can either have a power or not
- if(acceptNumber(lp)){
- return 1; // this is the case in which a term has all the possible combinations
- }
- else return 0;
- }
- else return 1;
- }
- else return 1;
- } else {
- if(acceptIdentifier(lp)){ // The case in which the identifier has no number before it
- if(acceptCharacter(lp, '^')){
- if(acceptNumber(lp)){
- return 1;
- }
- else return 0;
- }
- else return 1;
- }
- }
- return 0; // in this case if it does not go into any of the ifs then it is not a term.
- }
- int acceptExpression(List *lp){ // function to recognize an expression, we assume it is an expression until we find a mistake and return 0
- if(acceptCharacter(lp,'-')){ // We can either have a '-' or not
- if(!acceptTerm(lp)){
- return 0;
- }
- } else {
- if(!acceptTerm(lp)){
- return 0;
- }
- }
- while(acceptCharacter(lp, '+') || acceptCharacter(lp, '-')){
- if(!acceptTerm(lp)){
- return 0;
- }
- }
- return 1;
- }
- int acceptEquation(List *lp){ // the easy function to determine an equation
- if(!acceptExpression(lp)){
- return 0;
- }
- if(!acceptCharacter(lp,'=')){
- return 0;
- }
- if(!acceptExpression(lp)){
- return 0;
- }
- return 1;
- }
- int checkPower(List *lp){ // our function that computes the highestdegree
- int highestdegree=1; // we assume that the equation starts with the degree 1
- int degree=0;
- while (*lp!=NULL) { // we iterate thorugh the list and everytime we find a higher degree we update our variable.
- if (((*lp)->t).symbol == '^') {
- *lp = (*lp)->next;
- degree=(*lp)->t.number;
- }
- if(degree>highestdegree){
- highestdegree=degree;
- }
- *lp = (*lp)->next;
- }
- free(*lp);
- return highestdegree; // we output the highest degree.
- }
- int checkIdentifier(List *lp){ //our funtion that returns 1 if the equation is in one variable.
- char f[10000]=""; // we initialise the strings empty
- char g[10000]="";
- int validCheck=1;
- 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
- while(*lp!=NULL){ // we iterate through the list and we store the identifiers in 2 strings.
- if (variableCounter == 0 && (*lp)->tt == Identifier) {
- strcpy(f,(*lp)->t.identifier);
- strcpy(g,(*lp)->t.identifier);
- variableCounter++;
- } else if(variableCounter>0 && (*lp)->tt == Identifier){ // if we already encountered an identifier we store the new one in only one string
- strcpy(g,(*lp)->t.identifier);
- }
- 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
- validCheck=0;
- }
- if(validCheck==0){ // we use this if to get out of the while loop if we already found 2 different identifiers
- return 0;
- }
- *lp = (*lp)->next;
- }
- 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
- return 0;
- }
- free(*lp);
- return 1; // If no different identifiers and the strings are non empty we return 1
- }
- float solveEquation(List *lp){
- int identifiercounter,auxcounter,freeterms;
- float finalvalue;
- identifiercounter=0;
- auxcounter=0;
- freeterms=0;
- int powerholder;
- while((*lp)->t.symbol != '='){
- if(((*lp)->t).symbol == '-'){
- *lp = (*lp)->next; // - 3 x ^ 1 - 3 x + 3 - 3 + x - x
- if((*lp)->tt == Number){
- auxcounter=(*lp)->t.number;
- *lp = (*lp)->next;
- if((*lp)->tt == Identifier){
- *lp = (*lp)->next;
- if((*lp)->t.symbol == '^'){
- *lp = (*lp)->next;
- powerholder=(*lp)->t.number;
- if(powerholder==1){
- identifiercounter=identifiercounter-auxcounter;
- auxcounter=0;
- }
- else if(powerholder==0){
- freeterms=freeterms-auxcounter;
- auxcounter=0;
- }
- }
- identifiercounter=identifiercounter-auxcounter;
- auxcounter=0;
- } else {
- freeterms=freeterms-auxcounter;
- auxcounter=0;
- }
- } else if((*lp)->tt == Identifier){
- identifiercounter=identifiercounter-1;
- }
- } else if ((*lp)->tt == Number ){
- auxcounter=(*lp)->t.number;
- *lp = (*lp)->next;
- if((*lp)->tt == Identifier){
- identifiercounter=identifiercounter+auxcounter;
- auxcounter=0;
- *lp = (*lp)->next;
- } else {
- freeterms=freeterms+auxcounter;
- auxcounter=0;
- }
- } else if ( (*lp)->tt == Identifier) {
- identifiercounter=identifiercounter+1;
- *lp = (*lp)->next;
- } else if ( ((*lp)->t).symbol == '+' ){
- *lp = (*lp)->next;
- }
- } // end brace for while
- *lp = (*lp)->next;
- printf("We ve existed the first while loop\n" );
- printf("identifiercounter after the first while is %d\n",identifiercounter );
- printf("freeterm value after the first while is %d\n",freeterms );
- while(*lp!=NULL){
- printf("I got in the second while\n" );
- if(((*lp)->t).symbol == '-'){
- *lp = (*lp)->next; // 3 x - 3 x + 3 - 3 + x - x =
- if((*lp)->tt == Number){
- auxcounter=(*lp)->t.number;
- *lp = (*lp)->next;
- if(*lp==NULL){
- freeterms=freeterms-auxcounter;
- auxcounter=0;
- break;
- }
- if((*lp)->tt == Identifier){
- identifiercounter=identifiercounter+auxcounter;
- auxcounter=0;
- *lp = (*lp)->next;
- } else {
- freeterms=freeterms+auxcounter;
- auxcounter=0;
- }
- } else if((*lp)->tt == Identifier){
- identifiercounter=identifiercounter+1;
- *lp = (*lp)->next;
- }
- } else if ((*lp)->tt == Number ){
- auxcounter=(*lp)->t.number;
- *lp = (*lp)->next;
- if(*lp==NULL){
- freeterms=freeterms-auxcounter;
- auxcounter=0;
- break;
- }
- if((*lp)->tt == Identifier){
- identifiercounter=identifiercounter-auxcounter;
- auxcounter=0;
- *lp = (*lp)->next;
- } else {
- freeterms=freeterms-auxcounter;
- auxcounter=0;
- }
- } else if ( (*lp)->tt == Identifier) {
- identifiercounter=identifiercounter-1;
- *lp = (*lp)->next;
- } else if ( ((*lp)->t).symbol == '+' ){
- printf("i got to the plus\n" );
- *lp = (*lp)->next;
- }
- printf("I am here at the end\n" );
- }
- printf("We ve existed the second while loop\n" );
- printf("freeterm is %d\n",freeterms );
- printf("identifiercounter is %d\n",identifiercounter );
- finalvalue=(float)freeterms/identifiercounter;
- finalvalue=finalvalue*(-1);
- if(finalvalue==-0.000){
- finalvalue=0.000;
- }
- if(identifiercounter==0){
- return 69;
- }
- return finalvalue;
- }
- /* The function recognizeEquation tells us if it is an equation or not */
- void recognizeEquation() {
- char *ar;
- List tl,tl1,tl2,tl3; // we created 2 more lists because we did not if we are allowed to use tl
- int idenvar,power; // this variables hold the values for the identifier and degree
- float solution;
- ar = readInput();
- while (ar[0] != '!') {
- tl = tokenList(ar);
- printf("give an equation: ");
- printList(tl);
- tl1 = tl;
- tl2 = tl1;
- tl3 = tl1;
- idenvar=checkIdentifier(&tl2); // we use tl2 to check for identifiers
- power=checkPower(&tl3); // and tl3 to check for powers
- if (acceptEquation(&tl1) && tl1 == NULL) {
- if(idenvar==1){ // if it is an equation and it has only one variable
- if(power==1){
- solution=solveEquation(&tl);
- if(solution!=69){
- printf("this is an equation in 1 variable of degree %d\n",power);
- printf("solution: %0.3f",solution);
- } else {printf("this is an equation in 1 variable of degree %d\n",power);
- printf("not solvable");}
- } else {
- printf("this is an equation in 1 variable of degree %d",power);
- }
- } else { // it is an equation but not in one variable
- printf("this is an equation, but not in 1 variable\n");
- }
- } else {
- printf("this is not an equation\n");
- }
- printf("\n" );
- free(ar);
- freeTokenList(tl);
- ar = readInput();
- }
- free(ar);
- printf("give an equation: good bye\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement