#include <iostream.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct{
int * base;
int *top;
int stacksize;
}Stack;
int InitStack(Stack &S)
{
S.base = (int *) malloc(STACK_INIT_SIZE*sizeof(int));
if (!S.base) exit(OVERFLOW);
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return 1;
}
int Push(Stack &S, int e )
{
if (S.top-S.base >= S.stacksize)
{
S.base = (int *) realloc(S.base, (S.stacksize + T) *sizeof(int));
if (!S.base) exit(OVERFLOW);
S.top = S.base + S.stacksize;
S.stacksize += T;
}
*S.top++ = e;
return 1;
}
int Pop(Stack &S, int &e)
{
if (S.top == S.base) return 0;
e = *--S.top;
return 1;
}
int StackEmpty(Stack S)
{
if (S.top == S.base)
return 1;
else return 0;
}
void conversion(int N, int r)
{
Stack S;
int e;
InitStack(S);
while(N)
{
Push(S, N%r);
N = N/r;
}
}
while(!StackEmpty(S)){
Pop(S, e);
cout << e;
}
void main()
{
int N, r;
cin >> N;
cin >> r;
cout << N << "由十进制转换为" << r << "进制后的值为:";
conversion(N, r);
cout << endl;
}
括号匹配检验
#include<iostream.h>
#include<string.h>
typedef struct node{
elemtype data;
struct node *next;
}stack;
stack *top;
void push(elemtype n){
stack *p;
p=new stack;
p->data=n;
p->next=top->next;
top->next=p;
}
void pop(){
stack *q;
q=top->next;
top->next=top->next->next;
delete q;
}
elemtype gettop(){
return top->next->data;
}
void judge(char a[]){
int i,tag=0,len=strlen(a);//tag==1
for(i=0;i<len;i++)
if(a[i]=='('||a[i]=='['||a[i]=='{')
{push(a[i]);continue;}
if(top->next==NULL)
{
cout<<"No"<<endl;
break;
tag=1;
}
if(a[i]!=')'&&a[i]!=']'&&a[i]!='}')
continue;
if(a[i]-gettop()==1||a[i]-gettop()==2)
pop();
else
{
cout<<"No"<<endl;
break;
tag=1;
}
}
if(tag==0&&i==len)
{
if(top->next==NULL)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
top->next=NULL;
}
int main()
{
top=new stack;
top->next=NULL;
char a[100];
int n;
cin>>n;
while(n--){
cin>>a;
judge(a);
}
return 0;
博主ma6174对本博客文章(除转载的)享有版权,未经许可不得用于商业用途。转载请注明出处http://www.cnblogs.com/ma6174/
对文章有啥看法或建议,可以评论或发电子邮件到ma6174@163.com