实验十二参考答案

更新时间:2023-11-18 17:49:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

实验十二

12_1..

#include #include #include

int stoi(char *s,int *i)/*void */ { long n=0; while(isdigit(s[*i])) n=n*10+s[(*i)++]-'0';/*'0'*/ return n; }

long add(char *s) { int i=0,*pi=&i;/*=&i*/ char op; long a=0,b=0,c; a=stoi(s,pi); op=s[i++]; b=stoi(s,pi); switch(op){ case '+':return a+b; case '-':return a-b; case '*':return a*b; case '/':if(b==0) {printf(\ else return a/b; /*else*/ default: printf(\ } }

int main(void) { char s[80]; gets(s); printf(\ printf(\ return 0; }

12_2.

#include

int match(char *pat, char *str)/*void */ { char *p=pat,*q=str; int i=1; /*i=0 */ while((*p!='\\0')&&(*q!='\\0'))

if(*p==*q||*p=='?') p++,q++; else if(*p=='*'){

p++;

while(*q!=*p && *q!='\\0')q++; } else {i=0; break; } /*{ }*/ if(*p!='\\0'||*q!='\\0') i=0; return i; }

int main(void) { char a[10],b[10]; int yesno; gets(a); gets(b); yesno=match(a,b);/*match(a[10],b[10]);*/ printf(\ return 0; }

12_3.

#include #include #include

int isident(char s[]);

int main(void) { char s[100],ch; int freq[26]={0},i=0; clrscr(); gets(s); for(i=0;s[i];i++) if(isalpha(s[i])){ ch=(s[i]>='A'&&s[i]<='Z')?s[i]+32:s[i];

freq[ch-'a']++; } for(i=0;i<26;i++) if(freq[i]) printf(\ return 0; }

12_4.

#include #include #include

int fun(char a[]);

int main(void) { char s[100],ch; int n; clrscr(); gets(s); n=fun(s); printf(\ puts(s); return 0; }

int fun(char a[]) { int i,j=0,count=0; for(i=0;a[i];i++) if(!isdigit(a[i])) a[j++]=a[i]; else count++; a[j]='\\0'; return count; }

12_5.

#include #include #include

void fun(char xx[]);

int main(void) { char s[100]; clrscr(); gets(s); fun(s); puts(s); return 0; }

void fun(char xx[]) { int i,count=0; for(i=0;xx[i];i++) if(isdigit(xx[i])) count++; for(i=strlen(xx);i>=0;i--) { xx[i+count]=xx[i]; if(isdigit(xx[i])){ xx[i+count-1]='$'; count--; } } }

12_6.

#include #include #include #include

void FindLWord(char *a,char *b);

int main(void) { char s[100],str_long[20]=\ clrscr(); gets(s); FindLWord(s,str_long);

puts(str_long); return 0; }

void FindLWord(char *a,char *b) { char temp[20]; int word=0,k=0,i,j; for(i=0;i<=strlen(a);i++) if(isalpha(a[i])){ if(word==0) k=0; temp[k++]=a[i]; word=1; }else if(word==1){ temp[k]='\\0'; word=0; if(strlen(temp)>strlen(b)) strcpy(b,temp); } }

12_7.

#include #include #include

int replace_str(char *s,char *t,char *g); int findsub(char *a,char *b);

int main(void) { char s[100],t[20],g[20]; int count; clrscr(); printf(\ gets(s); printf(\ gets(t); printf(\ gets(g); count=replace_str(s,t,g); printf(\ count=%d\

return 0; }

int replace_str(char *s,char *t,char *g) { char tempstr[100]; int position,k,tail; if(strlen(s)==0) return 0; position=findsub(s,t); if(position==-1) return 0; k=strlen(t); strcpy(tempstr,s+position+k); s[position]='\\0'; strcat(s,g); tail=strlen(s); strcat(s,tempstr); return 1+replace_str(s+tail,t,g); }

int findsub(char *a,char *b) { int i,j,k; for(i=0;a[i]!='\\0';i++){ for(j=i,k=0; a[j]==b[k]&&k

return -1; }

return 0; }

int replace_str(char *s,char *t,char *g) { char tempstr[100]; int position,k,tail; if(strlen(s)==0) return 0; position=findsub(s,t); if(position==-1) return 0; k=strlen(t); strcpy(tempstr,s+position+k); s[position]='\\0'; strcat(s,g); tail=strlen(s); strcat(s,tempstr); return 1+replace_str(s+tail,t,g); }

int findsub(char *a,char *b) { int i,j,k; for(i=0;a[i]!='\\0';i++){ for(j=i,k=0; a[j]==b[k]&&k

return -1; }

本文来源:https://www.bwwdw.com/article/q95v.html

Top