小弟期末考试题目,希望哪位高手帮帮忙...
第1个回答 2009-01-06
#include <stdio.h>
#define MAXN 100
typedef enum { fail, success } status;
typedef enum { false, true} boolean;
char s[MAXN], s1[MAXN], s2[MAXN];
void scopy(char s1[],char s2[])
{int i=0;
while(s2[i]!='\0')
{ s1[i]=s2[i];
i++;
}
s1[i]='\0';
}
int strlen(char s[] )
{ int i;
for (i=0; s[i]!='\0'; i++ );
return (i);
}
status strcat(char s1[],char s2[])
{int i, j, k;
if ((i=strlen (s1)) + (j=strlen (s2) )>=MAXN)
return (fail);
for (k=0; k<=j; k++ )
s1[i+k]=s2[k];
return (success);
}
status strins(char s1[], int i, char s2[ ])
{int m, n, k;
if (i<0||i>(m=strlen(s1))||m+(n=strlen(s2))>MAXN)
return(fail);
if(m+n<=MAXN){
for(k=m+n-1;k>=n+i;k--)
s1[k]=s1[k-n];
for(k=0;k<n;k++)s1[k+i]=s2[k];
m=m+n;
}
else if(k+n<=MAXN){
for(k=MAXN-1;k>n+i-1;k--)s1[k]=s1[k-n];
for(k=0;k<n;k++)s1[k+i]=s2[k];
m=MAXN;
}
else{
for(k=0;k<MAXN-i;k++)s1[k+i]=s2[k];
m=MAXN;
}
return(success);
}
status strsub (sl, i, j, s2)
char sl[ ], s2[ ];
int i,j;
{ int m, k;
if (i<0 || i>= (m=strlen(sl)))
return (fail );
if (j<0 || i+j>m)
return (fail );
else{
for(k=0;k<j;k++)s2[k]=s1[k+i];
j=strlen(s2);
return (success);
}
}
void main( )
{ int i;
i=strlen("aassddd") ;
printf("%d\n",i);
scopy(s1,"abcd");
scopy(s2,"hijkl");
printf("%s\n",s1);
printf("%s\n",s2);
strcat(s1,s2);
printf("%s\n",s1);
printf("%s\n",s2);
strins(s1,4,s2);
printf("%s\n",s1);
printf("%s\n",s2);
}
本回答被提问者采纳
#define MAXN 100
typedef enum { fail, success } status;
typedef enum { false, true} boolean;
char s[MAXN], s1[MAXN], s2[MAXN];
void scopy(char s1[],char s2[])
{int i=0;
while(s2[i]!='\0')
{ s1[i]=s2[i];
i++;
}
s1[i]='\0';
}
int strlen(char s[] )
{ int i;
for (i=0; s[i]!='\0'; i++ );
return (i);
}
status strcat(char s1[],char s2[])
{int i, j, k;
if ((i=strlen (s1)) + (j=strlen (s2) )>=MAXN)
return (fail);
for (k=0; k<=j; k++ )
s1[i+k]=s2[k];
return (success);
}
status strins(char s1[], int i, char s2[ ])
{int m, n, k;
if (i<0||i>(m=strlen(s1))||m+(n=strlen(s2))>MAXN)
return(fail);
if(m+n<=MAXN){
for(k=m+n-1;k>=n+i;k--)
s1[k]=s1[k-n];
for(k=0;k<n;k++)s1[k+i]=s2[k];
m=m+n;
}
else if(k+n<=MAXN){
for(k=MAXN-1;k>n+i-1;k--)s1[k]=s1[k-n];
for(k=0;k<n;k++)s1[k+i]=s2[k];
m=MAXN;
}
else{
for(k=0;k<MAXN-i;k++)s1[k+i]=s2[k];
m=MAXN;
}
return(success);
}
status strsub (sl, i, j, s2)
char sl[ ], s2[ ];
int i,j;
{ int m, k;
if (i<0 || i>= (m=strlen(sl)))
return (fail );
if (j<0 || i+j>m)
return (fail );
else{
for(k=0;k<j;k++)s2[k]=s1[k+i];
j=strlen(s2);
return (success);
}
}
void main( )
{ int i;
i=strlen("aassddd") ;
printf("%d\n",i);
scopy(s1,"abcd");
scopy(s2,"hijkl");
printf("%s\n",s1);
printf("%s\n",s2);
strcat(s1,s2);
printf("%s\n",s1);
printf("%s\n",s2);
strins(s1,4,s2);
printf("%s\n",s1);
printf("%s\n",s2);
}
本回答被提问者采纳
第2个回答 2008-12-24
哪5个基本函数???