一个简单的函数

biany2 2013-03-13 02:15:59


char *strstr(const char *s1, const char *s2)
{
int n;
if (*s2)
{
while (*s1)
{
for (n=0; *(s1 + n) == *(s2 + n); n++)
{
if (!*(s2 + n + 1))
return (char *)s1;
}
s1++;
}
return NULL;
}
else
return (char *)s1;
}


求大神指教6行以后是什么意思,本人新手
...全文
171 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ytlcainiao 2013-03-13
  • 打赏
  • 举报
回复
/* 4.3 在计算器中加入取模%功能,注意考虑负数的情况 */ #include <stdio.h> #include <stdlib.h> //for atof函数 #include <math.h> #define MAXOP 100 #define NUMBER '0' int getop(char []); void push(double); double pop(void); int main() { int type; double op2; char s[MAXOP]; while((type = getop(s))!='EOF') { switch (type) { //是数则压入栈 case NUMBER: push(atof(s)); break; //是操作符则取出操作需要数目的数,运算后将结果压入栈 case '+': push(pop()+pop()); break; case '-': op2 = pop(); push(pop()-op2); break; case '*': push(pop()*pop()); break; case '/': op2 = pop(); if (op2!=0.0) push(pop()/op2); else printf("error:zero divisor.\n"); break; case '%': op2 = pop(); if (op2!=0.0) push(fmod(pop(),op2)); else printf("error:zero divisor.\n"); case '\n': printf("计算结果:%.8g\n",pop()); break; default: printf("error:unknown command.\n"); break; } } return 0; } #define MAXVAL 100 int sp = 0; double val[MAXVAL]; //push操作 把值压入堆栈 void push(double f) { if (sp<MAXVAL) { val[sp++] = f; } else printf("error: stack full, can't push %g\n",f); } //pop操作 从堆栈中弹出值 double pop(void) { if (sp>0) { return val[--sp]; } else { printf("error: stack empty\n"); return 0.0; } } #include <ctype.h> int getch(void); void ungetch(int); //getop:获取下一个操作数或运算符 int getop(char s[]) { int i,c; //跳过空格和制表符 while((s[0] = c =getch()) == ' '||c == '\t') ; s[1]='\0'; //不是数字,返回 if (!(isdigit(c)) && c!='.' && c!='-') return c; i = 0; if (c == '-') { if (isdigit(c = getch())||c == '.') { s[++i] = c; } else { if (c!=EOF) ungetch(c); return '-'; } } //收集整数部分 if(isdigit(c)) while(isdigit(s[++i]= c= getch())) ; //收集小数部分 if(c == '.') while(isdigit(s[++i]= c= getch())) ; s[i] = '\0'; if (c!=EOF) { ungetch(c); } return NUMBER; } #define BUFSIZE 100 char buf[BUFSIZE]; int bufp = 0; //缓冲区不为空则getch从缓冲区读出字符,否则用getchar函数直接从输入读取 int getch(void) { return(bufp>0)?buf[--bufp]:getchar(); } //把要压回的字符放到缓冲区 void ungetch(int c) { if (bufp>=BUFSIZE) { printf("ungetch:too many characters\n"); } else buf[bufp++] = c; }
woaishuo2011 2013-03-13
  • 打赏
  • 举报
回复
引用 3 楼 z1173076201 的回复:
C/C++ code?12345678910111213141516171819202122char *strstr(const char *s1, const char *s2){//函数 判断S1从开始位置开始,是否包含S2子串,是则返回*s1,否则返回NULL int n;//用于字符串循环 if (*s2) {//*s2不为空 while (*s1) ……
//可以判断S1任何位置开始是否包含字符串S2如果包含,返回S1剩余字符串的首地址
赵4老师 2013-03-13
  • 打赏
  • 举报
回复
艰涩的代码通常不是被读懂而是被单步调试懂的。 VC调试时按Alt+8、Alt+7、Alt+6和Alt+5,打开汇编窗口、堆栈窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应堆栈、内存和寄存器变化,这样过一遍不就啥都明白了吗。 对VC来说,所谓‘调试时’就是编译连接通过以后,按F10或F11键单步执行一步以后的时候,或者在某行按F9设了断点后按F5执行停在该断点处的时候。 对学习编程者的忠告: 眼过千遍不如手过一遍! 书看千行不如手敲一行! 手敲千行不如单步一行! 单步源代码千行不如单步对应汇编一行!
jimette 2013-03-13
  • 打赏
  • 举报
回复
一楼正解!!!
暮雨朝夕 2013-03-13
  • 打赏
  • 举报
回复

char *strstr(const char *s1, const char *s2)
{//函数 判断S1从开始位置开始,是否包含S2子串,是则返回*s1,否则返回NULL
  int n;//用于字符串循环
  if (*s2)
  {//*s2不为空
    while (*s1)
    {//循环直到*s1为空,及S1字符串结束
      for (n=0; *(s1 + n) == *(s2 + n); n++)
      {//S1字符串从开始位置开始与S2相同的字符
        if (!*(s2 + n + 1))
        //S2结束,表示S1从第一个字符开始包含S2子串
          return (char *)s1;
      }
      s1++;
    }
    //表示S2长度大于S1,及S1从第一个字符开始不包含子串S1
    return NULL;
  }
  else
    //S2为空
    return (char *)s1;
}
mujiok2003 2013-03-13
  • 打赏
  • 举报
回复
在纸上画一下他的执行过程: strstr("let me go", "me");
fishion 2013-03-13
  • 打赏
  • 举报
回复
这段代码应该是在s1中查找跟s2一样的子串吧,你想想这个查找思路就明白这代码的含义了

69,335

社区成员

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

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