在上週的博客上也寫到要增長打代碼的時間,但願從這一週嚴格執行,遇上學習的進度。算法
簡要介紹題目 求子串在母串中最後一次出現的地址 給定程序中函數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