High-Precision power

Source: Internet
Author: User
High-Precision power

Personal information: He is a junior majoring in software engineering at yanda University;

My blog: Google searches for "cqs_2012;

My hobbies: I love data structures and algorithms and hope to make my own contribution to algorithm work in the future;

Programming Language: C ++;

Programming Environment: Windows 7 Professional Edition x64;

Programming tools: vs2008;

Drawing tools: Office 2010 PowerPoint;

Hardware information: 7g-3 Notebook;

Zhenyan

Go to the flame Hill, and then refine the red eye

Question

1001 Power Operation

Web http://bailian.openjudge.cn/practice/1001/

Solution

String processing.

I am not very familiar with this system. After multiple attempts, I found that this system is not compatible with many things, as shown below:

System ("pause ");

<Windows. h>

For input and output control, the number of input and output cannot be specified according to the example given, while (CIN>) is used to control whether to continue the input.

Ah, a tear.

Question: this is a power operation. It is not difficult to calculate high-precision computation. Consider integer overflow and use the string to simulate integer computation.

Algorithm

The personal algorithm is represented as follows in C ++ code (some of the Code has used previously written programs, so it looks messy, but the code written only by the individual is not copied)

# Include <iostream> # include <string> using namespace STD; // decimal power string floatpower (string a, unsigned int B); bool xiaoshucheck (string ); bool check_all_number (string a); // fractional multiplication string floatm ult (string a, string B); STD: pair <string, int> standardxiaoshu (string ); string mult_int (string a, string B); bool standardization (string & A); string inttochar (int I); int chartonumber (char C); string add_int (string a, STR Ing B); string addint (string a, string B); string subint (string a, string B); char compare (string a, string B); char comparexiaoshu (string, string B); // The decimal power string floatpower (string a, unsigned int B) {If (! A. empty () & xiaoshucheck (A) {string result = "1"; for (unsigned int I = B-1; I <B; I --) {result = floatm ult (, result);} If (result [0] = '0' & Result [1] = '. ') Result = result. substr (1, result. length ()-1); return result;} else if (! A. Empty ()&&! Xiaoshucheck (A) {string result = "1"; for (unsigned int I = B-1; I <B; I --) {result = mult_int (A, result );} return result ;}} bool xiaoshucheck (string a) {bool Xiaoshu = false; string B = A; // find the decimal point for (unsigned int I = 0; I <B. length (); I ++) if (B [I] = '. ') {Xiaoshu = true; // after removing the decimal point, determine whether the integer Form B = B. substr (0, I) + B. substr (I + 1, B. length ()-i-1); If (check_all_number (B )! = True) Xiaoshu = false; break;} return Xiaoshu;} // decimal multiplication string floatm ult (string a, string B) {// extend the decimal number to an integer, then perform the following operations: // (A * B)/(sum of decimal points) // STD: pair <string, int> SA = standardxiaoshu (a); STD :: pair <string, int> Sb = standardxiaoshu (B); A = sa. first; B = sb. first; // a * B. The enlarged result is string r = mult_int (a, B). // What should I do if the result is negative? Convert it to bool fushu = false; If (R [0] = '-') {fushu = true; r = R. substr (1, R. length ()-1);} // The result produces many zeros, and the decimal length is greater than 0 unsigned int maxlength = sa. second + sb. second; while (R [R. length ()-1] = '0' & maxlength> 0) {r = R. substr (0, R. length ()-1); maxlength --;} // if the result is decimal and smaller than 1, add 0 while (R. length () <= maxlength) {r = "0" + R;} If (maxlength = 0 | compare (R. substr (R. length ()-maxlength, maxlength), "0") = ') r = R. SUBST R (0, R. length ()-maxlength); else // Add the decimal point R = R. substr (0, R. length ()-maxlength) + ". "+ R. substr (R. length ()-maxlength, maxlength); // What if the result is negative? If (fushu = true) r = '-' + R; // return result return r ;} bool check_all_number (string a) {if (. empty () return false; string: size_type L =. size (), I = 0; if (a [0] = '-') I ++; while (I <L) {if (a [I] <'0' | A [I]> '9') return false; I ++;} return true;} STD :: pair <string, int> Standard Xiaoshu (string a) {// data domain bool fushu = false; string B; unsigned int I; // check whether a is a decimal if (a [0] = '-') {fushu = true; A =. substr (1,. length ()-1);} // A is a decimal if (xiaoshucheck (A) = true) {// remove 0 at the end of the decimal part while (true) {if (a [. length ()-1] = '0') A =. substr (0,. length ()-1); else break;} // remove the first 0 of the integer from for (I = 0; I <. length (); I ++) if (a [I] = '. ') {B =. substr (0, I); A =. substr (I + 1,. length ()-i-1); break;} standardization (B); = B + A; I =. length ()-B. length (); If (fushu = true) A = "-" + A; // return STD: make_pair (A, I );} // A is an integer else if (check_all_number (A) = true) {standardization (a); If (fushu = true) A = "-" + A; return STD:: make_pair (A, 0) ;}} string mult_int (string a, string B) {// exception of inputif (. empty () return B; else if (B. empty () Return "0"; if (! Check_all_number (a) |! Check_all_number (B) {return "exception of input multiplies_int";} standardization (a); Standardization (B); string: size_type I =. size (), j = B. size (); string c = "0", D = ""; bool fushu = (a [0] = '-' & B [0]! = '-') | (A [0]! = '-' & B [0] = '-'); if (a [0] = '-') A =. substr (1,. size (); If (B [0] = '-') B = B. substr (1, B. size (); int Jinwei = 0; For (j = B. size ()-1; j <B. size (); j --) {// each number of B to * A Jinwei = 0; for (I =. size ()-1; I <. size (); I --) {d = inttochar (chartonumber (A [I]) * chartonumber (B [J]) + Jinwei) % 10) + d; jinwei = (chartonumber (A [I]) * chartonumber (B [J]) + Jinwei)/10;} If (Jinwei) d = I Nttochar (Jinwei) + d; // Add all number resultc = add_int (c, d); D = ""; unsigned int zero = 0; while (zero <B. size ()-j) {d = d + '0'; zero ++ ;}} standardization (c); If (fushu & C! = "0") return '-' + C; else return C;} bool standardization (string & A) {If (! Check_all_number (A) {cout <A <"exception of input standardization" <Endl; return false;} string: size_type I = 0; bool fushu = false; if (A [0] = '-') {fushu = true; I = 1;} while (I <. size () {if (a [I]! = '0') break; I ++;} if (I =. size () I --; A =. substr (I,. size ()-I); If (fushu &! = "0") A = '-' + A; return true;} string inttochar (int I) {if (I> = 0 & I <= 9) {string c = ""; return C + char (I + 48);} else {cout <I <"exception of input inttochar" <Endl; return "E" ;}}// make Char to the int numberint chartonumber (char c) {If (C> = '0' & C <= '9 ') return int (c-'0'); else {cout <C <"exception of input chartonumber" <Endl; return 0 ;}; string add_int (string, string B) {// exception of I Nputif (A. Empty () return B; else if (B. Empty () Return "0"; if (! Check_all_number (a) |! Check_all_number (B) {return "exception of input add_int";} standardization (a); Standardization (B); if (a [0]! = '-' & B [0]! = '-') Return addint (a, B); else if (a [0]! = '-' & B [0] = '-') return subint (A, B. substr (1, B. size (); else if (a [0] = '-' & B [0]! = '-') Return subint (B,. substr (1,. size (); else return '-' + addint (. substr (1,. size (), B. substr (1, B. size () ;}; string subint (string a, string B) {// exception of inputif (! Check_all_number (a) |! Check_all_number (B) Return "exception of input minusint"; Standardization (a); Standardization (B); // participant ular string of inputif (. empty () {If (B. empty () Return "0"; elsereturn "-" + B;} else if (B. empty () {return a;} // normal number A <bstring c = ""; bool check = true; If (compare (a, B) = ') return "0"; else if (compare (a, B) = '<') {c = A; A = B; B = C; C = ""; check = false;} // normal number A> = bstring:: Size_type I =. size ()-1, j = B. size ()-1; int jiewei = 0, now; while (I <. size () & J <B. size () {now = chartonumber (A [I])-chartonumber (B [J])-jiewei; If (now <0) {jiewei = 1; now = 10 + now;} else jiewei = 0; C = inttochar (now) + C; I --; j --;} while (I <. size () {now = chartonumber (A [I])-jiewei; If (now <0) {jiewei = 1; now = 10 + now;} else jiewei = 0; C = inttochar (now) + C; I --;} standardizat Ion (c); If (! Check) C = '-' + C; return C ;}; string addint (string a, string B) {// exception of inputif (. empty () return B; else if (B. empty () Return "0"; if (! Check_all_number (a) |! Check_all_number (B) {return "exception of input addint";} standardization (a); Standardization (B); string: size_type I =. size ()-1, j = B. size ()-1, K = 0; string c = ""; int Jinwei = 0; while (I <. size () & J <B. size () {c = inttochar (chartonumber (A [I]) + chartonumber (B [J]) + Jinwei) % 10) + C; jinwei = (chartonumber (A [I]) + chartonumber (B [J]) + Jinwei)/10; j --; I --;} while (j <B. size () {c = Inttochar (chartonumber (B [J]) + Jinwei) % 10) + C; Jinwei = (Jinwei + chartonumber (B [J])/10; j --;} while (I <. size () {c = inttochar (chartonumber (A [I]) + Jinwei) % 10) + C; Jinwei = (Jinwei + chartonumber (A [I]) /10; I --;} If (Jinwei) C = inttochar (Jinwei) + C; Standardization (c); Return C ;}; char compare (string a, string B) {if (. empty () | B. empty () {cout <"error of input compare"; retu Rn 'E';} else {If (! Check_all_number (a) |! Check_all_number (B) {return 'E';} standardization (a); Standardization (B); if (a [0] = '-' & B [0]! = '-') Return '<'; else if (a [0]! = '-' & B [0] = '-') return '>'; bool fushu = (a [0] = '-'); if (. size ()> B. size () return fushu? '<': '>'; Else if (. size () = B. size () {for (string: size_type I = 0; I <. size (); I ++) {if (a [I]> B [I]) return fushu? '<': '>'; If (a [I] <B [I]) return fushu? '>': '<';} Return '=';} return fushu? '>': '<';} Char comparexiaoshu (string a, string B) {If (xiaoshucheck (a) & xiaoshucheck (B) {pair <string, int> SA = standardxiaoshu (a); pair <string, int> Sb = standardxiaoshu (B); int length = sa. second-Sb. second; while (length! = 0) {If (length <0) {SA. first + = "0"; length ++;} else if (length> 0) {sb. first + = "0"; Length --;} return compare (SA. first, sb. first) ;}}int main () {string a; int B; while (CIN >>>a> B) cout <floatpower (a, B) <Endl; // system ("pause"); Return 0 ;}







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.