C++经典机试题目

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实现


转载于:https://my.oschina.net/u/178169/blog/103027

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值