using namespace std;
int strcmp2(char* pStr1, char* pStr2)
{
if (pStr1 == pStr2)
return 0;
else
return 1;
}
int func()
{
static int iNumber1 = 10;
int iNumber2 = 0;
iNumber1++;
iNumber2++;
return (iNumber1 + iNumber2);
}
int foo(int x,int y)
{
while (x)
{
++x;
++y;
}
if (y)
--y;
return x + y;
}
void funct1(int *x)
{
x = (int *)malloc(sizeof(int));
*x = 12;
}
int Test(int n)
{
int i;
int ret = 0;
for (i = 1; i <= --n; i++) {
if (i > 12)
{
ret /= 2;
break;
}
if (i >= 4 && i <= 6)
continue;
++ret;
}
return ret;
}
void printsum(int x, int y)
{
int sum = 0;
#ifdef DO_IT
sum = x + y;
#endif
printf("%d\n", sum);
}
void test1()
{
printsum(1, 2);
}
#define DO_IT
void test2()
{
printsum(3, 4);
}
void m_14()
{
int a = 5;
{
int b = 10;
++a;
++b;
{
int a = 20;
++a;
a = ++b;
}
++a;
++b;
printf("%d %d ", a,b);
}
printf("%d\n", a);
}
#define SQUARE(z) z * z
void foo3(int x, int *py)
{
int n = SQUARE(x + 1);
x = *py;
*py = n;
}
int foo2(int iParam1,int iParam2)
{
int iRet;
if (iParam1 = 2 || iParam2 == 2)
iRet = iParam1 + iParam2;
else
iRet = iParam1 - iParam2;
return iRet;
}
int main()
{
int opt = 0;
cout << "請輸入想知道第幾題答案" << endl;
cin >> opt;
switch (opt)
{
case 1:
{
cout << "第" << opt << "題:";
char str1[] = "abc";
char str2[] = "abc";
int iVal = strcmp2(str1, str2);
printf("%d\n", iVal);
break;
}
case 2:
{
cout << "第" << opt << "題:";
int iVal;
int i;
for (i = 0; i < 2; i++)
iVal = func();
printf("%d\n", iVal);
break;
}
case 3:
{
cout << "第" << opt << "題:";
int a = 0x7700;
int b = 0x8800;
int c = 0xff00;
int d = (a && b) || c;
int e = (a | b)&c;
printf("%d\n", d);
break;
}
case 4:
{
cout << "第" << opt << "題:";
int a = 0x7700;
int b = 0x8800;
int c = 0xff00;
int d = (a && b) || c;
int e = (a | b) &c;
printf("%X\n", e);
break;
}
case 5:
{
cout << "第" << opt << "題:";
union u_tag{
char cVal[16];
int nVal;
double dVal;
}u;
struct s_tag{
char cVal[16];
int nVal;
double dVal;
}s;
char chVals[16];
char* pVals = chVals;
printf("u= %d , s= %d , chval= %d , pvals= %d", sizeof(u), sizeof(s), sizeof(chVals), sizeof(pVals));
break;
}
case 6:
{
cout << "第" << opt << "題:沒有" << endl;
break;
}
case 7:
{
cout << "第" << opt << "題:";
printf("%d",foo(0, -1));
break;
}
case 8:
{
cout << "第" << opt << "題:";
int x = 10;
funct1(&x);
printf("%d\n", x);
break;
}
case 9:
{
cout << "第" << opt << "題:";
printf("%d\n", Test(5));
break;
}
case 10:
{
cout << "第" << opt << "題:";
printf("%d\n", Test(12));
break;
}
case 11:
{
cout << "第" << opt << "題:";
int n;
for (int c = 0; c <= 6; c++)
{
n = 0;
switch (c){
case 1: n++;
case 2: n++; break;
case 3: n++;
case 4: n++;
case 5: n++; break;
default: n++; break;
}
}
printf("%d\n", n);
break;
}
case 12:
{
cout << "第" << opt << "題:";
test1();
break;
}
case 13:
{
cout << "第" << opt << "題:";
test2();
break;
}
case 14:
{
cout << "第" << opt << "題:";
m_14();
break;
}
case 15:
{
cout << "第" << opt << "題:";
int sum = 0;
int ary[][4] = { 1, 3, 5, 7, 9, 13, 15, 17, 19, 21 };
int(*ptr)[4] = ary;
sum = (*ptr)[1] + ary[0][3] + ary[1][2];
printf("%d", sum);
break;
}
case 16:
{
cout << "第" << opt << "題:自己開註解 答案是5"<<endl;
/*
int x;
while (x < 100)
printf("%d", x);
*/
break;
}
case 17:
{
cout << "第" << opt << "題:";
int ans = foo2(2, 2);
printf("%d\n", ans);
break;
}
case 18:
{
cout << "第" << opt << "題:";
int a = 2;
int b = 10;
foo3(a, &b);
++b;
printf("%d\n", a);
break;
}
case 19:
{
cout << "第" << opt << "題:";
int a = 2;
int b = 10;
foo3(a, &b);
++b;
printf("%d\n", b);
break;
}
case 20:
{
cout << "第" << opt << "題:";
char ary[] = { 0, 1,, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
//int* ++ = +4 byte
char*
char *p1 = ary;
char *p2;
int *pn;
p1 += 2;
pn = (int*)&p1[2];
pn += 1;
p1 = (char*)pn;
--pn;
p2 = (char*)pn;
--p2;
printf("%d\n", *p1);
break;
}
case 21:
{
cout << "第" << opt << "題:";
char ary[] = { 0, 1, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
char *p1 = ary;
char *p2;
int *pn;
p1 += 2;
pn = (int*)&p1[2];
pn += 1;
p1 = (char*)pn;
--pn;
p2 = (char*)pn;
--p2;
printf("%d\n", *p2);
break;
}
case 22:
{
cout << "第" << opt << "題:";
int na[3] = { 30, 20, 10 };
int *pn;
int **ppn;
pn = na;
ppn = &pn;
*pn -= 1;
pn = &na[1];
**ppn += 1;
pn++;
printf("%d\n", na[0]);
break;
}
case 23:
{
cout << "第" << opt << "題:";
int na[3] = { 30, 20, 10 };
int *pn;
int **ppn;
pn = na;
ppn = &pn;
*pn -= 1;
pn = &na[1];
**ppn += 1;
pn++;
printf("%d\n", na[1]);
break;
}
case 24:
{
cout << "第" << opt << "題:";
int na[3] = { 30, 20, 10 };
int *pn;
int **ppn;
pn = na;
ppn = &pn;
*pn -= 1;
pn = &na[1];
**ppn += 1;
pn++;
printf("%d\n", *pn);
break;
}
case 25:
{
cout << "第" << opt << "題:";
int na[3] = { 30, 20, 10 };
int *pn;
int **ppn;
pn = na;
ppn = &pn;
*pn -= 1;
pn = &na[1];
**ppn += 1;
pn++;
printf("%d\n", **ppn);
break;
}
default:
return 0;
}
return 0;
}
沒有留言:
張貼留言