You are on page 1of 21

PART 02

C/C++
C/C++ program design
C/C++ C/C++
C
C Java
C++ Java C++
Java C/C++
C++ Java

strcpy
strcpy strcpy
10 2 C++

chapter

5.1

5.2 i++

5.3

5.4

5.5 And/Or/Not

5.6 ab

5.7 C C++

5.8

05

[ ]

5.1

What does the following program print? [


2005 12 ]
#include <iostream>
using namespace std;
int main()
{
int x=2,y,z;
x *=(y=z=5); cout << x << endl;
z=3;
x ==(y=z);

cout << x << endl;

x =(y==z);
x =(y&z);
x =(y&&z);
y=4;
x=(y|z);
x=(y||z);
return 0;

cout << x << endl;


cout << x << endl;
cout << x << endl;
cout << x << endl;
cout << x << endl;

x *=(y=z=5) 5 zz yx=x*y x 2*5=10


x ==(y=z) z y x y x
10
x =(y==z) y z 1
0 y z 3 1 1 x x
1
x =(y&z) y z bit-wise andy 3z 3y
0011z 0011bit-wise and

5-2

05

y&z

y&z 0011 3 x x 3
x =(y&&z) y z and y z
(y&&z) 1 yz 3 1 x 1
x =( y|z ) y z bit - wise ory 4z 3y
0100z 0011
y

y&z

y|z 0111 7 x x 7
x =(y||z) y z or y z
(y||z) 1 yz 1 x 1
10101371

What does the following program print ? [


2005 12 ]
#include <iostream>
using namespace std;
int Vac=3;
int main() {
int Vac=10;

A. 11 11

B. 11 4

::Vac++;
cout<<::Vac << endl;
cout<<Vac << endl;
return 0;
}

C. 10 4

D. 4 10

5-3

[ ]

5.2 i++

[ 2007 10 ]
1
#include<iostream>
using namespace std;
main()
{
int a,x;
for(a=0,x=0;a<=1 &&!x++;a++)

a++;

}
cout<<a<<x<<endl;
}

2
#include<iostream>
using namespace std;
main()
{
int a,x;
for(a=0,x=0;a<=1 &&!x++;)

a++;

}
cout<<a<<x<<endl;
}

for for(a=0,x=0; a<=1 &&!x++;a++)


for(a=0,x=0;a<=1 &&!x++;)
1
1 a=0x=0
2 a 1x 1
3 x++ x 1
4 a++a 1
5 for(a=0,x=0;a<=1 &&!x++;a++) a++a 2
6 a 2 1 && !x++
x 1

5-4

05

7 a x 2 1
2
1 a=0x=0
2 a 1x 1
3 x++ x 1
4 a++a 1
5 a 1 1 && !x++ x
1x 0 x++ 2
6 a x 1 2
21 12

What will be the output of the following C code? [


H 2007 7 ]
#include <stdio.h>
main()
{
int b=3;
int arr[]={6,7,8,9,10};
int *ptr=arr;

A. 8 8

B. 130 8

*(ptr++)+=123;
printf( "%d,%d\n ",*ptr,*(++ptr));
}

C. 7 7

D. 7 8

C printf

printf( "%d\n ",*ptr); ptr 6


*(ptr++)+=123 *ptr=*ptr+123;ptr++ ptr 7

5-5

[ ]

printf( "%d\n ",*(ptr-1)); 129


printf( "%d\n ",*ptr); 7 ptr 7
printf( "%d,%d\n ",*ptr,*(++ptr)); (++ptr) ptr++*ptr=8
ptr 8 8
A

5.3

We have two pieces of code , which one do you prefer, and tell why.
[ 2005 10 ]
A.

B.
// a is a variable

1
if( 'A'==a ) {
a++;

2
if( a=='A' ) {
a++;
}

1
for(i=0;i<8;i++) {
X= i+Y+J*7;
printf("%d",x);
}

2
S= Y+J*7;
for(i=0;i<8;i++) {
printf("%d",i+S);
}


A. 'A'==a == =

B.

5-6

05

5.4

[ CPU 2007 10 ]
#include <iostream>
using namespace std;
int main()
{
unsigned char a=0xA5;

unsigned char b=~a>>4;


//cout <<b;
printf("b=%d\n",a);
return 0;

unsigned char b=~a>> 4 a


4 int
unsigned char b
>> ~ 0xA5 4
1010 0101 4 0000 1010 1111 0101
245

1 oat a=10.2;int b;b=a/2;b


5.1 2 float 5.1 5.1
int 5 b
2 unsigned char a=0xa5,b;b=a*2;b
unsigned char
a int unsigned char

8 b 1652 256=74

5-7

[ ]

255
unsigned char

245

[ S 2007 8 ]
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <conio.h>
using namespace std;
int main()
{
oat a = 1.0f;
cout << (int)a << endl;
cout << &a << endl;

cout << (int&)a << endl;


cout << boolalpha << ( (int)a == (int&)a )
<< endl;
//
oat b = 0.0f;
cout << (int)b << endl;
cout << &b << endl;
cout << (int&)b << endl;
cout << boolalpha << ( (int)b == (int&)b )
<< endl;
//
return 0;
}

cout << (int&)a << endl; 1065353216


1(int&)a
sizeof(int) int float
(int&)a 1
oat a = 1.0f 3f800000
(int&)a 3f8000000 int
10653532160x3f800000
false true 0 1

5-8

05

[ S 2007 8 ]
#include <stdio.h>
int
{

unsigned char i = (unsigned char)a;


char* b = (char*)&a;

main()
unsigned int a = 0xFFFFFFF7;

printf("%08x, %08x", i,*b);

unsigned int unsigned char



char* b = (char*)&a a uint
char char char

&a a &a
unsigned int a
char *b = (char *)&a ;

unsigned int *p = &a;


char *b = (char *)p;

//papa
//ba

unsigned int char


char char
a x
p + 1 = x + 1*sizeof(int) = x + 1* 4 = x + 4;
b + 1 = x + 1*sizeof(char) = x + 1* 1 = x + 1;

000000f7fffffff7

5-9

[ ]

C++

1.
Arithmetic
Conversion

int ival = 3;
double dval = 3.14159;

// ivaldouble3.0
ival + dval;

2.

0 int int* double


int
// 0 int*
int *pi = 0;

// dval int 3
ival = dval;

3.

extern double sqrt( double );


// 2 double2.0
cout << "The square root of 2 is "<< sqrt( 2 ) << endl;

4.

double difference( int ival1,int ival2 )


{
// double

return ival1 - ival2;

5-10

05

long double
long double
long double a long double
ASC 97 long double
3.14159L + 'a';

long double double


double
int ival;
oat fval;
double dval;

//fvalival
double
dval + fval + ival;

double oat
oat
char cval;
int ival;
oat fval;

//ivalcval
double
cval + fval + ival;

3
int
integral promotion
charsigned charunsigned char short int
int unsigned short short
int unsigned short int int
unsigned intwchar_t underlying
type (enum)
enum status { bad, ok };

0 1 char
char char status
int

5-11

[ ]

char cval;
bool found;
enum mumble { m1, m2, m3 } mval;

unsigned long ulong;


cval + ulong; ulong + found;
mval + ulong;

cval found mval int


unsigned long
unsigned long ulong 3
unsigned long unsigned long
long long
char cval;
long lval;
// cval1024long
cval + 1024 + lval;

long long unsigned int


long unsigned int
32 long int word

unsigned int long unsigned
long long unsigned int
unsigned int int

5.5 And/Or/Not

1
N

X 2 (2,4,8,16,)[
CPU 2007 10 ]

24816 10100100010000 X 1 X and


N

0 X 2
!(X&(X 1))
5-12

05

[ S 2007 8 ]
# include <iostream>
# include <string>
using namespace std;
main(){
int count=0;
int m=9999;

while(m){
count++;
m=m&(m-1);
}
cout << count ;

m 1
8

5.6 ab

There are two int variables: a and b, don t use if , ? : , switch or other judgement statements,
find out the biggest one of the two numbers. a b if ?: switch
[ 2005 ]

int max = ((a+b)+abs(a-b)) / 2

int c = a -b;
char *strs[2] = {"a","b"};

c = unsigned(c) >> (sizeof(int) * 8 - 1);


cout << str[c];

ab

5-13

[ ]

xor (overow)

a=a+b;
b=a-b;
a=a-b;

ab

a=a^b;
b=a^b;
a=a^b;

a=a+b
bit-wise xor Bit-wise xor

xor 1
9

00001001^00000101 0000110012
main(){
int a=9;
a=a^5;

printf("a=%d\n",a);}
00001001^0000010100001100
00001001^0000110000000101
00000101^0000010100001001


a=a^b;
b=a^b;
a=a^b;

5.7 C C++

C++ C extern "C"



C++ C C++ C
void foo(int x, int y) C
_foo C++ _foo_int_int
C++ C extern "C"

5-14

05

ifndef/define/endif

C C++


C C

C++

C/C++ Java/.NET
FORTRAN MATLAB COM
.NET Windows
.NET
COM .NET

5.8
1
2007 12 31 23 59 59 2008 1 1 0 0 0 [
H 2007 11 ]

2 29

5-15

[ ]

#include <iostream>
#include <string>
using namespace std;
void ResetTheTime(int *year,int *month,
int *date,int *hour, int *minute,
int *second)
{
int dayOfMonth[12]={31,28,31,30,31,30,
31,31,30,31,30,31};
if( *year < 0 || *month < 1 ||
*month > 12 ||
*date < 1 || *date > 31 || *hour
< 0 || *hour > 23 ||
*minute < 0 ||*minute > 59|| *second
<0 || *second >60 )
return;
if( *year < 0 || *month < 1 ||
*month > 12 )
if( *year%400 == 0 || *year%100 !=
0 && *year%4 == 0 )
dayOfMonth[1] = 29;
*second += 1;
if(*second >= 60)
{
*second = 0;
*minute += 1;
if(*minute >= 60)
{
*minute = 0;
*hour += 1;
if(*hour >= 24)

[*month-1])

*hour = 0;
*date += 1;
if(*date > dayOfMonth
{

*date = 1;
*month += 1;
if(*month > 12)
{
*month=1;
*year += 1;
}

cout << *year << ' ' << *month << '
'<< *date << ' '<< *hour <<
' ' << *minute << ' ' << *second;
return;

}
int main()
{
int y1 = 2004; int m1=2; int d1=28;
int h1=23; int mm=59; int se=59;

ResetTheTime(&y1,&m1,&d1,&h1,
&mm,&se);
return 0;

C/C++ [
H 2007 11 ]

1 // //

5-16

05

2 /* pos1 */
pos2 pos2 /*
/*
*/ */
pos2 0 pos2
3

// /*

// /*

// /*

///*

/* //*/

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
/**
* C/C++
*
* C/C++
*/
void
remove_comment(char *buf, size_t size)
{
char
*p, *end, c;
char
*sq_start, *dq_start;
char
*lc_start, *bc_start;
size_t len;
p = buf;
end = p + size;
sq_start = NULL;
dq_start = NULL;

lc_start = NULL;
bc_start = NULL;
while (p < end) {
c = *p;
switch (c) {
case '\'': /* */
if (dq_start || lc_start ||
bc_start) {
/**/
p++;
continue;
}
if (sq_start == NULL) {
sq_start = p++;
} else {
len = p++ - sq_start;
if (len == 2 && *(sq_start + 1)
== '\\') {

5-17

[ ]

}
}

/* */
continue;

sq_start = NULL;

break;
case '\"': /* */
if (sq_start || lc_start ||
bc_start) {
/* */
p++;
continue;
}

}
break;
case '*': /* */
if (sq_start || dq_start ||
lc_start || bc_start == NULL) {
/*
*/
p++;
continue;
}
if (*(p + 1) != '/') {
/* */
p++;
continue;
}

if (dq_start == NULL) {
dq_start = p++;

p += 2;

} else {
if (*(p++ - 1) == '\\') {
/* */
continue;
}
dq_start = NULL;
}
break;
case '/': /* */
if (sq_start || dq_start || lc_start
|| bc_start) {
/* */
p++;
continue;
}

memset(bc_start, ' ', p - bc_start);


bc_start = NULL;
break;
case '\n': /* */
if (lc_start == NULL) {
p++;
continue;
}
c = *(p - 1);
memset(lc_start, ' ',
(c == '\r' ? (p++ - 1) : p++)
lc_start);

c = *(p + 1);

lc_start = NULL;

if (c == '/') {
lc_start = p;
p += 2;

5-18

break;
default:
p++;
break;

} else if (c == '*') {
bc_start = p;
p += 2;

}
}

} else {
/* */
p++;

if (lc_start) {
memset(lc_start, ' ', p - lc_start);
}

05

//printf("test\n");

int
main(int argc, char *argv[])
{
int fd, n;
char buf[102400];
fd = open("C:\\uuu.txt",
_O_RDONLY, 0);
if (fd == -1) {
return -1;
}
n = read(fd, buf, sizeof(buf));
if (n == -1 || n == 0) {
close(fd);
return -1;
}

remove_comment(buf, n);
*(buf + n) = '\0';
printf(buf);
/***********\\\/////// */
close(fd);
return 0;
}
//
dkdkddlsdkkkkkkkkkkkkkkk
/* dkdkdk
*/ // ididiiddddddddddddddddd
// dkdkdkkkkkkkkkkkkkkkk
/***********\\\////*////

5 ABCDE*4=EDCBA 5
[ GS 2008 4 ]

#include <iostream>
using namespace std;
int main()
{
for(int i=10000;i <100000;i++)
{
int j=0;
int t=i;
while(t!=0)
{

j=j*10+t%10;
t/=10;

}
if((i <<2)==j)
{
cout <<i;
break;
}

5-19

[ ]

5-20

You might also like