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;
}
输出结果展示: