C++经典机试题目

1. 表达式求值 - 中缀转后缀

1#include <iostream> 2#include <stack> 3 4using namespace std; 5 6int Priority(char oper) 7{ 8 switch(oper) { 9 case '(': 10 return 0; 11 case '+': 12 case '-': 13 return 1; 14 case '*': 15 case '/': 16 return 2; 17 } 18} 19 20int mid_to_post(const string& ori) 21{ 22 23 char c, t; 24 stack<char> syms; 25 syms.push('('); 26 for (int i = 0; i < ori.size(); i++) { 27 c = ori[i]; 28 switch(c) { 29 case '0': 30 case '1': 31 case '2': 32 case '3': 33 case '4': 34 case '5': 35 case '6': 36 case '7': 37 case '8': 38 case '9': 39 //EnQueue(post, c); 40 cout << c; 41 break; 42 case '(': 43 syms.push(c); 44 break; 45 case ')': 46 case ';': 47 do { 48 t = syms.top(); 49 syms.pop(); 50 //Pop(S,t); 51 if(t!='(') 52 //EnQueue(post, t); 53 cout << t; 54 } while(t!='(' && !syms.empty()); 55 break; 56 case '+': 57 case '-': 58 case '*': 59 case '/': 60 while(Priority(c)<=Priority(syms.top())) { 61 //Pop(S,t); 62 //EnQueue(post, t); 63 t = syms.top(); 64 syms.pop(); 65 cout<<t; 66 } 67 //Push(S,c); 68 syms.push(c); 69 break; 70 } 71 } 72 73 return 0; 74} 75 76int main() 77{ 78 cout<<"1+2*3-4;"<<endl; 79 mid_to_post("1+2*3-4;"); 80 81 getchar(); 82 83 return 0; 84}

2. 链表归并

1#include "stdio.h" 2#include "stdlib.h" 3#include "malloc.h" 4 5typedef struct LNode { 6 int data; 7 LNode* next; 8} LNode, *LinkList; 9LinkList La, Lb, Lc; 10 11LNode* Create() 12{ 13 LinkList l, p, q; 14 int x; 15 l=NULL; 16 l=(LNode*)malloc(sizeof(LNode)); 17 if(l==NULL) exit(-1); 18 l->next = NULL; 19 scanf("%d", &x); 20 while(x!=9999) { 21 p=(LNode*)malloc(sizeof(LNode)); 22 p->data = x; 23 if((l->next)==NULL) { 24 l->next = p; 25 q=p; 26 } else { 27 q->next = p; 28 q=p; 29 } 30 scanf("%d",&x); 31 } 32 p->next = NULL; 33 return (l); 34} 35void print_LinkList(LinkList l) 36{ 37 LinkList p; 38 p=l; 39 printf("---"); 40 while(p->next!=NULL) { 41 p = p->next; 42 printf("%5d",p->data); 43 } 44 printf("\n\n"); 45} 46LinkList MergeList(LinkList La, LinkList Lb) 47{ 48 LinkList pa, pb, pc; 49 pa = La->next; 50 pb = Lb->next; 51 Lc = pc = La; 52 while(pa && pb) { 53 if(pa->data<=pb->data) { 54 pc->next = pa; 55 pc = pa; 56 pa = pa->next; 57 } else { 58 pc->next = pb; 59 pc=pb; 60 pb = pb->next; 61 } 62 } 63 pc->next = pa?pa:pb; 64 free(Lb); 65 return(Lc); 66} 67int main(int argc, char* argv[]) 68{ 69 La = Create(); 70 Lb = Create(); 71 MergeList(La,Lb); 72 print_LinkList(Lc); 73 return 0; 74}

3. 超长浮点数相加

1function Sum_TwoFloatNumber(val1, val2) 2{ 3 var TotalNum; 4 val1 = val1 + '' ; 5 var sp_val1 = val1.split(".") ; 6 7 val2 = val2 + '' ; 8 var sp_val2 = val2.split(".") ; 9 10 11 if ((sp_val1.length==2) && (sp_val2.length==2)) { 12 //---兩個數字都是有小數的話--- 13 TotalNum = TotalNum + 0 ; 14 TotalNum = parseFloat(sp_val1[0]) + parseFloat(sp_val2[0]) ; 15 16 var length1 = sp_val1[1].length; 17 var length2 = sp_val2[1].length; 18 19 var length; 20 21 if(length1>=length2) { 22 length = length1; 23 sp_val2[1] = sp_val2[1]*Math.pow(10,length1 - length2); 24 } else if(length1<length2) { 25 length = length2; 26 sp_val1[1] = sp_val1[1]*Math.pow(10,length2 - length1); 27 } 28 29 var temp_second_part = Number(sp_val1[1]) + Number(sp_val2[1]); 30 31 temp_second_part = temp_second_part/Math.pow(10,length); 32 33 TotalNum = TotalNum + temp_second_part; 34 } else { 35 TotalNum = parseFloat(val1) + parseFloat(val2) ; 36 } 37 return TotalNum; 38}

4. _string类

1class _string 2{ 3 friend std::istream& operator>>(std::istream& is, _string& a); 4 friend std::ostream& operator<<(std::ostream& os,_string& a); 5 6public: 7 _string() //???????? 8 { 9 length = 0; 10 b=new char[1]; 11 b[0]='\0'; 12 } 13 _string(char *a); //?????? 14 _string(int n,char a); 15 ~_string(); //???????? 16 _string(_string &a); //????????? 17 int size(){return length;} //???????? 18 _string operator+(const _string& a); //????'+'?????? 19 _string& operator+=(const _string& a); //????'+='?????? 20 _string& operator=(const _string& a); //???????????? 21 char& operator[]( int n); //?????±?????? 22 _string substr(int pos,int n); //????????? 23 _string substr(int pos); //????????? 24 25 26 private: 27 char *b; 28 int length; 29}; 30 31 32// _string.cpp : ??????????ó???????? 33// 34 35#include "stdafx.h" 36#include<iostream> 37#include<string.h> 38#include"_string.h" 39#include<stdlib.h> 40using namespace std; 41 42_string::_string(char *a) //?????? 43{ 44 length = strlen(a); 45 b = new char[length+1]; 46 for(int i= 0;i<length;i++) 47 { 48 b[i] = a[i]; 49 } 50 b[length] = '\0'; 51} 52 53_string::_string(int n,char a) 54{ 55 b=new char[n+1]; 56 for(int i= 0;i<n;i++) 57 { 58 b[i] =a; 59 } 60 b[n] = '\0'; 61} 62 63_string::~_string() //???????? 64{ 65 delete []b; 66 length=0; 67} 68 69_string::_string(_string &a) //????????? 70{ 71 length=a.size(); 72 b=new char [length+1]; 73 for(int i = 0;i<length;i++) 74 { 75 b[i] = a.b[i]; 76 } 77 b[length] = '\0'; 78} 79 80_string _string::operator+(const _string& a) //????+ 81{ 82 int newLen = length+a.length; 83 char *str; 84 str = new char[newLen+1]; 85 int count = 0; 86 for(int i = 0;i<length;i++) 87 { 88 str[i] = this->b[i]; 89 count++; 90 } 91 92 for(int i =0;count<newLen;count++,i++) 93 { 94 str[i] = a.b[i]; 95 } 96 str[newLen] = '\0'; 97 _string temp(str); 98 99 return temp; 100} 101 102_string& _string:: operator+=(const _string& a) //????+= 103{ 104 int newLen = length+a.length; 105 char *str; 106 str = new char[newLen+1]; 107 int count = 0; 108 for(int i = 0;i<length;i++) 109 { 110 str[i] = this->b[i]; 111 count++; 112 } 113 114 for(int i =0;count<newLen;count++,i++) 115 { 116 str[i] = a.b[i]; 117 } 118 str[newLen] = '\0'; 119 _string temp(str); 120 *this=temp; 121 return *this; 122 } 123_string& _string:: operator=(const _string &a) //????= 124 { 125 if(this==&a) 126 return *this; 127 128 delete []b; 129 length = a.length; 130 b = new char[length+1]; 131 for(int i = 0;i<length;i++) 132 { 133 b[i] = a.b[i]; 134 } 135 b[length] = '\0'; 136 return *this; 137 138} 139 140 char& _string:: operator[]( int n) //?????±?????? 141 { 142 if(n>length) 143 return b[length-1]; 144 else 145 return b[n]; 146 } 147 148 ostream& operator<<(ostream& os, _string& a) //????????? 149 { 150 os<<a.b; 151 return os; 152 } 153 istream& operator>>(std::istream& is, _string& a) //????????? 154 { 155 is>>a.b ; 156 a.length=strlen(a.b); 157 return is; 158 } 159 160 _string _string::substr(int pos, int n) //???????????????substr?????????? 161 { 162 char *p = new char[n+1]; 163 for(int i=0;i<n;i++) 164 { 165 p[i]=b[pos]; 166 pos++; 167 } 168 p[n]='\0'; 169 _string k(p); 170 k.length=n; 171 return k; 172 } 173 174 _string _string::substr(int pos) 175 { 176 int len=length; 177 char *p=new char[len-pos+1]; 178 int t=pos; 179 for(int i=0;t<len;t++,i++) 180 { 181 p[i]=b[t]; 182 } 183 p[t]='\0'; 184 _string k(p); 185 k.length=len-pos; 186 return k; 187 }

5. memcpy

1typedef struct memcpy_data_size 2{ 3 int a[16]; 4}DATA_SIZE, *P_DATA_SIZE; 5 6void *mymemcpy(void *to, const void *from, size_t size) 7{ 8 P_DATA_SIZE dst = (P_DATA_SIZE)to; 9 P_DATA_SIZE src = (P_DATA_SIZE)from; 10 11 int new_len = size/sizeof(DATA_SIZE)-1; 12 int remain = size%sizeof(DATA_SIZE)-1; 13 14 while (new_len >= 0) 15 { 16 *dst++ = *src++; 17 new_len--; 18 } 19 while (remain >= 0) 20 { 21 *((char *)dest + remain) = *((char *)src + remain); 22 remain--; 23 } 24 return to; 25}

6. 各种排序 sort

7. 基本容器:queue、stack实现

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Opencv中Mat矩阵相乘——点乘、dot、mul运算详解

Opencv中Mat矩阵相乘——点乘、dot、mul运算详解2016年09月02日00:00:36 \牧野(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fme.csdn.net%2Fdcrmg) 阅读数:59593

C# Aspose.Cells导出xlsx格式Excel,打开文件报“Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃”

报错信息:最近打开下载的Excel,会报如下错误。(xls格式不受影响)!(https://oscimg.oschina.net/oscnet/2b6f0c8d7f97368d095d9f0c96bcb36d410.png)!(https://oscimg.oschina.net/oscnet/fe1a8000d00cec3c

Linux查看GPU信息和使用情况

1、Linux查看显卡信息:lspci|grepivga2、使用nvidiaGPU可以:lspci|grepinvidia!(https://oscimg.oschina.net/oscnet/36e7c7382fa9fe49068e7e5f8825bc67a17.png)前边的序号"00:0f.0"是显卡的代