This question already has answers here:
How to use an array index as an operator?
(4 answers)
Closed 6 years ago.
please look at my c++ program.
#include<stdio.h>
int main()
{
char sign[]={'+','-','/','*'};
int i,j,k,l=0;
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
for(k=0;k<=3;k++)
{
if(sign[j]!=sign[i]&&sign[k]!=sign[i]&&sign[k]!=sign[j])
{
printf("%c %c %c\n",sign[i],sign[j],sign[k],l);
}
}
}
}
return 0;
}
the output of this program is like this..
+ - /
+ - *
+ / -
+ / *
....
I want to use them between numbers...
like this..
#include<stdio.h>
int main()
{
char sign[]={'+','-','/','*'};
int i,j,k,l=0;
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
for(k=0;k<=3;k++)
{
if(sign[j]!=sign[i]&&sign[k]!=sign[i]&&sign[k]!=sign[j])
{
int l;
l=18sign[i]12sign[j]4sign[k]5;
printf("18%c12%c4%c5=%d",sign[i],sign[j],sign[k],l);
}
}
}
}
return 0;
}
I want the output like this..
18+12-4/5=29
18+12-4*5=10
18+12/4-5=16
18+12/4*5=33
.....
then what will be the code for this??
[I can't use switch tag because for that I will have to declare 24 cases. Is there any way to use the indexes as operator??]
No.
C (and C++) are static compiled languages. You can't make decisions at run-time that require different code to be present, since the compiler is no longer around to generate that code.
In C++ you can do various template tricks to make the compiler generate code for various cases at compile-time, but it's not quite clear how that would apply, here.
You have to use an expression parser in order to handle the correct operator precedence. Look at Infix Calculator Expression Parser for a possible solution.
Following may help:
int main()
{
int numbers[] = {18, 12, 4, 5};
char signs[] = {'+', '-', '/', '*'};
std::map<char, std::function<int(int, int)>> m = {
{'+', [](int a, int b){ return a + b; }},
{'-', [](int a, int b){ return a - b; }},
{'/', [](int a, int b){ return a / b; }},
{'*', [](int a, int b){ return a * b; }},
};
std::sort(std::begin(signs), std::end(signs));
do {
int res = m[signs[2]](m[signs[1]](m[signs[0]](numbers[0], numbers[1]), numbers[2]), numbers[3]);
std::cout << numbers[0] << signs[0] << numbers[1] << signs[1]
<< numbers[2] << signs[2] << numbers[3] << " = " << res << std::endl;
} while (std::next_permutation(std::begin(signs), std::end(signs)));
return 0;
}
Live demo
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct exp {
char op;
char *term;
struct exp *left;
struct exp *right;
} Exp;
Exp *make_exp2(char *str){
if(!str || !*str) return NULL;//*str == '\0' is format error.
char *mul = strrchr(str, '*');
char *div = strrchr(str, '/');
Exp *node = malloc(sizeof(*node));
if(mul == NULL && div == NULL){
node->op = '\0';
node->term = str;
node->left = node->right = NULL;
return node;
}
char *op;
op = mul < div ? div : mul;
node->op = *op;
*op = '\0';
node->left = make_exp2(str );
node->right = make_exp2(op+1);
return node;
}
Exp *make_exp(char *str){
if(!str || !*str) return NULL;//*str == '\0' is format error.
char *minus = strrchr(str, '-');
char *plus = strrchr(str, '+');
if(minus == NULL && plus == NULL)
return make_exp2(str);
char *op;
Exp *node = malloc(sizeof(*node));
op = minus < plus ? plus : minus;
node->op = *op;
*op = '\0';
node->left = make_exp(str );
node->right = make_exp(op+1);
return node;
}
int multiplication(int a, int b){
return a * b;
}
int division(int a, int b){
return a / b;
}
int addition(int a, int b){
return a + b;
}
int subtraction(int a, int b){
return a - b;
}
int calc(Exp *exp){
switch(exp->op){
case '*' :
return multiplication(calc(exp->left), calc(exp->right));
case '/' :
return division(calc(exp->left), calc(exp->right));
case '+' :
return addition(calc(exp->left), calc(exp->right));
case '-' :
return subtraction(calc(exp->left), calc(exp->right));
default :
return atoi(exp->term);
}
}
void drop_exp(Exp *exp){
if(exp){
drop_exp(exp->left);
drop_exp(exp->right);
free(exp);
}
}
int main(void) {
char expstr[128];
sprintf(expstr, "18%c12%c4%c5", '+', '-', '/');//18+12-4/5
Exp *exp = make_exp(expstr);
printf("%d\n", calc(exp));//30
drop_exp(exp);
return 0;
}
Related
This is a function for finding all Longest common sequence for X and Y sequence.
But this program is in c++ but I want to write it in C.
Is there any way to use array instead of the set?
For eg. if input is
X = < A, A, T, C, C, >
Y = < A, C, A, C, G, >
then the output should be
< A, C, C, >
< A, A, C, >
m and n are size of sequence X and Y respectively.
/* source : https://www.geeksforgeeks.org/printing-longest-common-subsequence-set-2-printing/ */
/* Returns set containing all LCS for X[0..m-1], Y[0..n-1] */
set<string> findLCS(string X, string Y, int m, int n)
{
// construct a set to store possible LCS
set<string> s;
// If we reaches end of either string, return
// a empty set
if (m == 0 || n == 0)
{
s.insert("");
return s;
}
// If the last characters of X and Y are same
if (X[m - 1] == Y[n - 1])
{
// recurse for X[0..m-2] and Y[0..n-2] in
// the matrix
set<string> tmp = findLCS(X, Y, m - 1, n - 1);
// append current character to all possible LCS
// of substring X[0..m-2] and Y[0..n-2].
for (string str : tmp)
s.insert(str + X[m - 1]);
}
// If the last characters of X and Y are not same
else
{
// If LCS can be constructed from top side of
// the matrix, recurse for X[0..m-2] and Y[0..n-1]
if (L[m - 1][n] >= L[m][n - 1])
s = findLCS(X, Y, m - 1, n);
// If LCS can be constructed from left side of
// the matrix, recurse for X[0..m-1] and Y[0..n-2]
if (L[m][n - 1] >= L[m - 1][n])
{
set<string> tmp = findLCS(X, Y, m, n - 1);
// merge two sets if L[m-1][n] == L[m][n-1]
// Note s will be empty if L[m-1][n] != L[m][n-1]
s.insert(tmp.begin(), tmp.end());
}
}
return s;
}
Here is an example for a self-made C unordered_set using arrays.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Buckets 1000
struct Node {
char *key;
struct Node *next;
};
void initNode(struct Node **node, const char *str) {
*node = (struct Node *) malloc(sizeof(struct Node));
size_t l = strlen(str);
(*node)->key = (char *) malloc(l * sizeof(char));
strncpy((*node)->key, str, l);
(*node)->next = NULL;
}
void freeNode(struct Node *node) {
if (node->next) {
freeNode(node->next);
}
free(node->key);
free(node);
}
struct Set {
struct Node *buckets[Buckets];
};
void initSet(struct Set *set) {
for (unsigned int i = 0; i < Buckets; ++i) {
set->buckets[i] = NULL;
}
}
void freeSet(struct Set *set) {
for (unsigned int i = 0; i < Buckets; ++i) {
if (set->buckets[i]) {
free(set->buckets[i]);
}
}
}
unsigned int hash(const char *str) {
unsigned int sum = 0;
for (; *str; ++str) {
sum += *str;
}
return sum % Buckets;
}
int insert(struct Set *set, const char *str) {
const unsigned int h = hash(str);
if (!set->buckets[h]) {
initNode(&set->buckets[h], str);
return 1;
}
struct Node *node = set->buckets[h];
while (node->next && strcmp(str, node->key)) node = node->next;
if (!strcmp(str, node->key)) return 0;
initNode(&node->next, str);
return 1;
}
int main() {
struct Set set;
initSet(&set);
printf("%d", insert(&set, "Text"));
printf("%d", insert(&set, "Text2"));
printf("%d", insert(&set, "Text"));
freeSet(&set);
}
I am writing a code for famous algorithm Huffman Encoding. I am getting a fatal error which turn system into blue screen and then restart. This error occurs in display_Codes which have recursive calls. The error occurs on the following lines:
display_Codes(root->l, s + "0");
display_Codes(root->r, s + "1" );
Following is the complete code.
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class HeapNode_Min {
public:
char d;
unsigned f;
HeapNode_Min *l, *r;
HeapNode_Min(char d, unsigned f)
{
this->d = d;
this->f = f;
}
~HeapNode_Min()
{
delete l;
delete r;
}
};
class Analyze {
public:
bool operator()(HeapNode_Min* l, HeapNode_Min* r)
{
return (l->f > r->f);
}
};
void display_Codes(HeapNode_Min* root, string s)
{
if(!root)
return;
if (root->d != '$')
cout << root->d << " : " << s << "\n";
display_Codes(root->l, s + "0");
display_Codes(root->r, s + "1" );
}
void HCodes(char data[], int freq[], int s)
{
HeapNode_Min *t,*r, *l ;
priority_queue<HeapNode_Min*, vector<HeapNode_Min*>, Analyze> H_min;
int a=0;
while (a<s){H_min.push(new HeapNode_Min(data[a], freq[a])); ++a;}
while (H_min.size() != 1) {
l = H_min.top(); H_min.pop();
r = H_min.top(); H_min.pop();
t = new HeapNode_Min('$', r->f + l->f);
t->r = r; t->l = l;
H_min.push(t);
}
display_Codes(H_min.top(), "");
}
int main()
{
try
{
int frequency[] = { 3, 6, 11, 14, 18, 25 }; char alphabet[] = { 'A', 'L', 'O', 'R', 'T', 'Y' };
int size_of = sizeof(alphabet) / sizeof(alphabet[0]);
cout<<"Alphabet"<<":"<<"Huffman Code\n";
cout<<"--------------------------------\n";
HCodes(alphabet, frequency, size_of);
}
catch(...)
{
}
return 0;
}
You never set l or r to nullptr, but your code relies on the pointers being either valid or nullptr:
void display_Codes(HeapNode_Min* root, string s)
{
if(!root)
return;
if (root->d != '$')
cout << root->d << " : " << s << "\n";
display_Codes(root->l, s + "0");
display_Codes(root->r, s + "1" );
}
Pass a root with no left and no right node, then neither root->l nor root->r have a value that you could use for anything. Passing them to the next recursion lets you dereference them which invokes undefined behavior.
To fix that you need to initialize the pointers, eg in the constructor:
HeapNode_Min(char d, unsigned f) : d(d),f(f),l(nullptr),r(nullptr) { }
Also your class does not follow the rule of 3/5/0.
Hello Im new to programming and I'm trying to make a RPN calculator in Cpp. I sort of understand how it works but i dont know how to code it. So after searching on youtube i found a video of someone with a Stack calulator im planning to use on my project. I copied everything the program runs but it doesnt display the desired result. I debugged on Visual Studio and it gives me the error {data=0xcccccccccccccccc top=-858993460 size=-858993460 }. The program keeps running but doesnt do what it is supposed to do. any help would be appreciate it.
This is the firs file that defines the stack
#pragma once
// arraystack.h
#define STACK_H
template <class T>
class stack
{
private:
T* data; // Pointer.
int top;
int size;
void resize();
bool needToResize();
public:
stack()
{
size = 5;
data = new T[size];
top = 0;
}
void push(T item);
T peek();
T pop();
bool isEmpty();
};
//#include "arraystack.cpp"
#include <stdexcept> // para manejar la excepcion de fuera de rango.
template <class T>
void stack<T>::push(T item)
{
if (needToResize())
resize();
data[top] = item;
top++;
}
template < class T >
T stack<T>::peek()
{
if (top <= 0)
throw std::out_of_range("Attempting to peek an empty stack.");
return data[top - 1];
}
template<class T>
T stack<T>::pop()
{
if (top <= 0)
throw std::out_of_range("Attempting to pop an empty stack.");
top--;
return data[top];
}
template <class T>
bool stack<T>::needToResize()
{
return (top == size);// si top es igual al size entonce sse necesita resize.
}
template <class T>
void stack<T>::resize()
{
T* newdata = new T[2 * size];
for (int i = 0; i < size; i++)
newdata[i] = data[i];
data = newdata;
size *= 2;
}
template <class T>
bool stack<T>::isEmpty()
{
return (top == 0);
}
This is the second file that defines the calculator
#pragma once
// calculator.h
#include <iostream>
#include <string>
#include "arraystack.h"
using namespace std;
// check if char is a digit.
bool isDigit(char c)
{
return (c <= '0' && c <= '9'); // valor unicode de los numeros en 48 empieza 0.
}
//check if char is an operator
bool isOp(char c)
{
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')' || c == '^');
}
// determine the precedence of an operator
int getPrecendence(char c)
{
switch (c)
{
case '+':
case '-': return 1;
case '*':
case '/': return 2;
case '(':
case ')': return 3;
default: return -1;
}
}
// evaluate an arithmetic expression.
int operate(int val1, int val2, char op)
{
if (op == '+') return val1 + val2;
if (op == '-') return val1 - val2;
if (op == '*') return val1 * val2;
return val1 / val2; // se retorna esta porque es la unica op left. (sin expo)
}
//evaluate a string.
int evaluate(string s)
{
stack<int> vals;
stack<char> ops;
int val = 0;
int pos = 0;
while (pos < s.length())
{
char spot = s[pos];
if (isDigit(spot))
{
val = (val * 10) + (int)(spot - '0');
}
else if (isOp(spot))
{
if (spot == '(')
{
ops.push(spot);
val = 0;
}
else if (vals.isEmpty())
{
vals.push(val);
ops.push(spot);
val = 0;
}
else if (spot == ')')
{
vals.push(val);
while (ops.peek() != '(')
{
spot = ops.pop();
val = vals.pop();
int prev = vals.pop(); // previous on the stack
val = operate(prev, val, spot);
vals.push(val);
}
ops.pop();
vals.pop();
}
else
{
char prev = ops.peek();
if (getPrecendence(spot) > getPrecendence(prev))
{
vals.push(val);
ops.push(spot);
val = 0;
}
else
{
int prevval = vals.pop();
int prevop = ops.pop();
prevval = operate(prevval, val, prevop);
vals.push(prevval);
ops.push(spot);
val = 0;
}
}
}
pos++;
}
while (!ops.isEmpty())
{
int prev = vals.pop();
char spot = ops.pop();
val = operate(prev, val, spot);
}
return val;
}
This is the file that tests
// arraystack.cpp (the tester class)
#include <iostream>
#include "calculator.h"
#include <string>
using namespace std;
int main()
{
cout << evaluate("3 + 4 + 5") << endl; // expected result 12
cout << evaluate("3 * 4 + 5") << endl; // expected result 17
cout << evaluate("3 + 4 * 5") << endl; // expected result 23
cout << evaluate("(3 +4) * 5") << endl; // expected result 35
system("pause");
return 0;
}
The results i get
Results after running program
The errors showing in the visual studio debugger
Visual Studio Debbuging screen
I have no idea of whats going on. Any help would be appreciate it.
The output i get:
-1667666
-1667666
-1667666
-9658676
Press any key to continue . . .
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hi i am trying to write a palindrome class but am getting the wrong results.
I need to create a Palindrome class and return whether the phrase is a Palindrome.
Here is my code.
Palindrome.h:
#ifndef PALINDROME_H
#define PALINDROME_H
#include <iostream>
#include<cstring>
using namespace std;
class Palindrome{
private:
char str[1024];
char s1[1024];
char s2[1024];
int a;
int b;
public:
Palindrome(char s2[1024], int a, int b)
{
s2[1024] = { 0 };
a = 0;
b = 0;
}
void removeNonLetters(char str[]);
void lowerCase(char s1[]);
bool isPalindrome(char s2[], int a, int b);
}; // End of class definition
#endif
Palindrome.cpp:
#include "Palindrome.h"
void Palindrome::removeNonLetters(char str[])
{
char s1[1024] = { 0 };
int j = 0;
int l1 = strlen(str);
for (int i = 0; i < l1; i++)
{
if (str[i] <= '9' && str[i] >= '0')
{
s1[j++] = str[i];
}
else if ((str[i] >= 'A' && str[i] <= 'Z')
|| (str[i]) >= 'a' && str[i] <= 'z')
{
s1[j++] = str[i];
}
}
cout << s1 << endl;
}
void Palindrome::lowerCase(char s1[])
{
char s2[1024] = { 0 };
int l2 = strlen(s1);
int g = 0;
for (int i = 0; i < l2; i++)
{
if (s1[i] >= 'a' && s1[i] <= 'z')
{
s2[g++] = s1[i];
}
else if (s1[i] >= 'A' && s1[i] <= 'Z')
{
s2[g++] = s1[i] + 32;
}
}
cout << s2 << endl;
}
bool Palindrome::isPalindrome(char s2[], int a, int b)
{
if (a >= b)
return true;
cout << "Yes" << endl;
if (s2[a] != s2[b])
return false;
else
return isPalindrome(s2, a + 1, b - 1);
cout << "No" << endl;
}
Main.cpp:
#include "Palindrome.h"
int main()
{
char str[1024] = { 0 };
char s1[1024] = { 0 };
char s2[1024] = { 0 };
cout << "input a string:" << endl;
cin.getline(str, sizeof(str));
Palindrome removeNonLetters(char str[]);
Palindrome lowerCase(char s1[]);
int length = strlen(s2);
Palindrome isPalindrome(s2, 0, length - 1);
return 0;
}
You teacher may not like this, but this is how we do it in the real world.
First things first, reach for the standard library:
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
A function to strip non-alpha characters from a string:
std::string strip(std::string s)
{
s.erase(std::remove_if(std::begin(s),
std::end(s),
[](auto c) { return !std::isalpha(c); }),
std::end(s));
return s;
}
A function to transform a string to lower case:
std::string to_lower(std::string s)
{
std::transform(std::begin(s),
std::end(s),
std::begin(s),
[](auto c) { return std::tolower(c); });
return s;
}
A function to check that a string is the same in reverse as it is forwards:
bool is_palindrome(const std::string& s)
{
return std::equal(std::begin(s), std::end(s),
std::rbegin(s), std::rend(s));
}
Putting it all together in a test:
int main()
{
auto word = std::string("a!b B <>A");
if (is_palindrome(to_lower(strip(word)))) {
std::cout << "palindrome" << std::endl;
}
else {
std::cout << "not palindrome" << std::endl;
}
return 0;
}
Complete listing:
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
std::string strip(std::string s)
{
s.erase(std::remove_if(std::begin(s),
std::end(s),
[](auto c) { return !std::isalpha(c); }),
std::end(s));
return s;
}
std::string to_lower(std::string s)
{
std::transform(std::begin(s),
std::end(s),
std::begin(s),
[](auto c) { return std::tolower(c); });
return s;
}
bool is_palindrome(const std::string& s)
{
return std::equal(std::begin(s), std::end(s),
std::rbegin(s), std::rend(s));
}
int main()
{
auto word = std::string("a!b B <>A");
if (is_palindrome(to_lower(strip(word)))) {
std::cout << "palindrome" << std::endl;
}
else {
std::cout << "not palindrome" << std::endl;
}
return 0;
}
There are many things wrong with your code. I hope these pointers help:
You should be using std library.
Why does the constructor for the class take any parameters? Nothign uses them
Why are there any member variables? Nothing uses them.
Why are the functions in a class at all? They're just functions - they should be in a functions library or similar.
The functions just write to cout so are useless.
Your main function doesn't even seem to call the functions correctly.
I tried this:
char str[1024] = { 0 };
cout << "input a string:" << endl;
cin.getline(str, sizeof(str));
int length = strlen(str);
Palindrome a(str,0, length);
a.removeNonLetters(str);
a.lowerCase(str);
a.isPalindrome(str, 0, length - 1);
cin.getline(str, sizeof(str));
return 0;
I don't get the exception but get the following output:
input a string:
EVIL rats on no star **** live
EVILratsonnostarlive
evilratsonnostarlive
Yes
However this works too:
input a string
hello
hello
hello
Yes
So the first two functions seem to work (if removing spaces was also intentional) but the third does not.
I am trying to call a function for my stack class. If I have all of the functions within the main file the project works, however, when called from the class it says the the Error: identifier "function name" is undefined. I think it is a simple syntax error, but i can't find it.
main.cpp
#include<iostream>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "stack.h"
#define MAX 10
#define EMPTY -1
struct stack
{
char data[MAX];
int top;
};
int mystack::isempty(struct stack *s)
{
return (s->top == EMPTY) ? 1 : 0;
}
void mystack::emptystack(struct stack* s)
{
s->top=EMPTY;
}
void mystack::push(struct stack* s,int item)
{
if(s->top == (MAX-1))
{
printf("\nSTACK FULL");
}
else
{
++s->top;
s->data[s->top]=item;
}
}
char mystack::pop(struct stack* s)
{
char ret=(char)EMPTY;
if(!isempty(s))
{
ret= s->data[s->top];
--s->top;
}
return ret;
}
void mystack::display(struct stack s)
{
while(s.top != EMPTY)
{
printf("\n%d",s.data[s.top]);
s.top--;
}
}
int isoperator(char e)
{
if(e == '+' || e == '-' || e == '*' || e == '/' || e == '%' || e == '^')
return 1;
else
return 0;
}
int priority(char e)
{
int pri = 0;
if(e =='%' || e == '^')
pri = 3;
else
{
if (e == '*' || e == '/' || e =='%')
pri = 2;
else
{
if(e == '+' || e == '-')
pri = 1;
}
}
return pri;
}
void infix2postfix(char* infix, char * postfix, int insertspace)
{
char *i,*p;
struct stack X;
char n1;
emptystack(&X); // any time a class like this is called it says Error: identifier "emptystack"
// is undefined
i = &infix[0];
p = &postfix[0];
while(*i)
{
while(*i == ' ' || *i == '\t')
{
i++;
}
if( isdigit(*i) || isalpha(*i) )
{
while( isdigit(*i) || isalpha(*i))
{
*p = *i;
p++;
i++;
}
if(insertspace)
{
*p = ' ';
p++;
}
}
if( *i == '(' )
{
push(&X,*i);
i++;
}
if( *i == ')')
{
n1 = pop(&X);
while( n1 != '(' )
{
*p = n1;
p++;
if(insertspace)
{
*p = ' ';
p++;
}
n1 = pop(&X);
}
i++;
}
if( isoperator(*i) )
{
if(mystack::isempty(&X))
push(&X,*i);
else
{
n1 = pop(&X);
while(priority(n1) >= priority(*i))
{
*p = n1;
p++;
if(insertspace)
{
*p = ' ';
p++;
}
n1 = pop(&X);
}
push(&X,n1);
push(&X,*i);
}
i++;
}
}
while(!isempty(&X))
{
n1 = pop(&X);
*p = n1;
p++;
if(insertspace)
{
*p = ' ';
p++;
}
}
*p = '\0';
}
int main()
{
char in[50],post[50];
strcpy(&post[0],"");
printf("Enter Infix Expression : ");
fflush(stdin);
gets(in);
infix2postfix(&in[0],&post[0],1);
printf("Postfix Expression is : %s\n",&post[0]);
return 0;
}
stack.h
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
using namespace std;
class mystack
{
public:
int isempty(struct stack *s);
void emptystack(struct stack* s);
void push(struct stack* s,int item);
char pop(struct stack* s);
void display(struct stack s);
};
I am using visual studio if that helps.
EDIT: added comment for clarity.
Thanks,
Ryan
At a cursory glance, this function:
void emptystack(struct stack* s)
{
s->top=EMPTY;
}
Is missing the scope operator (::), so you probably intended to write it as:
void mystack::emptystack(struct stack* s)
{
s->top=EMPTY;
}
I'm not sure if that's your problem though, since "I'm trying to call a function" is a bit vague. You might want to narrow down precisely where the error is occurring, then edit your question with additional information.
Edit: In looking at your implementation a bit more, I'm not sure why you created the mystack class at all. It looks like you just want to define a bunch of functions that operate on your stack struct, which doesn't require a class definition. If you want to do it this way for some unusual reason, then you'll have to instantiate a mystack object before you can call its member functions. Something of the nature:
mystack * myStackObj = new mystack();
myStackObj->emptystack(&X);
Though I'm not sure why you would want to do this. The other alternative is to roll your stack struct into the class instead, either by making the whole struct a member of the class or by simply adding data and top to the class. Then if you instantiated a mystack object it would have the data of the stack and could call methods on its own data. I'd also suggest looking at a tutorial/documentation/book related to C++ classes and their usage. Here's one, but there are undoubtedly plenty of others.