Hello,

so the problem was to create a program that took input from command line to function as a rudimentary calculator: e.g 3 + 2, 5 * 4, 2/3 and so on. We were to take those arguments and pass them and a pointer to a function in to the "calc" function and do the appropriate task and print the result. I attempted casting the functions as int and returning the values, and I have attempted to use pointers for the parameters but I have been unable to get program to work properly. When it's passing values into the calc function I get a segmentation fault. Any ideas what I might be doing wrong? I've never been the strongest coder, and most of my experience is in Java. Any help would be greatly appreciated!

#include <stdio.h>
void calc(void(* f)(int, int), int n1, int n2);
void add(int n1, int n2);
void sub(int n1, int n2);
void mult(int n1, int n2);
void div(int n1, int n2);

int main(int num1, char op, int num2) {
	if 	(strcmp(op, "+") == 0)
		calc(add, num1, num2);
	else if (strcmp(op, "+") == 0)
		calc(sub, num1, num2);
	else if (strcmp(op, "+") == 0)
		calc(mult, num1, num2);
	else if (strcmp(op, "+") == 0)
		calc(div, num1, num2);
	
	printf("Nothing done!\n");
	return 0;
}

void calc(void(* f)(int, int), int n1, int n2) {
	(*f)(n1, n2);
}

void add(int n1, int n2) {
	int total = n1 + n2;
	printf("%d", total);
}
void sub(int n1, int n2) {
	int total = n1 - n2;
	printf("%d", total);
}
void mult(int n1, int n2) {
	int total = n1 * n2;
	printf("%d", total);
}
void div(int n1, int n2) {
	int total = n1 / n2;
	printf("%d", total);
}
}

Recommended Answers

All 4 Replies

> int main(int num1, char op, int num2)
Read your book again. You can't just make up an interface for main.

It's int main ( int argc, char *argv[] ) argc tells you how many args there are
each argv[i] (from 0 to argc-1) is a string parameter.

Quick question can you do something of this sort ? Will help you to simplify the code ?

void calc()
{
      // Check which operation is to be done
      // Then call the appropriate function
}

Check this link for more information on function pointers
http://www.cprogramming.com/tutorial/function-pointers.html

Quick question can you do something of this sort ? Will help you to simplify the code ?

void calc()
{
      // Check which operation is to be done
      // Then call the appropriate function
}

Check this link for more information on function pointers
http://www.cprogramming.com/tutorial/function-pointers.html

I *think* I can? This is the text of the question as provided to me:

Starting with the code in argc-argv.c and ptr-func.c, write a simple calculator utility that performs four basic operations: +. -. /, and * on two operands. The parameters and the operation should be provided as command line arguments. If you call the utility calc, then a sample command line invocation may be:

> calc 3 + 6 9

Implement the calculator in such a way that there is one function calc(...) that takes a pointer to an actual function that can perform the operation (add(), sub(), mult(), or div()), and two operands.

So most likely I was reading the problem wrong originally and I could skip on the main function, but would void calc() accept command line arguments in the same way?

So after some google magic and finding more examples on similar programs, I am pretty much there. The program now adds, subtracts, and divides just fine. But for some strange reason it won't multiply.
Haven't figured it out yet, but here is the revised code:
edit: For some reason '*' works just fine in Eclipse, but when I compile using gcc in terminal only the '*' doesn't work. Should be good enough for the teacher though, so I'm calling it done. Thanks for the suggestions!

#include <stdio.h>
void calc(void(* f)(int, int), int n1, int n2);
void add(int n1, int n2);
void sub(int n1, int n2);
void mult(int n1, int n2);
void div(int n1, int n2);

int main(int argc, char *argv[]) {
	int num1 = atoi(argv[1]);
	int num2 = atoi(argv[3]);
	
	if (argv[2][0] == '+')
		calc(add, num1, num2);
	else if (argv[2][0] == '-')
		calc(sub, num1, num2);
	else if (argv[2][0] == '*')
		calc(mult, num1, num2);
	else if (argv[2][0] == '/')
		calc(div, num1, num2);
	else
		printf("Nothing done!\n");
	return 0;
}

void calc(void(* f)(int, int), int n1, int n2) {
	(*f)(n1, n2);
}

void add(int n1, int n2) {
	int total = n1 + n2;
	printf("%d\n", total);
}
void sub(int n1, int n2) {
	int total = n1 - n2;
	printf("%d\n", total);
}
void mult(int n1, int n2) {
	int total = n1 * n2;
	printf("%d\n", total);
}
void div(int n1, int n2) {
	int total = n1 / n2;
	printf("%d\n", total);
}
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.