Home c Calculator binary numbers

# Calculator binary numbers

Author

Date

Category

In general, you need to make a calculator binary real numbers on BBC, tried through crutch: translated into decimal SS, performs an arithmetic operation and then translated back into binary, but this method is not very suitable, because at first it is a crutch, and in Secondly consider the real numbers in such manner is impossible or is even bigger spike. I believe that it is possible to realize all with the help of bit operations, so you need a hint. Like any bitwise operations should be used for addition, subtraction and multiplication of binary numbers real?

You have given completely unspecific description of the problem …

1. it allowed complex expression (such as: 2.3 / 4.7 * 101.9)?
2. Do parentheses are allowed in the expression (such as (2.0 + 3.0) / 7.6)?
3. Does the mixture is allowed to integer and real constants?
4. Does the GUI need?

Well, a lot more then. If the answer to all these questions is ‘Yes’, then the program becomes quite complicated and can hardly be a topic of the laboratory work. Therefore, assuming that in fact, the task was formulated something like this:

1. Enter the first operand
2. Enter second operand
3. Enter the sign of the operation
4. Perform calculations
5. Print the results.

To such a simple task code may look something like this:

``````# include & lt; stdio.h & gt;
#Include & lt; String.h & gt;
#include & lt; strings.h & gt;
#Include & lt; math.h & gt;
// function to convert chtroki kind 10101011.011100 to float number
// cp - input string of zeros characters, units and terms
// returns a value of type float, resulting from this line
float char2float (char * in_string) {
// Splits a string into integer and fractional part
char * cp_dot, * cp_fract;
int len_int, len_fract;
int val_int, val_fract;
int j;
cp_dot = index (in_string, '.'); // position of the point in the string
* Cp_dot = '\ 0'; // Cut the string into integer and fractional part
cp_fract = cp_dot + 1; // Start the fractional part
len_int = strlen (in_string);
len_fract = strlen (cp_fract);
if (cp_fract [len_fract-1] == 0x0A) {
cp_fract [len_fract-1] = '\ 0'
len_fract = strlen (cp_fract);
}
// value of the integer part
val_int = 0;
for (j = 0; j & lt; len_int; j ++) {
val_int = val_int & lt; & lt; 1;
if (in_string [j] '1' ==) val_int + = 1;
}
// value of the fractional part of
val_fract = 0;
for (j = 0; j & lt; len_fract; j ++) {
val_fract = val_fract & lt; & lt; 1;
if (cp_fract [j] '1' ==) val_fract + = 1;
}
// Obedinyaem integer and fractional parts into a single number
float a = val_int;
float b = val_fract;
b = b / powf (2.0, (float) len_fract);
Return A + B;
}
int main (int argc, char * argv []) {
float a, b, c;
char op;
char buf ;
printf ( "Enter the first operand:");
fgets (buf, 32, stdin);
a = char2float (buf);
printf ( "Enter the second operand:");
fgets (buf, 32, stdin);
b = char2float (buf);
printf ( "Enter the sign of the operation:");
while ((op = getchar ()) == 0x0A);
// Perform calculations
switch (op) {
Case '+':
c = a + b;
Break;
Case '-':
c = a-b;
Break;
Case '*':
c = a * b;
Break;
Case '/':
c = a / b;
Break;
Default:
printf ( "Unknown operation:% x \ n", op);
}
// Print the result.
printf ( "The calculation result is:% 6.3f \ n", c);
}
``````

When you run the get:

``````\$ ./a.out
Enter the first operand: 110,110
Type the second operand: 1.0
Enter the sign of operation: *
The result of calculations: 6.750
``````
Previous articlejava. Canvas. JFrame

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.