一
#include <iostream>
using namespace std;
typedef struct sta {
char data[100];
int cnt;
}Sta,*Stalist;
Sta s;
void init()
{
s.cnt = 0;
return;
}
bool get()
{
char ch;
while (cin >> ch) {
if (ch == '#') break;
else {
if (ch == '(' || ch == '[' || ch == '{') {
s.cnt++;
s.data[s.cnt] = ch;
}
else if (ch == ')') {
if (s.cnt && s.data[s.cnt] == '(') {
s.cnt--;
}
else return 0;
}
else if (ch == ']') {
if (s.cnt && s.data[s.cnt] == '[') {
s.cnt--;
}
else return 0;
}
else if (ch == '}') {
if (s.cnt && s.data[s.cnt] == '{') {
s.cnt--;
}
else return 0;
}
}
}
if (s.cnt) return 0;
return 1;
}
int main()
{
init();
cout << get() << endl;
return 0;
}
二
#include <iostream>
using namespace std;
typedef struct sta {
int data[100];
int cnt;
}Sta,*Stalist;
Sta s;
void init()
{
s.cnt = 0;
return;
}
void push()
{
int x;
while (cin>>x) {
if (x == 0) return;
s.cnt++;
s.data[s.cnt] = x;
}
}
void pop()
{
if (s.cnt == 0) {
cout << "none" << endl;
return;
}
cout << s.data[s.cnt] << " ";
s.cnt--;
}
int main()
{
init();
push();
while (s.cnt != 0) {
pop();
}
cout << endl;
return 0;
}
三
#include <iostream>
using namespace std;
typedef struct node {
int data;
struct node* next;
}Qnode,*Linkq;
typedef struct {
Linkq f, r;
}Linkqueue;
int init(Linkqueue& Q)
{
Q.f = Q.r = new Qnode;
if (!Q.f) {
cout << "error\n";
return 0;
}
Q.f->next = NULL;
return 1;
}
bool Linkempty(Linkqueue Q)
{
if (Q.f == Q.r) return 1;
else return 0;
}
void Linkin(Linkqueue& Q, int x)
{
Linkq p;
p = new Qnode;
p->data = x;
p->next = Q.r->next;
Q.r->next = p;
Q.r = Q.r->next;
}
int Linkout(Linkqueue& Q)
{
if (Q.f != Q.r) {
int ans;
Linkq p;
p = Q.f->next;
ans = p->data;
Q.f->next = p->next;
if (Q.r == p) Q.f = Q.r;
delete p;
return ans;
}
}
void solve()
{
Linkqueue Q;
init(Q);
int x;
while (cin >> x) {
if (x == 0) break;
Linkin(Q, x);
}
while (!Linkempty(Q)) {
cout << Linkout(Q) << " ";
}
cout << endl;
}
int main()
{
solve();
return 0;
}
四
#include <iostream>
using namespace std;
const int N = 200;
typedef struct {
int data[N];
int f, r;
}Squeue;
void init(Squeue& Q)
{
Q.f = Q.r = 0;
}
bool Seqempty(Squeue Q)
{
if (Q.f == Q.r) return 1;
return 0;
}
int Seqin(Squeue& Q, int x)
{
if ((Q.r + 1) % N == Q.f) {
cout << "队列满\n";
return 0;
}
Q.data[Q.r] = x;
Q.r = (Q.r + 1) % N;
return 1;
}
int Seqpop(Squeue& Q)
{
int ans;
if (Q.f == Q.r) {
cout << "Empty\n";
return 0;
}
ans = Q.data[Q.f];
Q.f = (Q.f + 1) % N;
return ans;
}
void solve()
{
Squeue Q;
init(Q);
int x;
while (cin >> x) {
if (x == 0) break;
Seqin(Q, x);
}
while (!Seqempty(Q)) {
cout << Seqpop(Q) << " ";
}
cout << endl;
}
int main()
{
solve();
return 0;
}
五
#include <iostream>
using namespace std;
typedef struct sta {
int data[100];
int cnt;
}Sta, * Stalist;
Sta s;
void init()
{
s.cnt = 0;
return;
}
int d;
void push()
{
int x;
cin >> x >> d;
cout << x << "(10)=";
while (x) {
s.cnt++;
s.data[s.cnt] = x % d;
x /= d;
}
}
void pop()
{
if (s.cnt == 0) {
cout << "none" << endl;
return;
}
cout << s.data[s.cnt];
s.cnt--;
}
int main()
{
init();
push();
while (s.cnt != 0) {
pop();
}
printf("(%d)\n", d);
return 0;
}
六
#include <iostream>
using namespace std;
const int N = 200;
typedef struct {
int data[N];
int f, r;
}Squeue;
void init(Squeue& Q)
{
Q.f = Q.r = 0;
}
bool Seqempty(Squeue Q)
{
if (Q.f == Q.r) return 1;
return 0;
}
int Seqin(Squeue& Q, int x)
{
if ((Q.r + 1) % N == Q.f) {
cout << "队列满\n";
return 0;
}
Q.data[Q.r] = x;
Q.r = (Q.r + 1) % N;
return 1;
}
int Seqpop(Squeue& Q)
{
int ans;
if (Q.f == Q.r) {
cout << "Empty\n";
return 0;
}
ans = Q.data[Q.f];
Q.f = (Q.f + 1) % N;
return ans;
}
void solve()
{
Squeue Q;
init(Q);
int n;
cin >> n;
for(int j=1;j<=n-1;j++)
cout<<" ";
printf("%2d \n",1);
Seqin(Q, 1);
Seqin(Q, 1);
int s = 0;
for (int i = 2; i <= n; i++) {
Seqin(Q, 0);
for(int j=1;j<=n-i;j++)
cout<<" ";
for (int j = 1; j <= i + 1; j++) {
int tmp;
tmp = Seqpop(Q);
Seqin(Q, s + tmp);
s = tmp;
if (s != 0) printf("%2d ",s);
}
cout << endl;
}
}
int main()
{
solve();
return 0;
}
七
#include<stdio.h>
#include <bits/stdc++.h>
using namespace std;
typedef struct
{
char data[1000];
int top;
}SqStack;
void initStack(SqStack &st)
{
st.top = -1;
}
int isEmpty(SqStack st)
{
if(st.top==-1)return 1;
else
return 0;
}
void Push(SqStack &S,char e)
{
if(S.top==999)
return;
S.data[++S.top]=e;
}
void Pop(SqStack &S,char &e)
{
if(isEmpty(S))
{
e='\0';
return ;
}
e=S.data[S.top--];
}
char getPop(SqStack &S)
{
if(isEmpty(S))return 0;
return S.data[S.top];
}
int print_S(SqStack st)
{
if( isEmpty(st) )
{
printf("Stack is Empty");
return 0;
}
int iPointer = st.top;
while(iPointer != -1)
{
printf("%d ",st.data[iPointer]);
--iPointer;
}
printf("\n");
return 1;
}
int precede(char ch1,char ch2)
{
if(ch1=='+'||ch1=='-')
{
if(ch2=='+'||ch2=='#'||ch2=='-'||ch2==')')
return 1;
else
return 3;
}
else if(ch1=='*'||ch1=='/')
{
if(ch2=='(')
return 3;
else
return 1;
}
else if(ch1=='(')
{
if(ch2==')')return 2;
else return 3;
}
else if(ch1=='#')
{
if(ch2=='#')return 2;
else return 3;
}
}
char hb(char b,char ch,char a)
{
int a1,b1,c;
char c1;
a1=a-'0';
b1=b-'0';
if(ch=='+')
c=a1+b1;
else if(ch=='-')
c=a1-b1;
else if(ch=='*')
c=a1*b1;
else if(ch=='/')
c=a1/b1;
c1=c+'0';
return c1;
}
int cal(SqStack &optr,SqStack &opnd)
{
int x;
char a,b,x1;
char ch,ch1;
Push(optr,'#');
cin>>ch;
while(ch!='#'||getPop(optr)!='#')
{
if(ch>='0'&&ch<='9')
{
Push(opnd,ch);
cin>>ch;
}
else
{
switch(precede(getPop(optr),ch))
{
printf("%d",precede(getPop(optr),ch));
case 1:Pop(optr,ch1);Pop(opnd,a);Pop(opnd,b);Push(opnd,hb(a,ch1,b));break;
case 2:Pop(optr,ch);cin>>ch;break;
case 3:Push(optr,ch);cin>>ch;break;
}
}
}
Pop(opnd,x1);
x=x1-'0';
return x;
}
int main()
{
int s;
char x;
SqStack optr,opnd;
initStack(optr);
initStack(opnd);
s=cal(optr,opnd);
printf("%d\n",s);
return 0;
}
八
#include <iostream>
using namespace std;
typedef struct node {
int data;
struct node* next;
}Snode,*Links;
void init(Links& top)
{
top = NULL;
}
bool Sempty(Links top)
{
if (top == NULL) return 1;
return 0;
}
void Sin(Links& top, int x)
{
Links p;
p = new Snode;
p->data = x;
p->next = top;
top = p;
}
int Sout(Links& top)
{
int ans;
Links p;
if (top != NULL) {
ans = top->data;
p = top;
top = top->next;
delete p;
return ans;
}
}
void solve()
{
Links top;
init(top);
int x;
while (cin >> x) {
if (x == 0) break;
Sin(top, x);
}
while (!Sempty(top)) {
cout << Sout(top) << " ";
}
cout << endl;
}
int main()
{
solve();
return 0;
}