Code progress of pairing project programming

Source: Internet
Author: User

Since the initial project was designed according to the idea of the module, it was already a modular program.

 

The program teacher raised the following requirements for the peer project:

 

 Calc()

This calc function accepts the input of a string (the string contains the operator formula, such as "5 + 3.5", "7/8-3/8", and "3 + 90*(-0.3 ), the return value of this module is a string. For example, the result of the previous examples is ("17.5", "1/2", "-24 ").

 

So I want to addDecimalAndNegativeComputing functions.

And set the return value of the module 【String] (The current return type is fraction)

Therefore, I added a tostring function to the fraction class. Which can convert both the score and decimal point to the string-type

String fraction: tostring () {// The calculation result is changed to string char T [10]; sprintf (T, "% d", num); string S1 = T; if (Den = 1) {return S1;} else {sprintf (T, "% d", Den); string S2 = T; return S1 + "/" + S2 ;}} string fraction: tostring2 (INT jingdu) {// The calculation result is changed to string char T [10]; float result = num * 1.0/DEN; float result2 = int (result * POW (10.0, jingdu)/POW (10.0, jingdu); sprintf (T, "% F ", result2); string S1 = T; return S1 ;}

 

For negative calculation, the negative number may appear at the beginning:-1 + 4/2*5 or 2 + 4 * (-4) + 2 with parentheses. So after finding this rule, I find a negative number and add 0 to it.

Exp: exp (string s) {STR = ""; // negative Computation for (INT I = 0; I <S. size (); I ++) {if (I = 0 & S [I] = '-' | s [I] = '-' & S [I-1] = '(') STR + = "0"; STR = STR + s [I];}

For decimal operations, I used to convert decimal points into scores.

For example, 3.45 is 345/100.

In addition, I have also designed three types of exceptions: "A maximum of several operators, the number of data ranges, the calculation precision (a few decimal places are retained, and so on)" and

void Exp::setting(int geshu,int jingdu,int max,int min){    this->geshu=geshu;    this->jingdu=jingdu;    this->max=max;    this->min=min;}

 

The following statement is added to the setfraction (string STR, int Max, int min) function and an exception is thrown.

If (Num> max | num <min | Den> max | Den <min) throw string ("out of range"); else if (Den = 0) throw string ("the denominator cannot be 0 ");

Add the following statement in setexp (string s) to throw an exception.

If (Cal> Geshu) throw string ("too many operators ");

 

Add the tostring2 function. The specific implementation method is as follows: [5, 4321] to retain two decimal places (the precision is 2), multiply the value by 5.4321, get the integer, and divide it by 100, it becomes 5.43.

String fraction: tostring2 (INT jingdu) {// The calculation result is changed to string char T [10]; float result = num * 1.0/DEN; float result2 = int (result * POW (10.0, jingdu)/POW (10.0, jingdu); sprintf (T, "% F", result2); string S1 = T; return S1 ;}

 

 

This is probably the case. For more information about the test, see my partner's blog:

Http://www.cnblogs.com/xDan/

 

Code progress of pairing project programming

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.