C++栈和递归实验
Posted 康小庄
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++栈和递归实验相关的知识,希望对你有一定的参考价值。
1. 利用栈的存储原理,补充完成数制转换程序
运行结果如图所示:
完整代码
#define _CRT_SECURE_NO_WARNINGS
#include"stdio.h"
#include"stdlib.h"
#define null 0
#define n 10
struct stack
int* base;
int* top;
int stacksize;
;
void initstack(struct stack* s)
s->base = (int*)malloc(20 * sizeof(int));
if (!s) exit(0);
s->top = s->base;
s->stacksize = 20;
return;
void push(struct stack* s, int e)
if (s->top - s->base >= s->stacksize)
s->base = (int*)realloc(s->base, (s->stacksize + n) * sizeof(int));
if (!s->base) exit(0);
s->top = s->base + s->stacksize;
s->stacksize += n;
*(s->top)++ = e;
return;
void pop(struct stack* s)
int e;
if (s->top == s->base) return;
e = *--s->top; printf("%d", e);
return;
int StackEmpty(struct stack* s)
if (s->top == s->base) return 1;
else return 0;
void conversion(int N) //10进制转换成2进制
stack s;
initstack(&s);
do push(&s, N & 1);
while (N >>= 1);
while (!StackEmpty(&s))
pop(&s);
int main()
int x;
printf("\\n请输入一个十进制数据:");
scanf("%d", &x);
printf("\\n 转换成二进制为: ", x);
conversion(x);
printf("\\n");
运行截图
2. 实现汉诺塔算法
运行结果如图所示:
完整代码
#define _CRT_SECURE_NO_WARNINGS
#include"stdio.h"
#include"stdlib.h"
void move(char x,int n,char y)
printf("第 %d 个 %c-->%c\\n",n,x,y);
void hanoi(int n,char x,char y,char z)
if (n == 1)move(x, 1, y);
else
hanoi(n - 1, x, z, y);
move(x, n, z);
hanoi(n - 1, y, x, z);
int main()
int m;
printf("请输入方块数目:");
scanf("%d",&m);
hanoi(m,'X','Y','Z');
运行截图
3. 写一个程序将读入的一个以“@”为结束符的字符序列逆序输出,如输入“asdf@”,则输出“fdsa”。要求使用栈结构完成。
运行结果如图所示:
完整代码
#define _CRT_SECURE_NO_WARNINGS
#include"stdio.h"
#include"stdlib.h"
#define null 0
#define n 10
struct stack
int* base;
int* top;
int stacksize;
;
void initstack(struct stack* s)
s->base = (int*)malloc(20 * sizeof(int));
if (!s) exit(0);
s->top = s->base;
s->stacksize = 20;
return;
void push(struct stack* s, int e)
if (s->top - s->base >= s->stacksize)
s->base = (int*)realloc(s->base, (s->stacksize + n) * sizeof(int));
if (!s->base) exit(0);
s->top = s->base + s->stacksize;
s->stacksize += n;
*(s->top)++ = e;
return;
void pop(struct stack* s)
int e;
if (s->top == s->base) return;
e = *--s->top; printf("%d", e);
return;
int StackEmpty(struct stack* s)
if (s->top == s->base) return 1;
else return 0;
void conversion(int N) //10进制转换成2进制
stack s;
initstack(&s);
do push(&s, N & 1);
while (N >>= 1);
while (!StackEmpty(&s))
pop(&s);
int main()
int x;
printf("\\n请输入一个十进制数据:");
scanf("%d", &x);
printf("\\n 转换成二进制为: ", x);
conversion(x);
printf("\\n");
```cpp
#include <stack>
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
const int MAX_SIZE = 100;
struct Stack
char* base; char* top; int stacksize;
Stack() InitStack(MAX_SIZE);
Stack(int i) InitStack(i);
void InitStack(int);
bool Push(char const& elem);
bool Pop();
bool Empty();
char& GetTop();
~Stack() delete base;
;
void Stack::InitStack(int i)
base = new char[i];
top = base;
stacksize = i;
bool Stack::Push(char const& elem)
if (top - base == stacksize)
return false;
*top++ = elem;
return true;
bool Stack::Pop()
if (top == base)
return false;
--top;
return true;
char& Stack::GetTop()
if (top != base)
return *(top - 1);
bool Stack::Empty()
if (top == base) return true;
else return false;
void reverse(string& s);
int main()
string s;
cin >> s;
if (s.back() == '\\@')
reverse(s);
cout << s << endl;
void reverse(string& s)
Stack stack;
for (int i = 0; i < s.length() - 1; i++)
stack.Push(s[i]);
s = "";
while (!stack.Empty())
s += stack.GetTop();
stack.Pop();
运行截图
以上是关于C++栈和递归实验的主要内容,如果未能解决你的问题,请参考以下文章