Software Technology Experiment Class Code
I. stack section
#include <stdio.h>
#include <stdlib.h>
#define maxsize 1024
typedef int datatype;
typedef struct
{
datatype elements[maxsize];
int Top;
}Stack;
void setNull(Stack *s)
{
s->Top = -1;
}
int isfull(Stack *s)
{
if(s->Top >= maxsize - 1)
return 1;
else
return 0;
}
int isempty(Stack *s)
{
if(s->Top == -1)
return 1;
return 0;
}
void push(Stack *s, datatype E)
{
if(s->Top >= maxsize -1 )
{
printf("Stack Overflow");
}
else
{
s->Top++;
s->elements[s->Top] = E;
}
}
datatype *pop(Stack *s)
{
datatype *temp;
if(isempty(s))
{
printf("Stack Underflow");
return (NULL);
}
else
{
s->Top--;
temp = (datatype *)malloc(sizeof(datatype));
*temp = s->elements[s->Top+1];
return temp;
}
}
void conversion(int n)
{
Stack s;
setNull(&s);
int r,m;
r = n;
while(r)
{
m = r%2;
if(isfull(&s))
printf("Overflow\n");
else
push(&s,m);
r/=2;
}
printf("转换后的二进制是\n");
while(!isempty(&s))
printf("%d",*pop(&s));
printf("\n");
}
int main()
{
int num;
printf("请输入需要转换的二进制数:\n");
scanf("%d",&num);
conversion(num);
}
The teacher will ask the octal and hexadecimal to write extensions
This write it on their own!