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();
}
}
运行截图
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/140682.html