GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-Oct-2006, 13:28
basketball749 basketball749 is offline
New Member
 
Join Date: Oct 2006
Posts: 9
basketball749 is on a distinguished road

Simple Calculator in C


Hey all im new as a member but have browsing for a little bit of time since ive had my basic c programming class this semester. im working on this simple calculator and the problem reads:

Write a program to model a simple calculator. Each data line should consist of the next
operation to be performed from the list below and the right operand. Assume the left
operand is the accumulator value (initial value of 0). You need a function scan_data with
two output parameters that returns the operator and right operand scanned from a data
line. You need a function do_next_op that performs the required operation. do_next_op
has two input parameters (the operator and operand) and one input/output parameter (theaccumulator). The valid operators are:
+ add
- subtract
* multiply
/ divide
^ power (raise left operand to power of right operand)q quit
Your calculator should display the accumulator value after each operation. A sample run
follows.
+ 5.0
result so far is 5.0
^ 2
result so far is 25.0
/ 2.0
result so far is 12.5
q 0
final result is 12.5

Ive got part of the program writen and i was wondering how you guys think it looks and if i just finish it up if it will work. thanks for any help.

heres what i have written:

CPP / C++ / C Code:
#include <stdio.h>
#include <math.h>
#define SENTINEL q

/*function prototype*/
void scan_data(double *nump, char *opp);
doulbe do_next_op(double *nump, char *opp, double *accump);
void add_operands(double num_1, double num_2, double *ansp);
void subtract_operands(double num_1, double num_2, double *ansp);
void multiply_operands(double num_1, double num_2, double *ansp);
void divide_operands(double num_1, double num_2, double *ansp);
void power_operands(double num_1, double num_2, double *ansp);
int
main(void)
{
double num_1; /*first operand*/
char op1; /*arithmetic operator + - * / ^  */
double num_2; /*second operand*/
double accum = 0; /*sets accumulator to 0*/
double ans; /*ans of operation*/

do {
   /* Gets problem to be calculated*/
   scan_data(&num_1, &op1);

   /*Computes operation*/
   switch (op1) {
   case '+':
        add_operands(num_1, num_2, &ans);
        break;
   case '-':
        subtract_operands(num_1, num_2, &ans);
        break;
   case '*':
        multiply_operands(num_1, num_2, &ans);
        break;
   case '/':
   if (num_2 == 0)
        printf("cannot divide by zero");
 
   else
        divide_operands(num_1, num_2, &ans);
        break;
   case '^':
        power_operands(num1, num_2, &ans); /*raises left operand to the power of right operand*/
        break;
   }
   do_next_op(&num_2, &op1, &accum);
        
   
        printf("\n"); /*skips line*/
        printf("op1, num_1");
        printf("result so far is %f",ans);
} while (op1 != SENTINEL);
return(0);
}
   
void scan_data (double *nump, char *opp)
{  
        double num_1;
        char op1;
        
        printf("Enter operand");
        scanf("%lf", num_1);
        printf("Enter and arithmetic operator (+,-,*,/,^, or q to quit");
        scanf("%c", op1);
Last edited by LuciWiz : 24-Oct-2006 at 14:01. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 24-Oct-2006, 15:39
basketball749 basketball749 is offline
New Member
 
Join Date: Oct 2006
Posts: 9
basketball749 is on a distinguished road

Re: Simple Calculator in C


oh ya and we are supposed to use pointers, and functions.
  #3  
Old 24-Oct-2006, 19:17
Justin Fox Justin Fox is offline
Junior Member
 
Join Date: Sep 2006
Posts: 46
Justin Fox is on a distinguished road

Re: Simple Calculator in C


so after you scan the stuff, do you wait for the next integer input, then
calculate and output?

or do you keep accumulating until they answer a negative number...

so far it looks good...
  #4  
Old 24-Oct-2006, 21:58
basketball749 basketball749 is offline
New Member
 
Join Date: Oct 2006
Posts: 9
basketball749 is on a distinguished road

Re: Simple Calculator in C


we are just supposed to keep accumulating until they press q
  #5  
Old 26-Oct-2006, 13:24
basketball749 basketball749 is offline
New Member
 
Join Date: Oct 2006
Posts: 9
basketball749 is on a distinguished road

Re: Simple Calculator in C


well ive revised my program quite a bit and it seems much simpler. i would have edited the post but i wasnt able to.

im still having some trouble on the accumulator. im getting errors there. so any help would be appriciated.

heres the code:
CPP / C++ / C Code:
#include <stdio.h>  
#include <math.h>
   
/*function prototype*/
void scan_data(char *opp, double *nump);
void do_next_op(char *op2p, double *num2p, double *accump);
   
int
main(void)
{
double num_1; /*first operand*/
char op1; /*arithmetic operator + - * / ^  */
double accum; /*sets accumulator to 0*/
double num2; /*second operand*/
char op2; /*second operator*/

 
accum=0.0;
while(!(op1 == 'q')) 
{
   /*Gets problem to be calculated*/
   scan_data(&op1, &num_1);
        
   printf("%c, %f", op1, num_1);
   printf("result so far is %f", accum);
 

   do_next_op(&op2, &num2, &accum);
 
}
return(0);
}

void scan_data(char *opp, double *nump)
{
        char op1;
        double num_1;
 
        printf("Enter arithmetic operator (+,-,*,/,^, or q to quit>");
        scanf("%c", op1);
        printf("Enter operand>");
        scanf("%lf", num_1);
}
        
        
                
do_next_op(char *op2p, double *num2p, double *accum)
{
 char op2;
        double num_2;
        double accum;

        printf("Enter arithmetic operator (+,-,*,/,^, or q to quit>");
        scanf("%c", op2);
        printf("Enter operand>");
        scanf("%lf", num_2);
 
        switch(op2){
        case '+':{
                accum = accum + num_2;
                break;
        }
        case '-':{
                accum = accum - num_2;
                break;
        }
        case '*':{
                accum = accum * num_2;
                break;
        }
        case '/':{
                accum = accum / num_2;
                break;
        }
        case '^':{
                accum = pow(accum, num_2);
                break;
        }
        case 'q':{
                break;
        }
    }
}


here are the errors when i try to compile the program

hw4.c:55: warning: conflicting types for 'do_next_op'
hw4.c:14: warning: previous declaration of 'do_next_op' was here
hw4.c: In function `do_next_op':
hw4.c:58: error: 'accum' redeclared as different kind of symbol
hw4.c:54: error: previous definition of 'accum' was here
Last edited by admin : 07-Apr-2007 at 11:36. Reason: Please insert your C code between [cpp] & [/cpp] tags
  #6  
Old 26-Oct-2006, 16:03
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: Simple Calculator in C


You didn't put a return type for do_next_op where you defined the function, whereas at the prototype it says void. The compiler assumes int if you put nothing there, so those are different and the compiler complains. It looks like you want it to be void, so make it void.

Now, in the do_next_op function there is a parameter accum, which is a pointer to a double, and then you try to declare a double variable accum. This won't work. To assign a value to the variable which the parameter accum points to, you just do this:
*accum = *accum + num_2;
Review pointers if this doesn't make sense.
  #7  
Old 26-Oct-2006, 20:05
basketball749 basketball749 is offline
New Member
 
Join Date: Oct 2006
Posts: 9
basketball749 is on a distinguished road

Re: Simple Calculator in C


thanks i made that change and everything compiles alright but the program isnt returning the values that i would like. i also cant get it to quit.

heres what i have:

CPP / C++ / C Code:
#include <stdio.h>
#include <math.h>
   
/*function prototype*/
void scan_data(char *op1p, double *num1p);
void do_next_op(char op1, double num1, double *accum);

int
main(void)
{
double num1; /*first operand*/
char op1; /*arithmetic operator + - * / ^  */
double accum; /*sets accumulator to 0*/
        

accum=0.0;
do{
        
   /*Gets problem to be calculated*/
   scan_data(&op1, &num1);

   do_next_op(op1, num1, &accum);
 
}
while(!(op1 == 'q')); 
        
return(0);
}
                
void scan_data(char *op1p, double *num1p)
{
        char op1;
        double num1;

        printf("Enter arithmetic operator (+,-,*,/,^, or q to quit and operand>");
        scanf(" %c %lf", &op1, &num1);
        printf("result so far is %c %f", &op1, &num1);
}
void do_next_op(char op1, double num1, double *accum)
{
 

        switch(op1){
        case '+':{
                *accum = *accum + num1;
                break;
        }
        case '-':{
                *accum = *accum - num1;
                break;
        }
        case '*':{
                *accum = *accum * num1;
                break;
        }
        case '/':{
                *accum = *accum / num1;
                break;
        }
        case '^':{
                *accum = pow(*accum, num1);
                break;
        }
        case 'q':{
                break;
 }
    }
}
Last edited by admin : 07-Apr-2007 at 11:37. Reason: Please insert your C code between [cpp] & [/cpp] tags
  #8  
Old 26-Oct-2006, 20:16
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: Simple Calculator in C


Your scan_data function does nothing. It reads in some values to local variables, and then deletes those variables. You forgot
CPP / C++ / C Code:
*op1p = op1;
*num1p = num1;
Or just use op1p and num1p directly in the scanf.
  #9  
Old 07-Apr-2007, 11:32
beginnercoder beginnercoder is offline
New Member
 
Join Date: Apr 2007
Posts: 2
beginnercoder is on a distinguished road

Re: Simple Calculator in C


sTill it is not working ... can u write the code properly
???
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to write a simple calculator code? Help! JavaLost Java Forum 2 24-Oct-2006 02:41
a simple question sharwar .NET Forum 3 28-Sep-2006 06:18
UpdateData - simple MFC project kobycool MS Visual C++ / MFC Forum 3 23-Oct-2005 03:45
Simple Calculator Application MOHAMMEDALI1989 Java Forum 1 06-Oct-2005 18:00
Just need one simple example or link to such example Doggonit MySQL / PHP Forum 2 31-Jul-2005 19:39

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:14.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.