-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];
}
你可能要開始與初始化'compare'和'compare_2'已知值。至少,這將不再具有未定義的行爲,因此您可以專注於* actual *問題。 – usr2564301 2014-10-18 14:06:28
'while((arr [i]!= NULL)&&(i <80)&& arr [i]!='\ n'){' - >'while(i <80 && arr [i]!=' \ 0'){' – BLUEPIXY 2014-10-18 14:10:06
感謝您對這些觀點的澄清,我的頭仍然處於C++的地步,如果未聲明,值將初始化爲0。謝謝你,並且對於'\ 0',在使用它之前,由於之前在代碼中出現了一個令人討厭的錯誤,很可能是由於使用了get ...感謝你。 – 2014-10-18 17:42:25