收藏 分销(赏)

C++程序设计编程实例大全.doc

上传人:xrp****65 文档编号:6127448 上传时间:2024-11-28 格式:DOC 页数:105 大小:528.50KB 下载积分:10 金币
下载 相关 举报
C++程序设计编程实例大全.doc_第1页
第1页 / 共105页
C++程序设计编程实例大全.doc_第2页
第2页 / 共105页


点击查看更多>>
资源描述
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 name[10]; cout<<"please input your name:"; cin>>name; cout<<"the name is "<<name<<endl; } #include "iostream" using namespace std; void main() { char name[10]; 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 int[n])==0) { cout<<"can't allocate more memory, terminating"<<endl; exit(1); } for(int i=0;i<n;i++) p[i]=i*2; cout<<"Now output the array: "<<endl; for(i=0;i<n;i++) cout<<p[i]<<" "<<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 int[n])==0) { cout<<"can't allocate more memory, terminating"<<endl; exit(1); } for(int i=0;i<n;i++) p[i]=i*i+1; cout<<"Now output the array: "<<endl; for(i=0;i<n;i++) cout<<p[i]<<" "; 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 &x,int &y); void main() { int x=5,y=6; cout<<"before swap, x:"<<x<<" ,y:"<<y<<endl; swap(x,y); cout<<"after swap, x:"<<x<<" ,y:"<<y; } void swap(int &rx,int &ry) { int t=rx; rx=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 array[6][4]={{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(array[i],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+=grade[i]; 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 x,int y) { int sum; sum=x+y; return sum; } int add(int x,int y,int z) { int sum; sum=x+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=a[0]; for(i=1;i<n;i++) if(minv>a[i]) minv=a[i]; 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_Square template<class T> double Circle_Square(T x) { return x*x*PI; } double Circle_Square(long x) { return x*x*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 Tdate { public: 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 x; x.set(10,23,2000); x.isLeapYear(); x.print(); } /* Note:Your choice is C IDE */ #include <iostream.h> class Tdate { public: 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 stk[SIZE]; 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; } stk[position++]=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 Tdate { public: 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 q[100]; 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++; q[sloc]=i; } int queue::qget() { if(rloc==sloc) { cout<<"queue is empty"<<endl; return 0; } rloc++; return q[rloc]; } 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 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() { test x; test y(10,21.5); test *px=new test; test *py=new test(10,21.5); } /* Note:Your choice is C IDE */ #include <iostream.h> class Tdate { public: 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)); name[sizeof(name)-1]='\0'; cout<<"the name is "<<name<<endl; } Student() { cout<<"call no parameter constructor"<<endl; } protected: char name[20]; }; 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 array[5]; cout<<"the second element of array is "<<array[1].getint()<<","<<array[1].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<<"Initializing\t"<<n<<endl; num=n; } test::test(int n,float f) { cout<<"Initializing\t"<<n<<","<<f<<endl; num=n; f1=f; } void main() { test array1[3]={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<<"Initializing\t"<<n<<endl; num=n; } test::test(int n,float f) { cout<<"Initializing\t"<<n<<","<<f<<endl; num=n; f1=f; } void main() { test array1[3]={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 Tdate{ public: 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<exception> #include<iostream> using namespace std; void main() { try { exception theError; throw(theError); } catch(const exception &theError) { cout<<theError.what()<<endl; } try { logic_error theLogicError("Logic Error!"); throw(theLogicError); } catch(const exception & theLogicError) { cout<<theLogicError.what()<<endl; } } #include<iostream> #include<string> using namespace std; class Person { public: Person(char *na) { cout<<"call constructor"<<endl; name=new char[strlen(na)+1]; if(name!=0) {strcpy(name,na);} } Person(Person&p) { cout<<"call copy constructor"<<endl; name=new char[strlen(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 SillyClass { public: SillyClass(int&i):te
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 应用文书 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服