Huawei Topic Big Data Calculator

Source: Internet
Author: User
Tags strcmp

2. Large number plus minus

Problem Description:

In the computer, due to the processor bit width limit, can only handle the finite precision decimal integer addition and subtraction, for example, in a 32-bit wide processor computer, the operand and the result of the participating operation must be between -231~231-1. If you need to do a wider range of decimal integer additions, you need to implement it in a special way, such as using a string to save the operand and the result, and to take a bitwise operation. As follows:

9876543210 + 1234567890 =?

Let the string num1= "9876543210", the string num2= "1234567890", and the result is saved in string results = "11111111100".

-9876543210 + (-1234567890) =?

Let the string num1= "-9876543210", the string num2= "1234567890", and the result is saved in the string results = "11111111100".

Requires programming to achieve the above-mentioned high-precision decimal addition.

Required implementation functions:

void Add (const char *NUM1, const char *num2, char *result)

"Input" NUM1: string as operand 1, if the operand is negative, then num1[0] is the sign bit '-'; num2: String form operand 2, if the operand is negative, then num2[0] is the sign bit '-'

Output Result: Saves the addition calculation result string, or result[0] as the sign bit if the result is negative.

Note:

I, when the input is positive, ' + ' will not appear in the input string, when the input is negative, '-' will appear in the input string, and must be in the leftmost position of the input string;

II, input string all bits represent a valid number, that is, there is no input string starting from ' 0 ', such as "0012", "0012" will not appear;

III, requires that the output string all bits are valid numbers, the result is positive or 0 o'clock ' + ' does not appear in the output string, the result is negative when the leftmost position of the output string is '-'.

Example

Input: NUM1 = "580"

num2 = "-50"

Output: result = "530"

Input: NUM1 = "580"

num2 = "-600"

Output: result = "-20"

#include <stdio.h> #include <string.h>//"input" NUM1: the string form operand 1, if the operand is negative, then num1[0] is the sign bit '-'; num2: string as operand 2, If the operand is negative, then num2[0] is the sign bit '-' output ' result: Saves the addition calculation results string, and if the result is negative, then result[0] is the sign bit. void Change (char * str) {int num =strlen (str); int i; int tmp;for (i = 0;i <= (NUM/2-1); i++) {tmp = Str[i];str[i] = str[ Num-i -1];str[num-1-i] = tmp;} }void Jinwei (char *str) {int num = strlen (str), if (str[num-1]! = '-') {for (int i = 0;i<num;i++) {if (Str[i] >= 0x3a) {str[ i+1]++;str[i]= Str[i]-10;if (i = = num-1) {str[i+1] = ' 1 ';}}} else{for (int i = 0;i<num-1;i++) {if (Str[i] >= 0x3a) {str[i+1]++;str[i]= str[i]-10;if (i = = num-2) {str[i+1] = ' 1 '; str [I+2] = '-';}}}} void Jinwei2 (char *str) {int num = strlen (str); for (int i =0;i<num-1;i++) {if (str[i]<0x30) {str[i+1]--;str[i]=str[i ]+10;}}} void Add (const char *num1,const char *num2, char *result) {char num11[50] = ""; char num22[50] = ""; strcpy (NUM11,NUM1); STRCP Y (num22,num2), change (NUM11), change (NUM22), int i1 = strlen (NUM11); int i2 = strlen (NUM22); int imin; int i;int flag;int t;if (num1[0]! = '-' && num2[0]! = '-') {I1 >= i2? (Imin = i2): (imin = i1);//Get a small string of I1 >= i2 in the plus number?  (flag = 2): (flag = 1); Flag bit which is the smaller string for (i=0;i<imin;i++) {result[i] = Num11[i] + num22[i]-0x30;} if (flag = = 2) for (; i<i1;i++) Result[i] = Num11[i];else if (flag = = 1) for (; i<i2;i++) result[i] = Num22[i];} if (num1[0] = = '-' && num2[0] = = '-') {I1 >= i2? (Imin = i2): (imin = i1);//Get a small string of I1 >= i2 in the plus number?  (flag = 2): (flag = 1); Flag bit which is the smaller string for (i=0;i<imin-1;i++) {result[i] = Num11[i] + num22[i]-0x30;} if (flag = = 2) for (; i<i1;i++) Result[i] = Num11[i];else if (flag = = 1) for (; i<i2;i++) result[i] = Num22[i];} if (num1[0]! = '-' && num2[0] = = '-') {change (NUM11); change (NUM22); flag = strcmp (NUM11, (num22+1)); if (Flag > 0) /positive number greater than negative {change (NUM11), change (NUM22), for (i =0;i< i1;i++) {if (i<i2-1) result[i] = Num11[i]-num22[i] + ' 0 '; Elseresult[i] = Num11[i];} if (flag < 0)//positive is less than negative {change (NUM11), change (NUM22), for (i= 0;i<i2;i++) {if (I&LT;I1) result[i] = Num22[i]-num11[i] + ' 0 '; elseresult[i] = Num22[i];}} if (flag = = 0) result[0] = ' 0 ';} if (num1[0] = = '-' && num2[0]! = '-') {change (NUM11); change (NUM22); flag = strcmp ((num11+1), NUM22); if (Flag > 0) /positive Xiao to negative {change (NUM11), change (NUM22), for (i = 0;i<i1;i++) {if (I&LT;I2) result[i] = Num11[i]-num22[i] + ' 0 '; Elseresult[i] = Num11[i]; }} if (flag < 0)//positive number da is negative {change (NUM11), change (NUM22), for (i =0;i< i2;i++) {if (i<i1-1) result[i] = Num22[i]- Num11[i] + ' 0 '; elseresult[i] = Num22[i];} } if (flag = = 0) result[0] = ' 0 ';}} void Qianyi (char *str) {int I =0;for (i = 1;i<strlen (str); i++) {str[i-1] = Str[i];}} void Xianshi (char *str) {int i;if (str[0] = = '-') for (i = 1;i<strlen (str);) {if (str[i] = = ' 0 ') {Qianyi ((str+i)); str[ strlen (str)-1] = ' + ';} Elsebreak;} Elsefor (i = 0;i<strlen (str); i++) {if (str[i] = = ' 0 ') Qianyi ((str+i)); elsebreak;}} int main () {char a1[] = "580"; char a2[] = " -600"; char a3[50] = "";//char a3[]; Add (A1,A2,A3); Change (A1);if (A1[0]!=a2[0]) jinwei2 (A3); Elsejinwei (A3); Change (A3); Xianshi (A3);p rintf ("%s", A3);}

  

Huawei Topic Big Data Calculator

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.