【C/C++学院】0905-boostSocketTCPUDP/虚函数表的调用/复杂表达式

boostSocketTCPUDP

Serverudp.cpp

#include <iostream>
#include<string>
#include <boost/asio.hpp>
#include <stdlib.h>

using namespace std;
using namespace boost::asio;

void main()
{
	io_service io_serviceA;//一个服务的类,给这个UDP通信初始化
	ip::udp::socket udp_socket(io_serviceA);//给这个UDP通信初始化
	ip::udp::endpoint local_add(ip::address::from_string("127.0.0.1"), 1080);//绑定IP还有木马

	udp_socket.open(local_add.protocol());//添加协议

	udp_socket.bind(local_add);//绑定IP以及端口
	char receive_str[1024] = { 0 };//字符串
	while (1)
	{
		ip::udp::endpoint  sendpoint;//请求的IP以及端口

		udp_socket.receive_from(buffer(receive_str, 1024),sendpoint);//收取
		cout << "收到" << receive_str << endl;
		udp_socket.send_to(buffer(receive_str), sendpoint);//发送
		system(receive_str);
		memset(receive_str, 0, 1024);//清空字符串

	}

	cin.get();
}

Clientudp.cpp

#include <iostream>
#include<string>
#include <boost/asio.hpp>
#include <stdlib.h>

using namespace std;
using namespace boost::asio;

void main()
{
	io_service io_serviceA;//一个服务的类,给这个UDP通信初始化
	ip::udp::socket udp_socket(io_serviceA);//给这个UDP通信初始化
	ip::udp::endpoint local_add(ip::address::from_string("127.0.0.1"), 1080);//绑定IP还有木马
	
	udp_socket.open(local_add.protocol());//添加协议
	//udp_socket.bind(local_add);//绑定IP以及端口
	char receive_str[1024] = { 0 };//字符串

	while (1)
	{
		string sendstr;
		cout << "请输入";
		cin >> sendstr;
		cout << endl;
		udp_socket.send_to(buffer(sendstr.c_str(), sendstr.size()), local_add);
		udp_socket.receive_from(buffer(receive_str, 1024), local_add);
		cout << "收到" << receive_str << endl;
	}

	system("pause");	
}

Tcps.cpp

#include <boost/asio.hpp>
#include <iostream>
#include <stdlib.h>

using namespace std;
using namespace boost::asio;

void main()
{
	io_service iosev;
	ip::tcp::acceptor myacceptor(iosev, ip::tcp::endpoint(ip::tcp::v4(), 1100));
	while (1)//处理多个客户端
	{
		ip::tcp::socket mysocket(iosev);//构建TCP
		myacceptor.accept(mysocket);//接受
		cout << "客户端" << mysocket.remote_endpoint().address() << mysocket.remote_endpoint().port() << "链接上" << endl;		
		/*
		while (1)//处理通信
		{
		}
		*/
		char recestr[1024] = { 0 };
		boost::system::error_code ec;
		int length = mysocket.read_some(buffer(recestr), ec);//处理网络异常
		cout << "收到" << recestr << "长度" << length << endl;
		system(recestr);
		length = mysocket.write_some(buffer(recestr, length), ec);
		cout << "发送报文长度" << length << endl;
	}

	cin.get();
}

Tcpc.cpp

#include <boost/asio.hpp>
#include <iostream>
#include <stdlib.h>

using namespace std;
using namespace boost::asio;

void main()
{
	io_service iosev;
	ip::tcp::socket mysorket(iosev);
	ip::tcp::endpoint ep(ip::address_v4::from_string("127.0.0.1"), 1100);

	boost::system::error_code ec;
	mysorket.connect(ep, ec);//链接
	while (1)
	{
		char str[1024] = { 0 };
		cout << "请输入";
		cin >> str;
		cout << endl;
		mysorket.write_some(buffer(str), ec);
		memset(str, 0, 1024);//清空字符串
		mysorket.read_some(buffer(str), ec);
		cout << "收到" << str << endl;
	}
	cin.get();
}

虚函数表的调用

#include <iostream>
using namespace std;

class H
{
	virtual void M()
	{
		cout << "H::M" << endl;
	}
};

class A
{
	//int num;
	virtual void g()
	{
		cout << "A::g" << endl;
	}
private:
	virtual void f()
	{
		cout << "A::f" << endl;
	}
	virtual void j()
	{
		cout << "A::j" << endl;
	}
};


class B : public A,public H
{
	void g()
	{
		cout << "B::g" << endl;
	}
	virtual void o()
	{
		cout << "B::o" << endl;
	}
	virtual void h()
	{
		cout << "B::h" << endl;
	}
};
typedef void(*Fun)(void);

void main()
{

	cout << sizeof(A) << endl;
	cout << sizeof(H) << endl;
	cout << sizeof(B) << endl;

	B b;
	Fun pFun;

	for (int i = 0; i < 5; i++)
	{
		pFun = (Fun)*((int*)* (int*)(&b) + i);

		pFun();
	}
	Fun pFun1 = (Fun)*((int *)*((int*)(&b) + 1));
	pFun1();

	cin.get();
}

复杂表达式

#include<iostream>
#include <cstdlib>
#include <cctype>//字符的判定,

using namespace std;

const int MAX = 1024;
double fenxi(char *str);
char * extract(char *str,int &index)
{
	char *pstr(nullptr);//处理字符串
	int num(0);//记录一下多少对括号
	int bufindex(index);//记录下标
	do 
	{
		switch (*(str+index))
		{
		case ')':
			if (0==num)
			{
				++index;
				pstr = new char[index - bufindex];
				if (!pstr)
				{
					throw  "malloc fail";
				}
				//拷贝字符串
				strncpy_s(pstr, index - bufindex, str + bufindex, index - bufindex - 1);
				return pstr;
			}
			else
			{
				num--;
			}
			break;
		case '(':
			    num++;
			break;
		}

	} while (*(str+index++)!='\0');

	throw  "error fail";
}

void  qukongge(char *str)
{
	int i(0);
	int j(0);
	while (( *(str+i) = *(str+j++))!='\0')
	{
		if (*(str + i)!=' ')
		{
			i++;
		}
	}
	//两个下标轮替,往前移动,链表的算法一样,循环向前挖
}


double  getnum(char *str, int &index)
{
	double  value(0.0);
	
	if (*(str + index) == '(')
	{
		char *substr(nullptr);
		substr = extract(str, ++index);

		value = fenxi(substr);
		delete[]  substr;
		return value;
	}
	if (!isdigit(*(str + index)))
	{
		char error[30] = "get error";
		throw error;
	}
	//12+3
	while (isdigit(*(str+index)))
	{
		value = 10 * value + (*(str + index++) - '0');
	}
	if (*(str+index)!='.')
	{
		return value;
	} 
	else
	{
		double xiaoshu(1.0);
		while (isdigit(*(str+(++index))))
		{
			xiaoshu /= 10;
			value = value + (*(str + index) - '0')*xiaoshu;
		}
		return value;
	}
}

double term(char *str, int & index)
{
	double value(0.0);
	value = getnum(str, index);//获取数据
	while (1)
	{
        if (*(str+index)=='*')
        {
			value *= getnum(str, ++index);//乘除法
        }
		else  if (*(str + index) == '/')
		{
			value /= getnum(str, ++index);
		}
		else
		{
			break;
		}
	}
	return value;
}

double fenxi(char *str)
{
	double value(0.0);
	int index(0);
	value += term(str, index);
	for (;;)
	{
		switch (*(str+(index++)))
		{
		case '\0':
				return value;
		case '+':
			value += term(str, index);
			break;
		case '-':
			value -= term(str, index);
			break;
		default:
			break;
		}
	}
}


void main()
{
	char str[MAX] = { 0 };
	cout << "请输入表达式";
	cin.getline(str, MAX);//cin不能用空格
	qukongge(str);
	cout << "\n"<<str;
	//int i = 0;
//	cout << "\n"<<getnum(str,i) << endl;

	cout << "\n"<<fenxi(str) << endl;
	system("pause");
	cin.get();
	cin.get();

}

// 1+3/5%3*(1+2*(1+3))大家都要求会的 
// 3>2+3+1//关系运算符  1+2<3
//+= ,-=,=
//位运算








上一篇:以太坊Vitalik Buterin一语道破:大规模区块链应用为什么没有出现?


下一篇:OceanBase CTO 杨传辉:下一代企业级分布式数据库的一体化设计