大正数减法(华为2013校园招聘上机笔试题 )

来源:互联网 发布:户口本生成器软件 编辑:程序博客网 时间:2024/05/21 12:50
问题描述:    
两个任意长度的正数相减,这两个正数可以带小数点,也可以是整数,请输出结果。 输入的字符串中,不会出现除了数字与小数点以外的其它字符,不会出现多个小数点以及小数点在第一个字符的位置等非法情况,所以考生的程序中无须考虑输入的数值字符串非法的情况。 
详细要求以及约束:
1.输入均为正数,但输出可能为负数; 
2.输入输出均为字符串形式;
3.如果输出是正数则不需要带符号,如果为负数,则输出的结果字符串需要带负号
例如:2.2-1.1 直接输出为“1.1”,1.1-2.2 则需要输出为“-1.1”
 4.输出的结果字符串需要过滤掉整数位前以及小数位后无效的0,小数位为全0的,直接输出整数位
例如相减结果为11.345,此数值前后均不可以带0,“011.345”或者“0011.34500”等等前后带无效0的均视为错误 输出。例如1.1-1.1结果为0.0,则直接输出0。
要求实现函数:
void Decrease(char *input1, char*input2, char *output)
【输入】 char *iinput1 被减数
char*nput2 减数 
【输出】 char *output 减法结果
【返回】 无
#include<iostream>#include<string>using namespace std;void decrease(char *input1,char *input2,char *output){string num1(input1);string num2(input2);string::size_type length1=num1.size(),beg1=0;//length1是num1的大小string::size_type length2=num2.size(),beg2=0;char xiaoshudian='.';string::size_type pos1=0,pos2=0;pos1=num1.find_first_of(xiaoshudian,0);pos2=num2.find_first_of(xiaoshudian,0);if(pos1==string::npos&&pos2==string::npos){int sum1=0;int first=0;while(beg1!=length1){first=first*10+(num1[beg1]-'0');beg1++;}int second=0;while(beg2!=length2){second=second*10+(num2[beg2]-'0');beg2++;}            sum1=first-second;itoa(sum1,output,10);cout<<output<<endl;return ;}else{float sum1=0;if(pos1!=string::npos&&pos2==string::npos){string::size_type dif=length1-length2-1,weishu_xiaoshudian=length1-pos1-1;num1.erase(pos1,1);while(dif--){num2.push_back('0');}int first=0;string::size_type temp_length1=num1.size();string::size_type temp_length2=num2.size();while(beg1!=temp_length1){first=first*10+(num1[beg1]-'0');beg1++;}int second=0;while(beg2!=temp_length2){second=second*10+(num2[beg2]-'0');beg2++;}            sum1=first-second;char output1[20];itoa(sum1,output1,10);string result(output1);string::size_type s=result.size();if(s>weishu_xiaoshudian){string::size_type a1=s-weishu_xiaoshudian;result.insert(a1,1,'.');string::size_type a2=result.size();result.copy(output,a2,0);output[a2]='\0';cout<<output<<endl;return ;}else{string::size_type b1=weishu_xiaoshudian-s+1;string temp;while(b1--){temp.push_back('0');}temp+=result;string::size_type b2=temp.size();string::size_type a1=b2-weishu_xiaoshudian;temp.insert(a1,1,'.');string::size_type b3=temp.size();temp.copy(output,b3,0);output[b3]='\0';cout<<output<<endl;return ;}}//下面第二数是小数if(pos2!=string::npos&&pos1==string::npos){string::size_type dif=length2-length1-1,weishu_xiaoshudian=length2-pos2-1;num2.erase(pos2,1);while(dif--){num1.push_back('0');}int first=0;string::size_type temp_length1=num1.size();string::size_type temp_length2=num2.size();while(beg1!=temp_length1){first=first*10+(num1[beg1]-'0');beg1++;}int second=0;while(beg2!=temp_length2){second=second*10+(num2[beg2]-'0');beg2++;}            sum1=first-second;char output1[20];itoa(sum1,output1,10);string result(output1);string::size_type s=result.size();if(s>weishu_xiaoshudian){string::size_type a1=s-weishu_xiaoshudian;result.insert(a1,1,'.');string::size_type a2=result.size();result.copy(output,a2,0);output[a2]='\0';cout<<output<<endl;return ;}else{string::size_type c1=weishu_xiaoshudian-s+2;string temp="-";while(c1--){temp.push_back('0');}temp+=result.substr(1);string::size_type b2=temp.size();string::size_type a1=b2-weishu_xiaoshudian;temp.insert(a1,1,'.');string::size_type b3=temp.size();temp.copy(output,b3,0);output[b3]='\0';cout<<output<<endl;return ;}}//进行第三种情况分析,两个数都是小数的情况if(pos2!=string::npos&&pos1!=string::npos){if(length2>length1){string::size_type dif=length2-length1,weishu_xiaoshudian1=length1-pos1-1,weishu_xiaoshudian2=length2-pos2-1;num1.erase(pos1,1);num2.erase(pos2,1);while(dif--){num1.push_back('0');}int first=0;string::size_type temp_length1=num1.size();string::size_type temp_length2=num2.size();while(beg1!=temp_length1){first=first*10+(num1[beg1]-'0');beg1++;}int second=0;while(beg2!=temp_length2){second=second*10+(num2[beg2]-'0');beg2++;}            sum1=first-second;char output1[20];itoa(sum1,output1,10);string result(output1);string::size_type s=result.size();if(s>weishu_xiaoshudian2+1){string::size_type a1=s-weishu_xiaoshudian2;result.insert(a1,1,'.');string::size_type a2=result.size();result.copy(output,a2,0);output[a2]='\0';cout<<output<<endl;return ;}else{if(sum1>0){string::size_type a1=weishu_xiaoshudian2-s+1;string temp;while(a1--){temp.push_back('0');}temp+=result;string::size_type b2=temp.size();string::size_type q1=b2-weishu_xiaoshudian2;temp.insert(q1,1,'.');string::size_type b3=temp.size();temp.copy(output,b3,0);output[b3]='\0';cout<<output<<endl;return ;}else{string::size_type a1=weishu_xiaoshudian2-s+2;string temp="-";while(a1--){temp.push_back('0');}temp+=result.substr(1);string::size_type b2=temp.size();string::size_type q1=b2-weishu_xiaoshudian2;temp.insert(q1,1,'.');string::size_type b3=temp.size();temp.copy(output,b3,0);output[b3]='\0';cout<<output<<endl;return ;}}  }//上面if对应的else{string::size_type dif=length1-length2,weishu_xiaoshudian1=length1-pos1-1,weishu_xiaoshudian2=length2-pos2-1;num1.erase(pos1,1);num2.erase(pos2,1);while(dif--){num2.push_back('0');}int first=0;string::size_type temp_length1=num1.size();string::size_type temp_length2=num2.size();while(beg1!=temp_length1){first=first*10+(num1[beg1]-'0');beg1++;}int second=0;while(beg2!=temp_length2){second=second*10+(num2[beg2]-'0');beg2++;}            sum1=first-second;char output1[20];itoa(sum1,output1,10);string result(output1);string::size_type s=result.size();if(s>weishu_xiaoshudian1+1){string::size_type a1=s-weishu_xiaoshudian1;result.insert(a1,1,'.');string::size_type a2=result.size();result.copy(output,a2,0);output[a2]='\0';cout<<output<<endl;return ;}else{if(sum1>0){string::size_type a1=weishu_xiaoshudian1-s+1;string temp;while(a1--){temp.push_back('0');}temp+=result;string::size_type b2=temp.size();string::size_type q1=b2-weishu_xiaoshudian1;temp.insert(q1,1,'.');string::size_type b3=temp.size();temp.copy(output,b3,0);output[b3]='\0';cout<<output<<endl;return ;}else{string::size_type a1=weishu_xiaoshudian1-s+2;string temp="-";while(a1--){temp.push_back('0');}temp+=result.substr(1);string::size_type b2=temp.size();string::size_type q1=b2-weishu_xiaoshudian1;temp.insert(q1,1,'.');string::size_type b3=temp.size();temp.copy(output,b3,0);output[b3]='\0';cout<<output<<endl;return ;}}  }//上面if对应的}}}/*void int_to_string(string &s1,int num){string temp;string::size_type pos=0;}*/int main(){    char *num1="22.201";char *num2="22.3";char output[100];decrease(num1,num2,output);system("pause");return 0;}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 孩子初中了书写越来越潦草怎么办 给孩子自由孩子无法无天怎么办 孩子挑食幼儿园老师该怎么办 老师说孩子挑食家长怎么办 工作中老是粗心不细心怎么办 小孩数学总是特别粗心该怎么办 孩子起范疙瘩的怎么办 做题马虎不认真怎么办 孩子考差了家长怎么办 小孩写作业不认真怎么办 小孩不认真检查作业怎么办 一年级的小孩作业不认真怎么办 一年级学生做题粗心怎么办 一年级的学生做题粗心怎么办 孩子做作业注意力不集中怎么办 小学三年孩子抄答案怎么办 孩子写作业不认真审题怎么办 一年级小孩审题不认真怎么办 孩子审题不认真马虎怎么办 孩子做作业不认真审题怎么办? 考老师考砸了怎么办 重要考试考砸了怎么办 二年级孩子做数学题粗心怎么办 二年级孩子考试粗心怎么办 二年级孩子考试总是粗心怎么办 二年级孩子总是粗心怎么办 小学一年级孩子抄别人作业怎么办 被老师发现抄答案怎么办 考试抄答案被老师发现怎么办 孩子撒谎不写作业怎么办 小学生做题容易马虎出错怎么办 小学生做题老是马虎怎么办 小学生做题马虎不认真怎么办 会做的题总做错怎么办 孩子数学做题粗心怎么办 孩子成绩考差了怎么办 孩子静不下心学习怎么办 孩子考试时总是粗心马虎怎么办 小学二年级学生厌学怎么办 三岁宝宝肚脐痛怎么办 做题速度太慢怎么办