📄 main.cpp
字号:
#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
#define OVERFLOW 0
#define status int
typedef struct HString{char *ch; //堆分配存储
int length;
}HString;
typedef struct StrHeadList{HString StrHead[100];
int CurNum;
}StrHeadList;
typedef struct ResultType{char CmdNo;
int s[3];
int num[2];
}ResultType;
StrHeadList List; //定义串头表为全局变量,List.StrHead数组0号单元不用
//函数定义
int chartoint(HString s)
{int i;
if(s.length==1) //只是单位数
i=s.ch[0]-48;
else if(s.length==2)
i=10*(s.ch[0]-48)+s.ch[1]-48;
else if(s.length==3)
i=100*(s.ch[0]-48)+10*(s.ch[0]-48)+s.ch[1]-48;
return i;
}
//-------------------------------------------------------------------------
void assign(HString t) //只负责赋值,输出显示由主函数完成
{List.CurNum++;
List.StrHead[List.CurNum]=t;
}
//----------------------------------------------------------------------------
status combine(HString &s,HString s1,HString s2)
{int i,j;
if(!(s.ch=(char*)malloc((s1.length+s2.length)*sizeof(char))))
exit(OVERFLOW);
for(i=0;i<s1.length;i++)
s.ch[i]=s1.ch[i];
for(j=0;j<s2.length;j++,i++)
s.ch[i]=s2.ch[j];
s.length=s1.length+s2.length;
s.ch[s.length]='\0';
return OK;
}
//---------------------------------------------------------------
void del(int i)
{if(i<0)
{printf("\n\n\n\n\n\n\n\t\tSorry,you input the wrong syntax");
return;
}
if(i>List.CurNum)
{printf("\n\n\n\n\n\n\n\t\tthere is not the data"); return;}
printf("\n\n\n\n\n\n\n\t\tYou have deleted:\n");
printf("\t\tInside name The string\n");
printf("\t\t%d '%s'\n",i,List.StrHead[i].ch);
for(;i<List.CurNum;i++)
List.StrHead[i]=List.StrHead[i+1];
List.CurNum--;
}
//---------------------------------------------------------
void display()
{int i;
printf("\n\n\n\n\n\n\t\tInside name The string\n");
for(i=1;i<=List.CurNum;i++)
printf("\t\t%d '%s'\n",i,List.StrHead[i].ch);
}
//--------------------------------------------------
void position(HString s,HString t)
{if(!t.length) {printf("\n\n\n\n\n\n\n\t\tSorry,you input the wrong syntax!");return;}
int len=t.length;
int nextval[50];
int i,j;
i=0;nextval[0]=-1;j=-1;
while(i<(len-1))
{if(j==-1||t.ch[i]==t.ch[j]){
++i; ++j;
if(t.ch[i]!=t.ch[j]) nextval[i]=j;
else nextval[i]=nextval[j];
}//if
else j=nextval[j];
}//while
//以上为建立nextval数组
i=0; j=0;
while(i<s.length&&j<t.length)
{if(j==-1||s.ch[i]==t.ch[j]) {++i;++j;}
else j=nextval[j];
}//while
if(j==t.length)
printf("\n\n\n\n\n\n\n\t\tThe position is: %d",(i-t.length+1));
else
printf("\n\n\n\n\n\n\n\t\tSorry,CANNOT FIND");
}//----------------------------------------------------
status replace(HString &s,HString s1,HString s2,HString s3) //s3替换s2
{int flag=0,pos;
int nextval[50];
int i,j,k;
i=0;nextval[0]=-1;j=-1;
while(i<(s2.length-1))
{if(j==-1||s2.ch[i]==s2.ch[j]){
++i; ++j;
if(s2.ch[i]!=s2.ch[j]) nextval[i]=j;
else nextval[i]=nextval[j];
}//if
else j=nextval[j];
}//while
//以上为建立nextval数组
s.ch=(char*)malloc(s1.length*sizeof(char));
for(k=0;k<s1.length;k++)
s.ch[k]=s1.ch[k];
s.length=s1.length; //将主串复制出来
i=0; j=0;
while(i<s.length)
{if(j==-1||s.ch[i]==s2.ch[j]) {++i;++j;}
else j=nextval[j];
if(j==s2.length)//找到了相应的子串
{flag=1;
pos=i;
if(s2.length>=s3.length)
for(;i<s.length;i++)
s.ch[i-(s2.length-s3.length)]=s.ch[i]; //腾出空位
else
{s.ch=(char*)realloc(s.ch,(s.length+s3.length-s2.length)*sizeof(char));
i=s.length-1;
for(;i>=pos;i--)
s.ch[i+s3.length-s2.length]=s.ch[i]; //腾出空位
}//else
i=pos-s2.length;
for(j=0;j<s3.length;)
s.ch[i++]=s3.ch[j++]; //赋值
s.length+=s3.length-s2.length;
j=0;
}//if
} //while
s.ch[s.length]='\0';
return flag;
}//repalce
//--------------------------------------------------------
status strcompare(HString s1,HString s2)
{int i;
if(s1.length!=s2.length)
{printf("\n\n\n\n\n\n\n\t\t\tUNEQUAL");
return ERROR;
}
if(!s1.length&&!s2.length)
{printf("\n\n\n\n\n\n\t\t\tEQUAL");
return OK;
}
for(i=0;i<s1.length;i++)
if(s1.ch[i]!=s2.ch[i])
{printf("\n\n\n\n\n\n\t\t\tUNEQUAL");
return ERROR;
}
printf("\n\n\n\n\n\n\t\t\tEQUAL");
return OK;
}
//-----------------------------------------------------------------
void strlength(HString s)
{printf("\n\n\n\n\n\n\n\t\tThe string's length is: %d\n",s.length);
}
//-----------------------------------------------------------------
status substring(HString &sub,HString s,int pos,int len)
{if(pos<0||len<0||pos>s.length||len>s.length-pos+1)
return ERROR;
int i,j;
if(!len){sub.ch=NULL;sub.length=0;return OK;}
else{
sub.ch=(char*)malloc(len*sizeof(char));
for(i=0,j=pos;j<=pos+len-1;j++,i++)
sub.ch[i]=s.ch[j] ;
sub.length=len;
sub.ch[sub.length]='\0';
}
for(i=0;i<sub.length;i++) //将双引号变成单引号
{if(sub.ch[i]==39&&sub.ch[i+1]==39)
{for(j=i+1;j<sub.length;j++)
sub.ch[j]=sub.ch[j+1];
sub.length--;
}//if
}//for
return OK;
}
//----------------------------------------------------------
status analyse(ResultType &act,char *c)
{int i=1,start,end,Len=0;
HString command,s1,s2,s3;
while(c[Len]!='\0') Len++; //求得命令串的长度
command.ch=c; //将HString类型的command指向输入来的字符串
command.length=Len;
act.CmdNo=c[0];
switch(act.CmdNo)
{case'R':{while(c[i]==' ') //串替换
i++;
if(c[i]!=39)
return ERROR;
i++; // 跳过单引号
start=i;
while(!(c[i]==39&&c[i+1]==' ')&&c[i]!='\0')
i++;
if(c[i]=='\0')
return ERROR;
end=i-1;
substring(s1,command,start,(end-start+1));//抽取串标识出来
assign(s1);//将抽取出来的串标识放到串头表中
act.s[0]=List.CurNum;
i++;//跳过单引号'
if(c[i]!=' ') //串标识必须以空格隔开
return ERROR;
while(c[i]==' ') //找第二个串标识
i++;
if(c[i]!=39)
return ERROR;
i++; // 跳过单引号
start=i;
while(!(c[i]==39&&c[i+1]==' ')&&c[i]!='\0')
i++;
if(c[i]=='\0')
return ERROR;
end=i-1;
substring(s2,command,start,(end-start+1));//抽取串标识出来
assign(s2);
act.s[1]=List.CurNum;
i++;// 跳过单引号
if(c[i]!=' ') //串标识必须以空格隔开
return ERROR;
while(c[i]==' ') //找第三个串标识
i++;
if(c[i]!=39)
return ERROR;
i++; // 跳过单引号
start=i;
while(c[i]!='\0'&&!(c[i]==39&&(c[i+1]==' '||c[i+1]=='\0')))
i++;
if(c[i]!=39)
return ERROR;
end=i-1;
substring(s3,command,start,(end-start+1));//抽取串标识出来
assign(s3);
act.s[2]=List.CurNum;
return OK;
}//case R
case'L':
case'A':{while(c[i]==' ')
i++;
if(c[i]!=39)
return ERROR;
i++; // 跳过单引号
start=i;
while(c[i]!='\0'&&!(c[i]==39&&(c[i+1]==' '||c[i+1]=='\0')))
i++;
if(c[i]!=39)
return ERROR;
end=i-1;
substring(s1,command,start,(end-start+1));//抽取串标识出来
assign(s1);//将抽取出来的串标识放到串头表中
act.s[0]=List.CurNum;
return OK;
}//case
case 'I':
case 'E':
case 'C':{while(c[i]==' ')
i++;
if(c[i]!=39)
return ERROR;
i++; // 跳过单引号
start=i;
while(!(c[i]==39&&c[i+1]==' ')&&c[i]!='\0')
i++;
if(c[i]=='\0')
return ERROR;
end=i-1;
substring(s1,command,start,(end-start+1));//抽取串标识出来
assign(s1);
act.s[0]=List.CurNum;
i++;//跳过单引号
if(c[i]!=' ') //串标识必须以空格隔开
return ERROR;
while(c[i]==' ') //找第二个串标识
i++;
if(c[i]!=39)
return ERROR;
i++; // 跳过单引号
start=i;
while(c[i]!='\0'&&!(c[i]==39&&(c[i+1]==' '||c[i+1]=='\0')))
i++;
if(c[i]!=39)
return ERROR;
end=i-1;
substring(s2,command,start,(end-start+1));//抽取串标识出来
assign(s2);
act.s[1]=List.CurNum;
return OK;
}//case
case 'D':{while(c[i]==' ')
i++;
if(c[i]!=39)
return ERROR;
i++; // 跳过单引号
start=i;
while(c[i]!='\0'&&!(c[i]==39&&(c[i+1]==' '||c[i+1]=='\0')))
i++;
if(c[i]!=39)
return ERROR;
end=i-1;
substring(s1,command,start,(end-start+1));//抽取串标识出来
act.s[0]=chartoint(s1);
break;
}//case D
case 'S':{while(c[i]==' ')
i++;
if(c[i]!=39)
return ERROR;
i++; // 跳过单引号
start=i;
while(!(c[i]==39&&c[i+1]==' ')&&c[i]!='\0')
i++;
if(c[i]=='\0')
return ERROR;
end=i-1;
substring(s1,command,start,(end-start+1));//抽取串标识出来
assign(s1);//将抽取出来的串标识放到串头表中
act.s[0]=List.CurNum;
i++;//跳过单引号
if(c[i]!=' ') //串标识必须以空格隔开
return ERROR;
while(c[i]==' ') //找第二个串标识
i++;
if(c[i]!='+')
return ERROR;
i++;//跳过'+'
if(c[i]!=39)
return ERROR;
i++; // 跳过单引号
start=i;
while(!(c[i]==39&&c[i+1]==' ')&&c[i]!='\0')
i++;
if(c[i]=='\0')
return ERROR;
end=i-1;
substring(s2,command,start,(end-start+1));//抽取串标识出来
act.num[0]=chartoint(s2); //将"数1"送到数值参数1
i++;// 跳过单引号
if(c[i]!=' ') //串标识必须以空格隔开
return ERROR;
while(c[i]==' ') //找第三个串标识
i++;
if(c[i]!='+')
return ERROR;
i++;//跳过'+'
if(c[i]!=39)
return ERROR;
i++; // 跳过单引号
start=i;
while(c[i]!='\0'&&!(c[i]==39&&(c[i+1]==' '||c[i+1]=='\0')))
i++;
if(c[i]!=39)
return ERROR;
end=i-1;
substring(s3,command,start,(end-start+1));//抽取串标识出来
act.num[1]=chartoint(s3); //将"数1"送到数值参数1
return OK;
}//case S
case 'P':
case 'Q':return OK;
default :return ERROR;
}//switch
}
//-----------------------------------------------------------------------
void main()
{ResultType act;
List.CurNum=0; //初始化内部名个数为0
char action[50];
int result;
HString s1;
printf("\n\n\n\n 系统名称: 串基本操作显示");
printf("\n\n\n 班级:计科4班 姓名:李励锋 学号:3106006552");
printf("\n\n\n 按任意键进入系统… ");
getch();
while(1)
{system("cls");
printf("\n 操作提示: 赋值: 格式:A□'标识串'□'回车'\n\n");
printf(" 判相等:格式:E□'标识串1'□'标识串2'□'回车'\n\n");
printf(" 联接: 格式:C□'标识串1'□'标识串2'□'回车'\n\n");
printf(" 求长度:格式:L□'标识串'□'回车'\n\n");
printf(" 求子串:格式:S□'标识串1'□+'数1'□+'数2'□'回车'\n\n");
printf(" 子串定位:格式:I□'标识串1'□'标识串2'□'回车'\n\n");
printf(" 串替换:格式:R□'标识串1'□'标识串2'□'标识串1'□'回车'\n\n");
printf(" 显示: 格式:P□'回车'\n\n");
printf(" 删除: 格式:D□'内部名'□'回车'\n\n");
printf(" 退出: 格式:Q□'回车'\n");
printf(" \n注意! ! 标识串之间必须有一个或以上空格隔开!\n 标识串中连续的两个单引号表示一个单引号符!\n\n");
printf("\tplease input the commamd:");
gets(action);
system("cls");
result=analyse(act,action);
if(!result) printf("\n\n\n\n\n\n\n\t\tSorry,you input the wrong syntax!");
else{
switch(act.CmdNo)
{ case 'A': {printf("\n\n\n\n\n\n\t\tInside name The string\n");
printf("\t\t%d '%s'\n",act.s[0],List.StrHead[act.s[0]].ch);
break ;
}//case A
case 'E':strcompare(List.StrHead[act.s[0]],List.StrHead[act.s[1]]);break;
case 'C':{ combine(s1,List.StrHead[act.s[0]],List.StrHead[act.s[1]]);
assign(s1);
printf("\n\n\n\n\n\n\t\tInside name The new string\n");
printf("\t\t%d '%s'\n",List.CurNum,List.StrHead[List.CurNum].ch);
break;
}//caase C
case 'L': strlength(List.StrHead[act.s[0]]); break;
case 'S': {substring(s1,List.StrHead[act.s[0]],act.num[0]-1,act.num[1]);
assign(s1);
printf("\n\n\n\n\n\n\t\tInside name The new string\n");
printf("\t\t%d '%s'\n",List.CurNum,List.StrHead[List.CurNum].ch);
break;
}//case S
case 'I': position(List.StrHead[act.s[0]],List.StrHead[act.s[1]]); break;
case 'R':{if(replace(s1,List.StrHead[act.s[0]],List.StrHead[act.s[1]],List.StrHead[act.s[2]]))
{ assign(s1);
printf("\n\n\n\n\n\n\t\tInside name The new string\n");
printf("\t\t%d '%s'\n",List.CurNum,List.StrHead[List.CurNum].ch);
}//if
else printf("\n\n\n\n\n\n\n\n\n\t\tSorry,CANNOT FIND") ;
break; }//case R
case 'P': display();break;
case 'D': del(act.s[0]);break;
case 'Q': exit(0);break;
default: printf("\n\n\n\n\n\n\n\n\t\tSorry,you input the wrong command!");
}//switch
}//else
printf("\n\n\n\t\tpress any key to continue..");getch();
}//while
}//main
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -