• 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
https://www.neowin.net/forum/topic/1217425-c-struct-expression/
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.

  • 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.

This topic is now closed to further replies.
  • Posts

    • At least on Mint... Debian is the 'backup' option (i.e. LMDE6) where as regular Mint is Ubuntu. also, Debian tends to play stuff a bit more conservatively than Ubuntu (at least based on Mint) with kernel and programs. also, even Mint's 'Update Manager' is more refined in regular Mint vs LMDE6 and installing NVIDIA driver is easier on regular Mint etc. not only that but LMDE6 only comes with Cinnamon which is a shame as they should offer the option for Xfce. I realize playing around with it in a VM one can install Xfce on it but it would be nice to have a more official Xfce release like how the regular Mint does. so while I heard Debian is a little snappier than Ubuntu (so better on some level), Ubuntu is probably the overall wiser choice unless people don't mind using a bit older stuff etc. but honestly, with some tweaks I would not mind if they changed the main Mint to Debian base instead of Ubuntu. but that's probably not likely to happen unless Ubuntu really does something the Mint team does not like. p.s. but I was building Super Mario 64 (native PC port on Linux) a while ago and it does not like regular Mint but works fine on LMDE6. but at that point I just transferred the final binary out of the VM back to my regular Mint and it works fine.
    • Excel is getting a highly requested PivotTable feature by Usama Jawad Microsoft Excel is one of the most popular software out there, both in the enterprise and personal space. It has a variety of use-cases including data analysis, data crunching, visualizations, and even planning and organization assistance. Microsoft regularly updates Excel with new features, and now, it is introducing a notable feature for PivotTables. Prior to today, PivotTables required a manual refresh from the user whenever new data was inserted into them. This wasn't a particularly complicated process, but it was tedious, so based on significant user feedback, Microsoft has decided to implement auto-refresh capabilities in PivotTables. The good thing is that Auto Refresh is enabled for all new PivotTables by default, but you can choose to disable it by selecting a PivotTable, navigating to the PivotTable Analyze tab, and then clicking on Auto Refresh. It is important to note that Auto Refresh is applicable on a per data source level, which means that the feature's state (on or off) will apply to all PivotTables derived from that source. Additionally, if Auto Refresh is disabled or a PivotTable is unable to synchronize, a message at the bottom of your workbook will say "PivotTable Refresh Needed". Once you click on it, all outdated PivotTables will refresh. Finally, you should keep in mind that external and asynchronous data sources do not support Auto Refresh and that the feature may become unavailable when a co-author is using an older version of Excel or if you are playing around with volatile functions like RAND() and NOW() in your data source. Auto Refresh for PivotTables is available right now in the Beta Channel for Excel for Windows version 2506 (Build 19008.2000) or later, and Excel for Mac version 16.99 (Build 250616106) or later. If you don't see it yet, it's better to wait as new capabilities are often rolled out in a staggered manner.
    • Hell of a legacy, hell of a bad person...
    • Man, what a legend. I know a lot of you don't like social media but the outpouring on the platforms has been really something else.
    • No download links, please.
  • Recent Achievements

    • First Post
      Electronic Person earned a badge
      First Post
    • Week One Done
      CyberCeps666 earned a badge
      Week One Done
    • Very Popular
      d4l3d earned a badge
      Very Popular
    • Dedicated
      Stephen Leibowitz earned a badge
      Dedicated
    • Dedicated
      Snake Doc earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      628
    2. 2
      ATLien_0
      240
    3. 3
      Xenon
      163
    4. 4
      neufuse
      126
    5. 5
      +FloatingFatMan
      124
  • Tell a friend

    Love Neowin? Tell a friend!