gpt4 book ai didi

c - 数组输出错误

转载 作者:行者123 更新时间:2023-11-30 20:26:11 25 4
gpt4 key购买 nike

我已经运行了几个小时,并继续得到错误的输出,并且似乎无法找出原因。似乎一切都应该有效,但我第一次实现这个时总是得到一个奇怪的字符,并且 token 没有按照应有的方式移动。这只是练习代码,要用汇编语言实现。

    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];
}

最佳答案

我想这就是你所需要的

试试这个代码

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];
}

关于c - 数组输出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26440544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com