Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

My goal is to print

program starts...
set value of num to 0
set value of res to 0
<type in> Bye!!
Input(msg): Bye!!

by implementing the file

  SNOL
  INTO num IS 0
  INTO res IS 0
  BEG msg
  INTO num IS 5
  INTO res IS MULT num num

and this is my code for it. Question: I am wondering how my code worked for some time then stopped working when I added some declaration statements.Now I deleted those statements and tried to change it when the code actually worked but now it only works until the scanning of input then after that the program stops.

Additional question: When I continued to finish my code, I got errors when I tried to evaluate the last statement in the file because I could not figure out how to compare if the number is float or int. After that, the result should be this where res = num*num.

Evaluating MULT: result is 25

bool objectFloat(const char* object){
//check if 1st character is a digit, if not then return false, otherwise return true.
if (!isdigit(object[0]))
    return false;

// Check if the 2nd character to the last are digits or periods.
// If not, return false otherwise return true
int periods = 0;  //initialize how many periods in the object to zero
int i;
//if character is a period then increment periods.
for(i = 1; i<strlen(object); i++){
    if(object[i]=='.'){
        periods++;
    }
//return false if character is not a digit
    else if(!isdigit(object[i])){
        return false;
    }
}
// return true if there is only one period.
return periods == 1;
}

    //function to check if character is a keyobject.
 bool isKeyobject(const char* object){
char keyobjects[11][11]= {"SNOL", "LONS", "INTO", "IS", "MULT", "BEG",           
"PRINT", "ADD", "SUB", "DIV", "MOD"};

    int i;
    for(i=0; i<11; i++){
    // Check if object is equal to keyobjects at index i
    // If yes, return true
    if (isLowerCase(object)){

    return false;}
    if(strcmp(object, keyobjects[i]) == 0){
       return true;
    }
    }
    //object is not equal to any of the keyobjects so return false
    return false;
 }

 //Function to check if every character is an integer
 // If not, return false otherwise return true
bool objectInt(const char* object){


int i;
for (i = 0; i < strlen(object); i++){
    if (!isdigit(object[i])) return false;
    }

   return true;
}

bool objectIsVariable(const char* object){


// Check if alphanumeric character & lower case
// If not, return false
int i;

for (i = 0; i < strlen(object); i++){

    if (!isalnum(object[i])&&!isLowerCase(object)) return false;
   }
   return true;
  }
   int setNum(const char* object){

if(objectInt(object)){
    int number, num;
    number=atoi(object);
    num=number;
    return num;
}
else if(objectFloat(object)){
    float number, num;
    number=atof(object);
    num=number;
    return num;
}


}
int setRes(const char* object){
    if(objectInt(object)){
    int number, res;
    number=atoi(object);
    res=number;
    return res;
}
else if(objectFloat(object)){
    float number, res;
    number=atof(object);
    res=number;
}
}




 int main(){

FILE *s_path  = fopen("test.snol", "r");

char object[100];

char msg;
const char* IsitIS = "IS";
const char* IsitMULT = "MULT";
const char* IsitMOD = "MOD";
const char* IsitSNOL= "SNOL";
const char* IsitLONS= "LONS";
const char* IsitINTO = "INTO";
const char* IsitADD= "ADD";
const char* IsitSUB= "SUB";
const char* IsitDIV = "DIV";
const char* IsitBEG = "BEG";
const char* IsitNum = "num";
const char* IsitRes = "res";
int number;
int flag = 0;
// Get each object from s_path until end-of-file is reached
while(fscanf(s_path, "%s", &object) != -1) {

    //if it doesn't start with SNOL then end
    if (isKeyobject(object)&&strcmp(object, IsitSNOL)==0){
        printf ("Program starts...

");
    }   
    else if (isKeyobject(object)&&strcmp(object, IsitINTO)==0){
    printf ("...Set value of "); 
    }


if(objectInt(object)) {
number = atoi(object);
if (flag == 1)
{
    setNum(object);
    printf("num to %d
", number);
}
else 
{
     setRes(object);
    printf("res to %d
", number);
}

}

else if(strcmp(object, IsitNum) == 0) {
flag = 1;
  }
  else if(strcmp(object, IsitRes) == 0) {
   flag = 2;
  }
     else if(isKeyobject(object)&&strcmp(object, IsitBEG)==0){
    printf("input msg
");
    scanf("%s",msg);
    fscanf(s_path," %s", &object);
    printf ("INPUT(%s): %s
",object,msg);
   }
  }   
   fclose(s_path);    
   }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
130 views
Welcome To Ask or Share your Answers For Others

1 Answer

I have found the error I had to change

char msg;

to

char msg[50];

I still need the answer to the additional question though.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...