编写expand(s1,s2),将字符串s1中类似于a-z类的速记符号在字符串s2中扩展为等价的完整列表abc.....xyz。该函数可以处理大小写字母和数字,

madelaine_ 2016-04-09 10:36:52
#include<stdio.h>
int main()
{
char alpha[100]={'a'},expand[100]={'a'};
int i=0,j=0,m;
//读入原始字符
for(i=0;(m=getchar())!='\n';i++)
alpha[i]=m;
alpha[i]='\0';
/*当s1[i+1]==‘-’ && s1[i] < s1[i+2]时,
将s1[i]至s1[i+2]之间的字符展开到s2中
否则
将s1[i]原封不动的写到s2中*/
for(i=0;i<sizeof(alpha);i++)
{
if(alpha[i]<alpha[i+2]&&alpha[i+1]=='-')
{
for(j=0;j<alpha[i+2]-alpha[i]+1;j++)
{
expand[j]=alpha[i];
alpha[i]+=1;
}
}
else
{
expand[j++]=alpha[i];
}
}
//输出扩展字符
for(j=0;j<sizeof(expand);j++)
printf("%c",expand);
return 0;
}
求解答!
...全文
513 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-04-11
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。 判断是否越界访问,可以在数组的最后一个元素之后对应的地址处设置数据读写断点。如果该地址对应其它变量干扰判断,可将数组多声明一个元素,并设置数据读写断点在该多出元素对应的地址上。
#include <time.h>
#include <stdlib.h>
#include <windows.h>
int main() {
    int a,b[11];//本来是b[10],为判断哪句越界,故意声明为b[11]

    srand((unsigned int)time(NULL));//按两次F11,等黄色右箭头指向本行时,调试、新建断点、新建数据断点,地址:&b[10],字节计数:4,确定。
    while (1) {//按F5,会停在下面某句,此时a的值为10,b[10]已经被修改为对应0..4之一。
        b[(a=rand()%11)]=0;
        Sleep(100);
        b[(a=rand()%11)]=1;
        Sleep(100);
        b[(a=rand()%11)]=2;
        Sleep(100);
        b[(a=rand()%11)]=3;
        Sleep(100);
        b[(a=rand()%11)]=4;
        Sleep(100);
    }
    return 0;
}
赵4老师 2016-04-11
  • 打赏
  • 举报
回复
仅供参考:
#include <conio.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
FILE *f;
int i,n,line,st;
int n1,n2,n3;
int b1,b2,b3;
int e1,e2,e3;
int w1,w2,w3;
int v1,v2,v3;
char ln[256];
char fmt[256];
char fmd[6];
char tmp[256];
char *s1,*s2,*s3,*s4;
char L,R,D;
void main(int argc,char *argv[]) {
    if (argc<2) {
    PMT:
        printf("%s filename.ext [ParenthesesChar] [DelimiterChar]\nParenthesesChar is '(' default or '[' or '{'\nDelimiterChar is ',' default",argv[0]);
        return;
    }
    if (argc==2) {
        L='(';R=')';D=',';
    } else {
        switch (argv[2][0]) {
            case '(':L='(';R=')';break;
            case '[':L='[';R=']';break;
            case '{':L='{';R='}';break;
            default:goto PMT;
        }
        D=',';
        if (argc>=4) D=argv[3][0];
    }
    sprintf(fmd,"%%d%c%%d",D);
    if ((f=fopen(argv[1],"rt"))==NULL) {
        printf("Can not open %s",argv[1]);
        return;
    }
    line=0;
    while (1) {
        fgets(ln,255,f);
        if (feof(f)) break;//
        line++;
        n=0;st=0;
        for (i=0;i<(int)strlen(ln);i++) {
            switch (st) {
            case 0:if (ln[i]==L) st=1;break;
            case 1:if (ln[i]==D) st=2;break;
            case 2:if (ln[i]==R) {st=0;n++;} break;
            }
        }
        switch (n) {
            case 0:
                printf("%s",ln);
            break;
            case 1:
                s1=strchr(ln,L);
                s2=strchr(s1,R);
                if (s2==NULL) {
                    cprintf("\a\a\a missing %c in line %d",R,line);
                    fclose(f);
                    return;
                }
                s1[0]=0;
                s1++;
                s2++;
                v1=sscanf(s1,fmd,&b1,&e1);
                if (v1==2) {
                    w1=1+(int)log10((double)e1);
                    sprintf(fmt,"%%s%%0%1dd%%s",w1);
                    for (n1=b1;n1<=e1;n1++) {
                        printf(fmt,ln,n1,s2);
                    }
                } else if (s1[1]==D) {
                    b1=s1[0];
                    e1=s1[2];
                    for (n1=b1;n1<=e1;n1++) {
                        printf("%s%c%s",ln,n1,s2);
                    }
                } else {
                    printf("%s",ln);
                }
            break;
            case 2:
                s1=strchr(ln,L);
                s2=strchr(s1,R);
                if (s2==NULL) {
                    cprintf("\a\a\a missing %c in line %d",R,line);
                    fclose(f);
                    return;
                }
                s1[0]=0;
                s1++;
                s2++;
                v1=sscanf(s1,fmd,&b1,&e1);
                if (v1==2) {
                    w1=1+(int)log10((double)e1);
                } else {
                    b1=s1[0];
                    e1=s1[2];
                }

                s1=strchr(s2,L);
                s3=strchr(s1,R);
                if (s3==NULL) {
                    cprintf("\a\a\a missing %c in line %d",R,line);
                    fclose(f);
                    return;
                }
                s1[0]=0;
                s1++;
                s3++;
                v2=sscanf(s1,fmd,&b2,&e2);
                if (v2==2) {
                    w2=1+(int)log10((double)e2);
                } else {
                    b2=s1[0];
                    e2=s1[2];
                }

                strcpy(fmt,"%s");
                if (v1==2) sprintf(tmp,"%%0%1dd",w1);
                else strcpy(tmp,"%c");
                strcat(fmt,tmp);
                strcat(fmt,"%s");
                if (v2==2) sprintf(tmp,"%%0%1dd",w2);
                else strcpy(tmp,"%c");
                strcat(fmt,tmp);
                strcat(fmt,"%s");
                for (n1=b1;n1<=e1;n1++) {
                    for (n2=b2;n2<=e2;n2++) {
                        printf(fmt,ln,n1,s2,n2,s3);
                    }
                }
            break;
            case 3:
                s1=strchr(ln,L);
                s2=strchr(s1,R);
                if (s2==NULL) {
                    cprintf("\a\a\a missing %c in line %d",R,line);
                    fclose(f);
                    return;
                }
                s1[0]=0;
                s1++;
                s2++;
                v1=sscanf(s1,fmd,&b1,&e1);
                if (v1==2) {
                    w1=1+(int)log10((double)e1);
                } else {
                    b1=s1[0];
                    e1=s1[2];
                }

                s1=strchr(s2,L);
                s3=strchr(s1,R);
                if (s3==NULL) {
                    cprintf("\a\a\a missing %c in line %d",R,line);
                    fclose(f);
                    return;
                }
                s1[0]=0;
                s1++;
                s3++;
                v2=sscanf(s1,fmd,&b2,&e2);
                if (v2==2) {
                    w2=1+(int)log10((double)e2);
                } else {
                    b2=s1[0];
                    e2=s1[2];
                }

                s1=strchr(s3,L);
                s4=strchr(s1,R);
                if (s4==NULL) {
                    cprintf("\a\a\a missing %c in line %d",R,line);
                    fclose(f);
                    return;
                }
                s1[0]=0;
                s1++;
                s4++;
                v3=sscanf(s1,fmd,&b3,&e3);
                if (v3==2) {
                    w3=1+(int)log10((double)e3);
                } else {
                    b3=s1[0];
                    e3=s1[2];
                }

                sprintf(fmt,"%%s%%0%1dd%%s%%0%1dd%%s%%0%1dd%%s",w1,w2,w3);
                strcpy(fmt,"%s");
                if (v1==2) sprintf(tmp,"%%0%1dd",w1);
                else strcpy(tmp,"%c");
                strcat(fmt,tmp);
                strcat(fmt,"%s");
                if (v2==2) sprintf(tmp,"%%0%1dd",w2);
                else strcpy(tmp,"%c");
                strcat(fmt,tmp);
                strcat(fmt,"%s");
                if (v3==2) sprintf(tmp,"%%0%1dd",w3);
                else strcpy(tmp,"%c");
                strcat(fmt,tmp);
                strcat(fmt,"%s");

                for (n1=b1;n1<=e1;n1++) {
                    for (n2=b2;n2<=e2;n2++) {
                        for (n3=b3;n3<=e3;n3++) {
                            printf(fmt,ln,n1,s2,n2,s3,n3,s4);
                        }

                    }
                }
            break;
            default:
                cprintf("\a\a\a > 3 repeat in line %d",line);
                fclose(f);
                return;
        }
    }
    fclose(f);
}
小灸舞 版主 2016-04-09
  • 打赏
  • 举报
回复
利用了一个指针来实现,楼主不能sizeof(expand)要用strlen(expand)

#include<stdio.h>
void main()
{
char alpha[100]={0},expand[100]={0};
int i=0,j=0,m;
//读入原始字符
for(i=0;(m=getchar())!='\n';i++)
alpha[i]=m;
alpha[i]='\0';
char *e = expand;
for(;j < i; j++)
{
*e++ = alpha[j];
if(alpha[j] == '-')
{
e--;
for(m = alpha[j - 1] + 1; m < alpha[j + 1]; m++){
*e++ = m;
}
}
}
*e = '\0';
/*当s1[i+1]==‘-’ && s1[i] < s1[i+2]时,
将s1[i]至s1[i+2]之间的字符展开到s2中
否则
将s1[i]原封不动的写到s2中
for(i=0;i<sizeof(alpha);i++)
{
if(alpha[i]<alpha[i+2]&&alpha[i+1]=='-')
{
for(j=0;j<alpha[i+2]-alpha[i]+1;j++)
{
expand[j]=alpha[i];
alpha[i]+=1;
}
}
else
{
expand[j++]=alpha[i];
}
}*/
//输出扩展字符
for(j=0;j<strlen(expand);j++)
printf("%c",expand[j]);
printf("\n");
}

madelaine_ 2016-04-09
  • 打赏
  • 举报
回复
谢谢! 不好意思我是新手,请多指教,麻烦您了。
小灸舞 版主 2016-04-09
  • 打赏
  • 举报
回复
楼主你都知道是数组越界的问题了。。。。 还不能改吗 单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。
madelaine_ 2016-04-09
  • 打赏
  • 举报
回复
引用 1 楼 qq423399099 的回复:
利用了一个指针来实现,楼主不能sizeof(expand)要用strlen(expand)

#include<stdio.h>
void main()
{
char alpha[100]={0},expand[100]={0};
int i=0,j=0,m;
//读入原始字符
for(i=0;(m=getchar())!='\n';i++)
alpha[i]=m;
alpha[i]='\0';
char *e = expand;
for(;j < i; j++)
{
*e++ = alpha[j];
if(alpha[j] == '-')
{
e--;
for(m = alpha[j - 1] + 1; m < alpha[j + 1]; m++){
*e++ = m;
}
}
}
*e = '\0';
/*当s1[i+1]==‘-’ && s1[i] < s1[i+2]时,
将s1[i]至s1[i+2]之间的字符展开到s2中
否则
将s1[i]原封不动的写到s2中
for(i=0;i<sizeof(alpha);i++)
{
if(alpha[i]<alpha[i+2]&&alpha[i+1]=='-')
{
for(j=0;j<alpha[i+2]-alpha[i]+1;j++)
{
expand[j]=alpha[i];
alpha[i]+=1;
}
}
else
{
expand[j++]=alpha[i];
}
}*/
//输出扩展字符
for(j=0;j<strlen(expand);j++)
printf("%c",expand[j]);
printf("\n");
}
[/code

为了实现a-a将被扩展为:a-a以及扩展输入a-c-u为:abcdefghijklmnopqrstu,而B比u值小,所以无法扩展,直接输出abcdefghijklmnopqrstu-B这些情况,我加了一些,但是出现了一个警告框。
[code=c]#include<stdio.h>
#include<string.h>
int main()
{
char alpha[100]={'a'},expand[100]={'a'};
int i=0,j=0,m=0,n=0;
char*p=expand;
//读入原始字符
for(i=0;(m=getchar())!='\n';i++)
alpha[i]=m;
alpha[i]='\0';
//找出扩展字符
for(j=0;j<i;j++)
{
*p++=alpha[j];//遍历alpha数组
if(alpha[j]=='-')//如果是‘-’就输出扩展字符
{
*p--;//指针跑到‘-’前面一位
for(n=alpha[j-1]+1;n<alpha[j+1];n++)
{
*p=n;
*p++;
}
for(;alpha[j-1]>=alpha[j+1];)//当出现类似‘u-B’的情况时,这个是我加的
*p++=alpha[j++];
}
}
*p='\0';
//输出扩展字符
for(j=0;j<strlen(expand);j++)
printf("%c",expand[j]);
return 0;
}

现在可以正常输出,但是这个框是怎么消除呢?我百度了一下,是数组越界的问题。



69,374

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧