C++经典机试题目

Wesley13
• 阅读 593

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

#include <iostream>
#include <stack>

using namespace std;

int Priority(char oper)
{
    switch(oper) {
    case '(':
        return 0;
    case '+':
    case '-':
        return 1;
    case '*':
    case '/':
        return 2;
    }
}

int mid_to_post(const string& ori)
{

    char c, t;
    stack<char> syms;
    syms.push('(');
    for (int i = 0; i < ori.size(); i++) {
        c = ori[i];
        switch(c) {
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            //EnQueue(post, c);
            cout << c;
            break;
        case '(':
            syms.push(c);
            break;
        case ')':
        case ';':
            do {
                t = syms.top();
                syms.pop();
                //Pop(S,t);
                if(t!='(')
                    //EnQueue(post, t);
                    cout << t;
            } while(t!='(' && !syms.empty());
            break;
        case '+':
        case '-':
        case '*':
        case '/':
            while(Priority(c)<=Priority(syms.top())) {
                //Pop(S,t);
                //EnQueue(post, t);
                t = syms.top();
                syms.pop();
                cout<<t;
            }
            //Push(S,c);
            syms.push(c);
            break;
        }
    }

    return 0;
}

int main()
{
    cout<<"1+2*3-4;"<<endl;
    mid_to_post("1+2*3-4;");

    getchar();

    return 0;
}

2. 链表归并

#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"

typedef struct LNode {
    int data;
    LNode* next;
} LNode, *LinkList;
LinkList La, Lb, Lc;

LNode* Create()
{
    LinkList l, p, q;
    int x;
    l=NULL;
    l=(LNode*)malloc(sizeof(LNode));
    if(l==NULL) exit(-1);
    l->next = NULL;
    scanf("%d", &x);
    while(x!=9999) {
        p=(LNode*)malloc(sizeof(LNode));
        p->data = x;
        if((l->next)==NULL) {
            l->next = p;
            q=p;
        } else {
            q->next = p;
            q=p;
        }
        scanf("%d",&x);
    }
    p->next = NULL;
    return (l);
}
void print_LinkList(LinkList l)
{
    LinkList p;
    p=l;
    printf("---");
    while(p->next!=NULL) {
        p = p->next;
        printf("%5d",p->data);
    }
    printf("\n\n");
}
LinkList MergeList(LinkList La, LinkList Lb)
{
    LinkList pa, pb, pc;
    pa = La->next;
    pb = Lb->next;
    Lc = pc = La;
    while(pa && pb) {
        if(pa->data<=pb->data) {
            pc->next = pa;
            pc = pa;
            pa = pa->next;
        } else {
            pc->next = pb;
            pc=pb;
            pb = pb->next;
        }
    }
    pc->next = pa?pa:pb;
    free(Lb);
    return(Lc);
}
int main(int argc, char* argv[])
{
    La = Create();
    Lb = Create();
    MergeList(La,Lb);
    print_LinkList(Lc);
    return 0;
}

3. 超长浮点数相加

function Sum_TwoFloatNumber(val1, val2)
{
    var TotalNum;
    val1 = val1 + '' ;
    var sp_val1 = val1.split(".") ;

    val2 = val2 + '' ;
    var sp_val2 = val2.split(".") ;


    if ((sp_val1.length==2) && (sp_val2.length==2)) {
        //---兩個數字都是有小數的話---
        TotalNum = TotalNum + 0 ;
        TotalNum = parseFloat(sp_val1[0]) + parseFloat(sp_val2[0]) ;

        var length1 = sp_val1[1].length;
        var length2 = sp_val2[1].length;

        var length;

        if(length1>=length2) {
            length = length1;
            sp_val2[1] = sp_val2[1]*Math.pow(10,length1 - length2);
        } else if(length1<length2) {
            length = length2;
            sp_val1[1] = sp_val1[1]*Math.pow(10,length2 - length1);
        }

        var temp_second_part = Number(sp_val1[1]) + Number(sp_val2[1]);

        temp_second_part = temp_second_part/Math.pow(10,length);

        TotalNum = TotalNum + temp_second_part;
    } else {
        TotalNum = parseFloat(val1) + parseFloat(val2) ;
    }
    return TotalNum;
}

4. _string类

class _string
{
    friend std::istream& operator>>(std::istream& is, _string& a);
    friend std::ostream& operator<<(std::ostream& os,_string& a);
    
public:
    _string()     //????????
    { 
        length = 0;
        b=new char[1];
        b[0]='\0';
   }             
    _string(char *a);   //??????
    _string(int n,char a);
    ~_string(); //????????
    _string(_string &a);  //?????????
    int size(){return length;}  //????????
    _string operator+(const _string& a);   //????'+'??????
    _string& operator+=(const _string& a);   //????'+='??????
    _string& operator=(const _string& a);   //????????????
    char& operator[]( int n);   //?????±??????
    _string  substr(int pos,int n);   //?????????
   _string  substr(int pos);   //?????????


 private:
    char *b;
    int length;
};


// _string.cpp : ??????????ó????????
//

#include "stdafx.h"
#include<iostream>
#include<string.h>
#include"_string.h"
#include<stdlib.h>
using namespace std;

_string::_string(char *a)     //??????
{
    length = strlen(a);   
    b = new char[length+1];
    for(int  i= 0;i<length;i++)
    {
        b[i] = a[i];
    }
    b[length] = '\0';
}

_string::_string(int n,char a)
{
    b=new char[n+1];
    for(int  i= 0;i<n;i++)
    {
        b[i] =a;
    }
    b[n] = '\0';
}

_string::~_string()     //????????
{
    delete []b;
    length=0;
}

_string::_string(_string &a)      //?????????
{
    length=a.size();
    b=new char [length+1];
   for(int  i = 0;i<length;i++)
    {
        b[i] = a.b[i];
    }
    b[length] = '\0';
}

_string  _string::operator+(const _string& a)    //????+
{
    int newLen = length+a.length;
    char *str;
    str = new char[newLen+1];
    int count = 0;
    for(int i = 0;i<length;i++)
    {
        str[i] = this->b[i];
        count++;
    }
    
    for(int i =0;count<newLen;count++,i++)
    {
        str[i] = a.b[i];
    }
    str[newLen] = '\0';
    _string temp(str);
    
    return temp;
}

_string&  _string:: operator+=(const _string& a)   //????+=
{
    int newLen = length+a.length;
    char *str;
    str = new char[newLen+1];
    int count = 0;
    for(int i = 0;i<length;i++)
    {
        str[i] = this->b[i];
        count++;
    }
    
    for(int i =0;count<newLen;count++,i++)
    {
        str[i] = a.b[i];
    }
    str[newLen] = '\0';
    _string temp(str);
    *this=temp;
    return *this;
    }
_string& _string:: operator=(const _string &a)    //????=
  {
      if(this==&a)
          return *this;

      delete []b;
      length = a.length;
      b = new char[length+1];
      for(int  i = 0;i<length;i++)
    {
        b[i] = a.b[i];
    }
    b[length] = '\0';
          return *this;

}

  char& _string:: operator[]( int n)  //?????±??????
  {
      if(n>length)
          return b[length-1];
      else
          return b[n];
  }

  ostream& operator<<(ostream& os, _string& a)  //?????????
  {
      os<<a.b;
      return os;
  }
  istream& operator>>(std::istream& is, _string& a)  //?????????
  {   
      is>>a.b ;
      a.length=strlen(a.b);
      return is;
  }

 _string  _string::substr(int pos, int n)    //???????????????substr??????????
  {
      char *p = new char[n+1];
      for(int i=0;i<n;i++)
      {
          p[i]=b[pos];
          pos++;
      }
      p[n]='\0';
      _string k(p);
      k.length=n;
      return  k;
 }
     
 _string  _string::substr(int pos)
 {
     int len=length;
     char *p=new char[len-pos+1];
     int t=pos;
     for(int i=0;t<len;t++,i++)
     {
         p[i]=b[t];
     }
     p[t]='\0';
      _string k(p);
      k.length=len-pos;
      return  k;
 }

5. memcpy

typedef struct memcpy_data_size
{
    int a[16];
}DATA_SIZE, *P_DATA_SIZE;

void *mymemcpy(void *to, const void *from, size_t size)
{
    P_DATA_SIZE dst = (P_DATA_SIZE)to;
    P_DATA_SIZE src = (P_DATA_SIZE)from;

    int new_len  = size/sizeof(DATA_SIZE)-1;
    int remain  = size%sizeof(DATA_SIZE)-1;
    
    while (new_len >= 0)
    {
       *dst++ = *src++;
       new_len--;
    }
    while (remain >= 0)
    {
        *((char *)dest + remain) = *((char *)src + remain);
        remain--;
    }
    return to;
}

6. 各种排序 sort

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

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
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
Stella981 Stella981
3年前
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
Stella981 Stella981
3年前
C# Aspose.Cells导出xlsx格式Excel,打开文件报“Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃”
报错信息:最近打开下载的Excel,会报如下错误。(xls格式不受影响)!(https://oscimg.oschina.net/oscnet/2b6f0c8d7f97368d095d9f0c96bcb36d410.png)!(https://oscimg.oschina.net/oscnet/fe1a8000d00cec3c
Stella981 Stella981
3年前
Linux查看GPU信息和使用情况
1、Linux查看显卡信息:lspci|grepivga2、使用nvidiaGPU可以:lspci|grepinvidia!(https://oscimg.oschina.net/oscnet/36e7c7382fa9fe49068e7e5f8825bc67a17.png)前边的序号"00:0f.0"是显卡的代
Stella981 Stella981
3年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Stella981 Stella981
3年前
C++语法学习笔记三十一: lambda表达式,for_each、find_if简介
实例代码//lambda表达式,for_each、find_if简介include<iostreaminclude<vectorinclude<mapinclude<functionalinclude<algorithm
Easter79 Easter79
3年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Stella981 Stella981
3年前
Github标星5300+,专门为程序员开发文档开源管理系统,我粉了
!(https://oscimg.oschina.net/oscnet/a11909a041dac65b1a36b2ae8b9bcc5c432.jpg)码农那点事儿关注我们,一起学习进步!(https://oscimg.oschina.net/oscnet/f4cce1b7389cb00baaab228e455da78d0
Wesley13 Wesley13
3年前
C 编程实现打印 0
有效进制范围\2,36\;include<cstdioinclude<windows.hintmain(){system("chcp437");for(inti0;i<256;i){switch(i){case0:printf("%03
Stella981 Stella981
3年前
Nginx反向代理upstream模块介绍
!(https://oscimg.oschina.net/oscnet/1e67c46e359a4d6c8f36b590a372961f.gif)!(https://oscimg.oschina.net/oscnet/819eda5e7de54c23b54b04cfc00d3206.jpg)1.Nginx反