第八周上机实践项目3——顺序串算法
来源:互联网 发布:淘宝内裤卷筒拍摄 编辑:程序博客网 时间:2024/06/05 14:29
问题及代码:
.头文件:sqString.h,包含定义链队数据结构的代码、宏定义、要实现算法的函数的声明;
#ifndef SqString_H_INCLUDED#define SqString_H_INCLUDED#define MaxSize 100 //最多的字符个数typedef struct{ char data[MaxSize]; //定义可容纳MaxSize个字符的空间 int length; //标记当前实际串长} SqString;void StrAssign(SqString &s,char cstr[]); //字符串常量cstr赋给串svoid StrCopy(SqString &s,SqString t); //串t复制给串sbool StrEqual(SqString s,SqString t); //判串相等int StrLength(SqString s); //求串长SqString Concat(SqString s,SqString t); //串连接SqString SubStr(SqString s,int i,int j); //求子串SqString InsStr(SqString s1,int i,SqString s2); //串插入SqString DelStr(SqString s,int i,int j) ; //串删去SqString RepStr(SqString s,int i,int j,SqString t); //串替换void DispStr(SqString s); //输出串#endif // SqString_H_INCLUDED
测试函数:main.cpp,完成相关的测试工作;
(1)
#include <stdio.h>#include "sqString.h"<pre class="cpp" name="code">#include <stdio.h>#include "sqString.h"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; }}int main(){ SqString s; StrAssign(s, "abcdefg"); Invert(s); DispStr(s); return 0;}
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;}int main(){ SqString s; StrAssign(s, "messages"); Trans(s, 'e', 'a'); DispStr(s); return 0;}
(2)
#include <stdio.h>#include "sqString.h"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; }}int main(){ SqString s; StrAssign(s, "abcdefg"); Invert(s); DispStr(s); return 0;}
(3)
#include <stdio.h>#include "sqString.h"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;}int main(){ SqString s; StrAssign(s, "message"); DellChar(s, 'e'); DispStr(s); return 0;}
(4)
#include <stdio.h>#include "sqString.h"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 s1, s2, s; StrAssign(s1, "message"); StrAssign(s2, "agent"); s = CommChar(s1, s2); DispStr(s); return 0;}
运行结果:
0 0
- 数据结构上机实践第八周项目3-顺序串算法
- 第八周上机实践项目3——顺序串算法
- 第八周上机实践—项目3—顺序串算法
- 第八周上机实践项目1——建立顺序串的算法库
- 第八周上机实践—项目1—建立顺序串算法库
- 第八周实践项目3—顺序串算法
- 第八周 数据结构实践——顺序串算法【项目3- 顺序串算法】
- 数据结构上机实践第八周项目1- 建立顺序串的算法库
- 第八周实践项目~顺序串算法
- 第八周实践项目3--顺序串算法
- 第八周实践项目3 顺序串一些算法操作
- 第八周上机实践项目2——建立链串的算法库
- 第八周上机实践—项目2—建立链串的算法库
- 第八周 数据结构实践项目——串 【项目1 - 建立顺序串的算法库】
- 第八周 数据结构实践项目——串 【项目1 - 建立顺序串的算法库
- 第八周实践项目1—建立顺序串的算法库
- 第八周项目3——顺序串算法
- 第八周项目3——顺序串算法
- 第七周项目1-建立顺序环形队列算法库
- Sql 2005 维护笔记
- 【第7周 项目1 - 建立顺序环形队列算法库】
- 第7周项目4队列数组
- 第七周 队列数组
- 第八周上机实践项目3——顺序串算法
- 字符串加密
- 第9周 项目2—对称矩阵压缩存储的实现和应用
- 第8周 项目4-字符串加密
- 第八周 项目1-建立顺序串的算法库
- Eclipse的Task View使用
- 第八周 项目5 模式匹配
- 第七周 项目一 建立顺序环形算法库
- 第六周项目4—数制转换