鱼C论坛

 找回密码
 立即注册
查看: 1566|回复: 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 ...
/* **************************************************************
 * 注意:请勿改动主程序main()中的任何语句。
 *  *************************************************************
 */

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

#define N 80

void  GenStr(char s[], char t[], char a[])
{
/**********Program**********/
        int index = 0;
        for(int i = 0; t[i]; ++i)
        {
                int flag = 0;
                for(int j = 0; s[j]; ++j)
                {
                        if(t[i] == s[j])
                        {
                                flag = 1;
                                break;
                        }
                }

                if(!flag)
                {
                        a[index++] = t[i];
                }
        }

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

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

int Calculate(char a[], int b[])
{
/**********Program**********/
        int index = 0;
        char *p = a;
        while(*p)
        {
                while(!isdigit(*p))
                        ++p;
                b[index] = strtol(p, &p, 10);

                char buf1[1024];
                char buf2[1024];
                char *s1 = itoa((int)pow(b[index], 2), buf1, 10);
                char *s2 = itoa(b[index], buf2, 10);
                if(strpbrk(s1, s2) != NULL)
                        ++index;
        }
        return index;

/**********  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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

#define N 80

void  GenStr(char s[], char t[], char a[])
{
/**********Program**********/
        int index = 0;
        for(int i = 0; t[i]; ++i)
        {
                int flag = 0;
                for(int j = 0; s[j]; ++j)
                {
                        if(t[i] == s[j])
                        {
                                flag = 1;
                                break;
                        }
                }

                if(!flag)
                {
                        a[index++] = t[i];
                }
        }

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

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

int Calculate(char a[], int b[])
{
/**********Program**********/
        int index = 0;
        char *p = a;
        while(*p)
        {
                while(!isdigit(*p))
                        ++p;
                b[index] = strtol(p, &p, 10);

                char buf1[1024];
                char buf2[1024];
                char *s1 = itoa((int)pow(b[index], 2), buf1, 10);
                char *s2 = itoa(b[index], buf2, 10);
                if(strpbrk(s1, s2) != NULL)
                        ++index;
        }
        return index;

/**********  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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

解释一下这句话

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

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

#define N 80

void  GenStr(char s[], char t[], char a[])
{
/**********Program**********/
        int index = 0;
        for(int i = 0; t[i]; ++i)
        {
                int flag = 0;
                for(int j = 0; s[j]; ++j)
                {
                        if(t[i] == s[j])
                        {
                                flag = 1;
                                break;
                        }
                }

                if(!flag)
                {
                        a[index++] = t[i];
                }
        }

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

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

int Calculate(char a[], int b[])
{
/**********Program**********/
        int index = 0;
        char *p = a;
        char buf[1024];
        while(*p)
        {
                while(!isdigit(*p))
                        ++p;
                b[index] = strtol(p, &p, 10);

                char *s = itoa((int)pow(b[index], 2), buf, 10);
                int count = 0;
                int max_count = 0;
                for(int i = 0; s[i]; ++i)
                {
                        switch(s[i])
                        {
                        case '1': case '2': case '3': case '4': case '5':
                        case '6': case '7': case '8': case '9':
                                ++count;
                                break;
                        default:
                                if(count > max_count)
                                        max_count = count;
                                count = 0;
                                break;
                        }
                }
                if(max_count == 3)
                        ++index;
        }
        return index;

/**********  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;
}
The string a is: E250C259G376
The integer list is: 250
请按任意键继续. . .
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-2-13 16:39:22 | 显示全部楼层
#include<人造人>
return 帖子内容;
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

???
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

输出不对的,376的平方141376,也是啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

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


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-2-13 20:38:18 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-3 08:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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