文档详情

C++课件(程序设计与C语言)(上).ppt

发布:2016-05-27约13.57万字共677页下载文档
文本预览下载声明
#include iostream.h int fib(int n); int main() {int n, answer; cout Enter number: ; cin n; cout \n\n; answer = fib(n); cout answer is the n th Fibonacci number\n; return 0; } int fib (int n) { cout Processing fib( n )... ; if (n 3 ) {cout Return 1!\n; return (1); } else {cout Call fib( n-2 ) and fib( n-1 ).\n; return( fib(n-2) + fib(n-1)); } } 例3-14 3-15 例 3-16 #include iostream.h templatetypename T void swap(Tx,Ty) { T z; z=x;x=y;y=z; } void main() { int i=1,j=2;   double v=3.0,w=4.0;   cout i=i j=jendl;   cout v=v w=wendl;   swap(i,j);   swap(v,w);   cout After swao: endl;  cout i=i j=jendl;   cout v=v w=wendl; } void swap(int x, int y) { int z; z=x;x=y;y=z; } #includeiostream.h #includemath.h double fx(double x); void main() { double x=3.14159/4; coutthe result isfx(x)endl; } 用牛顿法求:f(x)=cos(x)-x Xn+1=Xn+cos(Xn)-Xn)/sin(Xn)+1 初值为:3.14159/4 double fx(double x1) { double x0; do { x0=x1; x1=x0+(cos(x0)-x0)/(sin(x0)+1); }while( fabs(x1-x0)1e-6 ); return x1; } 习题课 (第四章) 4_5 4_7 4_8 #includeiostream.h class Dog {public: Dog(int initialAge,int initialWeight); ~Dog(){} int GetAge(){return itsAge;} void SetAge(int age){itsAge=age;} int GetWeight(){return itsWeight;} void SetWeight(int weight){itsWeight=weight;} private:  int itsAge,itsWeight; }; Dog::Dog(int initialAge=0,int initialWeight=5) { itsAge=initialAge; itsWeight=initialWeight; } void main() { Dog Jack(2,10); coutJack is a Dog who is ; coutJack.GetAge() years old and; coutJack.GetWeight() pounds weight.\n; Jack.SetAge(7); Jack.SetWeight(20); coutNow Jack is ; coutJack.GetAge() years old and; coutJack.GetWeight() pounds weight.\n; } 4_9 #includeiostream.h class Rectangle {public: Rectangle(int top,int left,int bottom,int right); ~Rectangle(){} int GetTop(){return itsTop;} int GetLeft(){return itsLeft;} int GetBottom(){return itsBottom;} int GetRight(){return itsRight;} void SetTop(int top){its
显示全部
相似文档