Question
i post this question 3 times ! plz this time give me the total answer! wigh build & run Experiment 2 Stack operation 1. Purpose
i post this question 3 times ! plz this time give me the total answer! wigh build & run
Experiment 2 Stack operation
1. Purpose of the experiment
a. Master the basic method of debugging stack with VC + + or Dev c++.
b. Master the basic operation of stack: the realization of insertion, deletion and other operations in the sequential storage structure.
c. Master the characteristics of stack storage structure.
2. Essential basic knowledge
Stack is a kind of linear table with limited operation, which only allows inserting and deleting data from one end. Each deleted element is the last element to be put on the stack, so the stack is also called last in first out (LIFO).There are two ways to store a stack: linear storage and linked storage (linked list).
a. Linear storage
The way of linear storage is basically the same as that of linear table, except that there is more limitation of last in first out. It is statically allocated, that is, its memory has been allocated in the form of array before use. Therefore, it is necessary to indicate the maximum number of nodes in the stack during initialization.
b. Linked storage
In fact, as long as the single linked list class is modified appropriately to restrict the behavior of inserting, deleting, modifying and accessing nodes to make it conform to the rule of stack first in and then out. In addition, we need to provide stack access interface functions, such as stack in, stack out, stack size acquisition, etc
3. Experimental content
Complete the entry and exit stack operations in your own familiar method.
The stack is used to convert decimal numbers into binary, octal and hexadecimal numbers.
[problem description] create a stack to convert decimal numbers into binary, octal, and hexadecimal numbers. For example, input decimal numbers and convert them into binary, octal or hexadecimal numbers for output.
[basic requirements] first, create a stack with an array. Then write the stack function and the stack function, and finally write the main function.
[test data] such as:
input the data you want to conversion
5
please input the scale you want to convert to
2
101
4.Program code
You can program your own programs, or refer to the following procedures. If you refer to the code I gave, please translate the Chinese text in the code into English.
#include
#include
#include
typedef char DataType;
#define STACK_SIZE 100
typedef struct{
DataType *top;
DataType *base;
int stack_size;
}seqstack;
void init(seqstack *s)
{
s->base=(DataType *)malloc(STACK_SIZE*sizeof(DataType));
if(!s->base) exit(-1);
s->top=s->base;
s->stack_size=STACK_SIZE;
}
int IsEmpty(seqstack *s)
{
return s->base==s->top;
}
int IsFull(seqstack *s)
{
return s->top-s->base==STACK_SIZE;
}
void Push(seqstack *s,char ch)
{
if(IsFull(s))
{
printf("overflow !");
exit(1);
}
else
*s->top++=ch;
}
char Pop(seqstack *s)
{
if(IsEmpty(s))
{
printf("Stack is empty ");
exit(1);
}
return *--s->top;
}
char Top(seqstack *s) // take the top element of the stack
{
if(IsEmpty(s))
{
printf("Stack is empty ");
exit(1);
}
return *(s->top-1);
}
void conversion(int a,int b)
{
int i;
seqstack s; //should not be declared as a pointer
init(&s);
while(a)
{
Push(&s,a%b);
a=a/b;
}
while(!IsEmpty(&s))
{
i=Pop(&s);
printf("%d",i);//When the base is greater than 9, the output result is wrong. Change to printf("%x", i); to achieve output in hexadecimal. Change to printf("%c", (i < 10) i+'0' : i - 10 + 'A'); to achieve output in 36 hexadecimal
}
free(s.base); //release
}
void main(int argc,char *argv[])
{
int a,b;
//int a;
printf("please input the data you want to conversion ");
scanf("%d",&a);
printf("please input the scale you want to convert to ");
scanf("%d",&b);
conversion(a,2);
system("pause");
}
5.Operation results
Please give the result of the program.
6.Experimental summary
What problems are encountered in the experiment and how to solve them?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started