栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

锐格————栈与队列

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

锐格————栈与队列

8563

#include 

using namespace std;
#define Maxsize 100
#define SElemtype int
#define OK 1
typedef struct {
SElemtype *base;
SElemtype *top;
int stacksize;
}sqstack;


void init(sqstack &s)
{
    s.base=new SElemtype[Maxsize];
    if(!s.base)
        exit(OVERFLOW);
    s.top=s.base;
    s.stacksize=Maxsize;

}
void in(sqstack &s,SElemtype e)
{
    *s.top++=e;
}
void out(sqstack &s,SElemtype &e)
{
    while(s.top!=s.base)
    {e=*--s.top;
    cout<>x>>n;
   printf("%d(%d)=",x,10);
    while(x/n!=0){
        e=x%n;
        x=x/n;
        in(s,e);
    }
    in(s,x);
    out(s,e);

    printf("(%d)",n);

    return 0;
}


#include
#include

using namespace std;

int Calculate_Func(int n1, char opt, int n2);

int main() {
	//创建两个栈
	stack optstack;
	stack numstack;

	//初数化符号栈
	optstack.push('#');

	char ch;
	ch = getchar();
	char ctemp;
	char ntemp1, ntemp2;

	while (ch != '#') {
		if (ch <= '9' && ch >= '0') {
			numstack.push(ch-'0');
		}

		if (ch == '(' || ch == ')' || ch == '[' || ch == ']' || ch == '*' || ch == '/' || ch == '+' || ch == '-' || ch == '{' || ch == '}') {
			switch (ch) {
			
			//左括号直接入栈
			case '{':
				optstack.push(ch);
				break;

			case '[':
				optstack.push(ch);
				break;

			case '(':
				optstack.push(ch);
				break;

			case '}':
					
					//提出运算符
					ctemp = optstack.top();
					optstack.pop();
					while (ctemp != '{') {
						//取出两个数字
						ntemp1 = numstack.top();
						numstack.pop();
						ntemp2 = numstack.top();
						numstack.pop();
						//计算
						numstack.push(Calculate_Func(ntemp1, ctemp, ntemp2));
						ctemp = optstack.top();
						optstack.pop();
					}

					if (ctemp == '[' || ctemp == '(') {
						cout << 0;
						return 0;
					}
					break;

			case ']':

				//提出运算符
				ctemp = optstack.top();
				optstack.pop();
				while (ctemp != '[') {
					//取出两个数字
					ntemp1 = numstack.top();
					numstack.pop();
					ntemp2 = numstack.top();
					numstack.pop();
					//计算
					numstack.push(Calculate_Func(ntemp1, ctemp, ntemp2));

					ctemp = optstack.top();
					optstack.pop();
				}

				if (ctemp == '{' || ctemp == '(') {
					cout << 0;
					return 0;
				}
				break;

			case ')':

				//提出运算符
				ctemp = optstack.top();
				optstack.pop();
				while (ctemp != '(') {
					//取出两个数字
					ntemp1 = numstack.top();
					numstack.pop();
					ntemp2 = numstack.top();
					numstack.pop();
					//计算
					numstack.push(Calculate_Func(ntemp1, ctemp, ntemp2));

					ctemp = optstack.top();
					optstack.pop();
				}

				if (ctemp == '{' || ctemp == '[') {
					cout << 0;
					return 0;
				}
				break;

			case '+':
				//提出运算符
				ctemp = optstack.top();
				optstack.pop();
				
				if (ctemp == '(' || ctemp == '{' || ctemp == '[' || ctemp == '+' || ctemp == '-') {
					optstack.push(ctemp);
					optstack.push(ch);
				}
				else {
					while (ctemp == '*' || ctemp == '/') {
						
						//取出两个数字
						ntemp1 = numstack.top();
						numstack.pop();
						ntemp2 = numstack.top();
						numstack.pop();
						//计算
						numstack.push(Calculate_Func(ntemp1, ctemp, ntemp2));
			
						ctemp = optstack.top();
						optstack.pop();
					}
					//将原运算符与新运算符压入栈顶
					optstack.push(ctemp);
					optstack.push(ch);
				}
				break;

			case '-':
				//提出运算符
				ctemp = optstack.top();
				optstack.pop();

				if (ctemp == '(' || ctemp == '{' || ctemp == '[' || ctemp == '+' || ctemp == '-') {
					optstack.push(ctemp);
					optstack.push(ch);
				}
				else {
					while (ctemp == '*' || ctemp == '/') {

						//取出两个数字
						ntemp1 = numstack.top();
						numstack.pop();
						ntemp2 = numstack.top();
						numstack.pop();
						//计算
						numstack.push(Calculate_Func(ntemp1, ctemp, ntemp2));

						ctemp = optstack.top();
						optstack.pop();
					}
					if (ctemp == ch) {
						optstack.push(ctemp);
						optstack.push('+');
						break;
					}
					//将原运算符与新运算符压入栈顶
					optstack.push(ctemp);
					optstack.push(ch);
				}
				break;


			case '*':
				optstack.push(ch);
				break;

			case '/':
				optstack.push(ch);
				break;

			}
		}

		ch = getchar();
	}

	//检查栈内容
	

	while (optstack.top() != '#') {
		//取出两个数字
		ntemp1 = numstack.top();
		numstack.pop();
		ntemp2 = numstack.top();
		numstack.pop();
		//取出运算符
		ctemp = optstack.top();
		optstack.pop();
		//计算
		numstack.push(Calculate_Func(ntemp1, ctemp, ntemp2));
	}
	cout << numstack.top() << endl;

	return 0; 
}

int Calculate_Func(int n1, char opt, int n2) {
	switch (opt) {
	
	case '+':
		return n1 + n2;
	
	case '-':
		return n2 - n1;

	case '*':
		return n1 * n2;

	case '/':
		return n2 / n1;

	}
}



8564

#include 

using namespace std;

#define Maxsize 100
#define Elemtype int

typedef struct node {

    int data;
	node* prior;
	node* next;

}node;

typedef struct queue {

	node* front;
	node* rear;

}linkqueue;


 void in(linkqueue &q,Elemtype e)
{

    node *p=new node;//千万不要错哦
      p->data=e;
      p->next=NULL;
      q.rear->next=p;
      q.rear=p;

 }

void out(linkqueue &q)
{
    while(q.front->next!=q.rear)
    {  node *p=new node;
        p=q.front->next;
      int  n=p->data;
       q.front->next=q.front->next->next;
       cout<data;
}

int main()
{
  linkqueue q;
   q.front=q.rear=new node;
   q.front->next=NULL;

   int n;
   cin>>n;
   while(n)
   {
       in(q,n);
       cin>>n;
   }
  out(q);
    return 0;
}

8565

#include 

using namespace std;

#define Maxsize 100
typedef struct
{
    int base[100];
    int front,rear;
}squeue;




void in(squeue &q,int n)
{
 q.rear=0;
  while(n)
{q.base[q.rear]=n;
q.rear++;
cin>>n;
}
}

void out (squeue &q)
{int e;
     while((q.rear-q.front+100)%100>0)
     {
         e=q.base[q.front];
         q.front++;
         cout<>n;
   in(q,n);
  out(q);

    return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/511969.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号