2栈的应用上机实验要求
实验二 栈和队列的基本操作实现及其应用

实验二栈和队列的基本操作实现及其应用一、实验目的1、熟练掌握栈和队列的基本操作在两种存储结构上的实现。
2、会用栈和队列解决简单的实际问题。
二、实验内容(可任选或全做)题目一、试写一个算法,判断依次读入的一个以@为结束符的字符序列,是否为回文。
所谓“回文“是指正向读和反向读都一样的一字符串,如“321123”或“ableelba”。
相关常量及结构定义:# define STACK_INIT_SIZE 100# define STACKINCREMENT 10# define OK 1# define ERROR 0typedef int SElemType;//栈类型定义typedef struct SqStack{ SElemType *base;SElemType *top;int stacksize;}SqStack;设计相关函数声明:判断函数:int IsReverse()栈:int InitStack(SqStack &S )int Push(SqStack &S, SElemType e )int Pop(SqStack &S,SElemType &e)int StackEmpty(s)题目二、编程模拟队列的管理,主要包括:出队列、入队、统计队列的长度、查找队列某个元素e、及输出队列中元素。
[实现提示]:参考教材循环队列的有关算法,其中后两个算法参考顺序表的实现。
题目三、RailsDescriptionThere is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds were extremely limited thattime. It was possible to establish only a surface track. Moreover, it turned out that the station could be only a dead-end one (see picture) and due to lack of available space it could have only one track.The local tradition is that every train arriving from the direction A continues in the direction B with coaches reorganized in some way. Assume that the train arriving from the direction A has N <= 1000 coaches numbered in increasing order 1, 2, ..., N. The chief for train reorganizations must know whether it is possible to marshal coaches continuing in the direction B so that their order will be a1, a2, ..., aN. Help him and write a program that decides whether it is possible to get the required order of coaches. You can assume that single coaches can be disconnected from the train before they enter the station and that they can move themselves until they are on the track in the direction B. You can also suppose that at any time there can be located as many coaches as necessary in the station. But once a coach has entered the station it cannot return to the track in the direction A and also once it has left the station in the direction B it cannot return back to the station.InputThe input consists of blocks of lines. Each block except the last describes one train and possibly more requirements for its reorganization. In the first line of the block there is the integer N described above. In each of the next lines of the block there is a permutation of 1, 2, ..., N. The last line of the block contains just 0.The last block consists of just one line containing 0.OutputThe output contains the lines corresponding to the lines with permutations in the input.A line of the output contains Yes if it is possible to marshal the coaches in the order required on the corresponding line of the input. Otherwise it contains No. In addition,there is one empty line after the lines corresponding to one block of the input. There is no line in the output corresponding to the last ``null'' block of the input. Sample Input51 2 3 4 55 4 1 2 366 5 4 3 2 1Sample OutputYesNoYes题目四、Sliding WindowDescriptionAn array of size n≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example:The array is [1 3 -1 -3 5 3 6 7], and k is 3.Window position Minimum value Maximum value[1 3 -1] -3 5 3 6 7 -131 [3 -1 -3] 5 3 6 7 -331 3 [-1 -3 5] 3 6 7 -351 3 -1 [-3 5 3] 6 7 -351 3 -1 -3 [5 3 6] 7 361 3 -1 -3 5 [3 6 7]37Your task is to determine the maximum and minimum values in the sliding window at each position.InputThe input consists of two lines. The first line contains two integers n and k which are the lengths of the array and the sliding window. There are n integers in the second line.OutputThere are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values.Sample Input8 31 3 -1 -3 5 3 6 7Sample Output-1 -3 -3 -3 3 33 3 5 5 6 7题目五(选作考查串知识)DNA Evolution【Description】Evolution is a seemingly random process which works in a way which resembles certain approaches we use to get approximate solutions to hard combinatorial problems. You are now to do something completely different.Given a DNA string S from the alphabet {A,C,G,T}, find the minimal number of copy operations needed to create another string T. You may reverse the strings you copy, and copy both from S and the pieces of your partial T. You may put these pieces together at any time. You may only copy contiguous parts of your partial T, and all copied strings must be used in your final T.Example: From S= “ACTG” create T= “GTACTAATAAT”1.Get GT......... by copying and reversing "TG" from S.2.Get GT AC... by copying "AC" from S.3.Get GTAC TA….. by copying "TA" from the partial T.4.Get GTACTA AT by copying and reversing "TA" from the partial T.5.Get GTACTAAT AAT by copying "AAT" from the partial T.【Input】The first line of input gives a single integer, 1 ≤k≤10, the number of test cases. Then follow, for each test case, a line with the string S , length of S is less then 19, and a line with the string T , length of T is less then 19.【Output】Output for each test case the number of copy operations needed to create T from S, or "impossible" if it cannot be done.【Sample Input】4ACGTTGCAACACGTTCGATCGAAAAAAAAAAAAAAAAAAAA【Sample output】1impossible46题目六(选作考查数组知识)Magic Squares描述Following the success of the magic cube, Mr. Rubik invented its planar version, called magic squares. This is a sheet composed of 8 equal-sized squares:1 2 3 48 7 6 5In this task we consider the version where each square has a different color. Colors are denoted by the first 8 positive integers. A sheet configuration is given by the sequence of colors obtained by reading the colors of the squares starting at the upper left corner and going in clockwise direction. For instance, the configuration of Figure 3 is given by the sequence (1,2,3,4,5,6,7,8). This configuration is the initial configuration.Three basic transformations, identified by the letters `A', `B' and `C', can be applied to a sheet:∙'A': exchange the top and bottom row,∙'B': single right circular shifting of the rectangle,∙'C': single clockwise rotation of the middle four squares.Below is a demonstration of applying the transformations to the initial squares given above:A:8 7 6 51 2 3 4B:4 1 2 35 8 7 6C:1 72 48 6 3 5All possible configurations are available using the three basic transformations.You are to write a program that computes a minimal sequence of basic transformations that transforms the initial configuration above to a specific target configuration.输入A single line with eight space-separated integers (a permutation of (1..8)) that are the target configuration.输出样例输入2 6 8 4 5 73 1样例输出7BCABCCB三、实验步骤㈠、数据结构与核心算法的设计描述㈡、函数调用及主函数设计(可用函数的调用关系图说明)㈢程序调试及运行结果分析㈣实验总结四、主要算法流程图及程序清单1、主要算法流程图:2、程序清单(程序过长,可附主要部分)//int IsReverse(){ ….while( (e=getchar())!='@'){e 依次入栈、入队 //push(S,e);EnQueue(Q,e);……..}While(!StackEmpty(S)) { pop(S,a);DeQueue(Q,b);If(a!=b) return 0;}return 1;}。
栈的基本操作指导

实验2栈的基本操作实验指导1.实验内容基本内容:输入一个中缀一位整数算术表达式,先将其转换为后缀表达式,转换的方法采用栈实现,并且考虑算符的优先级,然后用另一个栈来计算后缀表达式的值。
对输入的表达式,做如下假设:(1)只考虑+、-、*、/这四种运算符;(2)输入的中缀表达式中数字是一位整数,没有小数;(3)假定输入表达式是合法的。
2.测试用例中缀表达式:3*4-5+7#后缀表达式:34*5-7+测试结果:143.算法描述计算中缀算术表达式是有关栈的经典算法题,很多公司面试或者笔试的时候都会把计算中缀表达式作为一个考察点。
这里先讨论如何直接计算中缀表达式的值,再讨论如何把中缀表达式转化为后缀表达式,最后计算后缀表达式。
3.1直接计算中缀表达式设置两个工作栈,一个操作数栈,一个操作符栈,在(自左至右)扫描算术表达式时,遇到操作数直接入操作数栈,若遇到操作符,则根据操作符优先级判断下一步操作(“操作符优先级规则”):若其优先级高于栈顶操作符,则入栈,否则(相等或小于),弹出栈顶算符,并从操作数栈弹出两个操作数,计算,将计算结果入操作数栈,继续比较与栈顶操作符的优先级,若仍然等于或低于之,则计算,直至大于之,则将此操作符入栈;左括号一定入栈,且其优先级低于后续来到的任何操作符,右括号一定出栈并计算直至遇到左括号,另外栈的开始以“#”开始,算术表达式以“#”结束,做结束标志。
当栈空时,计算结束。
3.2 中缀表达式转为后缀表达式中缀转化为后缀,要先设一个栈存储运算符,从左到右遍历表达式,主要遵循以下原则:(1)读到操作数,直接输出;(2)栈为空时,遇到运算符,入栈;(3)遇到左括号,将其入栈;(4)遇到右括号,执行出栈操作,并将出栈的元素输出,直到弹出栈的是左括号,左括号不输出,左括号也要弹出;(5)遇到其他运算符’+”-”*”/’时,弹出所有优先级大于或等于该运算符的栈顶元素,然后将该运算符入栈,遇到左括号时不能再弹出;(6)最终将栈中的元素依次出栈,输出。
实验二 栈和队列的基本操作及其应用

实验二栈和队列的基本操作及其应用一、实验目的1、掌握栈和队列的顺序存储结构和链式存储结构,以便在实际中灵活应用。
2、掌握栈和队列的特点,即后进先出和先进先出的原则。
3、掌握栈和队列的基本运算,如:入栈与出栈,入队与出队等运算在顺序存储结构和链式存储结构上的实现。
二、实验内容本次实验提供2个题目,每个题目都标有难度系数,*越多难度越大,学生可以根据自己的情况任选一个!题目一:回文判断(*)[问题描述]对于一个从键盘输入的字符串,判断其是否为回文。
回文即正反序相同。
如“abba”是回文,而“abab”不是回文。
[基本要求](1)数据从键盘读入;(2)输出要判断的字符串;(3)利用栈的基本操作对给定的字符串判断其是否是回文,若是则输出“ok”,否则输出“fail”。
程序源代码如下:/**********************************用栈和队列进行回文判断输入字符以@结束***********************************/#include <stdio.h>/*定义一个栈*/typedef struct Stack{int size;char * Base;char * Top;}Stack;/*创建一个栈*/void CreateStack(Stack * S,int size) {S -> size = size;S -> Base = (char *)malloc(size);S -> Top = S -> Base;}/*推入一个元素*/void Push(Stack * S,char c){/*栈满了,不能插入了*/if(S -> Top - S -> Base == S -> size) {printf("Stack is full and can't push!"); return;}else{*(++S -> Top) = c;}}void Pop(Stack * S){/*栈空了*/if(S -> Top == S -> Base){printf("Stack is empty!");return;}else{S -> Top--;}}void main(){Stack S;int Begin;char c;CreateStack(&S,100);Begin = 0;while(1){scanf("%c",&c);if(c == '@')break;if(c == '&' && !Begin){Begin = 1;continue;}if(Begin){if(*(S.Top) == c){Pop(&S);}}elsePush(&S,c);}if(S.Top == S.Base){printf("ok\n");}else{printf("fail\n");}getch();}运行结果如下:图中的“ok”表示是回文,“fail”表现不是回文。
实验2栈实验报告

实验2 栈实验报告1. 实验目的通过本次实验,使学生了解栈的基本概念和应用,掌握栈的操作算法,培养学生的编程能力和算法思维。
2. 实验环境•编程语言:C/C++•开发工具:Visual Studio/Code/GCC•操作系统:Windows/Linux/macOS3. 实验内容本次实验主要包括以下几个方面的内容:1.实现一个简单的栈数据结构;2.实现栈的常见操作(如入栈、出栈、判空、长度等);3.利用栈解决括号匹配问题;4.利用栈实现逆波兰表达式求值;5.分析栈在函数调用中的应用。
4. 实验步骤4.1 栈的定义与实现首先,我们需要定义一个栈的数据结构。
在C/C++中,可以使用数组或链表来实现栈。
这里我们使用数组来实现。
#include <stdio.h>#include <stdlib.h>#define MAX_SIZE 100typedef struct {int data[MAX_SIZE];int top;} Stack;接下来,我们需要实现栈的常见操作。
入栈(push)void push(Stack* s, int value) {if (s->top >= MAX_SIZE - 1) {printf("栈已满,无法入栈\n");return;s->data[++s->top] = value;出栈(pop)int pop(Stack* s) {if (s->top < 0) {printf("栈已空,无法出栈\n");return -1;return s->data[s->top--];判空(isEmpty)int isEmpty(Stack* s) {return s->top < 0;获取栈长度(length)int length(Stack* s) {return s->top + 1;4.2 括号匹配问题括号匹配问题是栈的一个经典应用。
数据结构实验二 栈基本操作

实验二栈基本操作一、实验目的掌握栈的基本操作:初始化、判空、判满、入栈、出栈、取栈顶元素等运算。
二、实验要求包含有头文件和main函数;1.格式正确,语句采用缩进格式;2.设计子函数实现题目要求的功能;3.编译、连接通过,熟练使用命令键;4.运行结果正确,输入输出有提示,格式美观。
三、实验设备、材料和工具1.奔腾2计算机或以上机型2.turboc2,win-tc四、实验内容和步骤实验内容:1.分析程序。
2.完成程序编写和补充步骤:3.确定数据的结构;4.利用main函数调用各子函数;5.调试、分析运行结果。
五、实验报告要求1.根据实验内容初步设计好程序,并从理论上排除错误;2.针对程序的健壮性准备好测试数据;3.结果分析中如实填写运行后的结果,记录调试过程中产生的重要问题和解决方法。
六、根据实验过程填写下面内容基础部分1.构建下列头文件,文件名称seqstack.h#define TRUE 1#define FALSE 0#define Stack_Size 500#define StackElementType inttypedef struct{StackElementType elem[Stack_Size]; /*用来存放栈中元素的一维数组*/int top; /*用来存放栈顶元素的下标,top为-1表示空栈*/}SeqStack;void InitStack(SeqStack *S){ S->top = -1; }int IsEmpty(SeqStack *S) /*判断栈S为空栈时返回值为真,反之为假*/{ return(S->top==-1?TRUE:FALSE);}int IsFull(SeqStack *S) /*判断栈S为满栈时返回值为真,反之为假*/{ return(S->top==Stack_Size-1?TRUE:FALSE);}int Push(SeqStack *S,StackElementType x){ if(S->top==Stack_Size-1)return(FALSE); /*栈已满*/S->top++;S->elem[S->top] = x;return(TRUE);}int Pop(SeqStack *S,StackElementType *x){ if(S->top == -1) /*栈为空*/return(FALSE);else{*x = S->elem[S->top];S->top--; /* 修改栈顶指针*/return(TRUE);}}int GetTop(SeqStack *S,StackElementType *x){ if(S->top == -1) /*栈为空*/return(FALSE);else{ *x = S->elem[S->top];return(TRUE); }}2.构建判断回文序列的程序,完成运行和调试。
栈的表示与实现及栈的应用实验报告参考模板

实验二:栈的表示与实现及栈的应用【实验目的】(1) 掌握栈的顺序存储结构及其基本操作的实现。
(2) 掌握栈后进先出的特点,并利用其特性在解决实际问题中的应用。
(3) 掌握用递归算法来解决一些问题。
【实验内容】1. 编写程序,对于输入的任意一个非负十进制整数,输出与其等值的八进制数。
2. 编写递归程序,实现N !的求解。
3. 编写递归程序,实现以下函数的求解。
4. 编写程序,实现Hanoi 塔问题。
【实验步骤】1.打开VC++。
2.建立工程:点File->New ,选Project 标签,在列表中选Win32 Console Application ,再在右边的框里为工程起好名字,选好路径,点OK->finish 。
至此工程建立完毕。
3.创建源文件或头文件:点File->New ,选File 标签,在列表里选C++ Source File 。
给文件起好名字,选好路径,点OK 。
至此一个源文件就被添加到了你刚创建的工程之中。
4.写好代码5.编译->链接->调试1、#include <stdio.h>#include <stdlib.h>#include <malloc.h>#define OK 1⎩⎨⎧>-+-==1),2()1(0,1,)(n n Fib n Fib n n n Fib#define ERROR 0#define OVERFLOW -2typedef int Status;typedef int SElemType;#define STACK_INIT_SIZE 100#define STACKINCREMENT 10typedef struct{SElemType *base;SElemType *top;int stacksize;}SqStack;Status InitStack(SqStack &S){S.base=(SElemType *)malloc (STACK_INIT_SIZE*sizeof(SElemType));if (!S.base) return OVERFLOW;S.top=S.base;S.stacksize=STACK_INIT_SIZE;return OK;}//InitStackStatus Push(SqStack &S, SElemType e){if (S.top-S.base>=S.stacksize){S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));if (!S.base) exit(OVERFLOW);S.top=S.base+S.stacksize;S.stacksize+=STACKINCREMENT;}*S.top++=e;return OK;} //PUSHStatus Pop(SqStack &S, SElemType &e) {if(S.top==S.base)return ERROR;e=*--S.top;return OK;} //PopStatus StackEmpty(SqStack S){if (S.top==S.base)return OK;elsereturn ERROR;} //StackEmptyvoid conversion(){int N;int e;SqStack S;InitStack(S);printf("输入要转换的数据:");scanf("%d",&N);while (N){Push(S, N % 8);N = N/8;}printf("\n其对应的八进制数是:");while (!StackEmpty(S)){Pop(S,e);printf ( "%d", e );}}void main(){conversion();}2、#include <stdio.h>Int fact(int n){If(n==1)return 1;elsereturn n*fact(n-1);}void main(){Int n;printf(“输入一个数n:”);scanf(“%d”,&n);printf("fact(%d)=%d\n",n,fact(n)); }3、#include <stdio.h>int fib(int n){if(n>1)return fib(n-1)+fib(n-2);elsereturn n;}void main(){int n;printf("\n输入一个数n:");scanf("%d",&n);printf("fib(%d)=%d\n",n,fib(n));}4、#include <stdio.h>void move(char x,int n,char z){printf("将%d号盘从%c柱移到%c柱\n",n,x,z);}void hanoi (int n, char x, char y, char z) { if (n==1)move(x,1,z);else{hanoi(n-1, x, z, y);move(x, n, z);hanoi(n-1, y, x, z);}}void main(){int n;scanf("%d",&n);hanoi(n,'x','y','z');}【实验心得】这节课的实验内容是栈的表示与实现及栈的应用。
栈的操作(实验报告范文)

栈的操作(实验报告范文)栈的基本操作,附带源程序实验三栈和队列3.1实验目的:(1)熟悉栈的特点(先进后出)及栈的基本操作,如入栈、出栈等,掌握栈的基本操作在栈的顺序存储结构和链式存储结构上的实现;(2)熟悉队列的特点(先进先出)及队列的基本操作,如入队、出队等,掌握队列的基本操作在队列的顺序存储结构和链式存储结构上的实现。
3.2实验要求:(1)复习课本中有关栈和队列的知识;(2)用C语言完成算法和程序设计并上机调试通过;(3)撰写实验报告,给出算法思路或流程图和具体实现(源程序)、算法分析结果(包括时间复杂度、空间复杂度以及算法优化设想)、输入数据及程序运行结果(必要时给出多种可能的输入数据和运行结果)。
3.3基础实验[实验1]栈的顺序表示和实现实验内容与要求:编写一个程序实现顺序栈的各种基本运算,并在此基础上设计一个主程序,完成如下功能:(1)初始化顺序栈(2)插入元素(3)删除栈顶元素(4)取栈顶元素(5)遍历顺序栈(6)置空顺序栈分析:栈的顺序存储结构简称为顺序栈,它是运算受限的顺序表。
对于顺序栈,入栈时,首先判断栈是否为满,栈满的条件为:p->top==MA某NUM-1,栈满时,不能入栈;否则出现空间溢出,引起错误,这种现象称为上溢。
出栈和读栈顶元素操作,先判栈是否为空,为空时不能操作,否则产生错误。
通常栈空作为一种控制转移的条件。
注意:(1)顺序栈中元素用向量存放(2)栈底位置是固定不变的,可设置在向量两端的任意一个端点(3)栈顶位置是随着进栈和退栈操作而变化的,用一个整型量top (通常称top为栈顶指针)来指示当前栈顶位置参考程序:#include<tdio.h>#include<tdlib.h>#defineMA某NUM20栈的基本操作,附带源程序#defineElemTypeint/某定义顺序栈的存储结构某/ typedeftruct{ElemTypetack[MA某NUM]; inttop;}SqStack;/某初始化顺序栈某/voidInitStack(SqStack某p){if(!p)printf("Eorror");p->top=-1;}/某入栈某/voidPuh(SqStack某p,ElemType某){if(p->top<MA某NUM-1){p->top=p->top+1;p->tack[p->top]=某;}eleprintf("Overflow!\n");}/某出栈某/ElemTypePop(SqStack某p){ElemType某;if(p->top!=0){某=p->tack[p->top];printf("以前的栈顶数据元素%d已经被删除!\n",p->tack[p->top]);p->top=p->top-1;return(某);}ele{printf("Underflow!\n");return(0);}}/某获取栈顶元素某/ ElemTypeGetTop(SqStack某p) {ElemType某;if(p->top!=0){某=p->tack[p->top];return(某);}ele{printf("Underflow!\n");栈的基本操作,附带源程序return(0);}}/某遍历顺序栈某/ voidOutStack(SqStack某p) {inti;if(p->top<0)printf("这是一个空栈!");printf("\n");for(i=p->top;i>=0;i--)printf("第%d个数据元素是:%6d\n",i,p->tack[i]); }/某置空顺序栈某/voidetEmpty(SqStack某p){p->top=-1;}/某主函数某/main(){SqStack某q;inty,cord;ElemTypea;do{printf("\n");printf("第一次使用必须初始化!\n");printf("\n主菜单\n");printf("\n1初始化顺序栈\n");printf("\n2插入一个元素\n");printf("\n3删除栈顶元素\n");printf("\n4取栈顶元素\n");printf("\n5置空顺序栈\n");printf("\n6结束程序运行\n");printf("\n--------------------------------\n"); printf("请输入您的选择(1,2,3,4,5,6)");canf("%d",&cord);printf("\n");witch(cord){cae1:{q=(SqStack某)malloc(izeof(SqStack));InitStack(q);OutStack(q);}break;cae2:栈的基本操作,附带源程序{printf("请输入要插入的数据元素:a="); canf("%d",&a);Puh(q,a);OutStack(q);}break;cae3:{Pop(q);OutStack(q);}break;cae4:{y=GetTop(q);printf("\n栈顶元素为:%d\n",y); OutStack(q);}break;cae5:{etEmpty(q);printf("\n顺序栈被置空!\n"); OutStack(q);}break;cae6:e某it(0);}}while(cord<=6);}[实验2]栈的链式表示和实现实验内容与要求:编写一个程序实现链栈的各种基本运算,并在此基础上设计一个主程序,完成如下功能:(1)初始化链栈(2)链栈置空(3)入栈(4)出栈(5)取栈顶元素(6)遍历链栈分析:链栈是没有附加头结点的运算受限的单链表。
数据结构上机实验报告

数据结构上机实验报告数据结构上机实验报告1. 实验目的数据结构是计算机科学中非常重要的一门课程,通过本次上机实验,旨在帮助学生巩固和应用所学的数据结构知识,培养学生分析和解决实际问题的能力。
2. 实验背景本次实验涉及到两个常用的数据结构:栈和队列。
栈是一种后进先出(Last In First Out,LIFO)的数据结构,而队列是一种先进先出(First In First Out,FIFO)的数据结构。
通过实验,我们将学习如何使用这两种数据结构来解决实际问题。
3. 实验内容本次实验分为两个部分:栈的应用和队列的应用。
3.1 栈的应用在栈的应用部分,我们将实现一个简单的括号匹配算法。
该算法可以判断一个字符串中的括号是否匹配。
具体实现步骤如下:3.1.1 创建一个栈来存储括号字符;3.1.2 遍历字符串中的每个字符;3.1.3 如果遇到左括号,则将其入栈;3.1.4 如果遇到右括号,则判断栈顶元素是否是对应的左括号;3.1.5 如果栈为空或栈顶元素不是对应的左括号,则括号不匹配;3.1.6 如果栈顶元素是对应的左括号,则将其出栈;3.1.7 遍历完字符串后,如果栈为空,则括号匹配,否则括号不匹配。
通过实现这个算法,我们可以学习到如何使用栈来解决实际问题,并且理解栈的后进先出的特性。
3.2 队列的应用在队列的应用部分,我们将实现一个简单的任务调度算法。
该算法可以模拟多个任务按照一定的优先级进行调度的过程。
具体实现步骤如下:3.2.1 创建一个队列来存储任务;3.2.2 每个任务包含两个属性:任务名称和优先级;3.2.3 向队列中添加任务,并按照优先级进行排序;3.2.4 从队列中取出优先级最高的任务,并执行;3.2.5 执行完任务后,继续从队列中取出下一个优先级最高的任务,并执行,直到队列为空。
通过实现这个算法,我们可以学习到如何使用队列来实现任务调度,并且理解队列的先进先出的特性。
4. 实验结果与分析通过实验,我们成功实现了括号匹配算法和任务调度算法,并得到了正确的结果。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
⏹设计要求
首先设计一个含有多个菜单项的主控菜单程序,然后再为这些菜单项配上相应的功能。
主控菜单设计要求:程序运行后,显示一个标题“判断表达式括号匹配”,标题下方给出4个菜单项的内容和输入提示:
1.输入表达式
2.输出表达式
3.判断表达式的括号是否匹配并计算表达式的值
4.退出
请选择1—4:
⏹功能要求
完成各菜单的功能,能正确判断表达式的括号匹配(括号限圆括弧和方括弧两种),
若匹配,利用算符优先法计算表达式的值。
计算表达式值的3个相关函数(后面已给出,可以直接粘贴到程序中使用):
I n(c)---- //判断c是否为运算符
Precede(θ1, θ2) ----//判断两运算符θ1, θ2的优先关系
Operate(a, θ, b)---- //做四则运算a theta b,返回运算结果
⏹实现要求
1)可用VC++的引用或c语言用指针实现各功能的调用
2)必须用栈实现括号匹配和表达式计算
⏹实验提交要求
1)画流程图
2)实现功能的全部程序
3)说明程序设计过程中的难点、解决办法及编程小结或体会。
4)实验及报告必须于2015.4.29提交
Status In(SElemType c) //判断c是否为运算符
{
switch(c)
{
case'+':
case'-':
case'*':
case'/':
case'(':
case')':
case'#':
return TRUE;
default:
return FALSE;
}
}
char Precede(SElemType t1, SElemType t2) //判断两符号的优先关系{
SElemType f;
switch(t2)
{
case'+':
case'-':
if(t1=='('||t1=='#')
f='<';
else
f='>';
break;
case'*':
case'/':
if(t1=='*'||t1=='/'||t1==')')
f='>';
else
f='<';
break;
case'(':
if(t1==')')
{ printf("括号不匹配\n");
exit(OVERFLOW);
}
else
f='<';
break;
case')':
switch(t1)
{
case'(':
f='=';
break;
case'#':
printf("缺乏左括号\n");
exit(OVERFLOW);
default:
f='>';
}
break;
case'#':
switch(t1)
{
case'#':
f='=';
break;
case'(':
printf("缺乏右括号\n");
exit(OVERFLOW);
default:
f='>';
}
}
return f;
}
SElemType Operate(SElemType a,SElemType theta,SElemType b) //做四则运算a theta b,返回运算结果{
SElemType c;
switch(theta)
{
case'+':
return a+b;
case'-':
return a-b;
case'*':
return a*b;
}
return a/b;
}
教师:曹妍2015.4.10。