Professional Documents
Culture Documents
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
x =(y==z);
x =(y&z);
x =(y&&z);
y=4;
x=(y|z);
x=(y||z);
return 0;
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
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;
}
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
A. 8 8
B. 130 8
*(ptr++)+=123;
printf( "%d,%d\n ",*ptr,*(++ptr));
}
C. 7 7
D. 7 8
C printf
5-5
[ ]
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;
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;
5-8
05
[ S 2007 8 ]
#include <stdio.h>
int
{
main()
unsigned int a = 0xFFFFFFF7;
&a a &a
unsigned int a
char *b = (char *)&a ;
//papa
//ba
000000f7fffffff7
5-9
[ ]
C++
1.
Arithmetic
Conversion
int ival = 3;
double dval = 3.14159;
// ivaldouble3.0
ival + dval;
2.
// dval int 3
ival = dval;
3.
4.
5-10
05
long double
long double
long double a long double
ASC 97 long double
3.14159L + 'a';
//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;
5.5 And/Or/Not
1
N
X 2 (2,4,8,16,)[
CPU 2007 10 ]
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 c = a -b;
char *strs[2] = {"a","b"};
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++
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;
}
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