字符串顺序存储结构的基本运算

来源:互联网 发布:怎么查看视频源码 编辑:程序博客网 时间:2024/06/05 10:33
/** *@Name:字符串顺序存储结构的基本运算  *@Description:空串的创建,串初始化、遍历、元素的插入、删除和修改和移动。 *@Author:Freedoman *@Date: 2014-8-9 */ #include <stdio.h>#include <string.h>#include <stdlib.h>#include <conio.h>#define MAX 100 /*字符串类型*/ struct SeqString{int curIndex;char c[MAX];};typedef SeqString * PSeqString;/*函数声明*/PSeqString createNullSeqString();int isNullSeqString(PSeqString pstr);int isFullSeqString(PSeqString pstr);PSeqString initializeNullString(PSeqString pstr);char findElementByIndex(PSeqString pstr,int index);int findIndexByElement(PSeqString pstr,char c);int insertBeforeChar(PSeqStringpstr, char targetChar, char element);int insertAfterChar(PSeqStringpstr, char targetChar, char element);int deleteChar(PSeqString pstr, char targetChar);void printString(PSeqString pstr);/*------------创建一个空串-------------*/PSeqString createNullSeqString(){PSeqString pstr=(PSeqString)malloc(sizeof(SeqString));if(pstr != NULL){pstr->curIndex = -1;printf("创建成功!\n");}else{printf("创建失败!\n");free(pstr);}return pstr;}/*----------判断串是否为空--------------*/int isNullSeqString(PSeqString pstr){if(pstr->curIndex == -1){printf("空串!\n");return 1;}else{printf("非空串!\n");return 0;}}/*----------判断串是否为满--------------*/int isFullSeqString(PSeqString pstr){if(pstr->curIndex== MAX - 1){printf("满串!\n");return 1;}else{printf("非满串!\n");return 0;}}/*--------------初始化空串--------------*/PSeqString initializeNullString(PSeqString pstr){printf("请输入>>>");gets(pstr->c);pstr->curIndex = strlen(pstr->c);printf("初始化成功!\n");printString(pstr);return pstr;}/*-----------求指定下标元素--------*/char findElementByIndex(PSeqString pstr,int index){if(index >= pstr->curIndex ){printf("下标越界!\n");return -1;}return pstr->c[index];}/*-----------求指定元素下标----------*/int findIndexByElement(PSeqString pstr,char c){int i = 0; while(i < pstr->curIndex){if(pstr->c[i] != c){i++;continue;}return i;}printf("未找到!\n");return -1;}/*-------------在指定字符之前插入字符----------------*/int insertBeforeChar(PSeqStringpstr, char targetChar, char element){int targetIndex = findIndexByElement(pstr,targetChar);int i;if(targetIndex != -1){// 从后往前依次移动元素 for(i = pstr->curIndex; i > targetIndex ; i--){pstr->c[i] = pstr->c[i-1];}pstr->c[i] = element;pstr->curIndex ++;printf("插入成功!\n");printString(pstr);return 1;}printf("插入失败!\n");return -1;} /*----------在指定字符之后插入字符----------*/int insertAfterChar(PSeqString pstr, char targetChar, char element){int tarIndex = findIndexByElement(pstr,targetChar);int i;if(tarIndex != -1){// 从后往前移动元素for(i = pstr->curIndex; i > tarIndex + 1; i --){pstr->c[i] = pstr->c[i-1];}pstr->c[i] = element;pstr->curIndex++;printf("插入成功!\n");printString(pstr);return 1;}printf("插入失败!\n");return -1;} /*----------删除指定字符----------*/int deleteChar(PSeqString pstr, char targetChar){int tarIndex = findIndexByElement(pstr,targetChar);int i;if(tarIndex != -1){// 从前向后移动大量元素for(i = tarIndex; i < pstr->curIndex; i++){pstr->c[i] = pstr->c[i+1];}pstr->curIndex--;printf("删除成功!\n");printString(pstr);return 1;}printf("删除失败!\n");return -1;}/*----------打印当前字符串-----------*/void printString(PSeqString pstr){printf("打印[");int i; for(i = 0; i < pstr->curIndex; i++){printf("%c",pstr->c[i]);}printf("]\n");}int main(){int input,index;char ch,ele; PSeqString pstr = NULL;printf("\n--------字符串的基本操作---------\n");while(1){printf("\n 1_创建空串\n 2_判断当前是否为空\n 3_判断当前串是否为满\n");printf(" 4_初始化空串\n 5_求指定下标字符\n 6_求指定字符下标\n");printf(" 7_在指定字符之前插入字符\n 8_在指定字符之后插入字符\n");printf(" 9_删除指定字符\n 10_打印串\n");printf("\n请选择>>>"); scanf("%d",&input);scanf("%c",&ch); switch(input){case 1 : pstr = createNullSeqString();break;case 2 : isNullSeqString(pstr);break;case 3 : isFullSeqString(pstr);break;case 4 : initializeNullString(pstr);break;case 5 :printf("请指定下标>>>");scanf("%d",&index);char c = findElementByIndex(pstr,index);printf(">>>>%c\n",c);break;case 6 : printf("请指定字符>>>");scanf("%c",&ch);int i = findIndexByElement(pstr,ch);printf(">>>>%d",i);break;case 7 : printf("请指定两个字符>>>");scanf("%c,%c",&ch,&ele);insertBeforeChar(pstr,ch,ele);break;case 8 : printf("请指定两个字符>>>");scanf("%c,%c",&ch,&ele);insertAfterChar(pstr,ch,ele);break;case 9 : printf("请指定字符>>>");scanf("%c",&ch);deleteChar(pstr,ch);break;case 10: printString(pstr);break;default: printf("输入错误,请重新输入"); break;}}return 0;}

0 0