zoukankan      html  css  js  c++  java
  • 北理工计算机复试上机 2010

    1.输入一串整数,输入命令!
    要求
        1.输入a t在这串整数后添加整数t.
        2.输入c m 用n替换m.
        3.输入d t删除t
        4.输入s 排序

     1 /**
     2 1.输入一串整数,输入命令!
     3 要求
     4     1.输入a t在这串整数后添加整数t.
     5     2.输入c m 
    用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 }
  • 相关阅读:
    7.12
    Powerdesigner使用方法
    数据库中float类型字段,转化到前端显示,统一保留两位小数
    【1】直接插入排序
    KMP算法
    ssm框架下web项目,web.xml配置文件的作用
    客户要求输入框要记录下上一次输入的内容
    tomcat启动闪退
    页面第一次加载,JS没有效果,刷新一下就好了
    机器学习,安装python的支持包
  • 原文地址:https://www.cnblogs.com/PPWEI/p/8454836.html
Copyright © 2011-2022 走看看