锐格————栈与队列

8563

#include <bits/stdc++.h>

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<<e;}

}
int main()
{    sqstack s;
     init(s);
    int n,x,e;
    cin>>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<iostream>
#include<stack>

using namespace std;

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

int main() {
	//创建两个栈
	stack<char> optstack;
	stack<int> 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.empty()) {
		cout << optstack.top() << endl;
		optstack.pop();
	}
	while (!numstack.empty()) {
		cout << numstack.top() << endl;
		numstack.pop();
	}*/

	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 <bits/stdc++.h>

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<<n<<' ';
    }
    cout<<q.rear->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 <bits/stdc++.h>

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<<e<<' ';
     }

 }



int main()
{  squeue q;
   q.front=q.rear=0;
   int n;cin>>n;
   in(q,n);
  out(q);

    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值