22
{
23
tempNode=(PNode)malloc(sizeof(Node));
24
tempNode->data=i;
25
current->next=tempNode;
26
current=tempNode;
27
}
28
current->next=L->next;
29
current=L;
30
while (iLen>1)
31
{
32
current=current->next->next;
33
tempNode=current->next;
34
current->next=tempNode->next;
35
printf("%d\n",tempNode->data);
36
free(tempNode);
37
iLen--;
38
}
39
return current->data;
40
}
41
42
int main()
43
{
44
printf("lastof20is%d",getLast(20));
45
return 0;
46
}
编程题(共2题,第一题40分,第二题60分,共100分。
请上机编写轨范,按题目要求提交文件。
[详见考试申明]
本试题采用自动测试用例进行评分,测试用例过错考生公开,凡不满足提交要求导致不能运行或用例不经由过程,不予评分)。
1.删除字符串中所有给定的子串(40分)
问题描述:
在给定字符串中查找所有特定子串并删除,若是没有找到响应子串,则不作任何操作。
要求实现函数:
intdelete_sub_str(constchar*str,constchar*sub_str,char*result_str)
【输入】str:
输入的被操作字符串
sub_str:
需要查找并删除的特定子字符串
【输出】result_str:
在str字符串中删除所有sub_str子字符串后的终局
【返回】删除的子字符串的个数
I、 子串匹配只考虑最左匹配情况,即只需要从左到右进行字串匹配的情况。
比如:
在字符串"abababab"中,采用最左匹配子串"aba",可以匹配2个"aba"字串。
若是
匹配出从左到右位置2起头的"aba",则不是最左匹配,且只能匹配出1个"aba"字串。
II、 输入字符串不会跨越100Bytes,请不用考虑超长字符串的情况。
示例
输入:
str="abcde123abcd123"
sub_str="123"
输出:
result_str="abcdeabcd"
返回:
2
输入:
str="abcde123abcd123"
sub_str="1234"
输出:
result_str="abcde123abcd123"
返回:
0
2.高精度整数加法(60分)
问题描述:
在计较机中,由于措置器位宽限制,只能措置有限精度的十进制整数加减法,比如在32位宽措置器计较机中,
参与运算的操作数和终局必须在-231~231-1之间。
若是需要进行更年夜规模的十进制整数加法,需要使用特殊
的体式格局实现,比如使用字符串保存操作数和终局,采纳逐位运算的体式格局。
如下:
9876543210+1234567890=?
让字符串num1="9876543210",字符串num2="1234567890",终局保存在字符串result="11111111100"。
-9876543210+(-1234567890)=?
让字符串num1="-9876543210",字符串num2="-1234567890",终局保存在字符串result="-11111111100"。
要求编程实现上述高精度的十进制加法。
要求实现函数:
voidadd(constchar*num1,constchar*num2,char*result)
【输入】num1:
字符串形式操作数1,若是操作数为负,则num1[0]为符号位-
num2:
字符串形式操作数2,若是操作数为负,则num2[0]为符号位-
【输出】result:
保存加法计较终局字符串,若是终局为负,则result[0]为符号位。
I、 当输入为正数时,+不会出今朝输入字符串中;当输入为负数时,-会出今朝输入字符串中,且必然在输入字符串最左边位置;
II、 输入字符串所有位均代表有效数字,即不存在由0起头的输入字符串,比如"0012","-0012"不会呈现;
III、 要求输出字符串所有位均为有效数字,终局为正或0时+不出今朝输出字符串,终局为负时输出字符串最左边位置为-。
示例
输入:
num1="580"
num2="-50"
输出:
result="530"
输入:
num1="580"
num2="-600"
输出:
result="-20"
#include
usingnamespacestd;
intdeletestr(constchar*str,constchar*sub_str,char*result)
constchar*p=str;
constchar*p1=str;
constchar*q=sub_str;
intk=0;
while(*p!
=/0)
if((*p==*q)&&*q!
=/0)
p++;
q++;
constchar*ptr=p;
if(*p==/0)
*result=/0;
elseif(*q==/0)
q=sub_str;
k++;
p1=p;
elseif(*p!
=/0&&*q!
=/0)
*result++=*p1;
p1++;
p=p1;
q=sub_str;
if(*q!
=/0)
p=p1;
while(*p!
=/0)
*result++=*p++;
*result=/0;
}
voidalg(constchar*str1,constchar*str2,char*result)
constchar*p=str1;
constchar*q=str2;
intm=0,n=0;
while(*p!
=/0)
p++;
m++;
while(*q!
=/0)
q++;
n++;
p=p-1;
q=q-1;
if((*str1!
=-&&*str2!
=-)||(*str1==-&&*str2==-))
if(*str1==-)
m--;
n--;
*result=0;
while(m>0&&n>0)
if(((*result+*p+*q)-3*0)>=10)
*result=0+((*result+*p+*q)-3*0)%10;
result++;
*result=1;
p--;
q--;
else
*result=0+((*result+*p+*q)-3*0);
if((m==n)&&m!
=1)
result++;
*result=0;
elseif(m!
=n)
result++;
*result=0;
p--;
q--;
m--;
n--;
if(m>0)
while(m>0)
if((*result+*p-2*0)>=10)
*result=0+(*result+*p-2*0)%10;
result++;
*result=1;
else
*result=0+(*result+*p-2*0);
if(m!
=1)
result++;
*result=0;
p--;
m--;
elseif(n>0)
while(n>0)
if((*result+*q-2*0)>=10)
*result=0+(*result+*q-2*0)%10;
result++;
*result=1;
else
*result=0+(*result+*q-2*0);
if(n!
=1)
result++;
*result=0;
q--;
n--;
if(*str1!
=-)
result++;
*result=/0;
else
result++;
*result=-;
result++;
*result=/0;
else
intk1=0;
intk2=0;
constchar*p1;
constchar*p2;
constchar*ptr1;
constchar*ptr2;
if(*str1==-)
k1=m-1;
k2=n;
p1=p;
p2=q;
ptr1=str1+1;
ptr2=str2;
elseif(*str2==-)
k1=n-1;
k2=m;
p1=q;
p2=p;
ptr1=str2+1;
ptr2=str1;
inttemp=0;//long
constchar*temptr;//pointeroflong
inttemp1=0;
constchar*temptr1;
if(k1>k2)
temp=k1;
temp1=k2;
temptr=p1;
temptr1=p2;
elseif(k1 temp=k2;
temp1=k1;
temptr=p2;
temptr1=p1;
else
while(*ptr1==*ptr2)
++ptr1;
++ptr2;
if((*ptr1-*ptr2)>0)
temp=k1;
temp1=k2;
temptr=p1;
temptr1=p2;
else
temp=k2;
temp1=k1;
temptr=p2;
temptr1=p1;
*result=0;
while(temp1>0)
if((0-*result+*temptr-*temptr1)>=0)
*result=2*0-*result+*temptr-*temptr1;
result++;
*result=0;
temptr--;
temptr1--;
temp1--;
temp--;
else
*result=-*result+*temptr-*temptr1+1+9;
result++;
*result=1;
temp1--;
temp--;
temptr--;
temptr1--;
if(temp>0)
while(temp>0)
if((-*result+*temptr)>=0)
*result=0+(-*result+*temptr);
if(temp!
=1)
result++;
*result=0;
temp--;
temptr--;
else
*result=1+9-0+(-*result+*temptr);
result++;
*result=1;
temp--;
temptr--;
if(*result==0)
if(*temptr==-)
*result=-;
++result;
*result=/0;
else
*result=/0;
else
if(*temptr==-)
++result;
*result=-;
++result;
*result=/0;
else
++result;
*result=/0;
else
if(*result==0)
--result;
while(*result==0)
--result;
++result;
if(*temptr==-)
*result=-;
++result;
*result=/0;
else
*result=/0;
else
if(*temptr==-)
++result;
*result=-;
++result;
*result=/0;
else
++result;
*result=/0;
}
intmain()
char*str="abcdabbabcacbaabcdabcababc";
char*sub_str="ab";
charresult[100];
char*str1="-1462";
char*str2="473";
charre[10];
alg(str1,str2,re);
deletestr(str,sub_str,result);
cout< system("pause");
return0;
}
(注:
可编辑下载,若有不当之处,请指正,谢谢!
)