实验3
来源:互联网 发布:文件解压软件 编辑:程序博客网 时间:2024/05/02 19:31
一、题目
编制一个求子串的程序。
二、需求分析
本程序在Windows环境下用用Visual C++编写。
三、概要设计
定长顺序存储:
用一组地址连续的存储单元存储串值的字符序列,类似于线性表的顺序存储结构。
可以用定长数组来描述:
四、详细设计
#define MAXSTRLEN 255
// 用户可在255以内定义最大串长
typedef unsigned char SString[Maxstrlen + 1];
// 0号单元存放串的长度
五、测试结果
请输入主串:
asdfghjkl123456789
请输入主串中所求子串的位置和长度:
3
5
您所求子串是:
dfghj
六、调试分析
getchar()在<stdio.h>中定义为int型
<stdio.h>中的getchar()字符(串)输入方式
经过反复的调试终于成功了!
七、 源程序(带注释)
#include <stdio.h>
#define MAXSTRLEN 255
// 用户可在255以内定义最大串长
typedef unsigned char SString[MAXSTRLEN + 1];
// 0号单元存放串的长度
typedef int Status;
#define ERROR 0
#define OK 1
Status SubString(SString &Sub, SString S, int pos,int len)
{// 用Sub返回串S的第pos个字符起长度为len的子串。
// 其中1≤pos ≤StrLength(S) 且 0≤len≤StrLength(S)-pos+1
if (pos<1 ||pos>S[0]|| len<0 || len>S[0]-pos+1)
return ERROR;
for(int i=1;i<=len;i++)
Sub[i]=S[pos+i-1];
Sub[0]=len;
return OK;
} // SubString
void main()
{
SString Sub,S;
int pos,len;
printf("请输入主串:/n");
int i=1;
while((S[i++]=getchar())!='/n');
S[0]=i;
printf("请输入主串中所求子串的位置和长度:/n");
scanf("%d%d",&pos,&len);
SubString(Sub,S,pos,len);
printf("您所求子串是:/n");
i=1;
while(i<=Sub[0])
printf("%c",Sub[i++]);
putchar('/n');
}
- 实验3
- 实验3
- 实验3
- 实验3
- 实验3
- 实验3
- 实验3
- 实验3
- 接口与实验:实验3
- 上机实验3种排序实验8
- C++实验2-剩下3个实验
- 实验:C++实验3-项目1
- 实验:c++实验3-项目1
- 实验:C++ 实验3-项目1
- 实验:C++实验3-个人所得税计算器
- 数据压缩原理实验3_实验报告
- 【实验报告】高软实验3
- 实验二线性表综合实验3
- 空间复杂度
- 实验1 线性表的链表设计与实现
- Linux实用技巧——文件管理
- 实验2 表达式求值
- Preprocessor directives #define, #undef
- 实验3
- When surfers log on
- 实验4 图的基本操作
- 实验4 附加
- DOS命令技巧——文件管理
- 实验5 排序
- 实验5 附加
- 各大搜索引擎收录各种主流域名收录量分析
- Bash Shortcuts