I'm supposed to make a program using getchar() to input the operation entered, then return the result of the operation after extracting the operands and the operator and calculating the result.

if you enter 15 + 30

the result should be 45

or 15+30=45

Also using user-defined functions to do the calculations for each of the different operands used (+, -. *, /, %)
I was thinking of using functions like isdigit() or ispunct() to extract the operands and operator

How should I go about doing that?

Here's what I have so far.

#include<stdio.h>
#include<ctype.h>

int add (int a, int b);
int subtract (int a, int b);
int multiply (int a, int b);
double divide (int a, int b);
int mod (int a, int b);

int main(void)
{
	
	printf("Enter expression to calculate.\n");
	getchar();

}

int add (int a, int b)
{
	int c;
	c=a+b;
	return c;
}

int subtract (int a, int b)
{
	int c;
	c=a-b;
	return c;
}

int multiply (int a, int b)
{
	int c;
	c=a*b;
	return c;
}

double divide (int a, int b)
{
	double c;
	c=(1.0*a)/b;
	return c;
}

int mod (int a, int b)
{
	int c;
	c=a%b;
	return c;
}

I'm supposed to make a program using getchar() to input the operation entered, then return the result of the operation after extracting the operands and the operator and calculating the result.

I was thinking of using functions like isdigit() or ispunct() to extract the operands and operator

That seems to be a reasonable direction to head. Head that way and see how far you can get.

By the way, leave the computer alone until:
1) You write an equation on paper (15+30=45)
2) You look at and process the equation character by character on paper. In other words
What do you do with the 1
What do you do with the 5
etc.
3) You write down your process step by step
4) The process gives you a correct solution.

Then you have the information to start coding the process and it will go very easy.

WaltP>> By the way, leave the computer alone until:
I concur.. :)

One other pointer I can give is to google up a bit on pre/post/in-fix notations and their use.

okay, I took that advice, I'm thinking I could use isdigit() to extract the digits of the operands then somehow make the digits into numbers, store them into a and b, and use the operand to link to one of the functions and do the calculation, could someone give me pointers on how to do that?

okay, I took that advice,

Mine? Or thekashyap's?

You've got the isdigit() part right. What functions will convert a string into an integer?

If your need is only to support +, -, / & *, one at a time then you're on the correct path. But if you need to support say 3 + 4 / 5, then you should readup the items I suggested.

Mine? Or thekashyap's?

You've got the isdigit() part right. What functions will convert a string into an integer?

My guess is: Yours. :)

This article has been dead for over six months. Start a new discussion instead.