Professional Documents
Culture Documents
:
E_mail:
xyzhong@seu.edu.cn
4.1
C++
main
4.2
C++
main()
main()
C++
C++
main()
C++
C++
void
4.2
void main()
int max(int i,int j) {
{ int a,b,choice;
return i>j?i:j; cout<<" \n";
} cin>>a>>b;
int min(int i,int j) PrintMenu();
{ cin>>choice;
return i<j?i:j;switch(choice)
} {
void PrintMenu() case
{ 1:cout<<max(a,b)<<endl;
cout<<"1: break;
\n"; case 2:cout<<min(a,b)<<endl;
cout<<"2: break;
\n"; }
cout<<" }
4.2.3
1.
C++
void
2. return
return
return
return
main()
return
return
4.3
printstar( );
c=max (a,b);
max(max (a,b),c);
#include <iostream.h>
float max(float x,float y) //
{return (x>y?x:y;)
void main(void)
{float a,b;
cout<<" ";
cin>>a>>b;
cout<<" "<<max(a,b)<<'\n';
// max(a,b)
}
4.3.1
int max (int x,int y)
{ int z;
z=(x>y)? x : y ; x y z
return z; 2 3 3
}
void main (void )
{ int a,b,c;
cin>>a>>b;
2 3 3
c=max (a , b) ;
a b c
cout<<The max is
<< c<<endl;
}
4.3.2
C++
( )
void main (void)
{ float a,b, c;
float max (float,float);
cin>>a>>b;
c=max (a,b) ;
cout<<The max is<<c<<endl;
}
( )
int a=10;
2000
a 10
C++
#include <iostream.h> 4.7
void swap(int &i,int& j)
{ int t;
t=i;i=j; j=t;
} t
void main()
{ 10
int a=10,b=20; i j
cout<<"a="<<a
<<"\tb="<<b<<'\n'; 10
20 20
10
swap(a,b);
a b
cout<<a=<<a
<<"\tb="<<b<<'\n';
}
&
C++
int max ( int a, int b) int min ( int a, int b)
{ int c; { return ( a<b? a: b);}
int min ( int a, int b)
int max ( int a, int b)
{return ( a<b? a: b);
{ int c;
}
c=min(a,b);
c=min(a,b);
return ( a>b? a : b);
return ( a>b? a : b);
}
}
4.5
A A
A B B
A
int f(int x) int f1(int x) int f2(int t)
{ int y,z ; { int y,z ; { int a, c ;
.....
..... .....
z=f(y);
z=f2(y); c=f1(a);
....
.... ....
return (2*z);
return return
}
(2*z); (3+c);
} }
1.
2.
3.
5! 10!
:
#include <iostream.h>
void main(void)
{ int i,s=1;
for(i=1;i<=5;i++){s*=i;} //5!
cout<<"5!="<<s<<'\n';
s=1;
for(i=1;i<=10;i++){s*=i;} //10!
cout<<"10!="<<s<<'\n';
}
:
#include <iostream.h>
void main(void)
{
cout<< "5!="<<f(5)<<'\t'<<"10!="<<f(10)<<'\n';
}
:
#include <iostream.h>
void pout(int x)
{cout<<x<<"!="<<f(x)<<'\n'; }
void main(void)
{ pout(5);
pout(10);
}
:
1 n=0,1
n!=
n*(n-1)! n>1
1. n*(n-1)!
2. n=0 n=1
3. n>=0
#include <iostream.h>
long f(int n)
{ if(n==0||n==1) return(1);
else return n*f(n-1);
}
void main(void)
{
cout<<"5!="<<f(5)<<"10!="<<f(10)<< '\n';
}
f(5) f(4) f(3) f(2) f(1)
n=5 n=4 n=3 n=2 n=1
y=5*f(4) y=4*f(3) y=3*f(2) y=2*f(1) y=1
4.10
20
#include <iostream.h>
int fib(int n)
{ if((n==1)||(n==2)) return 1;
return fib(n-1)+fib(n-2);}
void main()
{ for(int i=1;i<=20;i++)
{ cout<<fib(i)<<'\t';
if(i%5==0)
cout<<'\n';
}
}
4.6
C++
C++ auto(
) register ( ) static(
) extern( )
4.6.1
main
int ab(void)
{ int i,j;
{ int i,j;
}
}
4.12
#include <iostream.h>
i1=5
void main()
i2=7
{
i3=5
int i=5;
{
cout<<"i1="<<i<<'\n //A
int i;
i=7;
cout<<"i2="<<i<<'\n'; //B
}
cout<<"i3="<<i<<'\n'; //C
}
#include<iostream.h>
void main(void)
{ int i=100,j=200,k=300;
cout<<i<<'\t'<<j<<'\t'<<k <<'\n';
{ int i=500,j=600;
k=i+j;
k=200;
cout<<i<<'\t'<<j<<'\n';
cout<<i<<'\t'<<j<<'\t'<<k <<'\n';
}
cout<<i<<'\t'<<j<<'\t'<<k <<'\n';
}
for
for
for
{ {
... ...
for (int i=1;i<10;i++) int i;
{ for (i=1;i<10;i++)
{
cout<<"i="<<i<<'\t'
} cout<<"i="<<i<<'\t'
cout<<"i="<<i
} }
cout<<"i="<<i
}
extern
::
#include<iostream.h>
int i=100; //i
void main(void)
{int i,j=50; //i,j
i=18;
::i=::i+4; // (::)i
j=::i+i; // (::)i i,j
cout<<"::i="<<::i<<'\n';
cout<<"i="<<i<<'\n';
cout<<"j="<<j<<'\n';
}
4.13
#include <iostream.h> 570
extern int i;
void main()
{
int i=5; //B
{
cout<<i; //C
int i=7; //D
cout<< i; //E
cout<<::i; //F
} extern
}
int i=0; //A
4.6.2
main()
(
)
C++ auto(
) register(
) static( ) extern(
)
auto
static
static
static
4.14
#include <iostream.h>
int f(int )
void main()
{
for (int i=0;i<3;i++) B c b c a+b+c
cout<<f(i)<<'\n'
1 0 3 1 4 5
}
2 0 4 1 5 7
int f(int a)
{ 3 0 5 1 6 9
int b=0
static int c=3
b=b+1 c=c+1
return (a+b+c)
}
extern
auto register static
extern
extern
f1.cpp f2.cpp
#include<iostream.h>
int i=200; extern int i;
int j=400; extern int j;
#include<iostream.h>
void abs(int i)
{ cout<<" \t";
cout<<(i>0?i:-i)<<'\n';}
void abs(float f)
{ cout<<" \t";
cout<<(f>0?f:-f)<<'\n';}
void abs(double df)
{ cout<<" \t";
cout<<(df>0?df:-df)<<'\n';}
void main()
{ int a=15;float b=-14.2;double c=1.03;
abs(a);abs(b);abs(c);
}
15
14.2
1.03
4.7.2
inline ( )
switch
if
inline
4.7.3
C++
4.18
#include<iostream.h>
int sum(int n=100)
{ int i,s;
s=0;
for(i=1;i<=n;i++)
s+=i;
return s;
} 1275
void main() 5050
{ cout<<sum(50)<<'\n';
cout<<sum()<<'\n';
}
4.19
#include<iostream.h>
int sum(int n=50);
void main()
{ cout<<sum()<<'\n';
int sum(int n=100);
cout<<sum()<<'\n';
}
int sum(int n)
{ int i,s=0;
for(i=1;i<=n;i++)
s+=i;
return s;
}
4.7.3
4.8
C++
#
C++
4.8.1
include
cpp
cpp
cpp
4.8.2
#define
#define ( )
#include<iostream.h>
#define PI 3.1415926
#define R 2.8
#define AREA PI*R*R
#define PROMPT " :"
#define CHAR '!'
void main(void)
{cout<<PROMPT<<AREA<<CHAR<<'\n';}
:24.6301!
#undef
#define VOLUME(a,b,c) a*b*c
b=VOLUME(2.0,7.8,1.215);
b=2.0*7.8*1.215;
b=VOLUME(1+2,2+3,3+4);
b=1+2*2+3*3+4;
\
\
C++
;
4.8.3
C++
......
1
# ifdef DEBUG
# else cout<<x<<\t<<y<<endl;
2 # endif
# end if
#define
) 1
2.
2 # ifndef # define DEBUG
1 ......
1 #define
#define DEBUG
) 1
2
3 # if # define DEBUG 1
......
1
# if DEBUG
# else cout<<x<<\t<<y<<endl;
2 # endif
# endif
( #define
) 1 DEBUG 0
2
#define DEBUG
void main(void)
{ int a=14, b=15, c;
c=a/b;
# ifdef DEBUG
cout<<a=<<oct<<a<< b=<<b<<endl;
# endif
cout<<c=<<dec<<c<<endl;
}
a=16 b=17
c=0
extern
f1.cpp time
f f
time 1
//f1.cpp
int time=0;
int f(int n)
{
int s=1;
for(int i=1;i<=n;i++)
s*=i;
time++;
return s;
}
f2.cpp
time f
time f
//f2.cpp
#include <iostream.h>
extern int time; // time
int f(int n); // f
void main()
{
int s=0;
for(int i=1;i<=5;i++)
s+=f(i);
cout<<"1!+2!+3!+4!+5!="<<s<<'\n';
cout<<" f "<<time<<'\n';
1.
file new
Add to project
2
project Add to project
Files
4.10 C++
iostream.h /
iomanip.h /
math.h
stdlib.h
string.h
4.11
4.11.1
4.11.2
int f(int x)
t
{
int t;
f(1)
if((x==0)||(x==1)) x (1)
t=1; t
else
t= x*f(x-1); f(2)
return t; x (2)
} t
f(3)
f(3) x (3)
f(2)
f(1)
f(3)f(1)
f(2)