i need help on my homework problem.

i have done the operations part (+, -, *, /, sqrt) but i am really having a problem on making my calculator accurate up to 40 digits and to truncate all the non-significant zeroes on the output. i don't know what data type to use since i know that the highest decimal place the program can give me is only up to 6 decimal places.

can anyone help me write the codes needed for this? i am really having a problem on it right now. thanks a lot!

show us what you've got

i need help on my homework problem.

i have done the operations part (+, -, *, /, sqrt) but i am really having a problem on making my calculator accurate up to 40 digits and to truncate all the non-significant zeroes on the output. i don't know what data type to use since i know that the highest decimal place the program can give me is only up to 6 decimal places.

can anyone help me write the codes needed for this? i am really having a problem on it right now. thanks a lot!

Stop using the built in data types, since obviously they will do you no good for 40 digits of accuracy. :) Remember the base 10 number system basics, and that char's are just 1 byte
numbers that work just like the small int's that they are (less than 256 or 512 depending on
whether they're unsigned char's or signed char's).

Try this:

``````printf("\n %c", 65);
printf("\n %d", 65);
printf("\n %c, 'A');
printf("\n %d, 'A');``````

Truncating the non-significant zero's is easy to do when you have your own display function of your new array of char's:

``````accuracy = -1; dotFlag = i = 0;
while(a[i++] == 0);  //runs the index past the leading zero's before the first digit
i = 0;
while(1)  {   //now print the real digits

if(a[i] == '.')  {
dotFlag = 1;
putchar('.');
}
else
printf("%d", a[i]);

if(dotFlag)
accuracy++;

if(accuracy == accuracyYouWant)
break;

++i;
}``````

You have to control every aspect of every operation: "carries" when you add two numbers that total more than 9, (which may cause yet another carry); "borrowing" when you need it in a subtraction, (which also may cause yet another borrow to be made in the array), etc.

If the calculator is going to be handling anything with parenthesis, (5 + 7) * 3 for example, then I'd use a stack, and one of the reverse polish notations, to push and pop numbers and operators. Otherwise the logic gets difficult, fast.

The code above is not tested, or even compiled. It's just for a starting place for your idea's, not cut and paste ready code.

That should get you started.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.