1. 程式人生 > >北理工計算機復試上機 2010

北理工計算機復試上機 2010

測試 div class 程序 find mes ack 應付 lac

1.輸入一串整數,輸入命令!
要求
1.輸入a t在這串整數後添加整數t.
2.輸入c \m \n用n替換m.
3.輸入d t刪除t
4.輸入s 排序

 1 /**
 2 1.輸入一串整數,輸入命令!
 3 要求
 4     1.輸入a t在這串整數後添加整數t.
 5     2.輸入c \m \n用n替換m.
 6     3.輸入d t刪除t
 7     4.輸入s 排序
 8 */
 9 #include<iostream>
10 #include<vector>
11 #include<algorithm>
12 #include<string
> 13 14 using namespace std; 15 16 int main(){ 17 vector<int> list; 18 int x=0; 19 vector<int>::iterator i; 20 cout<<"請輸入一串數字,0結束"<<endl; 21 while(cin>>x){ 22 if(x==0)break; 23 list.push_back(x); 24 } 25 cout<<"當前序列"<<endl; 26 for(i=list.begin();i!=list.end();i++)cout<<(*i)<<endl;
27 cout<<"請輸入命令,輸入z結束"<<endl; 28 char order; 29 30 while(cin>>order){ 31 if(order==z)break; 32 cout<<"order"<<order<<endl; 33 if(order==a){ 34 cin>>x; 35 list.push_back(x); 36 cout<<"插入命令,已插入"<<endl;
37 cout<<"當前序列"<<endl; 38 for(i=list.begin();i!=list.end();i++)cout<<(*i)<<endl; 39 }else if(order==c){ 40 char c1,c2; 41 int i1,i2; 42 cin>>c1>>i1>>c2>>i2; 43 replace(list.begin(),list.end(),i1,i2); 44 cout<<"當前序列"<<endl; 45 for(i=list.begin();i!=list.end();i++)cout<<(*i)<<endl; 46 47 }else if(order==d){ 48 cin>>x; 49 int *pos=find(list.begin(),list.end(),x); 50 list.erase(pos); 51 cout<<"當前序列"<<endl; 52 for(i=list.begin();i!=list.end();i++)cout<<(*i)<<endl; 53 54 }else if(order==s){ 55 sort(list.begin(),list.end()); 56 cout<<"當前序列"<<endl; 57 for(i=list.begin();i!=list.end();i++)cout<<(*i)<<endl; 58 } 59 } 60 }
2.輸入表達式,輸出值!分兩種情況:中綴表達式和後綴表達式(用例是我自己編的,程序僅僅能應付個位數的加減乘除)
    測試用例  1+2*3/(4-3)  7
             12343-/*+    7
  1 /**
  2 2.輸入表達式,輸出值!
  3 分兩種情況:中綴表達式和後綴表達式
  4 
  5  測試用例  1+2*3/(4-3)  7
  6            12343-/*+    7
  7 */
  8 #include<iostream>
  9 #include<string>
 10 #include<stack>
 11 
 12 using namespace std;
 13 
 14 int getpri(char c){
 15     if(c==+||c==-)return 1;
 16     if(c==*||c==/)return 2;
 17     if(c==(||c==))return 0;
 18 }
 19 string getpost(string s){
 20     string post="";
 21     stack<char> st;
 22     for(int i=0;i<s.length();i++){
 23         if(s[i]==+||s[i]==-||s[i]==*||s[i]==/){
 24             if(st.empty()){
 25                 st.push(s[i]);
 26             }else{
 27                 if(getpri(st.top())>getpri(s[i])){//棧頂元素優先級高 需出棧
 28                     while(getpri(st.top())>getpri(s[i])){
 29                         if(getpri(st.top())<=getpri(s[i]))break;
 30                         
 31                         post+=st.top();
 32                         st.pop();
 33                     }
 34                     st.push(s[i]);
 35                 }else{//棧頂元素優先級低 入棧
 36                     st.push(s[i]);
 37                 }
 38             }
 39         }else if(s[i]==(){
 40             st.push(s[i]);
 41         }else if(s[i]==)){//將括號裏面的算符出棧
 42             while(1){
 43                 if(st.top()==()break;
 44                 post+=st.top();
 45                 st.pop();
 46             }
 47             st.pop();//(出棧
 48         }else{//數字直接輸出
 49             post+=s[i];
 50         
 51         }
 52     }
 53     while(!st.empty()){
 54         post+=st.top();
 55         st.pop();
 56     }
 57 return post;
 58 }
 59 
 60 int  cal(string str){
 61     stack<int> st;
 62     for(int i=0;i<str.length();i++){
 63         if(str[i]!=+&&str[i]!=-&&str[i]!=*&&str[i]!=/){
 64         //數字直接進棧
 65             int zz=((int)str[i])-48;
 66             cout<<"入棧"<<zz<<endl;
 67             st.push(zz);
 68         }else if(str[i]==+){
 69             int a=st.top();
 70             st.pop();
 71             int b=st.top();
 72             st.pop();
 73             a=a+b;
 74             st.push(a);
 75         }else if(str[i]==-){
 76         
 77             int a=st.top();
 78             st.pop();
 79             int b=st.top();
 80             st.pop();
 81             a=b-a;
 82             st.push(a);
 83         }else if(str[i]==*){
 84         
 85             int a=st.top();
 86             st.pop();
 87             int b=st.top();
 88             st.pop();
 89             a=b*a;
 90             st.push(a);
 91         }else if(str[i]==/){
 92         
 93             int a=st.top();
 94             st.pop();
 95             int b=st.top();
 96             st.pop();
 97             a=b/a;
 98             st.push(a);
 99         }
100     
101     
102     }//for
103 return st.top();
104 
105 }
106 int main(){
107     
108     string str;
109     cout<<"請輸入中綴算式"<<endl;
110     cin>>str;
111     string post;
112     post=getpost(str);
113     cout<<"post "<<post<<endl;
114     int result=cal(post);
115     cout<<result<<endl;
116     cout<<"請輸入後綴算式"<<endl;
117     cin>>post;
118     result=cal(post);
119     cout<<result<<endl;
120     
121 }

北理工計算機復試上機 2010