Home > Back-end >  You can have a look at how to solve? Simple calculator
You can have a look at how to solve? Simple calculator

Time:03-06

Just learn c, made a simple calculator, what need to add code to support brackets, priority now add parentheses would collapse, and the logarithmic time, how to let the user enter the base number of logarithmic and then to calculate? Please help to see
# include & lt; Stdio. H>
# include & lt; Math. H>
# include & lt; Stdlib. H>
//preprocessor directive
Int main (void)
{
Double bNumber, Number, the Result;//variables defined for addition, subtraction, multiplication, and division
Int No;//options
the definition of the variablesDouble a, b, c, x1, x2, Rad.//to power index logarithm variable
Int Ary_10;//define a binary variable
Char string [32].//binary variables defined

Simple calculator system (" ");//the title of the program window


While (1)
{
//interface

Printf (" please choose you to calculate methods: \ n ");

Printf (" addition please press 1 hexadecimal conversion 5 \ n ");
Printf (" subtraction please press 2 please press 6 \ n ");
Printf (" multiplication please press 3 power press 7 \ n ");
Please press 4 for logarithmic printf (division please click exit 8 _0 \ n ");

Printf (" do write down the number: ");
The scanf (" % d ", & amp; No);

If (No==1)
{
//add
Printf (" please enter the augend: ");
Lf the scanf (" % ", & amp; BNumber);
Printf (" please enter a number: ");
Lf the scanf (" % ", & amp; Number);
Result=bNumber + Number;
Printf (" the Result is: % lf \ n \ n ", Result);
}
Else if (No==2)
{
//subtraction
Printf (" please enter the minuend: ");
Lf the scanf (" % ", & amp; BNumber);
Printf (" please input reduction: ");
Lf the scanf (" % ", & amp; Number);
Result=bNumber - Number;
Printf (" the Result is: % lf \ n \ n ", Result);
}
Else if (No==3)
{
//the multiplication
Printf (" please enter the multiplicand: ");
Lf the scanf (" % ", & amp; BNumber);
Printf (" please enter a multiplier: ");
Lf the scanf (" % ", & amp; Number);
Result=bNumber * Number;
Printf (" the Result is: % lf \ n \ n ", Result);
}
Else if (No==4)
{
//division
Printf (" please enter the dividend: ");
Lf the scanf (" % ", & amp; BNumber);
Printf (" please input divisor: ");
Lf the scanf (" % ", & amp; Number);
Result=bNumber/Number;
Printf (" the Result is: % lf \ n \ n ", Result);
}
Else if (No==5)
{
//hexadecimal conversion code
Printf (" please enter the need to convert decimal number: ");
The scanf (" % d ", & amp; Ary_10);
Itoa (Ary_10, string, 2);
Binary: printf (" % s \ n ", & amp; String);
Printf (" octal: % o \ n ", & amp; Ary_10);
Printf (" hexadecimal: % x \ n ", & amp; Ary_10);
}
Else if (No==6)
{
//
Printf (" please enter a number needs to be: ");
Lf the scanf (" % ", & amp; a);
Printf (" the result is: % lf \ n ", SQRT (a));
}
Else if (No==7)
{
//for a number of power
Printf (" please enter the base number: ");
Lf the scanf (" % ", & amp; a);
Printf (" please input index: ");
Lf the scanf (" % ", & amp; B);
Printf (" the result is: % lf \ n ", pow (a, b));
}
Else if (No==8)
{
//a number of logarithmic
Printf (" please enter a number to request log: ");
Lf the scanf (" % ", & amp; a);
Printf (" for e logs base: % lf \ n ", the log (a));
Printf (" for 10 logs base: % lf \ n ", log10 (a));



}
Else if (No==0)
{
//exit program
break;
}
The else
{
//input the wrong options
Printf (" please input the correct number, \ n \ n ");
}
System (" pause ");//press any key to continue
System (" CLS ");//clear screen
}
return 0;
getchar();
}

CodePudding user response:

Fyi:
 # pragma warning (4996) disable: 
/* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Functional calculator (vc + + 6.0, the Win32 Console)
Function:
Currently provides more than 10 common mathematical functions:
(1) sinusoidal sin
2 cosine cos
(3) tangent tan
(4) open square SQRT
5] arcsine arcsin
[6] the arccosine arccos
Once the arctangent arctan
Being common logarithm lg
'levies natural logarithm ln
Exp. ⑽ e index
⑾ been function ^
⑿ rounded up ceil
[13] the whole floor down
[14] round round round
⒂ take symbol sign
Has displayed take absolute value abs
Usage:
If asked 32 times the power of 2, can break into 2 ^ 32 & lt; Enter & gt;
If asked 30 degrees of tangent type tan (Pi/6) & lt; Enter & gt;
Note not scored: tan (30) & lt; Enter>
If required 1.23 radians sine, there are several approaches are effective:
Sin (1.23) & lt; Enter>
Sin 1.23 & lt; Enter>
Sin1.23 & lt; Enter>
If verification is cosine formula of sum of squares can be into sin (1.23) ^ 2 + cos (1.23) ^ 2 & lt; Enter> Or sin1.23 ^ 2 + cos1.23 ^ 2 & lt; Enter>
In addition two function together, automatic understanding for multiplication, such as: sin1.23 cos0.77 + cos1.23 sin0.77 is equivalent to the sin (1.23) * cos (0.77) + cos (1.23) * sin (0.77)
Of course, you can also be based on the triangle transform, reoccupy sin (1.23 + 0.77) or sin2 verify,
This calculator so give full consideration to the operator priority, such as: 2 + 3 * 4 ^ 2 effectively: 2 + (3) * (4 * 4)
In addition the function name if it is in front of the Numbers, then automatically think people.
Likewise, if a number is the right of the left parenthesis, automatically think that the number and implied a multiplication sign between a bracket,
Such as: 3 sin1. 2 ^ 2 + 5 cos2. 1 ^ 2 to 3 * sin2 + 5 * cos2 (1.2) (2.1)
Such as: 4 (3-2 (sqrt5-1) + ln2) + lg5 equivalent to 4 * (2 * 3-5 () - 1) + loge (2)) + log10 (5)
In addition, the calculator provides a Pi type letters case-insensitive, for ease of use,
Hexadecimal integer begin with 0 x or 0 x,
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- */
# include & lt; iostream>
# include & lt; Iomanip>
# include & lt; Cstdlib>
# include & lt; Cstring>
# include & lt; Cctype>
# include & lt; Cmath>
# include & lt; Stdio. H>
# include & lt; String. H>
# include & lt; Windows. H>
using namespace std;
Const char Tab=0 x9;
Const int DIGIT=1;
Const int MAXLEN=16384;
Char s [MAXLEN], * endss;
Int PCS=15;
Double sign (double dVal) {
If (dVal> Return 1.0 0.0);
Else if (dVal<0.0) the return - 1.0;
The else return 0.0;
}
Double round (double dVal, short iPlaces) {//iPlaces>=0
Char s [30];
Double dRetval;

Sprintf (s, "%. * lf iPlaces, dVal);
Sscanf (s, "% lf", & amp; DRetval);
Return (dRetval);
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related