c语言转向c++学习,弹幕,动图,面向对象,书写方式

c++基本格式


#include<iostream>/*stdio.h*/
#include<cstdlib>/*stdlib.h*/
#include<cstring>/*string.h*/
using namespace std;
	//声明命名三种方式:
	//1,std::cout
	//2,using namespace std
	//3,using std::cout
struct/*define identifiers*/ Testcpp
/*custom names start with capital letters*/
{
	int ID;
	char name[10];
	/*可以在结构体里定义函数*/
	void show()
	{
		cout << "我很帅" << endl;
	}
};
Testcpp stu;/*struct Testcpp stu;struct can be omitted*/
int main()
{
	int/*七个基本类型 bool、char、int、float、double、void、wchar_t*/ num;
	/*set a variable*/

	std::cin >> num;
	/*scanf("%d",&num);*//*don't have to take the address,automatic type recognition*/

	std::cout << "hello" <<num<< std::endl;
	/*printf("hello");*/

	char name[10] = "THIM";/*specified type output*/
		cout << name << endl;

		bool xiu = true;/*true or false*/
		cout << boolalpha << xiu << endl;
		/*specify the output.it doesn't automatically convent to 0 or 1*/
		xiu = false;
		cout << noboolalpha << xiu << endl;
	
		stu.show();

	return 0;
}

输出结果展示:

在这里插入图片描述


#include<iostream>
using namespace std;
void main()
{
	int a;
	for (int i = 0;i < 2;i++)
	{
		cin >> a;
		cout << a << endl;/*endl为换行符*/
	}
}

输出结果展示:

在这里插入图片描述


数据交换与模板类型的引用

#include<iostream>
void MySwap(int& a, int& b)/*data exchange*/
{
	int temp = a;
	a = b;
	b = temp;
} 
/*tempalte technology,编写模板可以忽略类型*/
//template<class T>/*利用T代替相应类型,template<typename T>*/
//void MySwap(T& a, T& b)/*template function*/
//{
//	T temp = a;
//	a = b;
//	b = temp;
//}
using namespace std;
int main(void)
{
	int a, b;
	cin >> a>> b;
	MySwap(a, b);
	cout << a<<'\t'<<b << endl;
	return 0;
	
}

输出结果展示:
在这里插入图片描述


模板函数

函数模板不允许自动类型转化
普通函数允许自动进行类型转化

#include<iostream>
using namespace std;
template<class T>/*template function*/
int MyAdd(T a, T b)
{
	return a + b;
}
int MyAdd(int a, char c)//普通函数可以进行自动类型转换
{
	return a + c;
}
void test()//cpp优先考虑普通函数
{
	int a = 10;
	int b = 20;
	char c1 = 'a';
	char c2 = 'b';
	MyAdd(a,c1);/*调用普通函数*/
	MyAdd(a,b);/*调用模板函数*/
	MyAdd(c1,b);/*调用普通函数*/
	cout << MyAdd(a, c1) << endl;
	cout << MyAdd(a,b) << endl;
	cout << MyAdd(c1,b) << endl;
	//MyAdd<>(a, b);强制调用模板函数
}
int main(void)
{
	test();
	return 0;
}

输出结果展示:
在这里插入图片描述


算法:冒泡排序

#include<iostream>
using namespace std;
void BubbleSort(int a[],int n)/*call the BubbleSort function*/
{
	for (int i = 0;i < n;i++)
		for (int j = 0;j < (n - 1);j++)/*循环嵌套,两两交换一共需要的次数*/
			if (a[j] > a[j + 1])
			{
				int temp = a[j];
				a[j] = a[j + 1];
				a[j + 1] = temp;
			}
	for (int c = 0;c < n;c++)
		cout << a[c]<<endl;/*output*/
}
int main()
{
	int a[10],r;
	for (r = 0;r < 10;r++)
	{
		cin >> a[r];
	}
	BubbleSort(a,10);/*calling a SubFunction*/
	return 0;
}

输出结果展示:

在这里插入图片描述


筛选一组数中的最大值

#include<iostream>
using namespace std;
int Max_use(int a[], int n)/*it's no different from C*/
{
	int max = 0;
	for (int i = 0;i < n;i++)
		if (a[i] > max)
			max = a[i];
	return max;
}
int main()
{
	int a[10],n=1;
	for (int i = 0;i < 10;i++)
	{
		cin >> a[i];
		n += 1;
	}
	Max_use(a, n);
	cout << "max=" << Max_use(a, n) << endl;
	return 0;
}

输出结果展示:

在这里插入图片描述


图形界面设置

#include<iostream>/*点的移动*/
#include<graphics.h>/*图形界面库*/
#include<time.h>
struct Point/*结构体*/
{
	int x;
	int y;
	int speed;
	COLORREF color;//整型颜色变量
};
int main()
{
	srand((unsigned)time(NULL));
	initgraph(500, 500);//创建一个窗口
	Point pos;
	pos.x = 50;
	pos.y = 50;
	pos.color = RGB(rand() % 256, rand() % 256, rand() % 256);//随机生成颜色
	pos.speed = rand() % 10 + 1;
	while (1)//移动点
	{
		putpixel(pos.x, pos.y, pos.color);
		pos.x += pos.speed;
		Sleep(100);//执行挂起一段时间
	}
	system("pause");//暂停
	return 0;
}

输出结果展示:

在这里插入图片描述


动态星空布景

#include<iostream>/*点的移动*/
#include<graphics.h>/*图形界面库*/
#include<time.h>
struct Point/*结构体*/
{
	int x;
	int y;
	int speed;
	COLORREF color;//整型颜色变量
};
Point pos[500];
void Initpos(int i)
{
	pos[i].x = 0;//从屏幕左上角开始
	pos[i].y = rand()%500;//对总列数取余随机生成点
	pos[i].color = RGB(rand() % 256, rand() % 256, rand() % 256);//随机生成颜色
	pos[i].speed = rand() % 10 + 1;//控制下一个点出现的位置
}
void MovePos(int i)
{
	putpixel(pos[i].x, pos[i].y,BLACK);//去除上一个位置的点
	pos[i].x += pos[i].speed;//移动点
	if (pos[i].x > 500)//超出界面的点重新创建
		Initpos(i);
	putpixel(pos[i].x, pos[i].y, pos[i].color);//画出新的点
}
int main()
{
	srand((unsigned)time(NULL));
	initgraph(500, 500);//创建一个窗口
	Point pos;
	for (int i = 0;i < 500;i++)
	{
		Initpos(i);//循环输入项
	}
	while (1)
	{
		for (int i = 0;i < 500;i++)
		{
			MovePos(i);
		}
		Sleep(20);//移动速度调整
	}
	system("pause");//暂停
	return 0;
}

输出结果展示:

在这里插入图片描述

文字显示:

#include<iostream>
#include<graphics.h>
#include<conio.h>
int main()
{
	initgraph(500, 500);//Window Settings
	outtextxy(50, 50, "今天依旧很帅");//打印文字
	system("pause");//引用DOS命令保留窗口
	return 0;
}

输出结果展示:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

THIM

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值