2014-10-18 75 views
-2

我一直在运行这几个小时,并继续得到错误的输出,似乎无法找出原因。看起来好像所有的东西都可以工作,但是我第一次执行这个操作时总是会有一个奇怪的角色,这些令牌没有按照他们应该的方式移动。这只是练习代码,要用汇编语言来实现。错误的数组输出

char get_line(void){ 
    //Char array, buf space 80, int array, hold the numerical value, 
    char arr[80]; 
    int int_arr[80]; 
    char arr_print[80]; 
    //Two points to compare whether the value in the given array changed. 
    int compare; 
    int compare_2; 
    //Array points, indexes and size counter. 
    int count = -1; 
    int i = 0; 
    int j = 0; 
    int k; 

    gets(arr);//Unsafe version of code, but for this implementation negligible. 

    while((arr[i] != NULL) && (i < 80) && arr[i] != '\n'){ 
    //Runs through and sets the value based on specs, #'s =1, alpha =2, ... 
    //For the comparison with the below code. 
    if(isalpha(arr[i])){ 
     int_arr[i] = 2;// printf("%c: 2", arr[i]); 
     compare = 2; 

    }else if(isdigit(arr[i])){ 
     int_arr[i] = 1;// printf("%d: 1", arr[i]); 
     compare = 1; 

    }else if(arr[i] == '$'){ 
     int_arr[i] = 5;// printf("%c: 5", arr[i]); 
     compare = 5; 

    }else if(arr[i] == '#'){ 
     int_arr[i] = 6;// printf("%c: 6", arr[i]); 
     compare = 6; 

    }else if(arr[i] == '(' || arr[i] == ')' || arr[i] == ',' || 
     arr[i] == '.' || arr[i] == ':'){ 
     int_arr[i] = 4;// printf("%c: 4", arr[i]); 
     compare = 4; 

    }else if(arr[i] == '*' || arr[i] == '+' || arr[i] == '-' || 
     arr[i] == '/'){ 
     int_arr[i] = 3;//printf("%c: 3", arr[i]); 
     compare = 3; 

    }else if(isspace(arr[i])) 
     int_arr[i] = 5;//Ignore the spaces in this implementation. 
    /* 
     Runs the comparison point to assure that the 
     tokens are matched up and grouped as needed. 
    */  
    if(compare_2 == 0 || (compare != compare_2)){ 
     if(compare_2 != 0){ 
    for(k=0; k<=j ;k++) 
     printf("%c", arr_print[k]); 

    j=0; 
     }  
     printf("\t\t%d \n", compare_2); 
     compare_2 = compare; 

    }else if(isspace(arr[i]) == 0){ 
     arr_print[j] = arr[i]; 
     //  printf("\t\t\t\t\t%c | %d\n", arr_print[j],j); 
     j++; 
    } 

    i++; 
    count++; 
    } 
    printf("\n\n"); 
    //Code for previous implementation in C 
    for(i=0; i<80 && arr[i] != NULL; i++) 
    printf("%c", arr[i]); 
    printf("\n"); 

    for(i=0; i< count+1; i++) 
    printf("%d", int_arr[i]); 
    printf("\n"); 

    if(i == 0 || count == -1) return '#'; 

    return arr[count]; 
} 
+2

你可能要开始与初始化'compare'和'compare_2'已知值。至少,这将不再具有未定义的行为,因此您可以专注于* actual *问题。 – usr2564301 2014-10-18 14:06:28

+2

'while((arr [i]!= NULL)&&(i <80)&& arr [i]!='\ n'){' - >'while(i <80 && arr [i]!=' \ 0'){' – BLUEPIXY 2014-10-18 14:10:06

+0

感谢您对这些观点的澄清,我的头仍然处于C++的地步,如果未声明,值将初始化为0。谢谢你,并且对于'\ 0',在使用它之前,由于之前在代码中出现了一个令人讨厌的错误,很可能是由于使用了get ...感谢你。 – 2014-10-18 17:42:25

回答

1

我觉得这是你需要什么样的

试试这个代码

char get_line(void) 
{ 
    //Char array, buf space 80, int array, hold the numerical value, 
    char arr[80]; 
    int int_arr[80]; 
    char arr_print[80]; 
    //Two points to compare whether the value in the given array changed. 
    int compare=0;  
    int compare_2=0; 
    //Array points, indexes and size counter. 
    int count = -1; 
    int i = 0; 
    int j = 0; 
    int k = 0; 
    int l = 0; // I add an int l for 

    gets(arr);//Unsafe version of code, but for this implementation negligible. 

    //while((arr[i] != NULL) && (i < 80) && arr[i] != '\n'){ 
    while(i < 80 && arr[i] != '\0') 
    { 
    //Runs through and sets the value based on specs, #'s =1, alpha =2, ... 
    //For the comparison with the below code. 
    if(isalpha(arr[i])) 
    { 
     int_arr[i] = 2;// printf("%c: 2", arr[i]); 
     compare = 2; 
    } 
    else if(isdigit(arr[i])) 
    { 
     int_arr[i] = 1;// printf("%d: 1", arr[i]); 
     compare = 1; 
    } 
    else if(arr[i] == '$') 
    { 
     int_arr[i] = 5;// printf("%c: 5", arr[i]); 
     compare = 5; 
    } 
    else if(arr[i] == '#') 
    { 
     int_arr[i] = 6;// printf("%c: 6", arr[i]); 
     compare = 6; 
    } 
    else if(arr[i] == '(' || arr[i] == ')' || arr[i] == ',' || arr[i] == '.' || arr[i] == ':') 
    { 
     int_arr[i] = 4;// printf("%c: 4", arr[i]); 
     compare = 4; 
    } 
    else if(arr[i] == '*' || arr[i] == '+' || arr[i] == '-' || arr[i] == '/') 
    { 
     int_arr[i] = 3;//printf("%c: 3", arr[i]); 
     compare = 3; 
    } 
    else if(isspace(arr[i])) 
     int_arr[i] = 5;//Ignore the spaces in this implementation. 
    /* 
     Runs the comparison point to assure that the 
     tokens are matched up and grouped as needed. 
    */  
    if(compare_2 == 0 || (compare != compare_2)) 
    { 
     if(compare_2 != 0) 
     { 
      for(k=0; k<=j ;k++) 
       printf("%c", arr[l+k]); // arr_print replaced by arr 
      j=0; 
      l+=k; // int l to go through array arr 
     } 
     printf("\t\t%d \n", compare_2); 
     compare_2 = compare; 
    } 
    else if(isspace(arr[i]) == 0) 
    { 
     arr_print[j] = arr[i]; 
     //printf("\t\t\t\t\t%c | %d\n", arr_print[j],j); 
     j++; 
    } 
    i++; 
    count++; 
    } 

    printf("%c", arr[count]);  // Repeated code to print the last element 
    printf("\t\t%d \n", compare_2); 
    compare_2 = compare; 

    printf("\n\n"); 
    //Code for previous implementation in C 
    for(i=0; i<80 && arr[i] != NULL; i++) 
    printf("%c", arr[i]); 
    printf("\n"); 

    for(i=0; i< count+1; i++) 
    printf("%d", int_arr[i]); 
    printf("\n"); 

    if(i == 0 || count == -1) return '#'; 

    return arr[count]; 
} 
+0

感谢您使用该代码片段,根据需要运行,以了解如何将其作为汇编语言实现。我发现我在一些问题上出现了问题,总是有点简单的问题。 – 2014-10-18 17:46:51