为什么我的代码较早工作就不再工作了?

时间:2018-11-24 15:30:38

标签: c string

我的目标是打印

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

通过实施file

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

这是我的代码问题: 我想知道代码如何工作一段时间,然后在添加一些声明语句时停止工作。现在,我删除了这些语句,并试图在代码实际起作用时进行更改,但现在它只在扫描输入之前有效,然后程序停止。

其他问题: 当我继续完成我的 code 时,尝试评估文件中的最后一条语句时出现错误,因为我无法弄清楚数字是float还是int时如何比较。之后,结果应为res = num*num

  

评估MULT:结果为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...\n\n");
    }   
    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\n", number);
}
else 
{
     setRes(object);
    printf("res to %d\n", 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\n");
    scanf("%s",msg);
    fscanf(s_path," %s", &object);
    printf ("INPUT(%s): %s\n",object,msg);
   }
  }   
   fclose(s_path);    
   }

1 个答案:

答案 0 :(得分:-1)

我发现了错误 我不得不改变

$(".servicesBTN").on("click", function (e) {
  //get the data target value of the button that was clicked which is the same as the accordian content IDs
  let dataTarget = this.getAttribute("data-target")
  let servicesDisplayValue = getComputedStyle(document.getElementById('servicesDescriptions')).display

  //all three console.logs fire and log expected results
  console.log('data target ' + dataTarget)
  console.log('services display value ' + servicesDisplayValue)
  console.log('test hasClass' + $(dataTarget).hasClass('show'))

  //if the clicked button's associated card does have the show class, set the data toggle to blank so it won't change
  //none of the logs in the if blocks fire
  if ($(dataTarget).hasClass("show") && servicesDisplayValue === 'block') {
    this.setAttribute("data-toggle", "")
    console.log('first block - already open - display block')
  } else if ($(dataTarget).hasClass("show") && servicesDisplayValue === 'none') {
    this.setAttribute("data-toggle", "")
    mobileShowServiceInfo($(this))
    console.log('second block - already open - display none - mobile assumption')
  }
  else {
    //give the clicked button a data-toggle of collapse so it will open
    this.setAttribute("data-toggle", "collapse")
    mobileShowServiceInfo($(this))
    changeAllAngleIcons($(this))
    console.log('third block - ')
  }
})

char msg;

尽管如此,我仍然需要回答其他问题。