学习4-9期视频笔记
1、汉诺塔
#define_CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int a[10][3] = {0 };
void hanno(intn, char A, char B, char C);//移动过程
void show(inta[10][3]);//显示
void move(charX, char Y);//移动,链接数据,视图
void show(inta[10][3])
{
printf("%5c%5c%5c", 'A', 'B','C');
printf("\n-------------------\n");
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%5d",a[i][j]);
}
printf("\n");
}
printf("\n");
}
void hanno(intn, char A, char B, char C)
{
if (n < 1)
{
return;
}
else if(n == 1)
{
printf("\n%c->%c\n",A, C);
move(A, C);
show(a);
}
else
{
hanno(n - 1, A, C, B);
printf("\n%c->%c", A,C);
move(A, C);
show(a);
hanno(n - 1, B, A, C);
}
}
void move(charX, char Y)
{
int m = X - 65;
int n = Y - 65;
int imove = -1;
for (int i = 0; i < 10; i++)
{
if (a[i][m] != 0)
{
imove = i;
break;
}
}
int jmove;
if (a[9][n] == 0)
{
jmove = 9;
}
else
{
jmove = 10;
for (int i = 0; i < 10; i++)
{
if (a[i][n] != 0)
{
jmove = i;
break;
}
}
jmove -= 1;
}
//int tmp=a[imove][m];
//a[imove][m] = a[jmove][n];
//a[jmove][n] = tmp;
a[imove][m] = a[imove][m] + a[jmove][n];
a[jmove][n] = a[imove][m] - a[jmove][n];
a[imove][m] = a[imove][m] - a[jmove][n];
}
void main()
{
int n = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
a[10 - 1 - i][0] = n - i;
}
show(a);
hanno(n, 'A', 'B', 'C');
system("pause");
}
2、程序跟随鼠标移动
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
void main()
{
HWND win =FindWindowA("TXGuiFoundation", "QQ");
while (1)
{
POINT pt;
pt.x = pt.y = 0;//鼠标的相对位置
GetCursorPos(&pt);
SetWindowPos(win, NULL, pt.x + 100,pt.y + 100, 0, 0, 1);
}
}
3、恶作剧换桌面
SystemParametersInfoA(20,0, "", 3);//20设定桌面背景,0默认,桌面素材路径,3即刻生效
ShellExecuteA(0,"open", "路径", 0, 0, 0);//第一个数据表示系统打开,第二个参数为执行操作,最后一个参数,0隐藏,1正常,3最大化,6最小化
4、volatile
Release(商业发行模式)模式下编译器会自动优化代码,debug模式则禁止优化
for(volatile int i=0;i<9999999;i++){ }//int 前加volatile,可使release模式下不进行优化
printf(“helloworld”);
5、源码补码反码
#define_CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
void bcode(intnum,char strb[33])
{
unsigned int data = 1 << 31;//左移
for (int i = 1; i <= 32; i++)
{
if (num &data)
{
strb[i - 1] = '1';
}
else {
strb[i- 1] = '0';
}
num <<= 1;
}
}
void fcode(intnum, char strf[33])
{
unsigned int data = 1 << 31;//左移
if (num < 0)
{
num = num - 1;//求出反码
}
for (int i = 1; i <= 32; i++)
{
if (num &data)
{
strf[i - 1] = '1';
}
else {
strf[i - 1] = '0';
}
num <<= 1;
}
}
void ycode(intnum, char stry[33])
{
unsigned int data = 1 << 31;//左移
if (num < 0)
{
num = ~num + 1;//求出
num = num | data;//处理符号位
}
for (int i = 1; i <= 32; i++)
{
if (num &data)
{
stry[i - 1] = '1';
}
else {
stry[i - 1] = '0';
}
num <<= 1;
}
}
void main()
{
int num;
scanf("%d", &num);
char stry[33] = { 0 };
char strf[33] = { 0 };
char strb[33] = { 0 };
bcode(num,strb);
printf("%s\n", strb);
fcode(num,strf);
printf("%s\n", strf);
ycode(num,stry);
printf("%s", stry);
system("pause");
}
6、进制转化
(1)
#define_CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
void to2(intnum);
void to8(intnum);
void to16(intnum);
void main()
{
int num=0;
scanf("%d", &num);
to2(num);
printf("\n");
to8(num);
printf("\n");
to16(num);
system("pause");
}
void to2(intnum)
{
if (num == 0)
{
return;
}
else {
//放在这里是顺序输出
to2(num / 2);
printf("%d", num%2);//放在后面是逆序输出
}
}
void to8(intnum)
{
if (num == 0)
{
return;
}
else {
//放在这里是顺序输出
to8(num / 8);
printf("%d", num % 8);//放在后面是逆序输出
}
}
void to16(intnum)
{
if (num == 0)
{
return;
}
else {
//放在这里是顺序输出
to16(num / 16);
printf("%x", num %16);//放在后面是逆序输出
}
}
(2)字符串型
#define_CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void to2(intnum);
void to8(intnum);
void to16(intnum);
void main()
{
int num=0;
int str2[32] = { 0 };
int str8[32] = { 0 };
int str16[32] = { 0 };
scanf("%d", &num);
to2(num,0,str2);
_strrev(str2);
printf("%s\n", str2);
to8(num,0,str8);
_strrev(str8);
printf("%s\n", str8);
to16(num,0,str16);
_strrev(str16);
printf("%s", str16);
system("pause");
}
void to2(intnum,int i,char str2[32])
{
if (num == 0)
{
return;
}
else {
//放在这里是顺序输出
to2(num / 2,i+1,str2);//位数向前
str2[i] = (num % 2) + '0';//字符整数的转化
}
}
void to8(intnum,int i,char str8[32])
{
if (num == 0)
{
return;
}
else {
//放在这里是顺序输出
to8(num / 8,i+1,str8);
//printf("%d", num %8);//放在后面是逆序输出
str8[i] = (num % 8) + '0';//字符整数的转化
}
}
void to16(intnum,int i,char str16[32])
{
if (num == 0)
{
return;
}
else {
//放在这里是顺序输出
to16(num / 16,i+1,str16);
//printf("%x", num %16);//放在后面是逆序输出
if (num % 16 < 10)
{
str16[i] = (num % 16) +'0';//字符整数的转化
}
else
{
str16[i] = (num % 16) - 10+ 'A';
}
}
}
(3)mfc
int num = 0;
CStringA str;
m_edit0.GetWindowTextA(str);//读取
sscanf(str, "%d", &num);//字符串赋值
char str2[32] = { 0 };
char str8[32] = { 0 };
char str16[32] = { 0 };
to2(num, 0, str2);//2进制转换函数
_strrev(str2);//字符串逆转
m_edit1.SetWindowTextA(str2);//输出
7、寄存器变量
(1)汇编语言 num=num+15;
_asm {
mov eax,num //eax是一个寄存器,num值传给eax
add eax,15 //eax+15
mov num,eax //eax值赋值给num
}
(2)寄存器变量
//寄存器变量在cpu内部,速度快
//频繁使用的变量,需放在寄存器
//vc会自动优化,即使没有声明寄存器变量,vc也会自动优化
//而gcc就不会自动优化
//静态参数,全局变量最好不用寄存器,会影响程序运行速度
//c不可以查看寄存器变量的地址,c++与c不同,c++会在内存中创建寄存器副本,
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<Windows.h>
void main()
{
time_t start, end;
time(&start);
//Sleep(3000);
registerdouble res = 0.0; //在非vc环境下测试,加上(2秒)与不加(10秒)时间差了很多
registerint i = 0;
for (; i < 1000000000; i++)
{
res += i;
}
printf("%f\n", res);
time(&end);
printf("%d",(unsignedint)(end-start));
getchar();
}
8、dll调用
(1)、创建dll动态库
#include<Windows.h>
_declspec(dllexport)void go()
{
MessageBoxA(0, "hello","world", 0);
}
_declspec(dllexport)int add(int a,int b)
{
return a + b;
}
(2)调用dll
#include<Windows.h>
#include<stdio.h>
voidmain()
{
HMODULE hmod = LoadLibraryA("Project7.dll");//读取project7.dll
if (hmod == NULL)
{
system("error load");
}
void(*p1)()=(void(*)())GetProcAddress(hmod, "go");//获取dll文件中的函数
int(*p2)() = (int(*)(int,intb))GetProcAddress(hmod, "add");
if (p1 == NULL||p2==NULL)//查看是否找到
{
system("error find");
}
else {
p1();
printf("%d", p2(10,20));
}
FreeLibrary(hmod);
system("pause");
}
9、使用位运算加减法
int add(int a,int b)//3,5
{
int wei = 0;
int jinwei = 0;
do
{
wei = a^b;
jinwei = (a&b)<<1;
a = wei;
b = jinwei;
} while (b != 0);
return a;
}
//0011a
//0101b
//0110a 6
//0010b 2
//0100a 4
//0100b 4
//0000a
//1000b
int add1(inta,int b)
{
int wei = a ^ b;
int jinwei = a & b;
return !jinwei ? wei : add(wei ,jinwei<<1);
}
减法
intsubtract1(int a ,int b)
{
if(!b)
return a;
else
{
int bit = a & b;
a^= bit;
b^= bit;
a|=b;
subtract1(a,b<<1);
}
}
//01015
//00113
//0001bit
//0100a
//0010b
//0110a
//0100b
//0100bit
//0010a
//0000b