鱼C论坛

 找回密码
 立即注册
查看: 1107|回复: 2

[已解决]关于输入一段整形数据,空格分开,用一段程序统计数据个数

[复制链接]
发表于 2020-10-16 11:54:26 | 显示全部楼层 |阅读模式

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

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

x
#include <stdio.h>
int main()
{
       
        int i=0;
        char *temp;                                                      //定义一个字符指针
        printf("请输入数据,并用空格分开:");
        while(getchar()!=EOF)                                       //EOF是监测输入的数据是否完毕
        {
                getchar();                                                //用getchar在循环体里面一个一个的接受缓存中的字符数据
                *temp=getchar();                                    //接收到的单个字符数据赋值给指针
                if(getchar()==' ')                                     //如果接收到的字符是空格键,则认为对应一个数据,技术变量i加1
               {
                i++; }
                 temp++;                                               // 指针指向下一个字符单元接受getchar接受到的字符
               
        }
        printf("这份数据共有%d份数据。",i);               //打印出统计空格键的计数i,就是整形数据的数量
        return 0;
}
我感觉整个逻辑没有问题啊,但是为什么编译过去了,运行时输入一段数据后,运行的界面就运行错误报警?
最佳答案
2020-10-16 12:56:17
本帖最后由 xieglt 于 2020-10-16 12:58 编辑

char * temp 没分配内存!
简单点
char * temp;
char buffer[1024];
temp = buffer;
...

或者
#include <stdlib.h>

char * temp;
temp = (char *)malloc(1024);

...
free(temp);
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-10-16 12:56:17 | 显示全部楼层    本楼为最佳答案   
本帖最后由 xieglt 于 2020-10-16 12:58 编辑

char * temp 没分配内存!
简单点
char * temp;
char buffer[1024];
temp = buffer;
...

或者
#include <stdlib.h>

char * temp;
temp = (char *)malloc(1024);

...
free(temp);
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-16 13:07:32 | 显示全部楼层
本帖最后由 xieglt 于 2020-10-16 13:25 编辑

如果你不需要保存输入的数据,只需要统计输入的数据有几份,可以这么写。


  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. int main(void)
  4. {
  5.         int i=0;
  6.         char c1,c2;
  7.         c2= ' ';
  8.        
  9.         printf("请输入数据,并用空格分开:");

  10.         while((c1 = getchar())!= '\n')
  11.         {
  12.                 c2 = c1;

  13.                 if(c2 == ' ')
  14.                 {
  15.                         i++;
  16.                 }
  17.         }

  18.         if(c2 != ' ')
  19.         {
  20.                 i++;
  21.         }

  22.         printf("这份数据共有%d份数据。",i);

  23.     return 0;
  24. }
复制代码


如果需要保留输入内容,可以这么写



  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. #define MEM_SIZE        (12)

  4. int main(void)
  5. {
  6.         int i=0;
  7.         int count = 0;
  8.         int oldSize = 0;
  9.         char c1,c2;
  10.         char * buffer;
  11.         char * temp;


  12.         buffer = (char *)malloc(MEM_SIZE);
  13.         temp = buffer;
  14.        
  15.         printf("请输入数据,并用空格分开:");

  16.         c2 = ' ';

  17.         while((c1 = getchar())!= '\n')
  18.         {
  19.                 c2 = c1;
  20.                
  21.                 temp[count ++] = c2;

  22.                 if(count >= MEM_SIZE)
  23.                 {
  24.                         oldSize += MEM_SIZE;
  25.                         buffer = (char *)realloc(buffer,oldSize + MEM_SIZE);
  26.                         temp = buffer + oldSize;
  27.                         count = 0;
  28.                 }

  29.                 if(c2 == ' ')
  30.                 {
  31.                         i++;
  32.                 }
  33.         }

  34.         temp[count] = 0;

  35.         if(c2 != ' ')
  36.         {
  37.                 i++;
  38.         }

  39.         printf("这份数据共有%d份数据。",i);

  40.         free(buffer);
  41.     return 0;
  42. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-13 05:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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