#include stdio.h
创新互联公司从2013年创立,是专业互联网技术服务公司,拥有项目成都网站制作、成都做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元乌苏做网站,已为上家服务,为乌苏各地企业和个人服务,联系电话:18980820575
#define MaxSize 100
typedef struct
{
char data[MaxSize];
int len; //串长
} SqString;
void StrAssign(SqString str,char cstr[]) //str为引用型参数
{
int i;
for (i=0;cstr[i]!='\0';i++)
str.data[i]=cstr[i];
str.len=i;
}
void StrCopy(SqString s,SqString t) //s为引用型参数
{
int i;
for (i=0;it.len;i++)
s.data[i]=t.data[i];
s.len=t.len;
}
int StrLength(SqString s)
{
return s.len;
}
SqString Concat(SqString s,SqString t)
{
SqString str;
int i;
str.len=s.len+t.len;
for (i=0;is.len;i++) //将s.data[0]~s.data[s.len-1]复制到str
str.data[i]=s.data[i];
for (i=0;it.len;i++) //将t.data[0]~t.data[t.len-1]复制到str
str.data[s.len+i]=t.data[i];
return str;
}
SqString SubStr(SqString s,int i,int j)
{
SqString str;
int k;
str.len=0;
if (i=0 || is.len || j0 || i+j-1s.len)
return str; //参数不正确时返回空串
for (k=i-1;ki+j-1;k++) //将s.data[i]~s.data[i+j]复制到str
str.data[k-i+1]=s.data[k];
str.len=j;
return str;
}
void DispStr(SqString s)
{
int i;
if (s.len0)
{
for (i=0;is.len;i++)
printf("%c",s.data[i]);
printf("\n");
}
}
SqString invert(SqString s) //例6-2的算法
{
SqString s1,s2;
if (StrLength(s)0)
{
s1=invert(SubStr(s,2,StrLength(s)-1));
s2=Concat(s1,SubStr(s,1,1));
}
else
StrCopy(s2,s);
return s2;
}
void main()
{
SqString s,t;
StrAssign(s,"abcd1234");
printf("s:");DispStr(s);
printf("s-t\n");
t=invert(s);
printf("t:");DispStr(t);
假设前面定义过链表p了,待查找的是n。
q=p;
while (q-next!=null *q!=n)
q=q-next;
if (*q!=n) printf("no suitable value")
1 #include string.h
2 #include stdio.h
3 #include stdlib.h
4
5 #define MAX_POS_NUM 100
6 #define MAX_STR_LEN 1024
7
8
9 //1. get all position of str_z in str_x
10 int get_sub_str_pos(const char * str_x, const char * str_z, int sub_str_pos[])
11 {
12 if (NULL == str_x || NULL == str_z)
13 {
14 printf("in error!\n");
15 return -1;
16 }
17
18 const char * pos_ptr = NULL;
19
20 pos_ptr = strstr(str_x,str_z);
21
22 int i=0;
23 while(pos_ptr)
24 {
25 printf("substring positon:%d\n",pos_ptr-str_x+1);
26 sub_str_pos[i] = pos_ptr - str_x + 1;
27 pos_ptr = strstr(pos_ptr+strlen(str_z),str_z);
28 i++;
29 }
30
31 return 0;
32 }
33
34 //2. get max length common string of str_x and str_y
35 char * get_max_com_str(const char * str_x, const char * str_y)
36 {
37 int x_len = strlen(str_x);
38 int y_len = strlen(str_y);
39
40 char * tmp_str = new char[y_len+1];
41
42 for(int i=y_len; i0; i--) // i is substring length
43 {
44 if (ix_len)
45 continue;
46 for(int j=0;j=y_len-i; j++) // j is substring start postion
47 {
48 snprintf(tmp_str,i+1,"%s",str_y);
49 if (strstr(str_x,tmp_str))
50 {
51 printf("%s\n",tmp_str);
52 printf("max common substring length:%d\n",i);
53 return tmp_str;
54 }
55 }
56 }
57
58 return NULL;
59 }
60
61 //3. replace all substring in question 1
62 char * replace_sub_str(const char * str_x, char * max_com_str, int sub_str_pos[], int sub_str_len)
63 {
64 char * replaced_str = new char[MAX_STR_LEN];
65
66 int sub_pos = sub_str_pos[0];
67 int l=0; // l is sub_str_pos index
68 int i=0,j=0; //i is str_x pos, j is replaced_str pos
69
70 while(*str_x)
71 {
72 if (i==sub_pos-1) // replace from this position
73 {
74 // printf ("i:%d,\n",i);
75 for (int k=0; kstrlen(max_com_str); k++)
76 {
77 *(replaced_str + j) = * (max_com_str + k);
78 j++;
79 }
80 i += sub_str_len;
81 str_x += sub_str_len;
82 l++;
83 sub_pos = sub_str_pos[l];
84 continue;
85 }
86 *(replaced_str+j) = *str_x++;
87 i++;
88 j++;
89 }
90
91 * (replaced_str + j) = '\0';
92
93 return replaced_str;
94 }
95
96 int main()
97 {
98 const char * str_x = "abcabcabc";
99 const char * str_y = "cabcd";
100 const char * str_z = "abc";
101
102 int sub_str_pos [MAX_POS_NUM] = {0};
103
104 char * max_com_str = NULL;
105
106 char * replaced_str = NULL;
107
108 get_sub_str_pos(str_x,str_z,sub_str_pos);
109 max_com_str = get_max_com_str(str_x,str_y);
110
111 printf("max common str: %s\n",max_com_str);
112
113 replaced_str = replace_sub_str(str_x, max_com_str, sub_str_pos, strlen(str_z));
114 printf("repalced str: %s\n",replaced_str);
115
116 return 0;
117 }
#includeiostream.h
#includemalloc.h
#define len sizeof(LNode)
typedef struct Lnode //结点结构声明
{ int nf; // 多项式系数
int ne; //多项式幂指数
struct LNode *next;
}LNode;
typedef LNode *Pol;
LNode *creat(void) //创建多项式函数
{ LNode *head,*q;
LNode *n;
head=q=n=(LNode *)malloc(len);
q-next=NULL;
do{n=(LNode *)malloc(len);
cinn-nen-nf;
q-next=n;
q=n;}while(n-nf!=0||n-ne!=0);
q-next=NULL;
return head;}
LNode *output(LNode *head) //输出多项式的函数
{LNode *p;
p=head-next;int n=0;
if(p-nf==0p-ne==0) {cout"0";return 0;}
else do{if(p-nf!=0)
{if(n==0)
{if(p-ne==0) {coutp-nf;break;}
else{coutp-nf"X"p-ne;n++;p=p-next;}}
else {if(p-ne==0) {if(p-nf0)coutp-nf;else cout"+"p-nf;break;}
else if(p-nf0) {coutp-nf"X"p-ne;p=p-next;}
else {cout"+"p-nf"X"p-ne;p=p-next;}}}
else p=p-next;}while(p-nf!=0||p-ne!=0);
if(n==0p-nf==0p-ne==0) {cout"0";return 0;}
return 0;}
LNode * add(Pol Pa,Pol Pb)//两个多项式相加的函数
{LNode *p1,*p2,*p,*pr,*p0;
p1=Pa-next;p2=Pb-next;
p0=pr=Pa;
while(p1-next!=NULL||p2-next!=NULL)
{if(p1-nep2-ne)
{pr=p1;p1=p1-next;}
else if(p1-ne==p2-ne)
{p1-nf=p1-nf+p2-nf;p2=p2-next;}
else {pr-next=p2;p2=p2-next; pr=pr-next;pr-next=p1;}
if(p1-ne==0p2-next==NULL) break;}
p1-next=p2;
return p0;
}
void main()//主函数
{char y;
for(;;){Pol p1,p2,p3;
cout"input ha"endl;
p1=creat(); //建立多项式HA
cout"input hb"endl;
p2=creat(); //建立多项式HB
cout"ha=";*output(p1);coutendl;//输出多项式HA
cout"hb=";*output(p2);coutendl; //输出多项式HB
p3=add(p1,p2); //多项式HA与HB相加
cout"ha+hb=";*output(p3);coutendl; 输出多项式HC
cout"ARE YOU CONTINUE?(Y|N)"endl;
ciny;
if(y=='n') break;}
#include stdio.h
#include string.h
int main()
{
char szText[100] = {0};
int nMaxBegin = 0, nMaxLen = 0, nCurLen = 0;
gets(szText);
szText[strlen(szText)] = ' ';
for (int i = 0; i strlen(szText); i++)
{
if (szText[i] != ' ')
{
nCurLen ++;
}
else
{
if (nMaxLen nCurLen)
{
nMaxBegin = i - nCurLen;
nMaxLen = nCurLen;
}
nCurLen = 0;
}
}
char szMaxString[100] = {0};
memcpy(szMaxString, szText + nMaxBegin, nMaxLen);
printf("%s\n", szMaxString);
return 0;
}
#includestdio.h
#includestring.h
char str[1005],ans[1005];
void main()
{
int MaxLen = 0;
int CurLen;
int i;
for(i=0;i10;++i)
{
gets(str);
CurLen = strlen(str);
if(CurLenMaxLen)MaxLen=CurLen,strcpy(ans,str);
}
puts(ans);
}