鱼C论坛

 找回密码
 立即注册
查看: 1918|回复: 7

[已解决]求助大佬!! C语言测试,字符串处理问题!!

[复制链接]
发表于 2019-2-13 12:02:46 | 显示全部楼层 |阅读模式
10鱼币
/*************************************************************

题目:主函数main()中由数字与字母组成的字符串s和t为测试数据,程
      序首先将未在字符串s中出现、而在字符串t中出现的字符,生成
      一个新的字符串a;然后计算字符串a中所有数字子串对应的整数,
      找出其平方数中有连续的三位数字就是该数本身的整数。

例如:当s="AB148",t="E250A148C259G376"时,字符串a为
      "E250C259G376"。a中数字子串"250",其对应整数平方数为
      62500,250便是所要找的一个满足条件的整数。

编写程序:
    1. 编写函数void GenStr(char s[],char t[],char a[]),将未在
       字符串s中出现、而在字符串t中出现的字符保存在字符数组a中。

    2. 编写函数int Calculate(char a[],int b[]),计算字符串a中数
       字子串,按上述要求找出所要的整数,结果保存在整数数组b中,
       函数返回整数个数。

**************************************************************
注意:请勿改动主程序main()中的任何语句。
*************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#define N 80

void  GenStr(char s[],char t[],char a[])
{
/**********Program**********/




/**********  End  **********/
}

int Calculate(char a[],int b[])
{
/**********Program**********/




/**********  End  **********/
}

int main()
{
    char s[]="AB148";
    char t[]="E250A148C259G376";
    char a[N];
    int b[N]={0};
    int num=0,i=0;

    FILE *fp;
    if((fp=fopen("DATA.TXT","w"))==NULL)
    {
        printf("File open error\n");
        exit(0);
    }

    GenStr(s,t,a);
    printf("The string a is: %s\n",a);
    fprintf(fp,"The string is: %s\n",a);

    num=Calculate(a,b);
    printf("The integer list is: ");
    fprintf(fp,"The integer list is: ");
    for(i=0;i<num;i++)
    {
        printf("%d ",b[i]);
        fprintf(fp,"%d ",b[i]);
    }

    printf("\n");
    fprintf(fp,"\n");

    fclose(fp);
   
    return 0;
}
最佳答案
2019-2-13 12:02:47
我是个汉子 发表于 2019-2-13 17:43
然后计算字符串a中所有数字子串对应的整数,找出其平方数中有连续的三位数字就是该数本身的整数。
比如2 ...
  1. /* **************************************************************
  2. * 注意:请勿改动主程序main()中的任何语句。
  3. *  *************************************************************
  4. */

  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <conio.h>
  8. #include <ctype.h>
  9. #include <string.h>
  10. #include <math.h>

  11. #define N 80

  12. void  GenStr(char s[], char t[], char a[])
  13. {
  14. /**********Program**********/
  15.         int index = 0;
  16.         for(int i = 0; t[i]; ++i)
  17.         {
  18.                 int flag = 0;
  19.                 for(int j = 0; s[j]; ++j)
  20.                 {
  21.                         if(t[i] == s[j])
  22.                         {
  23.                                 flag = 1;
  24.                                 break;
  25.                         }
  26.                 }

  27.                 if(!flag)
  28.                 {
  29.                         a[index++] = t[i];
  30.                 }
  31.         }

  32.         a[index++] = '\0';

  33. /**********  End  **********/
  34. }

  35. int Calculate(char a[], int b[])
  36. {
  37. /**********Program**********/
  38.         int index = 0;
  39.         char *p = a;
  40.         while(*p)
  41.         {
  42.                 while(!isdigit(*p))
  43.                         ++p;
  44.                 b[index] = strtol(p, &p, 10);

  45.                 char buf1[1024];
  46.                 char buf2[1024];
  47.                 char *s1 = itoa((int)pow(b[index], 2), buf1, 10);
  48.                 char *s2 = itoa(b[index], buf2, 10);
  49.                 if(strpbrk(s1, s2) != NULL)
  50.                         ++index;
  51.         }
  52.         return index;

  53. /**********  End  **********/
  54. }

  55. int main()
  56. {
  57.         char s[] = "AB148";
  58.         char t[] = "E250A148C259G376";
  59.         char a[N];
  60.         int b[N] = {0};
  61.         int num = 0, i = 0;

  62.         FILE *fp;
  63.         if((fp = fopen("DATA.TXT", "w")) == NULL)
  64.         {
  65.                 printf("File open error\n");
  66.                 exit(0);
  67.         }

  68.         GenStr(s, t, a);
  69.         printf("The string a is: %s\n", a);
  70.         fprintf(fp, "The string is: %s\n", a);

  71.         num = Calculate(a, b);
  72.         printf("The integer list is: ");
  73.         fprintf(fp, "The integer list is: ");
  74.         for(i = 0; i < num; i++)
  75.         {
  76.                 printf("%d ", b[i]);
  77.                 fprintf(fp, "%d ", b[i]);
  78.         }

  79.         printf("\n");
  80.         fprintf(fp, "\n");

  81.         fclose(fp);

  82.         return 0;
  83. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-2-13 12:02:47 | 显示全部楼层    本楼为最佳答案   
我是个汉子 发表于 2019-2-13 17:43
然后计算字符串a中所有数字子串对应的整数,找出其平方数中有连续的三位数字就是该数本身的整数。
比如2 ...
  1. /* **************************************************************
  2. * 注意:请勿改动主程序main()中的任何语句。
  3. *  *************************************************************
  4. */

  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <conio.h>
  8. #include <ctype.h>
  9. #include <string.h>
  10. #include <math.h>

  11. #define N 80

  12. void  GenStr(char s[], char t[], char a[])
  13. {
  14. /**********Program**********/
  15.         int index = 0;
  16.         for(int i = 0; t[i]; ++i)
  17.         {
  18.                 int flag = 0;
  19.                 for(int j = 0; s[j]; ++j)
  20.                 {
  21.                         if(t[i] == s[j])
  22.                         {
  23.                                 flag = 1;
  24.                                 break;
  25.                         }
  26.                 }

  27.                 if(!flag)
  28.                 {
  29.                         a[index++] = t[i];
  30.                 }
  31.         }

  32.         a[index++] = '\0';

  33. /**********  End  **********/
  34. }

  35. int Calculate(char a[], int b[])
  36. {
  37. /**********Program**********/
  38.         int index = 0;
  39.         char *p = a;
  40.         while(*p)
  41.         {
  42.                 while(!isdigit(*p))
  43.                         ++p;
  44.                 b[index] = strtol(p, &p, 10);

  45.                 char buf1[1024];
  46.                 char buf2[1024];
  47.                 char *s1 = itoa((int)pow(b[index], 2), buf1, 10);
  48.                 char *s2 = itoa(b[index], buf2, 10);
  49.                 if(strpbrk(s1, s2) != NULL)
  50.                         ++index;
  51.         }
  52.         return index;

  53. /**********  End  **********/
  54. }

  55. int main()
  56. {
  57.         char s[] = "AB148";
  58.         char t[] = "E250A148C259G376";
  59.         char a[N];
  60.         int b[N] = {0};
  61.         int num = 0, i = 0;

  62.         FILE *fp;
  63.         if((fp = fopen("DATA.TXT", "w")) == NULL)
  64.         {
  65.                 printf("File open error\n");
  66.                 exit(0);
  67.         }

  68.         GenStr(s, t, a);
  69.         printf("The string a is: %s\n", a);
  70.         fprintf(fp, "The string is: %s\n", a);

  71.         num = Calculate(a, b);
  72.         printf("The integer list is: ");
  73.         fprintf(fp, "The integer list is: ");
  74.         for(i = 0; i < num; i++)
  75.         {
  76.                 printf("%d ", b[i]);
  77.                 fprintf(fp, "%d ", b[i]);
  78.         }

  79.         printf("\n");
  80.         fprintf(fp, "\n");

  81.         fclose(fp);

  82.         return 0;
  83. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-2-13 14:29:47 | 显示全部楼层
然后计算字符串a中所有数字子串对应的整数,找出其平方数中有连续的三位数字就是该数本身的整数。

解释一下这句话

另外,这是我自己的理解,对上面那句话的理解
  1. /* **************************************************************
  2. * 注意:请勿改动主程序main()中的任何语句。
  3. *  *************************************************************
  4. */

  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <conio.h>
  8. #include <ctype.h>
  9. #include <string.h>
  10. #include <math.h>

  11. #define N 80

  12. void  GenStr(char s[], char t[], char a[])
  13. {
  14. /**********Program**********/
  15.         int index = 0;
  16.         for(int i = 0; t[i]; ++i)
  17.         {
  18.                 int flag = 0;
  19.                 for(int j = 0; s[j]; ++j)
  20.                 {
  21.                         if(t[i] == s[j])
  22.                         {
  23.                                 flag = 1;
  24.                                 break;
  25.                         }
  26.                 }

  27.                 if(!flag)
  28.                 {
  29.                         a[index++] = t[i];
  30.                 }
  31.         }

  32.         a[index++] = '\0';

  33. /**********  End  **********/
  34. }

  35. int Calculate(char a[], int b[])
  36. {
  37. /**********Program**********/
  38.         int index = 0;
  39.         char *p = a;
  40.         char buf[1024];
  41.         while(*p)
  42.         {
  43.                 while(!isdigit(*p))
  44.                         ++p;
  45.                 b[index] = strtol(p, &p, 10);

  46.                 char *s = itoa((int)pow(b[index], 2), buf, 10);
  47.                 int count = 0;
  48.                 int max_count = 0;
  49.                 for(int i = 0; s[i]; ++i)
  50.                 {
  51.                         switch(s[i])
  52.                         {
  53.                         case '1': case '2': case '3': case '4': case '5':
  54.                         case '6': case '7': case '8': case '9':
  55.                                 ++count;
  56.                                 break;
  57.                         default:
  58.                                 if(count > max_count)
  59.                                         max_count = count;
  60.                                 count = 0;
  61.                                 break;
  62.                         }
  63.                 }
  64.                 if(max_count == 3)
  65.                         ++index;
  66.         }
  67.         return index;

  68. /**********  End  **********/
  69. }

  70. int main()
  71. {
  72.         char s[] = "AB148";
  73.         char t[] = "E250A148C259G376";
  74.         char a[N];
  75.         int b[N] = {0};
  76.         int num = 0, i = 0;

  77.         FILE *fp;
  78.         if((fp = fopen("DATA.TXT", "w")) == NULL)
  79.         {
  80.                 printf("File open error\n");
  81.                 exit(0);
  82.         }

  83.         GenStr(s, t, a);
  84.         printf("The string a is: %s\n", a);
  85.         fprintf(fp, "The string is: %s\n", a);

  86.         num = Calculate(a, b);
  87.         printf("The integer list is: ");
  88.         fprintf(fp, "The integer list is: ");
  89.         for(i = 0; i < num; i++)
  90.         {
  91.                 printf("%d ", b[i]);
  92.                 fprintf(fp, "%d ", b[i]);
  93.         }

  94.         printf("\n");
  95.         fprintf(fp, "\n");

  96.         fclose(fp);

  97.         return 0;
  98. }
复制代码

  1. The string a is: E250C259G376
  2. The integer list is: 250
  3. 请按任意键继续. . .
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-2-13 16:39:22 | 显示全部楼层
#include<人造人>
return 帖子内容;
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-2-13 17:28:07 | 显示全部楼层
hiwch 发表于 2019-2-13 16:39
#include
return 帖子内容;

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

使用道具 举报

 楼主| 发表于 2019-2-13 17:35:23 | 显示全部楼层

输出不对的,376的平方141376,也是啊
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-2-13 17:43:26 | 显示全部楼层

然后计算字符串a中所有数字子串对应的整数,找出其平方数中有连续的三位数字就是该数本身的整数。
比如250的平方62500,62500中有250这个数,并且是连续的。关键是连续的。


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

使用道具 举报

 楼主| 发表于 2019-2-13 20:38:18 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-1 15:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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