473,320 Members | 1,825 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Implementing generic stack

Hi

Currently I am implementing a generic(int plus float etc) stack in C. However I am facing some problems.

stackgen.h
Expand|Select|Wrap|Line Numbers
  1. #define STACKSIZE 100
  2. #define INTEGER 1
  3. #define FLOAT 2
  4. #define STRING 3
  5. struct stackelement
  6.  {
  7.      int etype;
  8.      union
  9.       {
  10.           int i;
  11.           float f;
  12.       }element;
  13.  };
  14. struct stack
  15.  {
  16.      int top;
  17.      struct stackelement items[STACKSIZE];
  18.  };
  19. void init(struct stack *);
  20. void printtop(struct stack *);
  21. void push(struct stack *,int,void);
  22.  
stackgen.c
Expand|Select|Wrap|Line Numbers
  1. #include "stackgen.h"
  2. void init(struct stack *mycopy)
  3.  {
  4.      mycopy->top=-1;
  5.  }
  6. void printtop(struct stack *mycopy)
  7.  {
  8.      struct stackelement se;
  9.      se=mycopy->items[mycopy->top];
  10.      //printf("%d",se.etype);
  11.      switch(se.etype)
  12.       {
  13.           case INTEGER:
  14.             printf("%d\n",se.element.i);
  15.             break;
  16.           case FLOAT:
  17.             printf("%f\n",se.element.f);
  18.             break;
  19.       }
  20.  }
  21. void push(struct stack *mycopy,int etype,void *item)
  22.  {
  23.      int *intptr;
  24.      float *floatptr;
  25.      (mycopy->top)++;
  26.      mycopy->items[mycopy->top].etype=etype;
  27.      switch(etype)
  28.       {
  29.           case INTEGER:
  30.  
  31.             intptr=(int *)item;
  32.             (mycopy->items[mycopy->top].element.i)=*intptr;
  33.             break;
  34.  
  35.           case FLOAT:
  36.              floatptr=(float *)item;
  37.              (mycopy->items[mycopy->top].element.f)=*floatptr;
  38.              break;
  39.       }
  40.  
  41.  }
  42. int main()
  43.  {
  44.      struct stack my;
  45.      init(&my);
  46.  
  47.      int i=100;
  48.      float f=23.23;
  49.      push(&my,1,&i);
  50.      push(&my,2,&f);
  51.      printtop(&my);
  52.      return 0;
  53.  }
  54.  
The above code works absolutely fine. But what i need is the functionality that user enter either int or float at run time and push gets called accordingly. I do not want to ask from the user whether next input he will enter would be an int or float? How can I accomplish this?
Jun 26 '11 #1

✓ answered by donbock

When you say "I do not want to ask from the user whether next input he will enter would be an int or float" do you mean that the user will interactively type in values to be pushed onto the stack? If so, then your user input should be entered as a string that your program will parse into one of three categories: int, float, or invalid.

The Standard Library functions strtol and strtod can do the heavy lifting for you.

2 2878
donbock
2,426 Expert 2GB
When you say "I do not want to ask from the user whether next input he will enter would be an int or float" do you mean that the user will interactively type in values to be pushed onto the stack? If so, then your user input should be entered as a string that your program will parse into one of three categories: int, float, or invalid.

The Standard Library functions strtol and strtod can do the heavy lifting for you.
Jun 29 '11 #2
Thank You Donbock. I think I need to make my own function that will check the string for integer float or character.
Jun 29 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Chris Cranford | last post by:
I am in the process of implementing a stack machine virtual environment where each entry on the stack is a 32-bit value which then can reference to any memory pointer, numeric value, or alike. ...
8
by: Ben | last post by:
Hi all, I implemented a stack in C++ in 2 different ways and I'd like to know which approach is better than the other.. and if there is any difference between the two? I'd also like to know if...
3
by: KH | last post by:
I can't seem to figure this one out... I've searched MSDN and Goog, and made my best guesses to no avail,, so help would be much appreciated! public ref class T sealed : public...
13
by: Scamjunk | last post by:
I have the following code for a generic stack implementation. ---------------- void Push(void *value, void *Stack, int *top) { if(!(IsFull(Stack, top))) { (*top)++; Stack = *value; }
2
by: raghu | last post by:
What is a stack of stack? Can anyone please give me the idea to implement it.... Thanks a lot... Happy New Year Regards, Raghu
9
by: raylopez99 | last post by:
What's the best way of implementing a multi-node tree in C++? What I'm trying to do is traverse a tree of possible chess moves given an intial position (at the root of the tree). Since every...
20
by: deepak | last post by:
Hi All, In C I heard the stack grows from top to bottom and heap from bottom to top. Is it compiler depend?
4
by: Mahmoud Al-Qudsi | last post by:
Hi all, I'm working on a C# project, and at some point I'm needing to pass the same stack to multiple functions. I found out the hard way that when I poss a stack by value it doesn't actually...
14
by: Ellipsis | last post by:
Ok so I am converting to hexadecimal from decimal and I can only cout the reverse order of the hexadecimal?! How could I reverse this so its the right order? Heres my code: #include <iostream>...
6
by: ziamughal | last post by:
Dear Sir i have made a stack in which i can store the strings the whole code is below , i want to make this stack a generic stack, what i need to do, look at the code and give me some suggestions...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.