第八周项目二__顺序串算法
来源:互联网 发布:网络作家收入作假 编辑:程序博客网 时间:2024/06/06 00:12
/*烟台大学计算机学院文件名称:main.cpp ti.cpp head.cpp作者:王硕完成日期:2017年10月26日问题描述:第八周项目一采用顺序存储方式存储串,实现下列算法并测试:(1)试编写算法实现将字符串S中所有值为c1的字符换成值为c2的字符:void Trans(SqString *&s, char c1, char c2);(2)试编写算法,实现将已知字符串所有字符倒过来重新排列。如ABCDEF改为FEDCBA。void Invert(SqString &s)(3)从串s中删除其值等于c的所有字符。如从message中删除’e’,得到的是mssag。void DellChar(SqString &s, char c)(4)有两个串s1和s2,设计一个算法求一个这样的串,该串中的字符是s1和s2中公共字符。所谓公共子串,是由在s1中有,且在s2中也有的字符构成的字符。例s1为”message”,s2为”agent”,得到的公共子串是”eage”。SqString CommChar(SqString s1,SqString s2);*/
#include <stdio.h>#include "head.h"int main(){ SqString s,s1,s2; StrAssign(s, "messages"); Trans(s, 'e', 'a'); DispStr(s); StrAssign(s, "abcdefg"); Invert(s); DispStr(s); StrAssign(s, "message"); DellChar(s, 'e'); DispStr(s); StrAssign(s1, "message"); StrAssign(s2, "agent"); s = CommChar(s1, s2); DispStr(s); return 0;}
//body#include <stdio.h>#include <malloc.h>#include "head.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;}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;}void DispStr(SqString s){ int i; if (s.length>0) { for (i=0;i<s.length;i++) printf("%c",s.data[i]); printf("\n"); }}
//head#ifndef HEAD_H_INCLUDED#define HEAD_H_INCLUDED#define MaxSize 100 //最多的字符个数typedef struct{ char data[MaxSize]; //定义可容纳MaxSize个字符的空间 int length; //标记当前实际串长} SqString;void StrAssign(SqString &s,char cstr[]); //字符串常量cstr赋给串svoid Trans(SqString &s, char c1, char c2);void Invert(SqString &s);void DellChar(SqString &s, char c);SqString CommChar(SqString s1,SqString s2);void DispStr(SqString s); //输出串#endif // HEAD_H_INCLUDED
运行结果:
知识点总结:
应用顺序串算法所进行的操作。
学习心得:
这些算法都应该自己理清运行过程。
阅读全文
0 0
- 第八周项目二__顺序串算法
- 第八周项目二 顺序串算法
- 第八周项目一__建立顺序串的算法库
- 第八周——项目二 “顺序串算法”
- 第八周项目二——顺序串算法
- 第八周--项目三--顺序串算法
- 第八周 项目3 顺序串算法
- 第八周--项目3顺序串算法
- 第八周项目三顺序串算法
- 第八周 项目3 顺序串算法
- 第八周项目3 - 顺序串算法
- 第八周项目3顺序串算法
- 第八周-【项目3-顺序串算法】
- 第八周项目3--顺序串算法
- 第八周项目3-顺序串算法
- 第八周项目3-顺序串算法
- 第八周实践项目~顺序串算法
- (第八周项目3)顺序串算法
- 10CC寺库平台对接用户手册
- Linux内核中的常用宏--container_of(container_of(ptr, type, member)
- struts2 文件下载文件名中文乱码及变成空格问题
- 553 Mail from must equal authorized u ser
- 那些年错过的蓝桥杯
- 第八周项目二__顺序串算法
- (转载)Mybatis结合Oracle的foreach insert批量插入
- Spring AOP编程
- 上传图片能在HTML页面显示预览
- ORA-19505 与 ORA-39126
- Linux容器虚拟技术LXC简明手册
- 使用Verilog HDL语言实现4位超前进位加法器
- Linux常用命令
- 报错: Access restriction:The type JPEGCodec is not accessible due to restriction on required library