PDA

View Full Version : help convert cpp - > c


eeeyaan
10-15-2005, 09:06 AM
I am currently learning how to program using cpp and find it a bit hard. I am more comfortable using c compared to this. Can someone please convert this program to c?

#include <iostream.h>
#include <conio.h>

#define MAX 10 // MAXIMUM STACK CONTENT


class stack
{

private:
int arr[MAX]; // Contains all the Data
int top; //Contains location of Topmost Data pushed onto Stack

public:
stack() //Constructor
{
top=-1; //Sets the Top Location to -1 indicating an empty stack
}

void push(int a) // Push ie. Add Value Function
{
top++; // increment to by 1
if(top<MAX)
{
arr[top]=a; //If Stack is Vacant store Value in Array
}
else
{
cout<<"STACK FULL!!"<<endl;
top--;
}
}

int pop() // Delete Item. Returns the deleted item
{
if(top==-1)
{
cout<<"STACK IS EMPTY!!!"<<endl;
return NULL;
}
else
{
int data=arr[top]; //Set Topmost Value in data
arr[top]=NULL; //Set Original Location to NULL
top--; // Decrement top by 1
return data; // Return deleted item
}
}
};


void main()
{
stack a;
a.push(3);
cout<<"3 is Pushed\n";
a.push(10);
cout<<"10 is Pushed\n";
a.push(1);
cout<<"1 is Pushed\n\n";

cout<<a.pop()<<" is Popped\n";
cout<<a.pop()<<" is Popped\n";
cout<<a.pop()<<" is Popped\n";
getch();
}

Output:
3 is Pushed
10 is Pushed
1 is Pushed

1 is Popped
10 is Popped
3 is Popped

I know the basics but I really don't know what's private and public supposed to be. Thanks to those who will help.

aman
10-15-2005, 12:17 PM
A struct in C++ is the same as a class, except that all of the members of a struct are public by default.

Converting it to C is pretty easy, as long as you compile it with a C++ compiler (in most cases, naming the file with a .cpp extension will do the trick)

Converting to standard C to be compiled with a C compiler is a bit trickier, because you cannot (easily) include functions within structs in C.


So, for a C++ compiler, but using a struct instead of a class it would be like this:

#define MAX 10 // MAXIMUM STACK CONTENT

struct stack
{
int arr[MAX]; // Contains all the Data
int top; //Contains location of Topmost Data pushed onto Stack

stack() //Constructor
{
top=-1; //Sets the Top Location to -1 indicating an empty stack
}

void push(int a) // Push ie. Add Value Function
{
top++; // increment to by 1
if(top<MAX)
{
arr[top]=a; //If Stack is Vacant store Value in Array
}
else
{
printf("STACK FULL!!\n");
top--;
}
}

int pop() // Delete Item. Returns the deleted item
{
if(top==-1)
{
printf("STACK IS EMPTY!!!\n");
return NULL;
}
else
{
int data=arr[top]; //Set Topmost Value in data
arr[top]=NULL; //Set Original Location to NULL
top--; // Decrement top by 1
return data; // Return deleted item
}
}
};

int main()
{
stack a;
a.push(3);
printf("3 is Pushed\n");
a.push(10);
printf("10 is Pushed\n");
a.push(1);
printf("1 is Pushed\n\n");

printf("%d is Popped\n", a.pop());
printf("%d is Popped\n", a.pop());
printf("%d is Popped\n", a.pop());

return 0;
}

Kaja Fumei
10-16-2005, 09:38 PM
A struct in C++ is the same as a class, except that all of the members of a struct are public by default.

Converting it to C is pretty easy, as long as you compile it with a C++ compiler (in most cases, naming the file with a .cpp extension will do the trick)

Converting to standard C to be compiled with a C compiler is a bit trickier, because you cannot (easily) include functions within structs in C.


So, for a C++ compiler, but using a struct instead of a class it would be like this:

That's not C. What you refer to as "Standard C" is the real C language. The code you just posted is still C++ (which is why it only works on C++ compilers).

The C version of that code looks like this:
#include <stdio.h>
#include <conio.h>

#define MAX 10 // MAXIMUM STACK CONTENT

typedef struct _stack
{
int arr[MAX]; // Contains all the Data
int top; //Contains location of Topmost Data pushed onto Stack
} stack;

void stack_init(stack *s)
{
s->top = -1; //Sets the Top Location to -1 indicating an empty stack
}

void stack_push(stack *s, int a) // Push ie. Add Value Function
{
s->top++; // increment to by 1
if(s->top < MAX)
{
s->arr[s->top]=a; //If Stack is Vacant store Value in Array
}
else
{
printf("STACK FULL!!\n");
s->top--;
}
}

int stack_pop(stack* s) // Delete Item. Returns the deleted item
{
if(s->top == -1)
{
printf("STACK IS EMPTY!!!\n");
return 0;
}
else
{
int data = s->arr[s->top]; //Set Topmost Value in data
s->arr[s->top]=0; // No point to this honestly
s->top--; // Decrement top by 1
return data; // Return deleted item
}
}

int main()
{
stack a;
stack_init(&a); // "Constructor"
stack_push(&a, 3);
printf("3 is Pushed\n");
stack_push(&a, 10);
printf("10 is Pushed\n");
stack_push(&a, 1);
printf("1 is Pushed\n\n");

printf(%d is Popped\n", stack_pop(&a));
printf(%d is Popped\n", stack_pop(&a));
printf(%d is Popped\n", stack_pop(&a));
getch();

return 0;
}


I just wrote this right here and therefore didn't compile it and test it. Should work fine though.

aman
10-17-2005, 12:34 AM
That's not C. What you refer to as "Standard C" is the real C language. The code you just posted is still C++ (which is why it only works on C++ compilers).

Yes, I said that :)

But, he didn't really need it in standard C, just something without the class so he could understand better.

Oh well, now he can look at it both ways.