Page 1 of 4 123 ... LastLast
Results 1 to 10 of 35

Thread: Here's a C problem

  1. #1
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Talking Here's a C problem

    Write a C program for solving a term like a+b*c-a where the term may be of a maximum string length of 50, but only combinations of +,-,/,* and a,b,c,d are used. You can enter the term into the program followed by the values for a,b,c,d, then the result is displayed. (Hint: later)
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  2. #2
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default Re: Here's a C problem

    Here is a simple solution without error checking or precedence taken into consideration.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    double calculate(double a, double b, char oper);
    
    int main() {
    
    	char terms[50];
    	char ch, oper;
    	int a, b, c, d, i;
    
    	double result;
    
    	printf("enter the expression: ");
    	gets(terms);
    
    	printf("a = ");
    	scanf("%d", &a);
    
    	printf("b = ");
    	scanf("%d", &b);
    
    	printf("c = ");
    	scanf("%d", &c);
    
    	printf("d = ");
    	scanf("%d", &d);
    
    	i = 0;
    	while(terms[i] != '\0') {
    
    		ch = terms[i];
    
    		switch(ch) {
    
    			case 'a':
    				if(i == 0)
    					result = a;
    				else
    					result = calculate(result, a, oper);				
    			break;
    
    			case 'b':
    			if(i == 0)
    					result = b;
    				else
    					result = calculate(result, b, oper);				
    			break;	
    
    			case 'c':
    				if(i == 0)
    					result = c;
    				else
    					result = calculate(result, c, oper);				
    			break;				
    
    			case 'd':
    				if(i == 0)
    					result = d;
    				else
    					result = calculate(result, c, oper);
    			default:
    				if(ch == '+' || ch == '-' || ch == '*' || ch == '/')
    					oper = ch;
    				else
    					printf("invalid term: %c\n", ch);				
    
    		}
    		++i;
    	}
    
    	printf("\nThe result of the epression is: %.2f\n\n",result);
    	system("pause");
    	return 0;
    }
    
    
    double calculate(double a, double b, char oper) {
    
    	switch(oper) {		
    		case '+':
    			return a+b;
    		case '-':
    			return a-b;
    		case '/':
    			return a/b;
    		case '*':
    			return a*b;
    		default:
    			return 0;
    
    	}
    }

  3. #3
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default Re: Here's a C problem

    Seen father!
    I going to test it right away, U have skills.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  4. #4
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default Re: Here's a C problem

    @leoandru,
    I no know how you do it, but it works.

  5. #5
    Join Date
    Mar 2004
    Posts
    123
    Rep Power
    0

    Default Re: Here's a C problem

    Quote Originally Posted by crosswire
    Write a C program for solving a term like a+b*c-a where the term may be of a maximum string length of 50, but only combinations of +,-,/,* and a,b,c,d are used. You can enter the term into the program followed by the values for a,b,c,d, then the result is displayed. (Hint: later)
    Hey crosswire, why not take it up a notch and solve expressions like a+ (b*c-a)*(c/b)
    There is no wrong or right, only consequencies to action

    "It is the duty of man to make his knowledge so complete in life, so as to make it impossible for any other to take advantage of him" - Marcus Garvey

  6. #6
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default Re: Here's a C problem

    I never expected someone to work it out so soon

    Here is a Hint for all who did lost:
    * Look at it like a soso maths problem, and not as a C programming problem.
    * Given a+b+c+d
    * A maths student would work out (a+b) first so that the term is now e+c+d where e = a+b, and so on

    This is one method

    given a charcter string,
    a+b+c+d*c-a/b
    a+b+c+ e -a/b
    a+b+c+e-a/b
    a+b+c+e- f
    a+b+c+e-f
    a+b+c+e-f
    g +c+e-f
    g+c+e-f
    h +e-f
    h+e-f
    i -f
    i-f
    j

    In each step the precedence operation is found and worked out eg (d*c) = e then the string is updates by replacing "d*c" with " e ". Finally the white spaces are eaten so we have a new equation. Now repeat for the next precedence operator an so on.

    Hey crosswire, why not take it up a notch and solve expressions like a+ (b*c-a)*(c/b)
    Ok soon, and later add cos, sin sqrt
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  7. #7
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default Re: Here's a C problem

    Update
    Write a C/C++ program for solving a term like "a+(b*c-a)*(c/b)" where the term may be of a maximum string length of 50, but only combinations of +,-,/,* and a,b,c,d and brackets are used. The precedence of the operators are taken into consideration. You can enter the term into the program followed by the values for a,b,c,d, then the result is displayed.

    Our 1st test case is
    a+(b*c-a)*(c/b)

    Note : Only idlers need reply.
    Last edited by crosswire; Apr 4, 2005 at 03:42 PM. Reason: update: operation precedence is now a must
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  8. #8
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default Re: Here's a C problem

    Quote Originally Posted by crosswire
    Seen father!
    I going to test it right away, U have skills.
    it wasn't perfect i wanted to add precedence and better error checking but i didn't got the time for it. Even though while writing it it seems like i was doing work (cause i was writing code) but sooner or later somone would have recognized that it has nothing to do with the project im working on, so i just made it work. Im a bit of a perfectionist so i didn't like the code actually.

  9. #9
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default Re: Here's a C problem

    Quote Originally Posted by crosswire
    Note : Only idlers need reply.
    LOL, ok. i'll be idling @ luch time.. so i can enter!!

  10. #10
    Join Date
    Mar 2004
    Posts
    123
    Rep Power
    0

    Default Re: Here's a C problem

    I did something similar to that in my Analysis of Programming Language (APL) class at UTECH. We were suppose to write a code the check the validity of an expression, whether it be prefix, postfix or infix. The code should take into consideration the inclusion of bracket so we had to take into consideration the precedence of operators. That program got me into a lot of trouble and taught me a valuable lesson
    There is no wrong or right, only consequencies to action

    "It is the duty of man to make his knowledge so complete in life, so as to make it impossible for any other to take advantage of him" - Marcus Garvey

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •