1http://learn.tsinghua.edu.cn
Email,qiaolin@cic.tsinghua.edu.cn
Tel,62792961
2http://learn.tsinghua.edu.cn
–
–
–
–
–
–
3http://learn.tsinghua.edu.cn
4http://learn.tsinghua.edu.cn
– [];
– int a[8];
–
–
– 0 1
–
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]
5http://learn.tsinghua.edu.cn
– [] = {1,2,<,n};
– int a[8] = {10,20,30};
–
– 0
0
–
10 20 30
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]
6http://learn.tsinghua.edu.cn
12
#include <stdio.h>
int main()
{
float data[12];
float result1 = 0.0,result2 = 0.0;
int i;
printf(?Please input 12 floats:\n?);
for(i = 0; i < 12; i++) scanf(?%f?,&data[i]);
for(i = 0; i < 12; i++){
if(data[i] > 0.0) result1 += data[i]; else result2 += data[i];
}
printf(?Positives,%f\nNegatives,%f\n?,result1,result2);
return 0;
}
7http://learn.tsinghua.edu.cn
5
5 4 4
1
7
12
16
20
7
1
12
16
20
7
12
1
16
20
7
12
16
1
20
7
12
16
20
1
12
16
20
7
1
16
20
12
7
1
20
16
12
7
1
8http://learn.tsinghua.edu.cn
5
#include <stdio.h>
int main()
{
int i,j,temp;
static int a[5] = {1,7,12,16,20};
for(i = 0; i < 4; i++)
for(j = 0; j < 4 – i; j++){
if(a[j] < a[j+1]) //
{
temp = a[j]; a[j] = a[j+1]; a[j+1] = temp;
}
}
for(j = 0; j < 5; j++) printf(?a[%d] = %d\n?,j,a[j]);
return 0;
}
9http://learn.tsinghua.edu.cn
– [1][2+<;
– int a[2][8];
–
–
–
a[1][0] a[1][1] a[1][2] a[1][3] a[1][4] a[1][5] a[1][6] a[1][7]
a[0][0] a[0][1] a[0][2] a[0][3] a[0][4] a[0][5] a[0][6] a[0][7]
10http://learn.tsinghua.edu.cn
– int a[2][8] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
– int a[2][8] = {{0,1,2,3,4,5,6,7},{8,9,10,11,12,13,14,15}};
8 9 10 11 12 13 14 15
a[1][0] a[1][1] a[1][2] a[1][3] a[1][4] a[1][5] a[1][6] a[1][7]
0 1 2 3 4 5 6 7
a[0][0] a[0][1] a[0][2] a[0][3] a[0][4] a[0][5] a[0][6] a[0][7]
11http://learn.tsinghua.edu.cn
4 4
#include <stdio.h>
int main()
{
int matrix[4][4],i,j,result1 = 0,result2 = 0;
for(i = 0; i < 4; i++){
printf(?Line %d,?,i);
for(j = 0; j < 4; j++){
scanf(?%d?,&matrix[i][j]);
if(i == j) result1 += matrix[i][j]; if( i + j == 3 ) result2 += matrix[i][j];
}
}
printf(?Results,%d,%d\n?,result1,result2);
return 0;
}
12http://learn.tsinghua.edu.cn
–
–
– char s*6+ =,‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’};
– char s*6+ =?Hello?;
– putchar() getchar() printf() scanf()
13http://learn.tsinghua.edu.cn
#include <stdio.h>
int main()
{
char str0[41],str1[21]; //
int j,k; //
printf(?Please input the first string:\n?);
scanf(?%s?,str0);
printf(?Please input the second string:\n?);
scanf(?%s?,str1);
for(j = 0; str0[j+ != ‘\0‘; j++) ; // str0
for(k = 0; str1[k+ != ‘\0‘; k++,j++) // str1str0
str0[j] = str1[k];
str0[j+ = ‘\0‘; //
printf(?%s?,str0);
return 0;
}
14http://learn.tsinghua.edu.cn
15http://learn.tsinghua.edu.cn
– *;
– int *p; float *q; char *s;
0x00A0 0x00A4 < 0x1000 0x1004 0x1008 0x100C
p q
0x00A8
s
–
– int *p,*q;
16http://learn.tsinghua.edu.cn
–
–
–
–
0x00A0 0x00A4 < 0x1000 0x1004 0x1008 0x100C
p q
0x00A8
s
0x1000 0x1004 100 200
17http://learn.tsinghua.edu.cn
–
– * = ;
– int n = 100; int *p = &n;
– int a[8]; int *q = a; // a
0x00A0 0x00A4 < 0x1000 0x1004 0x1008 0x100C
p q
0x00A8
s n a[0]
0x1000 0x1004 100 200
18http://learn.tsinghua.edu.cn
0
19http://learn.tsinghua.edu.cn
–
– int a[8]; int *p = a; p++; // pa[1]
– n
– int a[8]; int *p = a; p = p + 2; // pa[2]
–
– int a[8]; int *p1 = a,*p2 = &a[7]; // p2 – p1? 7
20http://learn.tsinghua.edu.cn
–
– int a[8]; int *p = a; if( p != 0 ) *p = 100;
– int n; int *p; p = &n;
– int *p,*q; p = q;
– int a[8]; int *p; p = a;
– int *p,*q; p = q + 2; p += 2;
21http://learn.tsinghua.edu.cn
#include <stdio.h>
int main()
{
int m,n,*pm,*pn,temp; //
pm = &m; pn = &n; // pmmpnn
scanf(?%d%d?,&m,&n); // mn
// temp
temp = *pm; *pm = *pn; *pn = temp;
printf(?%d,%d\n?,m,n); // mn
printf(?%d,%d\n?,*pm,*pn); //
return 0;
} temp = pm; pm = pn; pn = temp;
22http://learn.tsinghua.edu.cn
#include <stdio.h>
int main()
{
float data[5],*pf = data;
int k;
printf(?Input data:\n?);
for(k = 0; k < 5; k++) scanf(?%f?,pf + k); //
for(k = 0; k < 5; k++){ printf(?%f,?,*pf ); pf++; } //
pf = data; //
printf(?\n?);
for(k = 0; k < 5; k++) printf(?%f,?,*(pf + k)); //
printf(?\n?);
return 0;
}
pf + k? &data[k]
*(pf + k)? data[k]
23http://learn.tsinghua.edu.cn
24http://learn.tsinghua.edu.cn
struct {
1 1;
2 2;
n n;
};
struct DATE
{
int year;
int month;
int day;
int yearday;
};
–
–
25http://learn.tsinghua.edu.cn
–
– struct ;
– struct DATE date;
–
– struct DATE date = { 2004,8,3 };
–
26http://learn.tsinghua.edu.cn
,
–,
– struct DATE date; date.year = 2004;
–>
– –>
– struct DATE *p = &date; p–>year = 2004;
– p–>year? (*p).year
y e a r
m o n t h
day
y e a r d a y
date
27http://learn.tsinghua.edu.cn
#include <stdio.h>
struct DATE{ int year; int month; int day; int yearday; };
int main()
{
int leap = 0,i;
struct DATE date;
static int month[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
printf(?\nEnter year month and day,\n?);
scanf(?%d,%d,%d?,&date.year,&date.month,&date.day);
if(date.year%4==0 && (date.year%100!=0||date.year%400==0)) leap = 1;
date.yearday = date.day + leap;
for(i = 1; i < date.month; i++) date.yearday += month[i];
printf(?yearday = %d\n?,date.yearday);
return 0;
}
28http://learn.tsinghua.edu.cn
29http://learn.tsinghua.edu.cn
–
–
union
{
1 1;
2 2;
n n;
};
union DATA
{
int n;
char* p;
char c;
};
30http://learn.tsinghua.edu.cn
ID S’
T’
#include <stdio.h>
#include <string.h>
struct PERSON{
int id;
char name[10];
char levelflag;
union{
int grade;
char jobtitle[10];
} level;
};
struct PERSON person;
levelPERSON
31http://learn.tsinghua.edu.cn
int main()
{
printf(?\nPersonal Info(ID,name,level-flag,grade or job-title),\n?);
scanf(?%d%s%s?,&person.id,person.name,&person.levelflag);
if(person.levelflag,‘S’)
scanf(?%d?,&person.level.grade);
else if(person.levelflag,‘T’)
scanf(?%s?,&person.level.jobtitle);
else
printf(?Input Error!\n?);
printf(?ID name job grade/position\n?);
if(person.levelflag,‘S’)
printf(?%–15d%–10s%–15d\n?,person.id,person.name,person.level.grade);
else if(person.levelflag,‘T’)
printf(?%–15d%–10s%–15s\n?,person.id,person.name,person.level.jobtitle);
return 0;
}
32http://learn.tsinghua.edu.cn
– enum { 1,2,<,n };
– enum MONTH{ JAN,FEB,MAR,APR,
MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC };
– enum MONTH month;
–
33http://learn.tsinghua.edu.cn
0
– enum WEEKDAY{ SUN=7,MON=1,TUE,WED,
THU,FRI,SAT };
– if( month == DEC ) <
34http://learn.tsinghua.edu.cn
– typedef ;
– typedef int DWORD;
– enum BOOL_TAG{ FALSE,TRUE }; typedef
enum BOOL_TAG BOOL;
–
– typedef CHAR* PCHAR; PCHAR p,
q; p q p q
35http://learn.tsinghua.edu.cn
106
– 1 6 7
Email,qiaolin@cic.tsinghua.edu.cn
Tel,62792961
2http://learn.tsinghua.edu.cn
–
–
–
–
–
–
3http://learn.tsinghua.edu.cn
4http://learn.tsinghua.edu.cn
– [];
– int a[8];
–
–
– 0 1
–
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]
5http://learn.tsinghua.edu.cn
– [] = {1,2,<,n};
– int a[8] = {10,20,30};
–
– 0
0
–
10 20 30
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]
6http://learn.tsinghua.edu.cn
12
#include <stdio.h>
int main()
{
float data[12];
float result1 = 0.0,result2 = 0.0;
int i;
printf(?Please input 12 floats:\n?);
for(i = 0; i < 12; i++) scanf(?%f?,&data[i]);
for(i = 0; i < 12; i++){
if(data[i] > 0.0) result1 += data[i]; else result2 += data[i];
}
printf(?Positives,%f\nNegatives,%f\n?,result1,result2);
return 0;
}
7http://learn.tsinghua.edu.cn
5
5 4 4
1
7
12
16
20
7
1
12
16
20
7
12
1
16
20
7
12
16
1
20
7
12
16
20
1
12
16
20
7
1
16
20
12
7
1
20
16
12
7
1
8http://learn.tsinghua.edu.cn
5
#include <stdio.h>
int main()
{
int i,j,temp;
static int a[5] = {1,7,12,16,20};
for(i = 0; i < 4; i++)
for(j = 0; j < 4 – i; j++){
if(a[j] < a[j+1]) //
{
temp = a[j]; a[j] = a[j+1]; a[j+1] = temp;
}
}
for(j = 0; j < 5; j++) printf(?a[%d] = %d\n?,j,a[j]);
return 0;
}
9http://learn.tsinghua.edu.cn
– [1][2+<;
– int a[2][8];
–
–
–
a[1][0] a[1][1] a[1][2] a[1][3] a[1][4] a[1][5] a[1][6] a[1][7]
a[0][0] a[0][1] a[0][2] a[0][3] a[0][4] a[0][5] a[0][6] a[0][7]
10http://learn.tsinghua.edu.cn
– int a[2][8] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
– int a[2][8] = {{0,1,2,3,4,5,6,7},{8,9,10,11,12,13,14,15}};
8 9 10 11 12 13 14 15
a[1][0] a[1][1] a[1][2] a[1][3] a[1][4] a[1][5] a[1][6] a[1][7]
0 1 2 3 4 5 6 7
a[0][0] a[0][1] a[0][2] a[0][3] a[0][4] a[0][5] a[0][6] a[0][7]
11http://learn.tsinghua.edu.cn
4 4
#include <stdio.h>
int main()
{
int matrix[4][4],i,j,result1 = 0,result2 = 0;
for(i = 0; i < 4; i++){
printf(?Line %d,?,i);
for(j = 0; j < 4; j++){
scanf(?%d?,&matrix[i][j]);
if(i == j) result1 += matrix[i][j]; if( i + j == 3 ) result2 += matrix[i][j];
}
}
printf(?Results,%d,%d\n?,result1,result2);
return 0;
}
12http://learn.tsinghua.edu.cn
–
–
– char s*6+ =,‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’};
– char s*6+ =?Hello?;
– putchar() getchar() printf() scanf()
13http://learn.tsinghua.edu.cn
#include <stdio.h>
int main()
{
char str0[41],str1[21]; //
int j,k; //
printf(?Please input the first string:\n?);
scanf(?%s?,str0);
printf(?Please input the second string:\n?);
scanf(?%s?,str1);
for(j = 0; str0[j+ != ‘\0‘; j++) ; // str0
for(k = 0; str1[k+ != ‘\0‘; k++,j++) // str1str0
str0[j] = str1[k];
str0[j+ = ‘\0‘; //
printf(?%s?,str0);
return 0;
}
14http://learn.tsinghua.edu.cn
15http://learn.tsinghua.edu.cn
– *;
– int *p; float *q; char *s;
0x00A0 0x00A4 < 0x1000 0x1004 0x1008 0x100C
p q
0x00A8
s
–
– int *p,*q;
16http://learn.tsinghua.edu.cn
–
–
–
–
0x00A0 0x00A4 < 0x1000 0x1004 0x1008 0x100C
p q
0x00A8
s
0x1000 0x1004 100 200
17http://learn.tsinghua.edu.cn
–
– * = ;
– int n = 100; int *p = &n;
– int a[8]; int *q = a; // a
0x00A0 0x00A4 < 0x1000 0x1004 0x1008 0x100C
p q
0x00A8
s n a[0]
0x1000 0x1004 100 200
18http://learn.tsinghua.edu.cn
0
19http://learn.tsinghua.edu.cn
–
– int a[8]; int *p = a; p++; // pa[1]
– n
– int a[8]; int *p = a; p = p + 2; // pa[2]
–
– int a[8]; int *p1 = a,*p2 = &a[7]; // p2 – p1? 7
20http://learn.tsinghua.edu.cn
–
– int a[8]; int *p = a; if( p != 0 ) *p = 100;
– int n; int *p; p = &n;
– int *p,*q; p = q;
– int a[8]; int *p; p = a;
– int *p,*q; p = q + 2; p += 2;
21http://learn.tsinghua.edu.cn
#include <stdio.h>
int main()
{
int m,n,*pm,*pn,temp; //
pm = &m; pn = &n; // pmmpnn
scanf(?%d%d?,&m,&n); // mn
// temp
temp = *pm; *pm = *pn; *pn = temp;
printf(?%d,%d\n?,m,n); // mn
printf(?%d,%d\n?,*pm,*pn); //
return 0;
} temp = pm; pm = pn; pn = temp;
22http://learn.tsinghua.edu.cn
#include <stdio.h>
int main()
{
float data[5],*pf = data;
int k;
printf(?Input data:\n?);
for(k = 0; k < 5; k++) scanf(?%f?,pf + k); //
for(k = 0; k < 5; k++){ printf(?%f,?,*pf ); pf++; } //
pf = data; //
printf(?\n?);
for(k = 0; k < 5; k++) printf(?%f,?,*(pf + k)); //
printf(?\n?);
return 0;
}
pf + k? &data[k]
*(pf + k)? data[k]
23http://learn.tsinghua.edu.cn
24http://learn.tsinghua.edu.cn
struct {
1 1;
2 2;
n n;
};
struct DATE
{
int year;
int month;
int day;
int yearday;
};
–
–
25http://learn.tsinghua.edu.cn
–
– struct ;
– struct DATE date;
–
– struct DATE date = { 2004,8,3 };
–
26http://learn.tsinghua.edu.cn
,
–,
– struct DATE date; date.year = 2004;
–>
– –>
– struct DATE *p = &date; p–>year = 2004;
– p–>year? (*p).year
y e a r
m o n t h
day
y e a r d a y
date
27http://learn.tsinghua.edu.cn
#include <stdio.h>
struct DATE{ int year; int month; int day; int yearday; };
int main()
{
int leap = 0,i;
struct DATE date;
static int month[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
printf(?\nEnter year month and day,\n?);
scanf(?%d,%d,%d?,&date.year,&date.month,&date.day);
if(date.year%4==0 && (date.year%100!=0||date.year%400==0)) leap = 1;
date.yearday = date.day + leap;
for(i = 1; i < date.month; i++) date.yearday += month[i];
printf(?yearday = %d\n?,date.yearday);
return 0;
}
28http://learn.tsinghua.edu.cn
29http://learn.tsinghua.edu.cn
–
–
union
{
1 1;
2 2;
n n;
};
union DATA
{
int n;
char* p;
char c;
};
30http://learn.tsinghua.edu.cn
ID S’
T’
#include <stdio.h>
#include <string.h>
struct PERSON{
int id;
char name[10];
char levelflag;
union{
int grade;
char jobtitle[10];
} level;
};
struct PERSON person;
levelPERSON
31http://learn.tsinghua.edu.cn
int main()
{
printf(?\nPersonal Info(ID,name,level-flag,grade or job-title),\n?);
scanf(?%d%s%s?,&person.id,person.name,&person.levelflag);
if(person.levelflag,‘S’)
scanf(?%d?,&person.level.grade);
else if(person.levelflag,‘T’)
scanf(?%s?,&person.level.jobtitle);
else
printf(?Input Error!\n?);
printf(?ID name job grade/position\n?);
if(person.levelflag,‘S’)
printf(?%–15d%–10s%–15d\n?,person.id,person.name,person.level.grade);
else if(person.levelflag,‘T’)
printf(?%–15d%–10s%–15s\n?,person.id,person.name,person.level.jobtitle);
return 0;
}
32http://learn.tsinghua.edu.cn
– enum { 1,2,<,n };
– enum MONTH{ JAN,FEB,MAR,APR,
MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC };
– enum MONTH month;
–
33http://learn.tsinghua.edu.cn
0
– enum WEEKDAY{ SUN=7,MON=1,TUE,WED,
THU,FRI,SAT };
– if( month == DEC ) <
34http://learn.tsinghua.edu.cn
– typedef ;
– typedef int DWORD;
– enum BOOL_TAG{ FALSE,TRUE }; typedef
enum BOOL_TAG BOOL;
–
– typedef CHAR* PCHAR; PCHAR p,
q; p q p q
35http://learn.tsinghua.edu.cn
106
– 1 6 7