第八周实践项目3 顺序串一些算法操作
来源:互联网 发布:淘宝试衣模特招聘合肥 编辑:程序博客网 时间:2024/06/05 13:27
sqString.h
#define MaxSize 100 //最多的字符个数typedef struct{ char data[MaxSize]; //定义可容纳MaxSize个字符的空间 int length; //标记当前实际串长} SqString;void StrAssign(SqString &s,char cstr[]); //字符串常量cstr赋给串sSqString DelStr(SqString s,int i,int j) ; //串删去void DispStr(SqString s); //输出串sqString.cpp
#include <stdio.h>#include <malloc.h>#include "SqString.h"void StrAssign(SqString &s,char cstr[]) //s为引用型参数{ int i; for (i=0;cstr[i]!='\0';i++) s.data[i]=cstr[i]; s.length=i;}SqString DelStr(SqString s,int i,int j){ int k; SqString str; str.length=0; if (i<=0 || i>s.length || i+j>s.length+1) //参数不正确时返回空串 return str; for (k=0;k<i-1;k++) //将s.data[0..i-2]复制到str str.data[k]=s.data[k]; for (k=i+j-1;k<s.length;k++) //将s.data[i+j-1..s.length-1]复制到str str.data[k-j]=s.data[k]; str.length=s.length-j; return str;}void DispStr(SqString s){ int i; if (s.length>0) { for (i=0;i<s.length;i++) printf("%c",s.data[i]); printf("\n"); }}main.cpp
#include <stdio.h>#include "sqString.h"void Trans(SqString &s, char c1, char c2){ int i; for (i=0; i<s.length; i++) if (s.data[i]==c1) s.data[i]=c2;}void Invert(SqString &s){ int i; char temp; for (i=0; i<s.length/2; i++) { temp = s.data[i]; s.data[i]=s.data[s.length-i-1]; s.data[s.length-i-1] = temp; }}void DellChar(SqString &s, char c){ int k=0, i=0; //k记录值等于c的字符个数 while(i<s.length) { if(s.data[i]==c) k++; else s.data[i-k]=s.data[i]; i++; } s.length -= k;}SqString CommChar(SqString s1,SqString s2){ SqString s3; int i,j,k=0; for (i=0; i<s1.length; i++) { for (j=0; j<s2.length; j++) if (s2.data[j]==s1.data[i]) break; if (j<s2.length) //s1.data[i]是公共字符 { s3.data[k]=s1.data[i]; k++; } } s3.length=k; return s3;}int main(){ SqString s; StrAssign(s, "messages"); Trans(s, 'e', 'a'); DispStr(s); StrAssign(s, "abcdefg"); Invert(s); DispStr(s); StrAssign(s, "message"); DellChar(s, 'e'); DispStr(s); SqString s1, s2 ; StrAssign(s1, "message"); StrAssign(s2, "agent"); s = CommChar(s1, s2); DispStr(s); return 0;}
阅读全文
0 0
- 第八周实践项目3 顺序串一些算法操作
- 第八周实践项目~顺序串算法
- 第八周实践项目3--顺序串算法
- 第八周实践项目3—顺序串算法
- 数据结构上机实践第八周项目3-顺序串算法
- 第八周 数据结构实践——顺序串算法【项目3- 顺序串算法】
- 第八周实践项目~建立顺序串的算法库
- 第八周 项目3 顺序串算法
- 第八周--项目3顺序串算法
- 第八周 项目3 顺序串算法
- 第八周项目3 - 顺序串算法
- 第八周项目3顺序串算法
- 第八周-【项目3-顺序串算法】
- 第八周项目3--顺序串算法
- 第八周项目3-顺序串算法
- 第八周项目3-顺序串算法
- (第八周项目3)顺序串算法
- 第八周项目3-顺序串算法
- 当make menuconfig时出现:mconf.c:(.text+0x744): undefined reference to `stdscr'
- makefile中的伪目标,强制目标和双冒号规则
- 深拷贝和浅拷贝的区别
- JS(12)
- flex布局
- 第八周实践项目3 顺序串一些算法操作
- 游戏开发中Json解析的套路
- python3 中的中文编码问题
- HDU 5534 Partial Tree (背包+trick)
- 求小球弹起高度
- [BIT0429]-Java 接口作方法形參,運行時傳遞實現接口方法類對象作實參
- 51nod- 1003 阶乘后面0的数量(求5的个数)
- libvirt、virsh、virt-manager 介绍及操作示例
- 8.scala方法定义