1http://learn.tsinghua.edu.cn
Email,qiaolin@cic.tsinghua.edu.cn
Tel,62792961
2http://learn.tsinghua.edu.cn


– C if switch if
switch
– C while for do-while

3http://learn.tsinghua.edu.cn







4http://learn.tsinghua.edu.cn


A B
– ; ; {}
5http://learn.tsinghua.edu.cn
#include <stdio.h>
int main()
{
float a,b,c,d,real,imaginary;
printf(?Input reals and imaginaries of two complexes\n?);
scanf(?%f,%f,%f,%f?,&a,&b,&c,&d);
printf(?Sum,%f + %fi\n?,a + c,b + d);
printf(?Difference,%f + %fi\n?,a – c,b – d);
real = a * c – b * d; imaginary = a * d + b * c;
printf(?Product,%f + %fi\n?,real,imaginary);
real = (a*c + b*d) / (c*c + d*d); imaginary = (b*c – a*d) / (c*c + d*d);
printf(?Quotient,%f + %fi\n?,real,imaginary);
return 0;
}
4
6http://learn.tsinghua.edu.cn


– A B
A
B
7http://learn.tsinghua.edu.cn
if-else
if() 1 else 2
1 2
1 2
1 2
else
8http://learn.tsinghua.edu.cn
if-else
0~9
#include <stdio.h>
int main()
{
char c;
printf(?Input a character,?);
c = getchar();
if(c >= 48 && c <= 57)
printf(?It is a number.\n?);
else
printf(?No,it is not a number.\n?);
return 0;
}
ASCII
ASCII48~57
ASCII
9http://learn.tsinghua.edu.cn
if-else
#include <stdio.h>
int main()
{
int n,abs;
printf(?Enter integer,?);
scanf(?%d?,&n);
abs = n;
if(abs < 0)
abs = –abs;
printf(?Original integer,%d,absolute value,%d\n?,n,abs);
return 0;
}
A
10http://learn.tsinghua.edu.cn
if-else if-else
– if(1) 1 else if(2) 2 < else n
1
1
2
2
...
n
11http://learn.tsinghua.edu.cn
if-else if-else
#include <stdio.h>
int main()
{
float score;
printf(?Input score,?);
scanf(?%f?,&score);
if(score >= 85 && score <= 100) printf(?Excellent.\n?);
else if(score >= 60 && score < 85) printf(?Pass.\n?);
else if(score >= 0 && score < 60) printf(?No pass.\n?);
else printf(?Error score.\n?);
return 0;
}
12http://learn.tsinghua.edu.cn
– 50 500 200 300
if(age > 50)
if(sal < 500)
sal += 200;
else
sal += 300;
– 50 500200 50
300
else if
else
if(age > 50)
{
if(sal < 500)
sal += 200;
}
else
sal += 300;
13http://learn.tsinghua.edu.cn
ax2 + bx + c = 0
#include <stdio.h>
#include <math.h> //
int main()
{
float a,b,c,x1,x2,p,q,m; //
printf(?Enter 3 coefficients,?) ;
scanf(?%f,%f,%f?,&a,&b,&c); //
if( a == 0.0 && b == 0 && c == 0 ) // 1 0
printf(?any value\n?);
else if( a == 0 && b != 0 ) // 2a0
printf(?x1 = x2 = %f\n",– c / b);
else // 3
{
m = b * b – 4.0 * a * c;
<
14http://learn.tsinghua.edu.cn
if( m >= 0 )
{ //
x1 = ( –b + sqrt(m) ) / ( 2.0 * a );
x2 = ( –b – sqrt(m) ) / ( 2.0 * a );
printf(?x1 = %f\n?,x1 ); //
printf(?x2 = %f\n?,x2 );
}
else
{ //
p = –b / ( 2.0 * a );
q = sqrt(–m) / ( 2.0 * a );
printf(?x1 = %f + %fi\n?,p,q);
printf(?x2 = %f – %fi\n?,p,q);
}
}
return 0;
}
15http://learn.tsinghua.edu.cn
switch



– default
– switch
– switch
– case case
– default case
– case
switch( )
{
case 1,1
case 2,2
case n,n
default,
}
16http://learn.tsinghua.edu.cn
switch
s witc h
c as e
1 2
...
s witc h
c as e 21
n
c as e n d ef au lt
17http://learn.tsinghua.edu.cn
switch
#include <stdio.h>
int main()
{
char grade; printf(?Input the grade(A,B,C,D,E):?); scanf(?%c?,&grade);
switch( grade )
{
case ‘A‘,printf(?90-100\n?); break;
case ‘B‘,printf(?80-89\n?); break;
case ‘C‘,printf(?70-79\n?); break;
case ‘D‘,printf(?60-69\n?); break;
case ‘E‘,printf(?0-59\n?); break;
default,printf(?Error\n?);
}
return 0;
}
18http://learn.tsinghua.edu.cn
switch
#include <stdio.h>
int main()
{
char grade; printf(?Input the grade(A,B,C,D,E):?); scanf(?%c?,&grade);
switch( grade )
{
case ‘A‘,printf(?90-100\n?);
case ‘B‘,printf(?80-89\n?);
case ‘C‘,printf(?70-79\n?);
case ‘D‘,printf(?60-69\n?);
case ‘E‘,printf(?0-59\n?);
default,printf(?Error\n?);
}
return 0;
}
breakswitch breakcaseswitch
casedefault
19http://learn.tsinghua.edu.cn
switch
#include <stdio.h>
int main()
{
char grade; printf(?Input the grade(A,B,C,D,E):?); scanf(?%c?,&grade);
switch( grade )
{
case ‘A‘:
case ‘B‘:
case ‘C‘:
case ‘D‘,printf(?Pass\n?); break;
case ‘E‘,printf(?Fail\n?); break;
default,printf(?Error\n?);
}
return 0;
}
case casebreak
20http://learn.tsinghua.edu.cn


– A
A
21http://learn.tsinghua.edu.cn
while
while while()
while


wh ile
22http://learn.tsinghua.edu.cn
while
while
#include <stdio.h>
int main()
{
int m,n,result;
printf(?Input two positive integers m,n:?);
scanf(?%d,%d?,&m,&n);
if(m > 0 && n > 0)
{
result = m < n? n,m;
while(result % m != 0 || result % n != 0) result++;
printf(?The least common multiple of m and n is %d.\n?,result);
}
else printf(?the number m or n is not a positive integer.\n?);
return 0;
}
23http://learn.tsinghua.edu.cn
do-while
do-while do{ }while();
do-while

while

d o -wh ile
24http://learn.tsinghua.edu.cn
do-while
1
#include <stdio.h>
int main(){
int m,n,result;
do{
printf(?Input two positive integers m,n:?); scanf(?%d,%d?,&m,&n);
if(m > 0 && n > 0){
result = m < n? n,m;
while(result%m!= 0 || result%n != 0) result++;
printf(?The least common multiple of m and n is %d.\n?,result);
}
else printf(?the number m or n is not a positive integer.\n?);
}while(m != 1 || n != 1);
return 0;
}
25http://learn.tsinghua.edu.cn
for
for
– for(1; 2; 3)
for
– 1 2
3 2
for
2
3
1
26http://learn.tsinghua.edu.cn
100
2
1k
k
for
#include <stdio.h>
int main()
{
int result,k;
result = 0; // 0
for( k = 1; k <= 100; k++ ) //
result += k * k; //
printf(?The result,%d\n?,result);
return 0;
}
27http://learn.tsinghua.edu.cn
for while do-while
while
for
for
for
28http://learn.tsinghua.edu.cn
for
#include <stdio.h>
#include <string.h>
int main( )
{
char s[50],c;
int i,j;
printf(?Input a string,?); scanf(?%s?,s);
for( i = 0,j = strlen(s) – 1; i < j; i++,j– –){
c = s[i]; s[i] = s[j]; s[j] = c;
}
printf(?%s\n?,s);
return 0;
}
29http://learn.tsinghua.edu.cn
for
#include <stdio.h>
int main(){
int i,j,k;
for( i = 1; i <= 9; i++ ){
for( j = i; j <= 9; j++ ){
k = i * j;
printf(?%d %d = %d\t?,i,j,k);
}
printf(?\n?) ;
}
return 0;
}
30http://learn.tsinghua.edu.cn
break
5 10 20,000
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(){
int i,j,r;
srand( time(NULL) );
for( i = 1; i <= 5; i++ ){
for( j = 1; j <= 10; j++ ){
r = rand(); printf(?%6d\t?,r); if(r > 20000) break; }
printf(?\n?); }
return 0;
}
srand()
time()
rand()
break
31http://learn.tsinghua.edu.cn
continue
#include <stdio.h>
int main(){
char c;
int num = 0;
printf(?Enter a string,\n?);
while( (c = getchar()) != ‘\n‘ ){
if(c < 97 || c > 122) continue; //
num++;
}
printf(?%d\n?,num);
return 0;
}
continue
32http://learn.tsinghua.edu.cn



33http://learn.tsinghua.edu.cn
n 9 n
10
#include <stdio.h>
int main(){
int i,j,n,line = 0;
printf(?Please input n:?); scanf(?%d?,&n);
if(n <= 1){ printf(?No number to output!\n?); return 1; }
for(i = 2; i <= n; i++){
for( j = 2; j < i; j++) if(i % j == 0) break;
if(i == j && i % 10 != 9){
printf(?%d,?,i); line++; if(line == 10){ printf(?\n?); line = 0; }
}
}
printf(?\n?);
return 0;
}
for
34http://learn.tsinghua.edu.cn
#include <stdio.h>
int main()
{
int i,j,n,line = 0;
printf(?Please input n:?); scanf(?%d?,&n);
if(n <= 1){ printf(?No number to output!\n?); return 1; }
i = 2;
while(i <= n){
j = 2;
while(j < i && i % j != 0) j++;
if(i == j && i % 10 != 9){
printf(?%d,?,i); line++; if(line == 10){ printf(?\n?); line = 0; }
}
i++;
}
printf(?\n?);
return 0;
}
while
35http://learn.tsinghua.edu.cn
5313
#include <stdio.h>
int main()
{
int x,y,z;
for(x = 0; x <= 100; x++)
for(y = 0; y <= 100; y++)
for(z = 0; z <= 100; z++)
if( x + y + z ==100 && 15 * x + 9 * y + z == 300)
printf(?x = %d,y = %d,z = %d\n?,x,y,z);
}
x y z
100x y z
15 3 10 0
3x y z1 5 9 3 0 0x y z
36http://learn.tsinghua.edu.cn
5313
#include <stdio.h>
int main()
{
int x,y,z;
for(x = 0; x <= 20; x++)
for(y = 0; y <= 33; y++)
for(z = 0; z <= 100; z++)
if( x + y + z ==100 && 15 * x + 9 * y + z == 300)
printf(?x = %d,y = %d,z = %d\n?,x,y,z);
}
x y z
100x y z
1 5 9 3 0 0x y z
2 0 ; 3 3 ; 1 0 0x y z
<= 100; x++)
<= 100; y++)
37http://learn.tsinghua.edu.cn
30
#include <stdio.h>
int main()
{
int n1,n2,n,count;
n1 = 0; n2 = 1;
printf(?%10d%10d?,n1,n2);
for(count = 3; count <= 30; count++)
{
n = n1 + n2;
printf(?%10d?,n);
if(count % 5 == 0) printf(?\n?); // 5
n1 = n2; n2 = n;
}
printf(?\n?);
}


00
11
1 2 1
n
F n n
F n F n n



38http://learn.tsinghua.edu.cn

– 1 2
– select() insert() update() del()
39http://learn.tsinghua.edu.cn
#include <stdio.h>
void insert();
void select();
void del();
void update();
int main()
{
/* generate menu */
char op;
printf(?\n********************************?);
printf(?\n* Menu Options *?);
printf(?\n* 1,Insert *?);
printf(?\n* 2,Select *?);
printf(?\n* 3,Delete *?);
printf(?\n* 4,Update *?);
printf(?\n* 5,Exit *?);
printf(?\n********************************?);
40http://learn.tsinghua.edu.cn
/* selection operation */
while(1)
{
printf(?\nPlease enter selection,?); scanf(?%d?,&op);
switch(op)
{ /* */
case ‘1‘,insert(); break;
case ‘2‘,select(); break;
case ‘3‘,del(); break ;
case ‘4‘,update(); break;
case ‘5‘,break;
default,printf(?\nSelection error!?); break;
}
if(op == ‘5’) break; /* */
}
} //
41http://learn.tsinghua.edu.cn
71-72
– 8 9 11
72-73
– 2 4