鱼C论坛

 找回密码
 立即注册
查看: 1957|回复: 4

[已解决]求助求助

[复制链接]
发表于 2021-8-19 13:25:55 | 显示全部楼层 |阅读模式
2鱼币
  1. #include<stdio.h>
  2. #define N 100

  3. int main()
  4. {
  5.         char b[N],*p;
  6.         int a[N],i,j,e10,count,m,sum;
  7.         printf("input string:");
  8.         gets(b);
  9.         i=0;
  10.         m=0;
  11.         p=&b[0];
  12.         count=0;
  13.         while(*p!='\0')
  14.         {
  15.                 if((*p>='0') && (*p<='9'))
  16.                 {
  17.                         m++;
  18.                 }
  19.                 else
  20.                 {
  21.                         if(m>0)
  22.                         {
  23.                                 sum=*(p+i-1)-'0';
  24.                                 e10=1;
  25.                                 for(j=1;j<m;j++)
  26.                                 {
  27.                                         e10=e10*10;
  28.                                         sum=sum+(*(p+i-1-j)-'0')*e10;
  29.                                 }
  30.                                 a[count]=sum;
  31.                                 count++;
  32.                                 m=0;
  33.                         }
  34.                 }
  35.                 i++;
  36.                 p+=i;
  37.         }
  38.         if(m>0)
  39.         {
  40.                 sum=*(p+i-1)-'0';
  41.                 e10=1;
  42.                 for(j=1;j<m;j++)
  43.                 {
  44.                         e10=e10*10;
  45.                         sum=sum+(*(p+i-1-j)-'0')*e10;
  46.                 }
  47.                 a[count]=sum;
  48.                 count++;
  49.         }
  50.         for(i=0;i<count;i++)
  51.         {
  52.                 printf("%d ",a[i]);
  53.         }
  54.         printf("\n");
  55.         return 0;
  56. }
复制代码


输出结果是乱码,请各位大神帮忙看看是哪有问题!!!

最佳答案
2021-8-19 13:25:56
应该也可以直接在输入的时候就直接挑选出来,有兴趣可以写写看
我没写注释,有问题可以回复

  1. /*
  2. * 在 str[N] 里接受输入,然后把里边的数字提出来存到 result[N] 数组里
  3. * 例如:A123x456 17960
  4. * 123 存到 result[0],456 存到 result[1]...
  5. */

  6. #include <stdio.h>
  7. #include <stdlib.h>

  8. #define N 100

  9. int main(void)
  10. {
  11.     char str[N], ch;
  12.     int i, j, idx, result[N];
  13.     char temp[N];
  14.    
  15.     temp[0] = '\0';

  16.     printf("请输入字符串:\n");
  17.     ch = getchar();

  18.     i = 0;
  19.     while ('\n' != ch && i < N)
  20.     {
  21.         str[i++] = ch;
  22.         ch = getchar();
  23.     }

  24.     i = j = idx = 0;
  25.     while (i < N)
  26.     {
  27.         if ( str[i] > '9' || str[i] < '0')
  28.         {
  29.             i++;
  30.         }
  31.         else
  32.         {
  33.             while ( str[i] <= '9' && str[i] >= '0')
  34.             {
  35.                 temp[j++] = str[i++];
  36.                 temp[j] = '\0';
  37.             }

  38.             result[idx++] = atoi(temp);
  39.             temp[0] = '\0';
  40.             j = 0;
  41.         }
  42.     }

  43.     if (idx <= N - 1)
  44.     {
  45.         result[idx] = '\0';
  46.     }

  47.     idx = 0;
  48.     while ('\0' != result[idx])
  49.     {
  50.         printf("%d ", result[idx++]);
  51.     }

  52.     return 0;
  53. }
复制代码


ss.jpg
asasb.jpg

最佳答案

查看完整内容

应该也可以直接在输入的时候就直接挑选出来,有兴趣可以写写看 我没写注释,有问题可以回复
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-19 13:25:56 | 显示全部楼层    本楼为最佳答案   
应该也可以直接在输入的时候就直接挑选出来,有兴趣可以写写看
我没写注释,有问题可以回复

  1. /*
  2. * 在 str[N] 里接受输入,然后把里边的数字提出来存到 result[N] 数组里
  3. * 例如:A123x456 17960
  4. * 123 存到 result[0],456 存到 result[1]...
  5. */

  6. #include <stdio.h>
  7. #include <stdlib.h>

  8. #define N 100

  9. int main(void)
  10. {
  11.     char str[N], ch;
  12.     int i, j, idx, result[N];
  13.     char temp[N];
  14.    
  15.     temp[0] = '\0';

  16.     printf("请输入字符串:\n");
  17.     ch = getchar();

  18.     i = 0;
  19.     while ('\n' != ch && i < N)
  20.     {
  21.         str[i++] = ch;
  22.         ch = getchar();
  23.     }

  24.     i = j = idx = 0;
  25.     while (i < N)
  26.     {
  27.         if ( str[i] > '9' || str[i] < '0')
  28.         {
  29.             i++;
  30.         }
  31.         else
  32.         {
  33.             while ( str[i] <= '9' && str[i] >= '0')
  34.             {
  35.                 temp[j++] = str[i++];
  36.                 temp[j] = '\0';
  37.             }

  38.             result[idx++] = atoi(temp);
  39.             temp[0] = '\0';
  40.             j = 0;
  41.         }
  42.     }

  43.     if (idx <= N - 1)
  44.     {
  45.         result[idx] = '\0';
  46.     }

  47.     idx = 0;
  48.     while ('\0' != result[idx])
  49.     {
  50.         printf("%d ", result[idx++]);
  51.     }

  52.     return 0;
  53. }
复制代码


BX4SE%LO_T[P07270RUW36I.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-19 16:38:10 | 显示全部楼层
输入的东西超了数组最大下标了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-19 16:45:10 | 显示全部楼层

  1.         char b[N],*p;
  2.         int a[N],i,j,e10,count,m,sum;
  3.         printf("input string:");
  4.         gets_s(b);
  5.                 i=0;
  6.         m=0;
  7.         p=&b[0];
  8.         count=0;
  9.         while(*p!='\0')
  10.         {
  11.                 if((*p>='0') && (*p<='9'))
  12.                 {
  13.                         m++;
  14.                 }
  15.                 else
  16.                 {
  17.                         if(m>0)
  18.                         {
  19.                                 sum=*(p-1)-'0';///////////////////////////////////
  20.                                 e10=1;
  21.                                 for(j=1;j<m;j++)
  22.                                 {
  23.                                         e10=e10*10;
  24.                                         sum=sum+(*(p-1-j)-'0')*e10;
  25.                                 }
  26.                                 a[count]=sum;
  27.                                 count++;
  28.                                 m=0;
  29.                         }
  30.                 }
  31.                 p++;////////////////////////////////////////////
  32.         }
  33.         if(m>0)
  34.         {
  35.                         sum=*(p-1)-'0';
  36.                         e10=1;
  37.                         for(j=1;j<m;j++)
  38.                         {
  39.                                 e10=e10*10;
  40.                                 sum=sum+(*(p-1-j)-'0')*e10;
  41.                         }
  42.                         a[count]=sum;
  43.                 count++;
  44.         }
  45.         for(i=0;i<count;i++)
  46.         {
  47.                 printf("%d ",a[i]);
  48.         }
  49.         printf("\n");
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-8-27 11:31:34 | 显示全部楼层
Max472 发表于 2021-8-19 18:34
应该也可以直接在输入的时候就直接挑选出来,有兴趣可以写写看
我没写注释,有问题可以回复

好的 谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-26 20:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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