2014-10-18 91 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