Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
#include <iostream> 
#include <stack>  
using namespace std;
void createStack(stack <int> mystack)
{
	stack <int> ms = mystack;
	while (!ms.empty())
	{
		cout << '\t' << ms.top();
		ms.pop();
	}
	cout << '\n';
}
int main()
{
	stack <int> st;
	st.push(32);
	st.push(21);
	st.push(39);
	st.push(89);
	st.push(25);

	cout << "The stack st is: ";
	createStack(st);
	cout << "\n st.size() : " << st.size();
	cout << "\n st.top() : " << st.top();
	cout << "\n st.pop() : ";
	st.pop();
	createStack(st);
	return 0;
}


What I have tried:

i didn't don't understand so i've not tried
Posted
Updated 14-Apr-21 3:31am

Pretty much, you can't.

This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it’ll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.

In this case, you're trying to convert template code that supports built in stack constructs to a language that doesn't have stacks directly accessible by your code, and doesn;t have templates - so you'd have to create both from scratch in order to use that code, and that would be a lot, lot more work than doing your homework properly for yourself.
 
Share this answer
 
Comments
CPallini 14-Apr-21 7:47am    
Do you really would create templates, for that?
:-D
OriginalGriff 14-Apr-21 7:52am    
No. But then, I'm not doing his homework, either ... :D
Apart the boilerplate code conversion, you just need to implement a stack in C.
After a quick search, the following page magically popped up: Stack Implementation in C[^].
 
Share this answer
 
v2
You're the only one who's going to convert that code. You're not going to get away with not thinking about the problem and using someone else's code to hand in as your own.

To convert this code, you have to understand exactly what the C++ code is doing and writing equivalent C code.

Your problem is that you're going to put in more work trying to convert that code than it would be to just solve the logic problem yourself and write the C code to implement that solution.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900