编程题(共2题,第一题40分,第二题60分,共100分。请上机编写程序,按题目要求提交文件。[详见考试说明]
本试题采用自动测试用例进行评分,测试用例不对考生公开,凡不满足提交要求导致不能运行或用例不通过,不予评分)。
?
1. 删除字符串中所有给定的子串(40分)
问题描述:
在给定字符串中查找所有特定子串并删除,如果没有找到相应子串,则不作任何操作。
要求实现函数:
int delete_sub_str(const char *str, const char *sub_str, char *result_str)
【输入】 str:输入的被操作字符串
???????? sub_str:需要查找并删除的特定子字符串
【输出】 result_str:在str字符串中删除所有sub_str子字符串后的结果
【返回】 删除的子字符串的个数
注:
I、?? 子串匹配只考虑最左匹配情况,即只需要从左到右进行字串匹配的情况。比如:
在字符串"abababab"中,采用最左匹配子串"aba",可以匹配2个"aba"字串。如果
匹配出从左到右位置2开始的"aba",则不是最左匹配,且只能匹配出1个"aba"字串。
II、? 输入字符串不会超过100 Bytes,请不用考虑超长字符串的情况。
示例
输入: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"。
?
要求编程实现上述高精度的十进制加法。
要求实现函数:
void add (const char *num1, const char *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<iostream>
using namespace std;
int deletestr(const char*str, const char*sub_str, char *result)
{
?const char*p = str;
?const char*p1 = str;
?const char*q = sub_str;
?int k=0;
?while(*p!='\0')
?{
? if((*p==*q)&&*q!='\0')
? {
?? p++;
?? q++;
? const char*ptr = p;
?? if(*p=='\0')
???? {
????? *result='\0';
????? }
?? }
? else if(*q=='\0')
??? {
???? q=sub_str;
???? k++;
???? p1 = p;
???? }
? else if(*p!='\0'&&*q!='\0')
?? {
???
???? *result++ = *p1;
????? p1++;
????? p=p1;
????? q=sub_str;
???? }???
? }
if(*q!='\0')
??? {
??????? p=p1;
??????? while(*p!='\0')
??????? *result++ = *p++;
??????? *result = '\0';
?????? }?
?}
void alg(const char*str1, const char*str2, char *result)
{
?const char *p = str1;
?const char *q = str2;
?int m = 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';
?????????????? }
?????????????? else if(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--;??
???????? }
??????? }
?????? else if(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
{
?? int k1=0;
?? int k2=0;
?? const char*p1;
?? const char*p2;
?? const char*ptr1;
?? const char*ptr2;
?? if(*str1=='-')
???? {
????? k1=m-1;
????? k2=n;
????? p1=p;
????? p2=q;
????? ptr1=str1+1;
????? ptr2=str2;
????? }
?? else if(*str2=='-')
??? {
???? k1=n-1;
???? k2=m;
???? p1=q;
???? p2=p;
???? ptr1=str2+1;
???? ptr2=str1;
???? }
?? int temp=0; //long
?? const char*temptr; //pointer of long
?? int temp1=0;
?? const char*temptr1;
?? if(k1>k2)
?? {
???? temp=k1;
???? temp1=k2;
???? temptr=p1;
???? temptr1=p2;
???? }
?? else if(k1<k2)
???? {
????? 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';
???????? }
?????? }????
??? }???????
?}????
?}
int main()
{
?char *str = "abcdabbabcacbaabcdabcababc";
?char *sub_str = "ab";
?char result[100];
?char *str1="-1462";
?char *str2="473";
?char re[10];
?alg(str1, str2, re);
?deletestr(str, sub_str, result);
?cout << result << " " << re;
?system("pause");
?return 0;
?}