C + + implements a large number of string types (with decimals)

Source: Internet
Author: User

Recently, did a large number of Ali to add the programming problem. The topic is as follows:

Enter a number of two string types, such as 12.223 11, to determine whether the input string is legal. Valid outputs True and the addition result (true 23.223), the output false "" "is illegal.

The period has been modified several times, in the judgment of the Legality of the exclusion of such as. 212 and 122. This error (errors that occur except for numbers and. also excluded).

The main idea is to separate the fractional parts from the integers and add them separately. Because fractional parts may want to be rounded up in integers, carry judgments are required.

The complete code is as follows:

#include <iostream> #include <string>using namespace std;string max_i,min_i;bool Carry_dem = false;// Determines whether decimals need to carry a void Count_int (string s1,string s2) to an integer//computes the integer part and {max_i = S1;min_i = S2;if (S2.size () >s1.size ()) {max_i = s2; Take a large number for max min_i = s1;} if (Carry_dem)//Complete carry max_i[max_i.size ()-1] ++;for (int i = min_i.size () -1,j = Max_i.size ()-1; i>=0;i--, j--)//The process of simulating integer addition {Max_i[j] + = min_i[i]-' 0 ';//choose to save the result in the max variable if (Max_i[j] > ' 9 ') {if (J > 0) {max_i[j-1] ++;max_i[j]-= 10;} Else{max_i = "1" + max_i;max_i[j]-= 10;}}} String Max_d,min_d;void count_dem (string s1,string s2)//Add to the decimal part {max_d = S1;min_d = S2;if (S2.size () >s1.size ()) {max _d = S2;min_d = S1;} for (int i=min_d.size () -1;i>=0;i--) {Max_d[i] + = min_d[i]-' 0 ', if (Max_d[i] > ' 9 ') {if (i > 0) {max_d[i-1] ++;max_d[ I]-= 10;} Else{carry_dem = true;//set carry flag max_d[i]-= 10;}}} int main () {string S1,s2;cin>>s1>>s2;int i,j,k;bool legal = true;for (i = 0; i< s1.size (); i++)// The decision to complete the string is valid {if ((s1[i]< ' 0 ' | | | s1[I]> ' 9 ') && (s1[i]! = '. ')) | | (S1[s1.size ()-1] = = '. ') | | (s1[0] = = '. ')) legal = false;} for (i = 0; i< s2.size (); i++) {if ((s2[i]< ' 0 ' | | S2[i]> ' 9 ') && (s2[i]! = '. ')) | | (S2[s2.size ()-1] = = '. ') | | (s2[0] = = '. ')) legal = false;} String integer_1,integer_2;//out the integer part of the string demical_1,demical_2;//the fractional portion of the string if (!legal) cout<< "false" < < "\" \ "\" \ ""; else{bool wi_dem1 = FALSE,WI_DEM2 = false;//Determines whether with a decimal int pos_s1 = 0,POS_S2 = 0;for (i = 0; i< s1.size (); i+ +) if (s1[i] = = '. ') {wi_dem1 = true;} for (i = 0; i< s2.size (); i++) if (s2[i] = = '. ') {wi_dem2 = true;} if ((!wi_dem1) && (!WI_DEM2))//two digits without a decimal part {count_int (S1,S2);cout<< "true" <<max_i;} Else{if (WI_DEM1)//first number with decimals separated integer, fractional part {pos_s1 = S1.find ("."); integer_1 = S1.substr (0,pos_s1);d emical_1 = S1.substr (pos_s1+1);} if (WI_DEM2)//The second number with decimals separates integers, fractional parts {pos_s2 = S2.find ("."); integer_2 = S2.substr (0,pos_s2);d emical_2 = S2.substr (pos_s2+1);} if (wi_dem2 && wi_dem1)//with decimal {Count_dem (demical_1,demical_2); Count_int (Integer_1,integer_2);cout<< "true" <<max_i<< "." <<max_d;} else if (!wi_dem2 && wi_dem1) {count_int (INTEGER_1,S2);cout<< "true" <<max_i<< "."  <<demical_1;} Else{count_int (INTEGER_2,S1);cout<< "true" <<max_i<< "." <<demical_2;}}} return 0;}

Operation Result:

Among them, some of the code can still be optimized (Bo Master more lazy ...) ), such as judging whether to have a decimal judgment and subsequent function integration.

C + + implements a large number of string types (with decimals)

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.