北理工计算机复试上机 2010

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 }

原文地址:https://www.cnblogs.com/PPWEI/p/8454836.html

时间: 2024-08-02 08:34:31

北理工计算机复试上机 2010的相关文章

北理工计算机复试上机 2014

本人也是练习,如果有错误,欢迎指正[email protected],也可留言 1. 系统中有最近打开文件的记录,现用整数表示打开的文件名,且显示最近3个打开的文件,输出文件序列. 示例: 输入:1  输出:1 输入:2  输出:2,1 输入:3  输出:3,2,1 输入:4  输出:4,3,2 输入:1  输出:1,4,3 输入:4  输出:1,4,3 输入:3  输出:1,4,3 1 // 2014_1.cpp : Defines the entry point for the consol

北理工计算机复试上机 2013

1. 求两个数的最大公约数(似乎有个辗转相除法,为什么不用呢,没错,我不会) 示例: 输入:24,18 输出:6 1 // 2013_1.cpp : Defines the entry point for the console application. 2 // 3 #include<iostream> 4 using namespace std; 5 6 int main(int argc, char* argv[]) 7 { 8 int a,b; 9 cout<<"

北理工计算机复试上机 2012

1.输入10个数,从小到大排序 示例: 输入:1,2,5,7,9,10,45,67,24,26 输出:1,2,5,7,9,10,24,26,45,67 1 #include<iostream> 2 using namespace std; 3 int main(){ 4 5 int a[10]={0}; 6 int x=0; 7 while(x<10){ 8 cin>>a[x]; 9 x++; 10 } 11 12 for(int i=10;i>0;i--){ 13 i

北理工计算机复试上机 2011

1.输入一组单词(区分大小写),统计首字母相同的单词的个数,相同的单词不累加,输出格式:"字母,个数" 1 // 2011_1.cpp : Defines the entry point for the console application. 2 // 3 4 #include<iostream> 5 #include<vector> 6 #include<string> 7 using namespace std; 8 /** 9 1. 10 输

北理工计算机复试上机 2009

1.请输入字符串,最多输入4个字符串,要求后输入的字符串排在前面,例如 输入:EricZ 输出:1=EricZ 输入:David 输出:1=David 2=EricZ 1 /** 2 1.请输入字符串,最多输入4个字符串,要求后输入的字符串排在前面,例如 3 输入:EricZ 4 输出:1=EricZ 5 输入:David 6 输出:1=David 2=EricZ 7 */ 8 #include<iostream> 9 #include<list> 10 #include<s

北理工计算机复试上机 2008

1.存储一组姓名,如Apple,Tom,Green,Jack 要求能排序.按字母顺序插入.并显示. 1 /** 2 1.存储一组姓名,如Apple,Tom,Green,Jack 3 要求能排序.按字母顺序插入.并显示. 4 */ 5 #include<iostream> 6 #include<string> 7 #include<vector> 8 #include<algorithm> 9 10 using namespace std; 11 bool i

北京理工大学复试上机--2010

1.输入一串整数,输入命令排序! 输入 a t 在这串整数后面添加整数 t, 输入 c\m\n 有 n 替换 m, 输入 d t 删除 t, 输入 s 排序. #include <iostream> #include <vector> #include <cstring> #include <algorithm> using namespace std; int tonum(string s, int l) { int n = 1, sum = 0; for

浙大计算机研究生复试上机考试-2010年 zoj问题

ZOJ问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2984 Accepted Submission(s): 906 Problem Description 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC. 是否AC的规则如下: 1. zoj能AC: 2. 若字符串形式为xzojx,则也能AC,其中x可以是N

浙大计算机研究生复试上机考试-2010年 最短路径问题

最短路径问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14405 Accepted Submission(s): 4408 Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的. Input 输入n,m