• 0

C struct expression


Question

Can someone help me on this i have no clue on how i should start this... I get this from one of my friend in college and I'm trying to learn it

 

Finish writing the definition for Value(), as described below. Your function should NOT do input or output. Don?t be concerned about results being too large or too negative (causing overflow), but DO be concerned about the possibility of division by zero.

#include <stdio.h>

struct Expression{
       int a;
       char op;
       int b;
};
typedef struct Expression EXPRESSION;
                       

// Function prototype
int Value(EXPRESSION expr);

int main(void)
{
       EXPRESSION expr1 = {3, ?*?, 7};
       int result = Value(expr1);
printf("Result is %d\n.", result};
       return 0;
}
//----------------------------------------------------------------------------
// Calculates and returns the value of an arithmetic expression of the form
// a op b, where a and b are int values, and op can be one of ?+?, ?-?, ?*?,
// or ?/?. Each struct instance holds the numbers of the expression as well as // the operator for the expression.
//----------------------------------------------------------------------------
int Value(EXPRESSION expr)
{
      

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

I'm going to answer in C#, because your title mentioned C#, however it looks like you're using C. In either case, the code is basically the same, just swap Console.WriteLine for printf if you're using C.

You can do this by using a switch statement. Switch statements are much like "if" statements, but allows you to do different things based on different values of the thing you're checking. For example:

 

int myInt = 1;

switch (myInt)
{
    case 1:
        Console.WriteLine("A");
        break;

    case 2:
        Console.WriteLine("B");
        break;

    default:
        Console.WriteLine("Default reached");
        break;
}
In this example, the letter "A" would be printed on screen because myInt is 1. If myInt was set to 2, "B" would be printed, and if myInt was set to 3, or 4, or any other number "Default reached" would be displayed on the screen.

 

You could also do this with an if...else if... else block:

 

int myInt = 1;
if (myInt == 1)
    Console.WriteLine("A");
else if (myInt == 2)
    Console.WriteLine("B");
else
    Console.WriteLine("Default reached");
This code is basically the same as a switch statement (there are differences, but don't concern yourself with them).

So, knowing that, consider how you would use these types of code with expr.op to work out your expression, and don't forget to check for division.

Hope this helps.

Link to comment
Share on other sites

  • 0

Your question couldn't be more vague; please read our sticky on asking help with assignments and post a new topic when you'll have something more specific to ask.

 

If you really don't understand any of the code you posted at all, don't rush things and try to solve a problem that involves many concepts that you haven't learned yet. Start with a basic C tutorial (yes, that's C, not C#) and work your way there. If you have to ask a question like "I have no idea where to start" then you need to start with a smaller problem.

Link to comment
Share on other sites

This topic is now closed to further replies.