#include <iostream>
using namespace std;
#define ok 0
#define error -1
class SeqList
{
private:
int *list;
int maxsize;
int size;
public:
SeqList(int max=0);
~SeqList();
int list_size();
int list_insert(int item, int i);
int list_del(int i);
int list_get(int i);
};
SeqList::SeqList(int max)
{ maxsize = max;
size = 0;
list = new int[maxsize];
}
SeqList::~SeqList()
{
delete []list;
}
int SeqList::list_size()
{
return size;
}
int SeqList::list_insert(int item, int i)
{
if (size==maxsize+1) {
return error;
}
if (i<1 || i>size+1) {
return error;
}
for (int j=size; j>=i-1; j--)
list[j+1] = list[j];
list[i-1] = item;
size++;
return ok;
}
int SeqList::list_del(int i)
{
if (size == 0 ) { return error; }
if (i<1 || i>size) {return error; }
int temp = list[i];
for (int j = i; j<size; j++)
list[j-1] = list[j];
size--;
return temp;
}
int SeqList::list_get(int i)
{
if (i<0 || i>size-1) {return error; }
return list[i];
}
int main()
{ int i;
int temp,len,item,inspos,delpos,getpos;
SeqList mylist(100);
cin>>len;
for(i=0;i<len;i++)
{
cin>>temp;
mylist.list_insert(temp,i+1);
}
for(i=0;i<mylist.list_size()-1;i++)
cout<<mylist.list_get(i)<<" ";
cout<<mylist.list_get(mylist.list_size()-1);
cout<<endl;
cin>>getpos;
cout<<mylist.list_get(getpos-1);
cout<<endl;
cin>>inspos>>item;
mylist.list_insert(item,inspos);
for(i=0;i<mylist.list_size()-1;i++)
cout<<mylist.list_get(i)<<" ";
cout<<mylist.list_get(mylist.list_size()-1);
cout<<endl;
cin>>delpos;
mylist.list_del(delpos);
for(i=0; i<mylist.list_size()-1; i++)
cout<<mylist.list_get(i)<<" ";
cout<<mylist.list_get(mylist.list_size()-1);
cout<<endl;
return 0;
}
#include <stdlib.h>
#include <iostream>
using namespace std;
#define error -1
#define OK 0
class ListNode
{
public:
ListNode *next;
int data;
ListNode(ListNode*ptrNext=NULL){
next=ptrNext;
}
ListNode(int item,ListNode*ptrNext=NULL){
data=item;
next=ptrNext;
}
~ListNode(void){}
};
class LinkList
{
private:
ListNode *head;
int size;
ListNode *index(int i);
public:
LinkList();
~LinkList();
int list_size();
int list_insert(int item,int i);
int list_del(int);
int list_get(int i);
};
LinkList::LinkList()
{
head=new ListNode();
size=0;
}
LinkList::~LinkList()
{
ListNode *p,*q;
p=head;
while(p!=NULL){
q=p;
p=p->next;
delete q;
}
size=0;
head=NULL;
}
ListNode *LinkList::index(int i)
{
if(i<-1||i>size-1){
cout<<"位置错误"<<endl;
exit(error);
}
if(i==-1)
return head;
ListNode *p=head->next;
int j=0;
while(p!=NULL&&j<i){
p=p->next;
j++;
}
return p;
}
int LinkList::list_size()
{
return size;
}
int LinkList::list_insert(int item,int i){
if(i<0||i>size){
cout<<"位置越界"<<endl;
exit(error);
}
ListNode *p=index(i-1);
ListNode *q=new ListNode(item,p->next);
p->next=q;
size++;
return OK;
}
int LinkList::list_del(int i)
{
if(size==0){
cout<<"链表空"<<endl;
exit(error);
}
if(i<-1||i>size-1){
cout<<"位置错误"<<endl;
exit(error);
}
ListNode *s,*p=index(i-1);
s=p->next;
p->next=p->next->next;
int temp=s->data;
delete s;
size--;
return temp;
}
int LinkList::list_get(int i)
{
if(i<-1||i>size-1){
cout<<"位置错误"<<endl;
exit(error);
}
ListNode *p=index(i);
return p->data;
}
int main()
{
int i,temp;
LinkList mylist;
int len,item,inspos,delpos,getpos;
cin>>len; //输出指定长度的表
for(i=0;i<len;i++)
{
cin>>temp;
mylist.list_insert(temp,i);
}
for(i=0;i<mylist.list_size();i++)
cout<<mylist.list_get(i)<<" ";
cout<<endl;
cin>>getpos;//取出指定位置元素
cout<<mylist.list_get(getpos-1);
cout<<endl;
cin>>inspos>>item;//指定位置插入指定元素
mylist.list_insert(item,inspos);
for(i=0;i<mylist.list_size();i++)
cout<<mylist.list_get(i)<<" ";
cout<<endl;
cin>>delpos;//删除指定位置元素
mylist.list_del(delpos);
for(i=0;i<mylist.list_size();i++)
cout<<mylist.list_get(i)<<" ";
cout<<endl;
return 0;
}
#include<iostream>
#include<stack>
using namespace std;
int main()
{
stack<int> mystack;
int t,m,k;
double b,a;
cin>>t;
while(t--)
{
int c,x[100],d=0,i=0,count=0;
cin>>b>>k;
m=b;
a=b-m;
while(m)
{
c=m%k;
m=m/k;
mystack.push(c);
}
while(1)
{
d=a*k;
if(d>=k)
break;
a=a*k;
x[i++]=d;
count++;
}
while(!mystack.empty())
{
if(mystack.top()<10)
{
cout<<mystack.top();
mystack.pop();
}
else
{
switch(mystack.top())
{
case 10: cout<<"A"; mystack.pop(); break;
case 11: cout<<"B"; mystack.pop(); break;
case 12: cout<<"C"; mystack.pop(); break;
case 13: cout<<"D"; mystack.pop(); break;
case 14: cout<<"E"; mystack.pop(); break;
case 15: cout<<"F"; mystack.pop(); break;
}
}
}
cout<<".";
for(i=0;i<count;i++)
{
if(count!=1)
cout<<x[i];
else
cout<<x[i]<<"0"<<"0";
}
cout<<endl;
}
return 0;
}
#include <iostream>
using namespace std;
#include <string.h>
#define ERROR -1
#define CORRECT 1
#define MAXSTACKSIZE 100
typedef struct SqStack{
char base[MAXSTACKSIZE];
char *top;
} SqStack;
SqStack MBStack;
int InitStack(SqStack &S)
{
// if(!S.base)return ERROR;
S.top=S.base;
return CORRECT;
}
int Push(SqStack &S,char e)
{
if(S.top-S.base>=MAXSTACKSIZE)return ERROR;
*S.top++=e;
return CORRECT;
}
int Pop(SqStack &S,char &e)
{
if(S.top==S.base)return ERROR;
e=*--S.top;
return CORRECT;
}
int StackEmpty(SqStack &S)
{
if(S.top==S.base)return ERROR;
return CORRECT;
}
int MatchBracket(SqStack &S,char *BracketString)
{
int i;
char C,sC;
InitStack(S);
for(i=0;i<strlen(BracketString);i++)
{
C=BracketString[i];
if((C=='(')||(C=='[')||(C=='{')) Push(S,C);
if((C==')')||(C==']')||(C=='}'))
{
if(StackEmpty(S)==ERROR) return(-2);
Pop(S,sC);
if((C==')')&&(sC!='(')) return(-1);
if((C==']')&&(sC!='[')) return(-1);
if((C=='}')&&(sC!='{')) return(-1);
}
}
if(StackEmpty(S)!=ERROR) return(-3);
return(0);
}
int main(int argc, char* argv[])
{
int i,SampleNo;
char BracketString[MAXSTACKSIZE];
cin>>SampleNo;
for(i=0;i<SampleNo;i++)
{
cin>>BracketString;
cout<<MatchBracket(MBStack,BracketString)<<endl;
}
return 0;
}
#include<string.h>
#include<iostream>
using namespace std;
class String
{
private:
char *str;
int size;
int maxSize;
void GetNext(const String& t, int next[])const;
int KMPFind(const String& t, int start, int next[])const;
public:
String(char *s="");
~String(void);
int KMPFindSubstr(const String& t, int start)const;
};
String::String(char *s)
{
size = strlen(s);
maxSize = size + 1;
str = new char[maxSize];
strcpy(str, s);
}
String::~String(void)
{
delete []str;
}
void String::GetNext(const String& t, int next[])const
{
int j=1, k=0;
next[0] = -1; next[1] = 0;
while(j<t.size-1)
{
if(t.str[j] == t.str[k])
{
next[j+1] = k+1;
j++;
k++;
}
else if(k==0)
{
next[j+1] = 0;
j++;
}
else k = next[k];
}
}
int String::KMPFind(const String& t, int start, int next[])const
{
int i = start, j=0, v;
while (i<size && j<t.size)
{
if (j==-1 || str[i] == t.str[j])
{
i++;
j++;
}
else j = next[j];
}
if(j == t.size)v=i-t.size;
else v = -2;
return v;
}
int String::KMPFindSubstr(const String& t, int start)const
{
int m = t.size,i;
int *next = new int[m];
GetNext(t, next);
for(i=0;i<m;i++)
{ if(i!=m-1)
cout<<next[i]+1<<' ';
else
cout<<next[i]+1;
}
cout<<endl;
int v = KMPFind(t, start, next);
delete []next;
return v;
}
int main ()
{
int testNo, m;
int index;
char str1[80], str2[80];
cin >> testNo;
cin.get();
for (m=0; m<testNo; m++)
{
cin.getline(str2,80);
cin.getline(str1,80);
String s1(str1), s2(str2);
index=s1.KMPFindSubstr(s2, 0)+1;
cout<<index<<endl;
}
return 0;
}
#include <iostream>
using namespace std;
int Sgrade(int);
int main()
{
int grade;
int i, n;
cin>>n;
for(i=0; i<n;i++)
{
cin>>grade;
if(i!=n-1)
{Sgrade(grade);
cout<<' ';
}
else
Sgrade(grade);
}
cout<<endl;
return 0;
}
int Sgrade(int grade)
{
if(grade>=85)
cout<<'A';
else if(grade>=75)
cout<<'B';
else if(grade>=65)
cout<<'C';
else if(grade>=60)
cout<<'D';
else
cout<<'F';
return 0;
}
#include <iostream>
using namespace std;
#define MAXLISTLEN 100
int ListLen=0;
int SeqList[MAXLISTLEN+1];
int CreateSeqList(int *r,int n)
{
int i;
if(n>MAXLISTLEN)
return(-1);
ListLen = n;
for(i=1;i<=n;i++)
{
SeqList[i]=r[i-1];
}
return(1);
}
void SearchSeqList(int Key)
{
bool flag;
int i;
flag = false;
for(i=ListLen;i>=0;--i)
{
if(SeqList[i]==Key)
{
cout<<"1"<<" "<<i<<" "<<ListLen-i+1<<endl;
flag = true;
}
}
if(flag)
cout<<"0"<<" "<<"0"<<" "<<ListLen-i<<endl;
}
int InsertSeqList(int NewItem,int i)
{
int j;
if((i>ListLen+1)||(i<1)||(ListLen==MAXLISTLEN))
return(-1);
for(j=ListLen;j>=i;j--)
SeqList[j+1]=SeqList[j];
SeqList[i]=NewItem;ListLen++;
return(1);
}
int DeleteSeqList(int i)
{
int j;
if((i>ListLen)||(i<1)||(ListLen==0))
return(-1);
for(j=i;j<ListLen;j++)
SeqList[j]=SeqList[j+1];
ListLen--;
return(1);
}
void ShowSeqList()
{
int i;
for(i=1;i<ListLen;i++)
cout<<SeqList[i]<<" ";
cout<<SeqList[i]<<endl;
}
int main(int argc,char* argv[])
{
int r[MAXLISTLEN],i,SampleNo,key,NewPos,NewItem,DelPos,j,k;
{
cin>>j;
for(k=0;k<j;k++)
{
cin>>SampleNo;
for(i=0;i<SampleNo;i++)
cin>>r[i];
CreateSeqList(r,SampleNo);
cin>>NewPos;
cin>>NewItem;
cin>>DelPos;
cin>>key;
InsertSeqList(NewItem,NewPos);
ShowSeqList();
DeleteSeqList(DelPos);
ShowSeqList();
SearchSeqList(key);
}
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef int datatype;
#define MAXLEN 100
typedef struct
{
datatype data[MAXLEN];
int top;
}seqstack;
void createseqstack(seqstack &s)
{
s.top=-1;
}
void add(seqstack &s)
{
int x,y;
x=s.data[s.top];
y=s.data[s.top-1];
s.data[s.top-1]=x+y;
s.top--;
}
void subtract(seqstack &s)
{
int x,y;
x=s.data[s.top];
y=s.data[s.top-1];
s.data[s.top-1]=y-x;
s.top--;
}
void multiply(seqstack &s)
{
int x,y;
x=s.data[s.top];
y=s.data[s.top-1];
s.data[s.top-1]=x*y;
s.top--;
}
void divide(seqstack &s)
{
int x,y;
x=s.data[s.top];
y=s.data[s.top-1];
s.data[s.top-1]=y/x;
s.top--;
}
void input(seqstack &s)
{
int i,sum,ch;
char str[MAXLEN];
scanf("%s",&str);
sum=strlen(str);
for(i=0;i<=sum+1&&ch!='\0';i++)
{
ch=str[i];
switch(ch)
{
case '+': add(s);break;
case '-': subtract(s);break;
case '*': multiply(s);break;
case '/': divide(s);break;
default:
while(ch-48>=0&&ch-48<=9)
{
if(MAXLEN-1==s.top)
{
printf("栈已满!");
exit(0);
}
else
{
s.top++;
s.data[s.top]=ch-48;
}
break;
}
break;
}
}
}
void output(seqstack &s)
{
printf("%d\n",s.data[s.top]);
}
int main()
{
int i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
{
seqstack s;
createseqstack(s);
input(s);
output(s);
}
return 0;
}
1、问题描述
给定一个后缀表达式,利用栈求取该后缀表达式的值。
2、算法
顺序扫描算术表达式,
⑴、如果输入字符为数值,压入操作数栈
⑵、如果输入字符为运算符,则从操作数栈顶弹出2个数值,运算后,将运算结果再压入操作数栈中
⑶、如果表达式全部处理完成,且操作数栈为空,则运算正确,返回结果,否则返回-1
Input
第一行:样本数目n
第二行:第一个样本(后缀表达式,每个字符代表一个数值(0--9)或一个运算符(+,-,*,/))
:
第n+1行:第n个样本(后缀表达式)
Output
第一行:第一个后缀表达式的值
:
第n行:第n个后缀表达式的值
Sample Input
2 1235+*4/+ 12+3*42/+
Sample Output
5 11
using namespace std;
#define ok 0
#define error -1
class SeqList
{
private:
int *list;
int maxsize;
int size;
public:
SeqList(int max=0);
~SeqList();
int list_size();
int list_insert(int item, int i);
int list_del(int i);
int list_get(int i);
};
SeqList::SeqList(int max)
{ maxsize = max;
size = 0;
list = new int[maxsize];
}
SeqList::~SeqList()
{
delete []list;
}
int SeqList::list_size()
{
return size;
}
int SeqList::list_insert(int item, int i)
{
if (size==maxsize+1) {
return error;
}
if (i<1 || i>size+1) {
return error;
}
for (int j=size; j>=i-1; j--)
list[j+1] = list[j];
list[i-1] = item;
size++;
return ok;
}
int SeqList::list_del(int i)
{
if (size == 0 ) { return error; }
if (i<1 || i>size) {return error; }
int temp = list[i];
for (int j = i; j<size; j++)
list[j-1] = list[j];
size--;
return temp;
}
int SeqList::list_get(int i)
{
if (i<0 || i>size-1) {return error; }
return list[i];
}
int main()
{ int i;
int temp,len,item,inspos,delpos,getpos;
SeqList mylist(100);
cin>>len;
for(i=0;i<len;i++)
{
cin>>temp;
mylist.list_insert(temp,i+1);
}
for(i=0;i<mylist.list_size()-1;i++)
cout<<mylist.list_get(i)<<" ";
cout<<mylist.list_get(mylist.list_size()-1);
cout<<endl;
cin>>getpos;
cout<<mylist.list_get(getpos-1);
cout<<endl;
cin>>inspos>>item;
mylist.list_insert(item,inspos);
for(i=0;i<mylist.list_size()-1;i++)
cout<<mylist.list_get(i)<<" ";
cout<<mylist.list_get(mylist.list_size()-1);
cout<<endl;
cin>>delpos;
mylist.list_del(delpos);
for(i=0; i<mylist.list_size()-1; i++)
cout<<mylist.list_get(i)<<" ";
cout<<mylist.list_get(mylist.list_size()-1);
cout<<endl;
return 0;
}
#include <stdlib.h>
#include <iostream>
using namespace std;
#define error -1
#define OK 0
class ListNode
{
public:
ListNode *next;
int data;
ListNode(ListNode*ptrNext=NULL){
next=ptrNext;
}
ListNode(int item,ListNode*ptrNext=NULL){
data=item;
next=ptrNext;
}
~ListNode(void){}
};
class LinkList
{
private:
ListNode *head;
int size;
ListNode *index(int i);
public:
LinkList();
~LinkList();
int list_size();
int list_insert(int item,int i);
int list_del(int);
int list_get(int i);
};
LinkList::LinkList()
{
head=new ListNode();
size=0;
}
LinkList::~LinkList()
{
ListNode *p,*q;
p=head;
while(p!=NULL){
q=p;
p=p->next;
delete q;
}
size=0;
head=NULL;
}
ListNode *LinkList::index(int i)
{
if(i<-1||i>size-1){
cout<<"位置错误"<<endl;
exit(error);
}
if(i==-1)
return head;
ListNode *p=head->next;
int j=0;
while(p!=NULL&&j<i){
p=p->next;
j++;
}
return p;
}
int LinkList::list_size()
{
return size;
}
int LinkList::list_insert(int item,int i){
if(i<0||i>size){
cout<<"位置越界"<<endl;
exit(error);
}
ListNode *p=index(i-1);
ListNode *q=new ListNode(item,p->next);
p->next=q;
size++;
return OK;
}
int LinkList::list_del(int i)
{
if(size==0){
cout<<"链表空"<<endl;
exit(error);
}
if(i<-1||i>size-1){
cout<<"位置错误"<<endl;
exit(error);
}
ListNode *s,*p=index(i-1);
s=p->next;
p->next=p->next->next;
int temp=s->data;
delete s;
size--;
return temp;
}
int LinkList::list_get(int i)
{
if(i<-1||i>size-1){
cout<<"位置错误"<<endl;
exit(error);
}
ListNode *p=index(i);
return p->data;
}
int main()
{
int i,temp;
LinkList mylist;
int len,item,inspos,delpos,getpos;
cin>>len; //输出指定长度的表
for(i=0;i<len;i++)
{
cin>>temp;
mylist.list_insert(temp,i);
}
for(i=0;i<mylist.list_size();i++)
cout<<mylist.list_get(i)<<" ";
cout<<endl;
cin>>getpos;//取出指定位置元素
cout<<mylist.list_get(getpos-1);
cout<<endl;
cin>>inspos>>item;//指定位置插入指定元素
mylist.list_insert(item,inspos);
for(i=0;i<mylist.list_size();i++)
cout<<mylist.list_get(i)<<" ";
cout<<endl;
cin>>delpos;//删除指定位置元素
mylist.list_del(delpos);
for(i=0;i<mylist.list_size();i++)
cout<<mylist.list_get(i)<<" ";
cout<<endl;
return 0;
}
#include<iostream>
#include<stack>
using namespace std;
int main()
{
stack<int> mystack;
int t,m,k;
double b,a;
cin>>t;
while(t--)
{
int c,x[100],d=0,i=0,count=0;
cin>>b>>k;
m=b;
a=b-m;
while(m)
{
c=m%k;
m=m/k;
mystack.push(c);
}
while(1)
{
d=a*k;
if(d>=k)
break;
a=a*k;
x[i++]=d;
count++;
}
while(!mystack.empty())
{
if(mystack.top()<10)
{
cout<<mystack.top();
mystack.pop();
}
else
{
switch(mystack.top())
{
case 10: cout<<"A"; mystack.pop(); break;
case 11: cout<<"B"; mystack.pop(); break;
case 12: cout<<"C"; mystack.pop(); break;
case 13: cout<<"D"; mystack.pop(); break;
case 14: cout<<"E"; mystack.pop(); break;
case 15: cout<<"F"; mystack.pop(); break;
}
}
}
cout<<".";
for(i=0;i<count;i++)
{
if(count!=1)
cout<<x[i];
else
cout<<x[i]<<"0"<<"0";
}
cout<<endl;
}
return 0;
}
#include <iostream>
using namespace std;
#include <string.h>
#define ERROR -1
#define CORRECT 1
#define MAXSTACKSIZE 100
typedef struct SqStack{
char base[MAXSTACKSIZE];
char *top;
} SqStack;
SqStack MBStack;
int InitStack(SqStack &S)
{
// if(!S.base)return ERROR;
S.top=S.base;
return CORRECT;
}
int Push(SqStack &S,char e)
{
if(S.top-S.base>=MAXSTACKSIZE)return ERROR;
*S.top++=e;
return CORRECT;
}
int Pop(SqStack &S,char &e)
{
if(S.top==S.base)return ERROR;
e=*--S.top;
return CORRECT;
}
int StackEmpty(SqStack &S)
{
if(S.top==S.base)return ERROR;
return CORRECT;
}
int MatchBracket(SqStack &S,char *BracketString)
{
int i;
char C,sC;
InitStack(S);
for(i=0;i<strlen(BracketString);i++)
{
C=BracketString[i];
if((C=='(')||(C=='[')||(C=='{')) Push(S,C);
if((C==')')||(C==']')||(C=='}'))
{
if(StackEmpty(S)==ERROR) return(-2);
Pop(S,sC);
if((C==')')&&(sC!='(')) return(-1);
if((C==']')&&(sC!='[')) return(-1);
if((C=='}')&&(sC!='{')) return(-1);
}
}
if(StackEmpty(S)!=ERROR) return(-3);
return(0);
}
int main(int argc, char* argv[])
{
int i,SampleNo;
char BracketString[MAXSTACKSIZE];
cin>>SampleNo;
for(i=0;i<SampleNo;i++)
{
cin>>BracketString;
cout<<MatchBracket(MBStack,BracketString)<<endl;
}
return 0;
}
#include<string.h>
#include<iostream>
using namespace std;
class String
{
private:
char *str;
int size;
int maxSize;
void GetNext(const String& t, int next[])const;
int KMPFind(const String& t, int start, int next[])const;
public:
String(char *s="");
~String(void);
int KMPFindSubstr(const String& t, int start)const;
};
String::String(char *s)
{
size = strlen(s);
maxSize = size + 1;
str = new char[maxSize];
strcpy(str, s);
}
String::~String(void)
{
delete []str;
}
void String::GetNext(const String& t, int next[])const
{
int j=1, k=0;
next[0] = -1; next[1] = 0;
while(j<t.size-1)
{
if(t.str[j] == t.str[k])
{
next[j+1] = k+1;
j++;
k++;
}
else if(k==0)
{
next[j+1] = 0;
j++;
}
else k = next[k];
}
}
int String::KMPFind(const String& t, int start, int next[])const
{
int i = start, j=0, v;
while (i<size && j<t.size)
{
if (j==-1 || str[i] == t.str[j])
{
i++;
j++;
}
else j = next[j];
}
if(j == t.size)v=i-t.size;
else v = -2;
return v;
}
int String::KMPFindSubstr(const String& t, int start)const
{
int m = t.size,i;
int *next = new int[m];
GetNext(t, next);
for(i=0;i<m;i++)
{ if(i!=m-1)
cout<<next[i]+1<<' ';
else
cout<<next[i]+1;
}
cout<<endl;
int v = KMPFind(t, start, next);
delete []next;
return v;
}
int main ()
{
int testNo, m;
int index;
char str1[80], str2[80];
cin >> testNo;
cin.get();
for (m=0; m<testNo; m++)
{
cin.getline(str2,80);
cin.getline(str1,80);
String s1(str1), s2(str2);
index=s1.KMPFindSubstr(s2, 0)+1;
cout<<index<<endl;
}
return 0;
}
#include <iostream>
using namespace std;
int Sgrade(int);
int main()
{
int grade;
int i, n;
cin>>n;
for(i=0; i<n;i++)
{
cin>>grade;
if(i!=n-1)
{Sgrade(grade);
cout<<' ';
}
else
Sgrade(grade);
}
cout<<endl;
return 0;
}
int Sgrade(int grade)
{
if(grade>=85)
cout<<'A';
else if(grade>=75)
cout<<'B';
else if(grade>=65)
cout<<'C';
else if(grade>=60)
cout<<'D';
else
cout<<'F';
return 0;
}
#include <iostream>
using namespace std;
#define MAXLISTLEN 100
int ListLen=0;
int SeqList[MAXLISTLEN+1];
int CreateSeqList(int *r,int n)
{
int i;
if(n>MAXLISTLEN)
return(-1);
ListLen = n;
for(i=1;i<=n;i++)
{
SeqList[i]=r[i-1];
}
return(1);
}
void SearchSeqList(int Key)
{
bool flag;
int i;
flag = false;
for(i=ListLen;i>=0;--i)
{
if(SeqList[i]==Key)
{
cout<<"1"<<" "<<i<<" "<<ListLen-i+1<<endl;
flag = true;
}
}
if(flag)
cout<<"0"<<" "<<"0"<<" "<<ListLen-i<<endl;
}
int InsertSeqList(int NewItem,int i)
{
int j;
if((i>ListLen+1)||(i<1)||(ListLen==MAXLISTLEN))
return(-1);
for(j=ListLen;j>=i;j--)
SeqList[j+1]=SeqList[j];
SeqList[i]=NewItem;ListLen++;
return(1);
}
int DeleteSeqList(int i)
{
int j;
if((i>ListLen)||(i<1)||(ListLen==0))
return(-1);
for(j=i;j<ListLen;j++)
SeqList[j]=SeqList[j+1];
ListLen--;
return(1);
}
void ShowSeqList()
{
int i;
for(i=1;i<ListLen;i++)
cout<<SeqList[i]<<" ";
cout<<SeqList[i]<<endl;
}
int main(int argc,char* argv[])
{
int r[MAXLISTLEN],i,SampleNo,key,NewPos,NewItem,DelPos,j,k;
{
cin>>j;
for(k=0;k<j;k++)
{
cin>>SampleNo;
for(i=0;i<SampleNo;i++)
cin>>r[i];
CreateSeqList(r,SampleNo);
cin>>NewPos;
cin>>NewItem;
cin>>DelPos;
cin>>key;
InsertSeqList(NewItem,NewPos);
ShowSeqList();
DeleteSeqList(DelPos);
ShowSeqList();
SearchSeqList(key);
}
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef int datatype;
#define MAXLEN 100
typedef struct
{
datatype data[MAXLEN];
int top;
}seqstack;
void createseqstack(seqstack &s)
{
s.top=-1;
}
void add(seqstack &s)
{
int x,y;
x=s.data[s.top];
y=s.data[s.top-1];
s.data[s.top-1]=x+y;
s.top--;
}
void subtract(seqstack &s)
{
int x,y;
x=s.data[s.top];
y=s.data[s.top-1];
s.data[s.top-1]=y-x;
s.top--;
}
void multiply(seqstack &s)
{
int x,y;
x=s.data[s.top];
y=s.data[s.top-1];
s.data[s.top-1]=x*y;
s.top--;
}
void divide(seqstack &s)
{
int x,y;
x=s.data[s.top];
y=s.data[s.top-1];
s.data[s.top-1]=y/x;
s.top--;
}
void input(seqstack &s)
{
int i,sum,ch;
char str[MAXLEN];
scanf("%s",&str);
sum=strlen(str);
for(i=0;i<=sum+1&&ch!='\0';i++)
{
ch=str[i];
switch(ch)
{
case '+': add(s);break;
case '-': subtract(s);break;
case '*': multiply(s);break;
case '/': divide(s);break;
default:
while(ch-48>=0&&ch-48<=9)
{
if(MAXLEN-1==s.top)
{
printf("栈已满!");
exit(0);
}
else
{
s.top++;
s.data[s.top]=ch-48;
}
break;
}
break;
}
}
}
void output(seqstack &s)
{
printf("%d\n",s.data[s.top]);
}
int main()
{
int i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
{
seqstack s;
createseqstack(s);
input(s);
output(s);
}
return 0;
}
1、问题描述
给定一个后缀表达式,利用栈求取该后缀表达式的值。
2、算法
顺序扫描算术表达式,
⑴、如果输入字符为数值,压入操作数栈
⑵、如果输入字符为运算符,则从操作数栈顶弹出2个数值,运算后,将运算结果再压入操作数栈中
⑶、如果表达式全部处理完成,且操作数栈为空,则运算正确,返回结果,否则返回-1
Input
第一行:样本数目n
第二行:第一个样本(后缀表达式,每个字符代表一个数值(0--9)或一个运算符(+,-,*,/))
:
第n+1行:第n个样本(后缀表达式)
Output
第一行:第一个后缀表达式的值
:
第n行:第n个后缀表达式的值
Sample Input
2 1235+*4/+ 12+3*42/+
Sample Output
5 11