Hi
Currently I am implementing a generic(int plus float etc) stack in C. However I am facing some problems.
stackgen.h -
#define STACKSIZE 100
-
#define INTEGER 1
-
#define FLOAT 2
-
#define STRING 3
-
struct stackelement
-
{
-
int etype;
-
union
-
{
-
int i;
-
float f;
-
}element;
-
};
-
struct stack
-
{
-
int top;
-
struct stackelement items[STACKSIZE];
-
};
-
void init(struct stack *);
-
void printtop(struct stack *);
-
void push(struct stack *,int,void);
-
stackgen.c -
#include "stackgen.h"
-
void init(struct stack *mycopy)
-
{
-
mycopy->top=-1;
-
}
-
void printtop(struct stack *mycopy)
-
{
-
struct stackelement se;
-
se=mycopy->items[mycopy->top];
-
//printf("%d",se.etype);
-
switch(se.etype)
-
{
-
case INTEGER:
-
printf("%d\n",se.element.i);
-
break;
-
case FLOAT:
-
printf("%f\n",se.element.f);
-
break;
-
}
-
}
-
void push(struct stack *mycopy,int etype,void *item)
-
{
-
int *intptr;
-
float *floatptr;
-
(mycopy->top)++;
-
mycopy->items[mycopy->top].etype=etype;
-
switch(etype)
-
{
-
case INTEGER:
-
-
intptr=(int *)item;
-
(mycopy->items[mycopy->top].element.i)=*intptr;
-
break;
-
-
case FLOAT:
-
floatptr=(float *)item;
-
(mycopy->items[mycopy->top].element.f)=*floatptr;
-
break;
-
}
-
-
}
-
int main()
-
{
-
struct stack my;
-
init(&my);
-
-
int i=100;
-
float f=23.23;
-
push(&my,1,&i);
-
push(&my,2,&f);
-
printtop(&my);
-
return 0;
-
}
-
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?
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
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.
Thank You Donbock. I think I need to make my own function that will check the string for integer float or character.
Sign in to post your reply or Sign up for a free account.
Similar topics
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.
...
|
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...
|
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...
|
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;
}
|
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
|
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...
|
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?
|
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...
|
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>...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |