帮忙看看这个程序问题出在哪?
程序代码:/**********************************************/
/* 用自定义函数的方法统计输入的字符出现数字, */
/* (空格,制表符,回车),和其它字符的次数. */
/**********************************************/
#include <stdio.h>
void main()
{
void num(int n[], char c); //统计数字
int blank( char c); //统计空格,回车,制表符
int others( char c); //统计其它字符
int i, s2, s3, n[10] = { 0 };
char c;
printf("input: \n");
while(c != '\0')
{
c = getchar();
putchar(c);
num(n, c);
s2 = blank(c);
s3 = others(c);
}
for(i = 0; i < 10; i++)
{printf("n[%d] = %d ", i, n[i]);}
printf("\nblank = %d", s2);
printf("\nothers = %d\n", s3);
}
void num(int n[], char c)
{
int i;
if(c >= 48 && c <= 97)
{
for(i = 0;i < 10; i++)
{
if(c == i + 48)
{
n[i]++;
}
}
}
}
int blank(char c)
{
static p = 0; //静态变量,P值出此函数后仍保持上次值。
if (c =='\n' || c == '\t' || c == ' ')
p = p + 1;
return p;
}
int others(char c)
{
extern q; //声明q为外部变量,也为了q值能保持上次值。
if (c != '\n' && c != '\t' && c != ' ' && (c < 48 || c > 97))
q = q + 1;
return q;
}
int q = 0;结果:我是用数组n[10]来表示数字出现次数,编译的时候,其它字符的统计总是出错,不知道为什么?
[ 本帖最后由 zpcg 于 2011-8-25 23:50 编辑 ]









