在上周的博客上也写到要增长打代码的时间,但愿从这一周严格执行,遇上学习的进度。算法
简要介绍题目 求子串在母串中最后一次出现的地址 给定程序中函数fun的功能是:求出在字符串中最后一次出现的子字符串的地址,经过函数值返回,在主函数中输出今后地址开始的字符串;若未找到,则函数值为 NULL。
先循环母串找到与子串相同的第一个字符 for i=0 to *(s+i)!=‘\0' 找到第一个相同的字符 if *(s+i)==*t then 先标记1,当找到一半有不一样的状况下标0 flag=1 for j=0 to *(t+j)循环子串寻找是否有不一样 if *(s+i+j)!=*(t+j)!='\0' then找到有不一样的 flag=0; break; 经过检验没被标0的代表与子串相同 if flag==1 then 记录位置 position=i 判断有否找到,并返回地址 if(flag==1) return (s+position); else return NULL;
题目代码
函数express
Q:为何出现部分错误
A:再返回地址的时候没有返回正确的地址,把传入的地址返回了,而后检查了很久前面的代码。数组
产生表达式,分红一级和二三级 if (*selectPtr)==1 then//一级 exp[0]=item[0]+'0' 数字 exp[1]=sign[*p1] 加减 exp[2]=item[1]+'0' 数字 exp[3]='=' 等号 exp[4]='\0' 终止 puts(exp) 输出 memset(exp, '\0', sizeof(exp)) 防止下一次产生的表达式粘在一块儿输出 if (*selectPtr)==2||(*selectPtr)==3 then//二三级 for i=0 to 3 if (*selectPtr)==2 then itoa(item[i],str1,10)整数化成字符,存入数组 strcat(exp,str1) end if if (*selectPtr)==3 then itoa(item[i],str2,10); strcat(exp,str2); end if 控制加减号和等号的输出 if i==0 then switch(*p2) case 0:strcat(exp,"+");break; case 1:strcat(exp,"-");break end if if i==1 then switch(*p3) case 0:strcat(exp,"+");break; case 1:strcat(exp,"-");break; end if end for strcat(exp,"=");//最后附上等号 puts(exp); memset(exp, '\0', sizeof(exp));//覆盖掉前一个表达式 end if
截图函数代码。函数
count 以0为加 以1为减 分红各类状况进行计算 例如 if *p2==0&&*p3==0 then return item[0]+item[1]+item[2] end if
输入题目的个数 循环排错非法字符 while scanf( "%d", &quantity)==0 printf("请不要输入无关字符.\n") fflush(stdin); end while for i = 1 to i <=response 控制题目的个数 产生随机符号 *p1=rand()%4; *p2=rand()%2; *p3=rand()%2; if (*selectPtr)==1 then 1级 产生10之内随机数,生成表达式 expression(&select,sign,item,&k,&x,&y); if response!=count1(&k,item) then no++; error(); printf("正确答案是%d\n", count1(&k,item)); end if else yes++; right() end else 二三级同上 end if end for if i==quantity+1 then printf("*你的得分是%d",yes*100/(no+yes) ) end if
改造前函数1和改造后的函数1学习
改造前函数2和改造后的函数2指针
改造前函数3和改造后的函数3调试
回顾两次做业编写过程,总结碰到问题及后续程序编写注意事项。code