实验5-基本操作及十进制转二进制

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 20
typedef int ElemType;
typedef struct
{
ElemType a[MAXSIZE];
int top;
}SqStack;
SqStack s1,s2;
int i=0;
void init_s(SqStack *s);
void out_s(SqStack *s);
void push(SqStack *s,ElemType e);
ElemType pop(SqStack *s);
ElemType gettop(SqStack *s);
int abc(SqStack s);
void change(ElemType e);
main()
{
int k;ElemType e,x,y,u,v,m; char ch;
init_s(&s1);
init_s(&s2);
do{
printf("\n");
printf("\n\n 1.数据元素e进栈");
printf("\n\n 2.出栈一个元素,返回其值");
printf("\n\n 3.求栈顶元素");
printf("\n\n 4.输出栈中元素");
printf("\n\n 5.判栈空");
printf("\n\n 6.将栈顶元素转换为二进制");
printf("\n\n 7.结束程序运行");
printf("\n============================");
printf("\n\n 请输入您的选择(1.2.3.4.5.9)");
scanf("%d",&k);
switch(k)
{
case 1:{printf("\n 进栈e=?"); scanf("%d",&e);
push(&s1,e); out_s(&s1);
}break;
case 2:{x=pop(&s1);
printf("\n出栈元素:%d",x);
out_s(&s1);
}break;
case 3:{y=gettop(&s1);
printf("栈顶元素为:%d",y);
}break;
case 4:out_s(&s1);break;
case 5:{
printf("0为空栈,1为非空\n");
u=abc(s1);
printf("结果为:%d",u);
}break;
case 6:{
v=gettop(&s1);
printf("将%d转换为二进制",v);
change(v);
for(;i>0;i--)
{
m=pop(&s2);
printf("%d",m);
}break;
case 7:break;
}
printf("\n----------------------------");
}
}while(k>=1 && k<7);
printf("\n 再见!");
printf("\n 打回车键,返回。");
}
void init_s(SqStack *s)
{
s->top=-1;
}
void out_s(SqStack *s)
{
char ch; int i;
if(s->top==-1) printf("\n Stack is NULL.");
else{i=s->top;
while(i!=-1){printf("\n data=%d",s->a[i]);
i--;}
}
printf("\n 打回车键,继续。");
}
void push(SqStack *s,ElemType e)
{
if (s->top==MAXSIZE-1) printf("\n Sstack is overflow!");
else{
s->top++;
s->a[s->top]=e;
}
}
ElemType pop(SqStack *s)
{
ElemType x;
if(s->top==-1)
{
printf("Stack is underflow!"); x=-1;}
else{x=s->a[s->top];
s->top--;
}
return(x);
}
ElemType gettop(SqStack *s)
{
ElemType x;
if(s->top==-1)
{
printf("Stack is underflow!");
x=-1;
}
else
{
x=s->a[s->top];
}
return(x);
}
int abc(SqStack s)
{
if(s.top==-1)
return (0);
else return (1);
}
void change(ElemType e)
{
int j=0;
while(e!=0)
{
j=e%2;
push(&s2,j);
e/=2;
i++;
}
}

你可能喜欢

  • 奥数二年级
  • 十进制十六进制转换
  • 小学奥数
  • 专题训练
  • c语言进制转换
  • C语言函数

实验5 基本操作及十进制转二进制相关文档

最新文档

返回顶部