鱼C论坛

 找回密码
 立即注册
查看: 1804|回复: 3

是编译器的问题? 还是我函数的问题?

[复制链接]
发表于 2021-11-18 17:37:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我这个程序可以编译,但是运行之后无法输入,
如果不加others的那个函数就没问题,我想请教一下为什么会这样?应该如何解决?

/*********************
编写一个用来统计输入的各个数字,空白符(空格、制表符、换行符)以及所有其他字符出现
次数的程序,分别存放在变量num[10],blank,others里面并打印出来。
**********************/

#include<stdio.h>
#include<conio.h>

int main(void)
{
    int blank1(int blank);
    int blank=0;
    int blank2;

        int others1(int others);
    int others=0;
        int others2;

        others2=others1(others);
    blank2=blank1(blank);

        printf("空白符有%d个.\n",blank2);
        printf("其余字符有%d个.\n",others2);
       
        printf("\n\n");
//        system("pause");
        return 0;
}

int blank1(int blank)      //负责空格符的函数
{
        int blank3=0;
        int c;
       
        while((c=getchar())!=EOF)                                       
        {
                if(c==' ' || c=='\t' || c=='\n')  
                        blank3++;
        }
        return blank3;
}

int others1(int others)   //负责其他字符的函数
{
    int others3=0;
        int c;

        while((c=getch())!=EOF)
        {
            if(c>=33 && c<=47 || c>=58 && c<=126)
                {
                  others3++;
                }
        }
        return others3;
}

/*
int main(void)
{
    int num[10]={0};
    int i,c;
        while((c=getchar())!=EOF)
        {
            if(c>='0'&&c<='9')
                {
                   i=c-'0';
                   num[i]++;
                }
        }
        for(i=0;i<10;i++)
        {
          printf("数字%d有%d个.\n",i,num[i]);
        }

    printf("\n\n");
        return 0;
}
*/
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-11-18 18:09:56 | 显示全部楼层
问题更新一下,修改完代码之后输入的字符出来了,
但是blank函数不起作用了。
代码如下:
  1. /*********************
  2. 编写一个用来统计输入的各个数字,空白符(空格、制表符、换行符)以及所有其他字符出现
  3. 次数的程序,分别存放在变量num[10],blank,others里面并打印出来。
  4. **********************/

  5. #include<stdio.h>
  6. #include<conio.h>

  7. int main(void)
  8. {
  9.     int blank1(int blank);
  10.     int blank=0;
  11.     int blank2;

  12.         int others1(int others);
  13.     int others=0;
  14.         int others2;

  15.         others2=others1(others);
  16.     blank2=blank1(blank);

  17.         printf("空白符有%d个.\n",blank2);
  18.         printf("其余字符有%d个.\n",others2);
  19.        
  20.         printf("\n\n");
  21. //        system("pause");
  22.         return 0;
  23. }

  24. int blank1(int blank)      //负责空格符的函数
  25. {
  26.         int blank3=0;
  27.         int c;
  28.        
  29.         while((c=getchar())!=EOF)                                       
  30.         {
  31.                 if(c==' ' || c=='\t' || c=='\n')  
  32.                         blank3++;
  33.         }
  34.         return blank3;
  35. }

  36. int others1(int others)   //负责其他字符的函数
  37. {
  38.     int others3=0;
  39.         int c;

  40.         while((c=getchar())!=EOF)
  41.         {
  42.             if(c>=33 && c<=47 || c>=58 && c<=126)
  43.                 {
  44.                   others3++;
  45.                 }
  46.         }
  47.         return others3;
  48. }

  49. /*
  50. int main(void)
  51. {
  52.     int num[10]={0};
  53.     int i,c;
  54.         while((c=getchar())!=EOF)
  55.         {
  56.             if(c>='0'&&c<='9')
  57.                 {
  58.                    i=c-'0';
  59.                    num[i]++;
  60.                 }
  61.         }
  62.         for(i=0;i<10;i++)
  63.         {
  64.           printf("数字%d有%d个.\n",i,num[i]);
  65.         }

  66.     printf("\n\n");
  67.         return 0;
  68. }
  69. */
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-11-18 17:38:28 | 显示全部楼层
  1. /*********************
  2. 编写一个用来统计输入的各个数字,空白符(空格、制表符、换行符)以及所有其他字符出现
  3. 次数的程序,分别存放在变量num[10],blank,others里面并打印出来。
  4. **********************/

  5. #include<stdio.h>
  6. #include<conio.h>

  7. int main(void)
  8. {
  9.     int blank1(int blank);
  10.     int blank=0;
  11.     int blank2;

  12.         int others1(int others);
  13.     int others=0;
  14.         int others2;

  15.         others2=others1(others);
  16.     blank2=blank1(blank);

  17.         printf("空白符有%d个.\n",blank2);
  18.         printf("其余字符有%d个.\n",others2);
  19.        
  20.         printf("\n\n");
  21. //        system("pause");
  22.         return 0;
  23. }

  24. int blank1(int blank)      //负责空格符的函数
  25. {
  26.         int blank3=0;
  27.         int c;
  28.        
  29.         while((c=getchar())!=EOF)                                       
  30.         {
  31.                 if(c==' ' || c=='\t' || c=='\n')  
  32.                         blank3++;
  33.         }
  34.         return blank3;
  35. }

  36. int others1(int others)   //负责其他字符的函数
  37. {
  38.     int others3=0;
  39.         int c;

  40.         while((c=getch())!=EOF)
  41.         {
  42.             if(c>=33 && c<=47 || c>=58 && c<=126)
  43.                 {
  44.                   others3++;
  45.                 }
  46.         }
  47.         return others3;
  48. }

  49. /*
  50. int main(void)
  51. {
  52.     int num[10]={0};
  53.     int i,c;
  54.         while((c=getchar())!=EOF)
  55.         {
  56.             if(c>='0'&&c<='9')
  57.                 {
  58.                    i=c-'0';
  59.                    num[i]++;
  60.                 }
  61.         }
  62.         for(i=0;i<10;i++)
  63.         {
  64.           printf("数字%d有%d个.\n",i,num[i]);
  65.         }

  66.     printf("\n\n");
  67.         return 0;
  68. }
  69. */
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-11-18 18:04:39 | 显示全部楼层
我自己找出来了  48行的函数用错了
应该用getchar()
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-25 13:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表