C程序的设计编程实例大全.doc
-C+程序编程实例大全#include<iostream>int main() std:cout<<"hello world!"#include "iostream"using namespace std;void main() int *p; p=new int; *p=5; /*p=new int(5);*/ cout<<*p; delete p; #include "iostream"using namespace std;void main() char name10; cout<<"please input your name:" cin>>name; cout<<"the name is "<<name<<endl; #include "iostream"using namespace std;void main() char name10; int age; cout<<"please input your name:" cin>>name; cout<<"how old are you:" cin>>age; cout<<"the name is "<<name<<endl; cout<<"the age is "<<age<<endl; #include "iostream"using namespace std;void main() struct student int no; float math; ; int n; cin>>n; student wang; wang.no=n; cin>>wang.math; cout<<wang.no<<" "<<wang.math<<endl; #include "iostream"using namespace std;void main() int *p; int n; cout<<"please input the length of the array: " cin>>n; if(p=new intn)=0) cout<<"can't allocate more memory, terminating"<<endl; e*it(1); for(int i=0;i<n;i+) pi=i*2; cout<<"Now output the array: "<<endl; for(i=0;i<n;i+) cout<<pi<<" "<<endl; delete p; #include "iostream"using namespace std;void main() int *p; int n; cout<<"please input the length of the array: " cin>>n; if(p=new intn)=0) cout<<"can't allocate more memory, terminating"<<endl; e*it(1); for(int i=0;i<n;i+) pi=i*i+1; cout<<"Now output the array: "<<endl; for(i=0;i<n;i+) cout<<pi<<" " cout<<endl; delete p; /* Note:Your choice is C IDE */#include <iostream>using namespace std;float a=2.4;void main() int a=8; cout<<a<<endl; cout<<:a<<endl; /* Note:Your choice is C IDE */#include <iostream.h>void main() int num=50; int &ref=num; ref+=10; cout<<"num="<<num<<endl; cout<<"ref="<<ref<<endl; num+=40; cout<<"num="<<num<<endl; cout<<"ref"<<ref<<endl; /* Note:Your choice is C IDE */#include <iostream.h>void main() int num; int &ref=num; num=5; cout<<"num="<<num<<endl; cout<<"ref="<<ref<<endl; cout<<"&num="<<&num<<endl; cout<<"&ref="<<&ref<<endl; /* Note:Your choice is C IDE */#include <iostream>using namespace std;void swap(int &*,int &y);void main() int *=5,y=6; cout<<"before swap, *:"<<*<<" ,y:"<<y<<endl; swap(*,y); cout<<"after swap, *:"<<*<<" ,y:"<<y;void swap(int &r*,int &ry) int t=r*; r*=ry; ry=t;#include<iostream>/using namespace std;void main()/* double radius; cout<<"please input radius:" cin>>radius; double result=radius*radius*3.14*4; cout<<"The result is "<<(double)result<<"n"*/double radius;std:cout<<"please input radius: "std:cin>>radius;std:cout<<"The result is "<<radius*radius*3.14*4<<"n"/* Note:Your choice is C IDE */#include <iostream>using namespace std;int array64=60,80,90,75,75,85,65,77,80,88,90,98,89,100,78,81,62,68,69,75,85,85,77,91;int &level(int grade,int size,int &tA,int &tB );void main() int typeA=0,typeB=0; int student=6; int gradesize=4; for(int i=0;i<student;i+) level(arrayi,gradesize,typeA,typeB)+; cout<<"number of type A is "<<typeA<<endl; cout<<"number of type B is "<<typeB<<endl; int &level(int grade,int size,int &tA,int &tB) int sum=0; for(int i=0;i<size;i+) sum+=gradei; sum/=size; if(sum>=80) return tA; else return tB;/* Note:Your choice is C IDE */#include <iostream>using namespace std;float &fn2(float r) float t; t=3.14*r*r; return t;void main() fn2(5.0)=12.4; cout<<fn2(5.0)<<endl; /* Note:Your choice is C IDE */#include <iostream>#include <string>using namespace std;void main() string s,t; cout<<"please input a zifuchuan:"<<endl; cin>>s; t="I like programming!" cout<<"zifuchuan output:"<<endl<<s<<endl<<t<<endl; cout<<s.append(" OK!")<<endl;/* Note:Your choice is C IDE */#include <iostream>using namespace std;inline double circumference(double radius);void main() double r=3.0,s; s=circumference(r); cout<<"the circumference is "<<s<<"."<<endl; inline double circumference(double radius) return 2*3.1415926*radius;/* Note:Your choice is C IDE */#include <iostream.h>int add(int *,int y) int sum; sum=*+y; return sum;int add(int *,int y,int z) int sum; sum=*+y+z; return sum;void main() int a,b; a=add(5,10); b=add(5,10,20); cout<<"a="<<a<<endl; cout<<"b="<<b<<endl;/* Note:Your choice is C IDE */#include <iostream.h>template<class T>T min(T a,int n) int i; T minv=a0; for(i=1;i<n;i+) if(minv>ai) minv=ai; return minv; void main() int a=1,3,0,2,7,6,4,5,2; double b=1.2,-3.4,6.8,9.8; int c=1,9,5,-6,7,8; cout<<"a de shuzu zhong min zhi wei:"<<min(a,9)<<endl; cout<<"b de shuzu zhong min zhi wei:"<<min(b,4)<<endl; cout<<"c de shuzu zhong min zhi wei:"<<min(c,6)<<endl; /* Note:Your choice is C IDE */#include <iostream.h>#define PI 3.1415926535#define CS Circle_Squaretemplate<class T>double Circle_Square(T *) return *PI;double Circle_Square(long *) return *PI;void main() int r1=1; double r2=2.0; long r3=3; cout<<"The first cs is "<<CS(r1)<<endl; cout<<"The second cs is "<<CS(r2)<<endl; cout<<"The third cs is "<<CS(r3)<<endl; /* Note:Your choice is C IDE */#include <iostream.h>void main() int i=0,&l=i,&k=l; i=+l-k; cout<<"i="<<i<<endl; /* Note:Your choice is C IDE */#include <iostream.h>class Tdatepublic:void set(int,int,int);int isLeapYear();void print();private:int month;int day;int year;void Tdate:set(int m,int d,int y) month=m;day=d;year=y;int Tdate:isLeapYear() return(year%4=0&&year%100!=0)|(year%400=0);void Tdate:print() cout<<month<<"/"<<day<<"/"<<year<<endl; cout<<isLeapYear()<<endl;void main() Tdate *; *.set(10,23,2000); *.isLeapYear(); *.print(); /* Note:Your choice is C IDE */#include <iostream.h>class Tdatepublic:void set(int,int,int);int isLeapYear();void print();private:int month;int day;int year;void Tdate:set(int m,int d,int y) month=m;day=d;year=y;int Tdate:isLeapYear() return(year%4=0&&year%100!=0)|(year%400=0);void Tdate:print() cout<<year<<"/"<<month<<"/"<<day<<endl;void someFunc(Tdate& refs) refs.print(); if(refs.isLeapYear() cout<<"error"<<endl; else cout<<"right"<<endl;void main() Tdate s,*pTdate=&s; s.set(2,15,1998); pTdate->print(); if(*pTdate).isLeapYear() cout<<"error"<<endl; else cout<<"right"<<endl; someFunc(s); #include<iostream>using namespace std;void sphere();int main() sphere();void sphere() double radius; cout<<"please input radius:" cin>>radius; if(radius<0) return; cout<<"the result is "<<radius*radius*3.14*4<<"n"/* Note:Your choice is C IDE */#include<iostream.h>const int SIZE=10;class Cstack private: char stkSIZE; int position; public: void init() position=0; char push(char ch); char pop();char Cstack:push(char ch) if(position=SIZE) cout<<"栈满"<<endl; return 0; stkposition+=ch; return ch;char Cstack:pop() if(position=0) cout<<"n栈空"<<endl; return 0; return stk-position;void main() Cstack s; s.init(); char ch; cout<<"please input some characters:"<<endl; cin>>ch; while(ch!='#'&&s.push(ch) cin>>ch; cout<<endl; cout<<"now output the data:"<<endl; while(ch=s.pop() cout<<ch;/* Note:Your choice is C IDE */#include <iostream.h>class Tdatepublic:void set(int m=5,int d=16,int y=1991) month=m;day=d;year=y;void print() cout<<month<<"/"<<day<<"/"<<year<<endl;private:int month;int day;int year;void main() Tdate a,b,c; a.set(4,12,1996); b.set(3); c.set(8,10); a.print(); b.print(); c.print(); /* Note:Your choice is C IDE */#include<iostream.h>class cube public: int volume(int ht,int wd) return ht*wd; int volume(int ht,int wd,int dp) return ht*wd*dp; private: int ht,wd,dp;void main() cube c; cout<<c.volume(10,20)<<endl; cout<<c.volume(10,20,30)<<endl; /* Note:Your choice is C IDE */#include <iostream.h>class queue/*默认私有*/int q100;int sloc,rloc;public:queue();void qput(int i);int qget();queue:queue() sloc=rloc=0; cout<<"queue initialized"<<endl;void queue:qput(int i) if(sloc=100) cout<<"queue is full"<<endl; return; sloc+; qsloc=i;int queue:qget() if(rloc=sloc) cout<<"queue is empty"<<endl; return 0; rloc+; return qrloc;void main() queue a,b; a.qput(10); b.qput(20); a.qput(20); b.qput(19); cout<<a.qget()<<" " cout<<b.qget()<<"n" cout<<a.qget()<<" " cout<<b.qget()<<"n"/* Note:Your choice is C IDE */#include <iostream.h>class testprivate:int num;float f1;public:test();test(int n,float f);int getint() return num; float getfloat() return f1; ;test:test() cout<<"Initializing default"<<endl; num=0; f1=0.0;test:test(int n,float f) cout<<"Initializing"<<n<<","<<f<<endl; num=n; f1=f;void main() test *; test y(10,21.5); test *p*=new test; test *py=new test(10,21.5);/* Note:Your choice is C IDE */#include <iostream.h>class Tdatepublic: Tdate(int m=5,int d=16,int y=1990) month=m;day=d;year=y; cout<<month<<"/"<<day<<"/"<<year<<endl; private: int month; int day; int year;void main() Tdate aday; Tdate bday(2); Tdate cday(3,12); Tdate dday(1,22,1998);/* Note:Your choice is C IDE */#include <iostream>#include <string>using namespace std;class Student public: Student(char *pName) cout<<"call one parameter constructor"<<endl; strncpy(name,pName,sizeof(name); namesizeof(name)-1='0' cout<<"the name is "<<name<<endl; Student() cout<<"call no parameter constructor"<<endl; protected: char name20;void main() Student noName; Student ss("Jenny"); /* Note:Your choice is C IDE */#include<iostream>using namespace std;class test private: int num; float f1; public: test(); test(int n,float f); int getint() return num; float getfloat() return f1; ;test:test() cout<<"Initializing default"<<endl; num=0; f1=0.0;test:test(int n,float f) cout<<"Initializing"<<n<<","<<f<<endl; num=n; f1=f;void main() cout<<"the main function:"<<endl; test array5; cout<<"the second element of array is "<<array1.getint()<<","<<array1.getfloat()<<endl; /* Note:Your choice is C IDE */#include<iostream>using namespace std;class test private: int num; float f1; public: test(int n); test(int n,float f);inline test:test(int n) cout<<"Initializingt"<<n<<endl; num=n;test:test(int n,float f) cout<<"Initializingt"<<n<<","<<f<<endl; num=n; f1=f;void main() test array13=1,2,3; test array2=test(2,3.5),test(4); test array3=test(5.5,6.5),test(7,8.5); test array4=test(5.5,6.5),7.5,8.5; /* Note:Your choice is C IDE */#include<iostream>using namespace std;class test private: int num; float f1; public: test(int n); test(int n,float f);inline test:test(int n) cout<<"Initializingt"<<n<<endl; num=n;test:test(int n,float f) cout<<"Initializingt"<<n<<","<<f<<endl; num=n; f1=f;void main() test array13=1,2,3; test array2=test(2,3.5),test(4); test array3=test(5.5,6.5),test(7,8.5); test array4=test(5.5,6.5),7.5,8.5; #include<iostream>using namespace std;int main() for(int i=1;i<=10;i+) for(int j=1;j<=i;+j) cout<<"M" cout<<endl; #include<iostream.h>class Tdatepublic:Tdate(int m=5,int d=16,int y=1990)month=m;day=d;year=y;cout<<month<<"/"<<day<<"/"<<year<<endl;private: int month; int day; int year;void main()Tdate aday;Tdate bday(2);Tdate cday(3,12);Tdate dday(1,21,1998);#include<e*ception>#include<iostream>using namespace std;void main() try e*ception theError; throw(theError); catch(const e*ception &theError) cout<<theError.what()<<endl; try logic_error theLogicError("Logic Error!"); throw(theLogicError); catch(const e*ception & theLogicError) cout<<theLogicError.what()<<endl; #include<iostream>#include<string>using namespace std;class Personpublic:Person(char *na) cout<<"call constructor"<<endl; name=new charstrlen(na)+1; if(name!=0) strcpy(name,na);Person(Person&p)cout<<"call copy constructor"<<endl;name=new charstrlen(p.name)+1; if(name!=0) strcpy(name,p.name);void printname() cout<<name<<endl;Person()delete name;private:char *name;void main()Person wang("wang");Person li(wang);wang.printname();li.printname();#include<iostream>using namespace std;class SillyClasspublic:SillyClass(int&i):ten(10),refI(i