• # 用类来封装动态数组:分文件编写


    用类来封装动态数组:分文件编写

    头文件:Cmyarrow.h

    #pragma once
    #include<iostream>
    using namespace std;
    #define DELE_OBJ(p) {if(p!=NULL){delete[] p;p=NULL;}}
    typedef int TYPE;
    class Cmyarrow
    {
    	TYPE* buff;
    	TYPE len;
    	TYPE maxsize;
    public:
    	void push_back(TYPE data);//尾插
    	void pop_back();//尾删
    	int length()const;//获取元素个数
    	void insert(TYPE index, TYPE data);//函数重载,在某个位置插入
    	TYPE& at(TYPE index);//获取下标为index的元素
    	void clear();//清空内存
    	void print();//输出动态数组
    public:
    	Cmyarrow();
    	~Cmyarrow();
    };
    
    
    

    函数实现:Cmyarrow.cpp

    #include "Cmyarrow.h"
    
    void Cmyarrow::push_back(TYPE data)
    {
    	if (len >= maxsize)
    	{
    		maxsize = maxsize + ((maxsize >> 1) > 1 ? (maxsize >> 1) : 1);
    		TYPE* tempbuff = new TYPE[maxsize];
    		memcpy(tempbuff, buff, sizeof(TYPE)*len);
    		DELE_OBJ(buff);
    		buff = tempbuff;
    	}
    	buff[len] = data;
    	len++;
    }
    
    void Cmyarrow::pop_back()
    {
    	len--;
    }
    
    int Cmyarrow::length()const
    {
    	return len;
    }
    
    void Cmyarrow::insert(TYPE index, TYPE data)
    {
    	if (index<0 || index>len)
    	{
    		cout << "请输入正确的下标" << endl;
    		return;
    	}
    	if (len >= maxsize)
    	{
    		maxsize = maxsize + ((maxsize >> 1) > 1 ? (maxsize >> 1) : 1);
    		TYPE* tempbuff = new TYPE[maxsize];
    		memcpy(tempbuff, buff, sizeof(TYPE)*len);
    		DELE_OBJ(buff);
    		buff = tempbuff;
    	}
    	for (int i = len; i > index; i--)
    	{
    		buff[i] = buff[i-1];
    	}
    	buff[index] = data;
    	len++;
    
    }
    
    TYPE & Cmyarrow::at(TYPE index)//用来输出元素
    {
    	if (index<0 || index>len)
    	{
    		cout << "请输入正确的下标" << endl;
    		exit(1);//异常退出
    	}
    	// TODO: 在此处插入 return 语句
    	return buff[index];
    }
    
    void Cmyarrow::clear()
    {
    	DELE_OBJ(buff);//释放内存
    	len = 0;
    	maxsize = 0;
    }
    
    void Cmyarrow::print()
    {
    	for (int i = 0; i < len; i++)
    	{
    		printf("%d  ", buff[i]);
    	}
    }
    
    Cmyarrow::Cmyarrow()//构造函数用来初始化数据成员的
    {
    	buff = NULL;
    	len = 0;
    	maxsize = 0;
    }
    
    Cmyarrow::~Cmyarrow()
    {
    	clear();
    }
    
    

    调用这个类,实现功能:main.cpp

    #include<iostream>
    
    
    #include"Cmyarrow.h"
    using namespace std;
    int main()
    {
    	Cmyarrow p;//先实例化对象
    	for (int i = 0; i < 10; i++)
    	{
    		p.push_back(i + 1);//插入数据
    	}
    	p.insert(3, 100);//在中间位置插入元素
    	p.pop_back();//删除最后一个元素
    	for (int i = 0; i < p.length(); i++)
    	{
    		cout << p.at(i) << " ";//输出动态数组
    	}
    	cout << endl;
    	cout << p.length() << endl;//获取元素个数
    
    
    	return 0;
    }
    

    裁剪图片

    #include<iostream>
    #include<easyx.h>
    using namespace std;
    int main()
    {
    	IMAGE img1, img2;//定义两张图片
    	loadimage(&img1, "c.jpg");//加载要裁剪的图片
    	initgraph(989, 989);//初始化窗口
    	putimage(0, 0, &img1);//将这张图片放在窗口上
    	char str[100] = { 0 };//保存你裁下来每一张图片的名字
    	int num = 1;//给每一张图片编号
    	//一共是30*30张图片,用两个for循环
    	for (int i = 0; i < 30; i++)
    	{
    		for (int j = 0; j < 30; j++)
    		{
    			sprintf(str, "rec/%d.jpg", num);//用rec这个文件来接收这900张图片
    			num++;
    			//裁剪大图片里的900张图照片
    			getimage(&img2, j * 33, i * 33, 32, 32);
    			saveimage(str, &img2);//保存裁下来的图片到str中
    		}
    	}
    	system("pause");
    	return 0;
    
    }
    
  • 相关阅读:
    Log4net快速搭建
    WebAPI中Area的使用
    (三)Redis for StackExchange.Redis
    (二)Redis for 阿里云公网连接
    Python+CGI,在Windows上快速部署Python到IIS
    腾讯云
    UIView添加事件
    Sublime Text Packages Control 安装
    乎乎测试
    常用第三方类库
  • 原文地址:https://www.cnblogs.com/Kissfly123/p/14426184.html
Copyright © 2020-2023  润新知