实验报告4 5000字精选word文档 10页Word文档下载推荐.docx
- 文档编号:15317387
- 上传时间:2022-10-29
- 格式:DOCX
- 页数:8
- 大小:16.79KB
实验报告4 5000字精选word文档 10页Word文档下载推荐.docx
《实验报告4 5000字精选word文档 10页Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《实验报告4 5000字精选word文档 10页Word文档下载推荐.docx(8页珍藏版)》请在冰豆网上搜索。
visualstudio201X
四、操作方法和实验步骤
实验代码如下:
intparserToPositiveInteger(charstr[],unsignedinti,unsignedintj)
{
if(!
str||i>
j)
return-1;
while(i<
=j&
&
(str[i]=='
'
||str[i]=='
\t'
))
//去掉前面的空白字符
i++;
while(j>
=i&
(str[j]=='
||str[j]=='
//去掉后面的空白字符
j--;
if(i>
else
intk;
for(k=i;
k<
=j;
k++)
//检查是否全部数字字符
('
0'
<
=str[k]&
str[k]<
='
9'
intn=0,p=1;
for(k=j;
k>
=i;
k--)
{//字符串转换为数字
n+=p*(str[k]-'
);
p*=10;
}
returnn;
//占位符
structPlaceholder
//占位符编号
unsignedintindex;
//占位符起始位置
unsignedintstart;
//占位符结束位置
unsignedintend;
};
/*采用占位符的动态字符串
占位符是指字符串中用{}括起来,括号了里面包含【一个正整数及任意空白字符】的子字符串。
如:
"
{0}"
"
{100}"
都是合法的占位符。
占位符中的正整数必须【从0开始,并且连续(建议,但是不强制)】。
相同的占位符可以【出现多次】。
hello{0},hello{1}.{1}hassendamailto{0}"
*/
structDynamicString
//原字符串(可以包含多行)
charsource[MAX_STRING_LENGTH];
structPlaceholderplaceholders[MAX_PLACEHOLDER_AMOUNT];
//占位符的总数
unsignedintplaceholdersAmount;
//替换占位符的具体内容
char
replaceContent[MAX_PLACEHOLDER_AMOUNT][MAX_CONTENT_LENGTH];
//替换后字符串
chartarget[MAX_STRING_LENGTH];
//替换标记
charreplaceMark[MAX_STRING_LENGTH];
/*
功能:
初始化动态字符串
参数:
ds动态字符串
返回:
如果参数非法(比如有空指针):
返回FALSE
如果初始化成功:
返回TRUE
intinit(structDynamicString*ds)
ds)
returnFALSE;
unsignedinti,j;
printf("
输入一动态字符串:
\n"
gets(ds->
source);
/*****************寻找所有占位符*****************/ds->
placeholdersAmount=0;
for(i=0;
ds->
source[i];
i++)
if(ds->
source[i]=='
{'
)
{//出现了占位符的开始标记
//寻找占位符的结束标记
j=i;
while(ds->
source[j]&
source[j]!
='
}'
)j++;
source[j]=='
{//出现了占位符的结束标记
intn=parserToPositiveInteger(ds->
source,i+1,j-1);
if(n>
=0)
{//找到了一个合法的占位符
ds->
placeholders[ds->
placeholdersAmount].index=n;
placeholdersAmount].start=i;
placeholdersAmount].end=j;
placeholdersAmount++;
//跳过占位符
i=j;
break;
returnTRUE;
intcomparePlaceholderByIndex(constvoid*a,constvoid*b){
return
intcomparePlaceholderByStart(constvoid*a,constvoid*b){
替换所有的占位符
参数:
((structPlaceholder*)a)->
start-((structPlaceholder*)b)->
start;
index-((structPlaceholder*)b)->
index;
参数非法(比如有空指针):
返回-1
替换成功:
替换的占位符的个数(一个正数)。
intreplaceAll(structDynamicString*ds)
unsignedinti,j,k;
unsignedintstart,end;
;
qsort(ds->
placeholders,ds->
placeholdersAmount,sizeof(ds->
placeholders[0]),comparePlaceholderByIndex);
for(i=0;
i<
placeholdersAmount;
{//输入替换内容
if(i==0
输入第%3d个替换内容:
ds->
placeholders[i].index);
replaceContent[ds->
placeholders[i].index]);
}
placeholders[0]),comparePlaceholderByStart);
for(i=0,k=0;
=ds->
i++)||ds->
placeholders[i].index!
placeholders[i-1].index)
{//开始替换
start=i>
0?
placeholders[i-1].end+1:
0;
end=i<
placeholdersAmount?
placeholders[i].start-1:
strlen(ds->
source)-1;
for(j=start;
j<
=end;
j++,k++)
{//源字符串中不用替换的部分
target[k]=ds->
source[j];
replaceMark[k]='
if(i==ds->
placeholdersAmount)
continue;
char*content
placeholders[i].index];
for(j=0;
content[j];
{//占位符部分进行文本替换
target[k]=content[j];
^'
target[k]='
\0'
replaceMark[k++]='
returnds->
}=
打印动态字符串
ds动态字符串
doPrintMark是否打印出替换标记
voidprintReplacedResult(conststructDynamicString
doPrintMark)
return;
%s\n"
target);
if(doPrintMark)
replaceMark);
intmain()
structDynamicStringdynamicString;
if(init(&
dynamicString))
replaceAll(&
dynamicString);
printReplacedResult(&
dynamicString,TRUE);
//可以执行多次替换
*ds,int
return0;
四、实验结果及分析
程序运行结果如下:
六:
实验心得
通过这次实验,我更加熟悉了vs软件的使用,加深的了对串的操作的理解。
这次实验报告由于自己的原因没有在上传时间内上传,是拖延症的毛病在作祟,坚决不会再犯了。
∙荐计算机上机实验内容及实验报告要求
∙荐构建学校德育管理与评价体系的实验报告
∙荐化学实验报告格式
∙荐大学物理实验课程设计实验报告
∙荐电路实验报告要求
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验报告4 5000字精选word文档 10页 实验 报告 5000 精选 word 文档 10