📄 金山问题.txt
字号:
#define FALSE 0
BOOL jiba(int *p1, int *p2, int**pt)
{
if ((p1 == NULL) || (p2 == NULL) ||
(p1[0] > p1[1]) || (p2[0] > p2[1]))
{
printf("输入有误\n");
return FALSE;
}
if (p2[0] <= p1[0])
{
if (p2[1] < p1[0])
return FALSE;
else
{
*pt = new int[2];
(*pt)[0] = p1[0];
(*pt)[1] = (p2[1]<=p1[1] ? p2[1]:p1[1]);
return TRUE;
}
}
else
{
if (p2[0] > p1[1])
return FALSE;
else
{
*pt = new int[2];
(*pt)[0] = (p1[0] >= p2[0] ? p1[0]:p2[0]);
(*pt)[1] = p1[1];
return TRUE;
}
}
return TRUE;
}
//test!
int rt[2] ={1,5};
int rt1[2]={3,9};
int main()
{
int *p = NULL;
BOOL b = jiba(rt,rt1,&p);
if (b)
{
printf("[%d,%d]与[%d,%d]的交集为[%d,%d]\n",rt[0],rt[1],rt1[0],rt1[1], p[0],p[1]);
delete p;
}
else
{
printf("[%d,%d]与[%d,%d]的交集为空\n",rt[0],rt[1],rt1[0],rt1[1]);
}
getchar();
return 0;
}
第一道:
两个数 int a, int b,不用中间变量交换两个值的C++代码.
第二道,分析程序输出.代码如下:
#include <iostream.h>
class B {
public:
int i, j;
B(int x = 999) :j(x), i(j)
{
cout << "B::B() invoked\n" << endl;
}
~B()
{
cout << "B::~B() invoked\n" << endl;
}
};
class D : public B {
public:
D()
{
cout << "D::D() invoked\n" <<endl;
}
~D()
{
cout << "D::~D() invoked\n" << endl;
}
};
void main()
{
D d;
cout << "d.i" << d.i << endl;
cout << "d.j=" << d.j << endl;
}
结果:
B::B() invoked
D::D() invoked
d.i-858993460
d.j=999
D::~D() invoked
B::~B() invoked
_cdecl
按从右至左的顺序压参数入栈,由调用者把参数弹出栈。对于“C”函数或者变量,修饰名是在函数名前加下划线。对于“C++”函数,有所不同。
如函数void test(void)的修饰名是_test;对于不属于一个类的“C++”全局函数,修饰名是?test@@ZAXXZ。
这是缺省调用约定。由于是调用者负责把参数弹出栈,所以可以给函数定义个数不定的参数,如printf函数。
_stdcall
按从右至左的顺序压参数入栈,由被调用者把参数弹出栈。对于“C”函数或者变量,修饰名以下划线为前缀,然后是函数名,然后是符号“@”及参数的字节数,如函数int func(int a, double b)的修饰名是_func@12。对于“C++”函数,则有所不同。
所有的Win32 API函数都遵循该约定。
_pascal
按从左至右的顺序压参数入栈 ...其它的与_stdcall相同;
_fastcall
头两个DWORD类型或者占更少字节的参数被放入ECX和EDX寄存器,其他剩下的参数按从右到左的顺序压入栈。 由被调用者把参数弹出栈,对于“C”函数或者变量,修饰名以“@”为前缀,然后是函数名,接着是符号“@”及参数的字节数,如函数int func(int a, double b)的修饰名是@func@12。对于“C++”函数,有所不同。
未来的编译器可能使用不同的寄存器来存放参数。
thiscall
仅仅应用于“C++”成员函数。this指针存放于CX寄存器,参数从右到左压栈。thiscall不是关键词,因此不能被程序员指定。
naked call
采用1-4的调用约定时,如果必要的话,进入函数时编译器会产生代码来保存ESI,EDI,EBX,EBP寄存器,退出函数时则产生代码恢复这些寄存器的内容。naked call不产生这样的代码。
naked call不是类型修饰符,故必须和_declspec共同使用,如下:
__declspec( naked ) int func( formal_parameters )
{
// Function body
}
便于更好理解, 看下面例子(函数调用的过程以汇编代码表示):
void cdecl fun1(int x,int y);
void stdcall fun2(int x,int y);
void pascal fun3(int x,int y);
****************************************
void cdecl fun1(int x,int y);
fun1(x,y);
调用 fun1 的汇编代码
push y
push x
call fun1
add sp,sizeof(x)+sizeof(y) ;跳过参数区(x,y)
fun1 的汇编代码:
fun1 proc
push bp
mov bp,sp
……
…
pop bp
ret ;返回,但不跳过参数区
fun1 endp
****************************************
void stdcall fun2(int x,int y);
fun2(x,y);
调用 fun2 的汇编代码
push y
push x
call fun2
fun2 的汇编代码:
fun2 proc
push bp
mov bp,sp
……
…
pop bp
ret sizeof(x)+sizeof(y) ;返回并跳过参数区(x,y)
fun2 endp
*****************************************
void pascal fun3(int x,int y);
fun3(x,y);
调用 fun3 的汇编代码
push x
push y
call fun3
fun3 的汇编代码:
fun3 proc
push bp
mov bp,sp
……
…
pop bp
ret sizeof(x)+sizeof(y) ;返回并跳过参数区(x,y)
fun3 endp
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -