New Member
💻 Oldtimer
- Joined
- Dec 3, 2002
- Messages
- 2,490
- Best answers
- 0
Here's the scenario: my keyboardist is trying to make a program that is essentially a very simple calculator. She's using an OSX mac, and using something called emacs, to code this very simple program in C. It's not working, she's at a loss, and if she doesn't finish it ASAP she won't be able to graduate.
Basically it's supposed to calculate, store and recall a value, I believe. Her exact words of her problem:
Also, if anyone is interested or can fix it, here is her code:. She's really desperate, for what it's worth. I know you guys are pretty sharp, hopefully someone will be able to help us out!
Basically it's supposed to calculate, store and recall a value, I believe. Her exact words of her problem:
If anyone could point me and her toward some kind of tutorial of some sort, that would be very helpful!its supposed to store numbers, and when you try to print out the memory theres a bunch of random like 30 digit numbers in there that i didnt store and if i try to clear the memory it doesnt work
Also, if anyone is interested or can fix it, here is her code:. She's really desperate, for what it's worth. I know you guys are pretty sharp, hopefully someone will be able to help us out!
</value2;></value2;></gmemsize)></stdio.h>#include <stdio.h>
//Declare the global size of the calculator memory.
const int gMemSize=20;
void clearMem(int value1, int value2, float n[])
{
int x;
if (value2>0 && value2<gmemsize) {="">
for (x>value1; x<=value2; ++x)
n[x]=0;
}
else
printf("out of range of defined memory\n");
}
void printResult(float result) //eliminates use of
printf statements
{
printf("%.2f\n", result);
}
float power(float value1, float value2)
{
int i;
int v;
if(value2 > 0)
{
v=value1;
for (i=1; i<value2; i++)="">
value1=value1*v;
printf("%.2f\n", value1);
}
else
if(value2==0)
{
value1=1;
printf("%.2f\n", value1);
}
else
if(value2< 0 && value1 != 0)
{
value2=value2 * -1;
v=value1;
for(i=0; i<value2; i++)="">
value1=value1/v;
printf("%.2f\n", value1);
}
else
if (value2<0 && value1==0)
{
printf("cannot divide by zero!!\n");
}
}
int main(void)
{
float value1;
float value2;
float n[gMemSize];
char operator;
int i;
int v;
\
clearMem(0, (gMemSize - 1), n);//Initialize the
calculator's memory to 0.0
printf ("Type in a math expression. \n");
while (1)
{
printf (">");
scanf ("%f %c %f", &value1, &operator, &value2);
switch (operator)
{
case 'A':
value2=n[(int)value2];
case '+':
printResult (value1+value2);
break;
case 'S':
value2=n[(int)value2];
case '-':
printResult (value1-value2);
break;
case 'M':
value2=n[(int)value2];
case '*':
case 'x':
printResult (value1*value2);
break;
case 'D':
value2=n[(int)value2];
case '/':
if(value2==0)
printf("cannot divide by zero!!\n");
else
printResult( value1/value2);
break;
case 'P':
value2=n[(int)value2];
case '^':
printResult(power(value1, value2));
break;
case 's': //SAVING MEMORY LOCATIONS
n[(int)value2] = value1;
printf("stored memory location #%i.\n",
(int)value2);
break;
case 'p': //RECALLING LOCATIONS
for(i=value1; i<=value2; i++)
printf("value #%i -- %.2f\n", i, n);
break;
case 'c': //clears memory
clearMem(value1, value2, n);
break;
default:
printf("operator unknown!!\n");
break;
}
}
return 0;
}