本人编程初学者,在C语言学习中遇到了困难
请问哪位可以帮我打一下这个代码,今晚十一点前要交
#include <stdio.h>
int str_to_int(char *s)
{
int n = 0;
while (*s)
{
n = n * 10 + (*s - '0');
s++;
}
return n;
}
int find_int_in_array(int *arr, int l, int n)
{
for (int i = 0; i < l; i++)
{
if (arr[i] == n)
return i;
}
return -1;
}
int main()
{
char str[100], buf[100];
int num[100], count[100] = {0}, inx = 0, cnt = 0, i = 0, n, t;
fgets(str, 100, stdin); // fgets输入末尾会包含\n
while (1)
{
if (str[i] != ' ' && str[i] != '\n')
buf[inx++] = str[i];
if ((i > 0 && str[i] == ' ') || str[i] == '\n')
{
buf[inx] = '\0';
n = str_to_int(buf);
t = find_int_in_array(num, cnt, n);
if (t == -1)
{
num[cnt] = n;
count[cnt]++;
cnt++;
}
else
count[t]++;
inx = 0;
}
if (str[i] == '\n')
break;
i++;
}
for (i = 0; i < cnt; i++)
{
printf("%d %d\n", num[i], count[i]);
}
scanf("%d", &n);
t = find_int_in_array(num, cnt, n);
if (t == -1)
printf("不存在");
else
printf("%d", count[t]);
return 0;
}