Huawei machine question -- integer subtraction

Source: Internet
Author: User

# Include <iostream> using namespace std;/* 3 positive subtraction problem description: two positive numbers of any length are subtracted. The two positive numbers can contain decimal points or integers. Please output the result. The entered string does not contain any other characters except the number and decimal point. It does not contain multiple decimal points or the location where the decimal point is the first character, therefore, you do not need to consider illegal numeric strings in your program. Detailed requirements and constraints: 1. the input value is positive, but the output value may be negative. 2. the input and output are strings. 3. if the output is a positive number, it does not need to be signed. If it is a negative number, the output result string must contain a negative number, for example, 2.2-1.1. The output result string is "1.1 ", 1.1-2.2 is output as "-1.1" 4. the output result string needs to filter out the invalid digits before and after the decimal point. If the decimal point is all 0, the output result is 11.345, this value cannot contain 0, 011.345, 0011.34500, or, and is regarded as an error output. For example, if the result of 1.1-1.1 is 0.0, 0 is directly output. Required implementation function: void Decrease (char * input1, char * input2, char * output) [input] char * iinput1 subtrahend char * nput2 subtrahend [output] char * output subtraction result [Return] No sample input: char * input1 = "2.2" char * input2 = "1.1" output: char * output = "1.1" input: char * input1 = "1.1" char * input2 = "2.2" output: char * output = "-1.1" */void Decrease (char * input1, char * input2, char * output) {int I; int n1 = 0; int n2 = 0; int m1 = 0; int m2 = 0; for (I = 0; input1 [I]! = '. '; I ++) {if (input1 [I] =' \ 0') {m1 =-1; break;} + + n1 ;} if (m1 =-1) m1 = 0; else {for (++ I; input1 [I]! = '\ 0'; I ++) {++ m1 ;}} for (I = 0; input2 [I]! = '. '; I ++) {if (input2 [I] =' \ 0') {m2 =-1; break ;}++ n2 ;} if (m2 =-1) m2 = 0; else {for (++ I; input2 [I]! = '\ 0'; I ++) {++ m2 ;}} cout <"n1" <n1 <"n2" <n2 <endl; cout <"m1" <m1 <"m2" <m2 <endl; int m3 = (m1> m2? M1: m2); int n3 = (n1> n2? N1: n2); int len = n3 + m3; char * s1 = new char [len]; char * s2 = new char [len]; char * s3 = new char [len + 1]; for (I = 0; I <n3; I ++) {if (n3-i> n1) s1 [I] = '0'; else s1 [I] = input1 [n1 + i-n3]; if (n3-i> n2) s2 [I] = '0 '; else s2 [I] = input2 [n2 + i-n3];} for (I = n3; I <m3 + n3; I ++) {if (i-n3 <m1) s1 [I] = input1 [n1 + 1 + i-n3]; else s1 [I] = '0'; if (i-n3 <m2) s2 [I] = input2 [n2 + 1 + i-n3]; else s2 [I] = '0';} cout <s1 <endl; cout <s2 <endl; int k = 0; for (I = 0; I <len; I ++) {if (s1 [I]> s2 [I]) break; else if (s1 [I] <s2 [I]) {k =-1; break;} int d = 0; if (k =-1) {for (I = 0; I <len; I ++) {swap (s1 [I], s2 [I]);} output [d ++] = '-';} cout <s1 <endl; cout <s2 <endl; k = 0; for (I = len-1; i>-1; I --) {if (k + s1 [I]-s2 [I])> = 0) {s3 [I] = '0' + k + s1 [I]-s2 [I]; k = 0 ;} else {s3 [I] = '0' + 10 + k + s1 [I]-s2 [I]; k =-1 ;} cout <"s3 []" <I <"" <s3 [I] <endl;} cout <s3 <endl; int start = 0; for (I = 0; I <n3; I ++) {if (start = 0 & s3 [I] = '0') continue; start = 1; output [d ++] = s3 [I];} if (start = 0) output [d ++] = '0'; start = 0; for (I = len-1; I> n3-1; I --) {if (start = 0 & s3 [I] = '0') continue; start = 1; break;} int a1 = I; if (start = 0) {output [d] = '\ 0';} else {output [d ++] = '. '; for (I = n3; I <a1 + 1; I ++) output [d ++] = s3 [I]; output [d] = '\ 0' ;}} int main (void) {char * input1 = "1.1"; char * input2 = "2.2 "; char * output = new char [100]; Decrease (input1, input2, output); cout <output; system ("pause"); return 0 ;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.