这是放假之前写的很乱的代码,主要解决字符串操作的问题,如串赋值、求串长、求子串等等
//#include "file.h"
//#include "function.c"
#include
#include
#include
#include
#include
#include
//#include "c1-1.h" using namespace std; typedef struct HString { char *ch; int length; }HString; int StrAssign(HString *T,char *chars) { int i,j; if(T->ch) free(T->ch); i=strlen(chars); if(!i) { T->ch=NULL; T->length=0; } else T->ch=(char*)malloc(i*sizeof(char)); if(!T->ch)//分配空间失败 exit(OVERFLOW); for(j=0;j
ch[j]=chars[j]; T->length=i; return 1; } int Length(HString T) { return T.length; } void InitString(HString *T) { T->length=0; T->ch=NULL; } void StrPrint(HString T)//所有的操作后的字符放到了T.ch中 { int i; for(i=0;i
ch) free(T->ch); T->ch=(char*)malloc((t.length)*sizeof(char)); if(!T->ch) exit(OVERFLOW); for(i=0;i
ch[i]=t.ch[i]; T->length=t.length; return 1; } void StrCat(HString *s,HString t,HString r) { int i; if(s->ch) free(s->ch); s->length=t.length+r.length; s->ch=(char *)malloc(s->length*sizeof(char)); if(!s->ch) exit(OVERFLOW); for(i=0;i
ch[i]=t.ch[i]; for(i=0;i
ch[t.length+i]=r.ch[i]; } int SubString(HString *Sub,HString t,int pos,int len) {//sub返回串s中第pos 个字符起长度为len的字符串; int i; if(pos<1||pos>t.length||len<0||len>t.length-pos+1) exit(OVERFLOW); if(Sub->ch) free(Sub->ch); if(!len) { Sub->ch=NULL; Sub->length=0; } else { Sub->ch=(char *)malloc(len*sizeof(char)); //if(!Sub->ch) //exit(OVERFLOW); for(i=0;i<=len-1;i++) { Sub->ch[i]=t.ch[pos-1+i]; } Sub->length=len; } return 0; } int StrComp(HString t,HString r) {//串比较 if(t.length>r.length) return 1; else return 0; } int StrCompare(HString s,HString t) {//比较两串是否相等,返回两串的差值 int i; for(i=0;i
0) {//其实更应该注意参数的合法性,这样才能保证程序的健壮性 n=Length(s); m=Length(t); i=pos; while(i<=n-m+1)//note:下标的取值范围为什么我自己不能想出来 {//从母串的第i个位置取得长度为m的字串 SubString(&sub,s,i,m); if(StrCompare(sub,t)!=0)//将模式串与子串一一比较 ++i;//why is not i++ else return i; } } return 0; } int StrInsert(HString *s,int pos,HString t) {//插入串为t,原串为s,t在s的第pos 位置之前插入 //程序执行起来有点毛病 int i; if(pos<1||pos>
s->length+1) printf("pos 位置不合法!!!:"); if(t.length) { s->ch=(char*)realloc(s->ch,(s->length+t.length)*sizeof(char)); if(!s->ch) exit(OVERFLOW); for(i=s->length-1;i>=pos-1;--i) s->ch[i+t.length]=s->ch[i]; for(i=0;i
ch[pos-1+i]=t.ch[i]; s->length=t.length; } return 0; } int StrEmpty(HString s) { if(s.length==0) printf("此串为空串"); else printf("此串不为空串!"); } void StrDelete(HString *s,int pos,int len) { int i; if(s->length
length;i++) s->ch[i]=s->ch[i+len]; s->length-=len; s->ch=(char*)realloc(s->ch,s->length*sizeof(char)); } int StrReplace(HString *s,HString t,HString v) {//从第几个位置替换:用V替换主串S中出现的所有与T相等的不重叠的子串 int i=1;// 从串S的第一个字符起查找串T if(StrEmpty(t)) printf("出错了,"); do { i=StrIndex(*s,t,i);//结果为从上一个I之后找到子串T的位置 if(i)//串S存在串T { StrDelete(s,i,Length(t));//删除该串T StrInsert(s,i,v);//在原串T的位置插入串V i+=Length(v); } }while(i); return i; } int StrClear(HString *s) { int i=0; /*while(s->ch[i]) { free(s->ch[i]); i++; } s->length=0;*/ //这是我一开始写的的代码,不能放弃任何学习的机会 while(s->ch) { free(s->ch); s->ch=NULL;// 这个很容易忘记 } s->length=0; return 1; } void StrDestory() { } int menu_select() { char c; int n; printf("\n\n"); printf(" 实现字符串操作的的部分功能! \n"); printf(" "); printf("\n ********** 于波&&学号2012070152 ***\n\n"); printf(" * * \n"); printf(" \n"); printf(" 菜 单 \n"); printf(" \n"); printf(" \n"); printf(" (1)--- 初始化 \n"); printf(" (2)--- 串赋值 \n"); printf(" (3)--- 求串长 \n"); printf(" (4)--- 求子串 \n"); printf(" (5)--- 串连接 \n"); printf(" (6)--- 串删除 \n"); printf(" (7)--- 串比较 \n"); printf(" (*0*)-退出
系统 \n"); printf(" (*9*)-显示菜单 \n"); printf(" 注意:目前仅支持字母,其他...*\n"); printf(" *========= 请在上述序号中选择一个并输入:==* "); } int main() { int i,m,n; in