C++课程学习代码汇总基础
来源:互联网 发布:华为手机网络连接错误 编辑:程序博客网 时间:2024/06/05 16:19
写在开头的话:
今天难得一天没课。打算把之前学习过的课程再重新看一遍。写了好多代码,编译运行之后都删掉写下一个了。
久而久之不免觉得浪费,现在把它们都贴在下面把。就跟看着自己的孩子一样。说不定以后有利用的价值,没事儿还可以翻出来看看。
(不定时补充)
发现好像写了蛮多的,在1楼做一个目录吧。方便以后的查阅。
- 文字信息统计
- 冒泡排序
- 简易计算器
- 使用string字符串替换拼接字符中的单词
- 三位逆序数
- 使用string取子字符串
- 词频统计
- 求三个数中的最大数
- Hanoi汉诺塔问题
- 利用指针实现三个数的排序
- 利用字符指针删除字符首部的空格
- 钟表类的完整定义
- 日期类的完整定义
- 分数运算函数的完整定义
- 可鸣笛、运动和播放MP3的汽车类定义
- 设计平面上的几何图形:圆
- 学生信息类1.0
- 学生信息类2.0
- 日期类的定义(构造函数版)
- 日期类的定义(重载构造函数版)
- 日期类的定义(析构函数版)
- 能翻译整数的机器人1.0
- 能翻译整数的机器人2.0
- Person类的定义
- this指针使用的例子
- 日期类输出定义
- person类(singer)综合定义
- person类继承Date类和Time类
- 智能手机类定义
下面是代码的集合:
- 文字信息统计
#include<iostream>using namespace std;int main(){const int N=100;char str[N];int len=0,capital=0,smallletter=0,digit=0,others=0;cout<<"Please input the sentence:"<<endl;cin.getline(str,N);int i=0;while(str[i]!='\0'){len++;if(str[i]>='A'&&str[i]<='Z'){capital++;}else if(str[i]>='a'&&str[i]<='z'){smallletter++;}else if(str[i]>='0'&&str[i]<='9'){digit++;}else{others++;}i++;}cout<<"please wait..."<<endl;system("pause");cout<<"there are "<<capital<<"capital, "<<smallletter<<"smallletter, "<<digit<<"digit and "<<others<<"others."<<endl;cout<<"thank you for using!"<<endl;return 0;}
- 冒泡排序
#include<iostream>#include<cmath>using namespace std;int main(){int N,i,j;double a[100];cout<<"Please input the number of tested numbers:"<<endl;cin>>N;cout<<"Please input the tested numbers:"<<endl;for(i=0;i<N;i++){cin>>a[i];}for(i=0;i<N-1;i++){for(j=0;j<N-1-i;j++){if(a[j]>a[j+1]){int tmp;tmp=a[j];a[j]=a[j+1];a[j+1]=tmp;} }}cout<<"Please wait..."<<endl;system("pause");for(i=0;i<N;i++){cout<<a[i]<<" ";}cout<<endl;cout<<endl;cout<<"Thanks for using!"<<endl;return 0;}
- 简易计算器
#include<iostream>#include<cmath>using namespace std;int main(){int num1,num2,result;char op;char s1[30]="Error!Divided by 0!";char s2[30]="Wrong Expression!";cout<<"Please input the expression:"<<endl;cin>>num1>>op>>num2;switch(op){case'+':result=num1+num2;cout<<num1<<op<<num2<<"="<<result<<endl;break;case'-':result=num1-num2;cout<<num1<<op<<num2<<"="<<result<<endl;break;case'*': result=num1*num2;cout<<num1<<op<<num2<<"="<<result<<endl;break;case'/': if(fabs(num2)==0){ cout<<s1<<endl; break;} else{result=num1/num2;cout<<num1<<op<<num2<<"="<<result<<endl;break;}}cout<<endl;cout<<"Thanks for using!"<<endl;return 0;}
- 使用string字符串替换拼接字符中俄单词
#include<iostream>#include<string>using namespace std;int main(){string text1("Heavy rains are pushing water levels beyoud the limit.");string text2,text3;int k;text2="Sluice gates at Three Gorges Dam opened to discharge water.";text3=text1+text2;k=text3.find("Heavy");text3.erase(k,sizeof("Heavy")-1);text3.insert(k,"Strong");cout<<text3<<endl;cout<<endl;cout<<"Thanks for using!";return 0;}
- 三位逆序数
#include<iostream>using namespace std;int main(){int a,b,c,n;cout<<"Please input a three-digit number:"<<endl;cin>>n;a=n%10;b=(n/10)%10;c=n/100;cout<<"the inversion number is "<<a<<b<<c<<endl;cout<<endl;cout<<"Thanks for using!"<<endl;return 0;}
- 使用string取子字符串
#include<iostream>using namespace std;int main(){char str[101];char sub[101];int len,k,l;int i,j=0;cout<<"Please input the sentence:"<<endl;cin.getline(str,100);len=0;while(str[len]!='\0'){len++;}cout<<"Please input the starting position and the lenth of the new sentence: ";cin>>k>>l;while(k!=0&&l!=0){j=0;for(i=k-1;i<k+l-1&&i<len;i++){sub[j]=str[i];j++;}sub[j]='\0';cout<<sub<<endl;cout<<"Please input the starting position and the lenth of the new sentence: ";cin>>k>>l;} return 0;}
- 词频统计
#include<iostream>#include<cstring>using namespace std;struct WordList{char word[20];int freq;};int main(){WordList list[1000];int N=0;int i,j,k;char tmp[20];cout<<"Please input some English words,and input 'xyz' to end."<<endl;cin>>tmp;while(strcmp(tmp,"xyz")!=0){for(i=0;i<N;i++){if(strcmp(list[i].word,tmp)==0){list[i].freq++;break;}}if(i>=N){strcpy(list[i].word,tmp);list[i].freq=1;N++;}cin>>tmp;}for(i=0;i<N-1;i++){k=i;for(j=i+1;j<N;j++){if(strcmp(list[i].word,list[k].word)<0){k=j;}}if(k!=i){WordList tmp;tmp=list[i];list[i]=list[k];list[k]=tmp;}}//输出结果cout<<"the result is below:"<<endl;for(i=0;i<N;i++){cout<<list[i].word<<'\t'<<list[i].freq<<endl;} cout<<endl;cout<<"Thanks for using!";return 0;}
- 求三个数中的最大数
#include<iostream>using namespace std;double maxThree(double a,double b,double c){double max;max=a>b?a:b;max=max>c?max:c;return max;}int main(){double x1,x2,x3;double max;cout<<"Please input 3 numbers:"<<endl;cin>>x1>>x2>>x3;max=maxThree(x1,x2,x3);cout<<"The biggest number is "<<max<<endl;cout<<endl;cout<<"Thanks for using!";return 0; }
- Hanoi汉诺塔问题
//教材中将两个自定义void函数放在了main函数之后,但是由于个人习惯,我还是将main函数放在了最后 #include<iostream>using namespace std;void hanoi(int n,int p1,int p2,int p3){void move(int,char,char);if(n==1){move(n,p1,p3);}else{hanoi(n-1,p1,p3,p2);move(n,p1,p3);hanoi(n-1,p2,p1,p3);} }void move(int n,char from,char to){cout<<n<<" 号盘从"<<from<<" 到"<<to<<endl;}int main(){int n;cout<<"Please input the number of the plates:"<<endl;cin>>n;void hanoi(int,int,int,int);hanoi(n,'A','B','C');cout<<endl;cout<<"Thanks for using!"; return 0;}
- 利用指针实现三个数的排序
#include<iostream>using namespace std;void swap(int *px,int *py){int t=*px;*px=*py;*py=t;}int main(){int a,b,c;cout<<"Please input 3 numbers:"<<endl;cin>>a>>b>>c;int *pa=&a;int *pb=&b;int *pc=&c;if(*pa>*pb){swap(pa,pb);}if(*pa>*pc){swap(pa,pc);}if(*pb>*pc){swap(pb,pc);}cout<<"The correct order is: "<<a<<" "<<b<<" "<<c<<endl;cout<<endl;cout<<"Thanks for using!";return 0;}
- 利用字符指针删除字符首部的空格
#include<iostream>using namespace std;char *ltrim(char s[]){int k,j;k=0;while(s[k]==' '){k++;}j=k;while(s[j]!='\0'){s[j-k]=s[j];j++;}s[j-k]='\0';return &s[0];}int main(){char str[100];cout<<"请输入首部带一个或若干个空格的字符:"<<endl; cin.getline(str,99);cout<<"去空格前-|"<<str<<"|"<<endl; cout<<"去空格后-|"<<ltrim(str)<<"|"<<endl; cout<<endl; cout<<"Thanks for using!"; return 0;}
- 钟表类的完整定义
#include<iostream> #include<windows.h> using namespace std; class Clock { private: int Hour; int Minute; int Second; float Price; public: void Set(int h,int m,int s,float p); void Run(); void Report_Time(); void Show_Time(){cout<<Hour<<":"<<Minute<<":"<<Second;}; }; void Clock::Set(int h,int m,int s,float p) { Hour=h; Minute=m; Second=s; Price=p; } void Clock::Run() { int i=0; for(i=0;i<10;i++){ Second++; if(Second==60){ Second=0; Minute++; if(Minute==60){ Minute=0; Hour++; if(Hour==24)Hour=0; } } cout<<"\r"; Show_Time(); Sleep(1000); } } void Clock::Report_Time() { Show_Time(); if(Minute==0&&Second==0){ for(int i=0;i<Hour;i++){ cout<<"\007"; Sleep(1000); } } } int main() { Clock XJTU_Big_Ben; XJTU_Big_Ben.Set(0,0,0,1000); cout<<"The Time that the Clock Set:\n"; XJTU_Big_Ben.Show_Time(); XJTU_Big_Ben.Report_Time(); XJTU_Big_Ben.Run(); system("pause"); XJTU_Big_Ben.Set(9,59,50,9000); cout<<"\n The Time that the Clock Set: \n"; XJTU_Big_Ben.Run(); XJTU_Big_Ben.Report_Time(); cout<<endl; XJTU_Big_Ben.Show_Time(); return 0; }
- 日期类的完整定义
#include<iostream> #include<time.h> using namespace std; class Date { private: int year,month,day; void SetSystemDate(); //取得系统日期 public: void init(int,int,int); void print_ymd(); void print_mdy(); int get_year(){return year;} int get_month(){return month;} //上中下均为内联函数 int get_day(){return day;} bool IsLeapYear(); //判断是否为闰年 }; void Date::SetSystemDate() { tm *gm; //tm是时间结构体 time_t t=time(NULL); //time_t是长整型 gm=gmtime(&t); year=1900+gm->tm_year; month=gm->tm_mon+1; day=gm->tm_mday; } void Date::init(int yy,int mm,int dd) //有效性检验 { if(yy>=1900&&yy<=9999) year=yy; else { SetSystemDate(); return; } if(mm>=1&&mm<=12) month=mm; else { SetSystemDate(); return; } if(dd>=1&&dd<=31) day=dd; else { SetSystemDate(); } return; } void Date::print_ymd() { cout<<year<<"."<<month<<"."<<day<<endl; } void Date::print_mdy() { cout<<month<<"."<<day<<"."<<year<<endl; } bool Date::IsLeapYear() { if(year%400==0||(year%100!=0&&year%4==0)) return true; else return false; } int main() { Date date1; cout<<"输出日期对象date1的日期值:"; date1.print_ymd(); system("pause"); date1.init(2008,3,28); cout<<"再输出日期对象date1的日期值:"; date1.print_ymd(); system("pause"); Date date2; date2.init(2006,13,28); cout<<"输出对象日期date2的日期值:"; date2.print_mdy(); system("pause"); if(date1.IsLeapYear()) cout<<date1.get_year()<<"是闰年"<<endl; else cout<<date1.get_year()<<"不是闰年"<<endl; return 0; }
- 分数运算函数的完整定义
#include<iostream> #include<cmath> using namespace std; class Fraction { protected: int a; //分子定义成保护成员 int b; //分母因为不能为0,因此定义为私有成员 int divisor(int p,int q); // 求最大公约数 public: void set(int aa,int bb); void show(); //显示分数 Fraction add(Fraction u); //加一个分数 }; void Fraction::set(int aa,int bb) { a=aa; if(bb!=0){ b=bb; } else{ a=0; b=1; } } void Fraction::show() { cout<<a<<"/"<<b; } Fraction Fraction::add(Fraction u) { int tmp; Fraction v; v.a=a*u.b+b*u.a; v.b=b*u.b; tmp=divisor(v.a,v.b); v.a=v.a/tmp; v.b=v.b/tmp; return v; } int Fraction::divisor(int p,int q) { int r; if(p<q) { int tmp; tmp=p; p=q; q=tmp; } r=p%q; while(r!=0) { p=q; q=r; r=p%q; } return q; } class Real:public Fraction { public: void show_real() { cout<<a<<'/'<<b<<'='<<a/(double)b<<endl; } }; int main() { Real f1,f2,f3; int a,b,c,d; cout<<"\n请分别输入两个分数的分子和分母。分母为0时退出\n"; cin>>a>>b; cin>>c>>d; f1.set(a,b); f2.set(c,d); f1.show_real(); f2.show_real(); cout<<endl; Fraction f4; f1.show(); cout<<"+"; f2.show(); f4=f1.add(f2); cout<<"="; f4.show(); cout<<endl; return 0; }
- 可鸣笛、运动和播放MP3的汽车类定义
#include<iostream>#include<cstring>#include<windows.h>#include<mmsystem.h>using namespace std;class automobile{private:char type[20];char color[20];float price;int carry_weight;int carry_customer;public:void set_data(char *t,char *c,float pri,int cw,int cc);void movecar(int l,int k);void horming(int num);void downcar(int l);void play_mp3(char *ps);char *show_type(){return type;}};void automobile::set_data(char *t,char *c,float pri,int cw,int cc){strcpy(type,t);strcpy(color,c);price=pri;carry_weight=cw;carry_customer=cc;}void automobile::movecar(int l,int k){cout<<"\n"<<type<<"水平直线运动:"<<endl;for(int i=0;i<l;i++){cout<<" "<<"o_o";Sleep(1000/k);cout<<"\b\b\b";}}void automobile::downcar(int l){cout<<"\n"<<type<<"垂直下降运动:"<<endl;for(int i=0;i<l;i++){cout<<"o_o";Sleep(500);cout<<"\b\b\b"<<" ";cout<<endl;}}void automobile::horming(int num){for(int i=0;i<num;i++){cout<<type;cout<<'\007'<<" di..."<<endl;Sleep(1000);}}void automobile::play_mp3(char *ps){char str[100]="play ";strcat(str,ps);cout<<str;mciSendString(str,NULL,0,NULL); //照写就行了! char a;cin>>a;}int main(){automobile nison;char tp[]="yyida";char ys[]="white";nison.set_data(tp,ys,200000,5,3);nison.horming(5);nison.movecar(10,3);nison.downcar(8);char mp[]="d:\\宋冬野 - 董小姐.flac";nison.play_mp3(mp);return 0; }
- 设计平面上的集合图形:圆
#include<iostream>using namespace std;class Point{private:int x,y; //如不写private则默认为私有成员 public:void InitPoint(int,int);int GetX(){return x;}int GetY(){return y;}void print();};void Point::InitPoint(int a,int b){x=a;y=b;}void Point::print(){cout<<"["<<x<<","<<y<<"]";}class Circle{private:double radius;Point Center;public: void InitCircle(double,Point);double GetRadius();Point GetCenter();double Area();void Print(); };void Circle::InitCircle(double r,Point p){radius=(r>0?r:0); //有效性检验 Center=p;}double Circle::GetRadius(){return radius;}Point Circle::GetCenter(){return Center;}double Circle::Area(){return 3.14159*radius*radius;}void Circle::Print(){cout<<"Center=";Center.print();cout<<"; Radius="<<radius<<endl; }int main(){Point p,center;p.InitPoint(30,50);center.InitPoint(120,80);Circle c;c.InitCircle(10.0,center);cout<<"Point p:";p.print();cout<<endl;cout<<"Circle c:";c.Print();cout<<"The center of the circle c:";c.GetCenter().print();cout<<"\nThe area of circle c:"<<c.Area()<<endl;cout<<endl;cout<<"Thanks for using!";return 0; }
- 学生信息类1.0
#include<iostream>#include<cstring>using namespace std;//设置信息函数Set_infoclass Info{private: //我还是喜欢把private打出来 int Id; char Name[20]; int Programming; int Network; int Database; int Total;public:void Set_info(int id,char *name,int programming,int network,int database); int Get_Pro(); int Get_Net(); int Get_Dat(); int Get_Tol(); void Show();};void Info::Set_info(int id,char *name,int programming,int network,int database){Id=id;strcpy(Name,name);Programming=programming;Network=network;Database=database;Total=programming+network+database;} int Info::Get_Pro(){return Programming;}int Info::Get_Net(){return Network;}int Info::Get_Dat(){return Database;}int Info::Get_Tol(){return Total;}//显示信息函数Show void Info::Show(){cout<<Id<<'\t';cout<<Name<<'\t';cout<<Programming<<'\t';cout<<Network<<'\t';cout<<Database<<'\t';cout<<Total<<endl;}int main(){const int COUNT=2;int i=0,j=0;int id;char name[20];int programming;int network;int database;Info student[COUNT],tmp;cout<<"请输入学生成绩的信息:"<<endl;cout<<"学号 姓名 程序设计 计算机网络 数据库"<<endl;for(i=0;i<COUNT;i++){cin>>id>>name>>programming>>network>>database;student[i].Set_info(id,name,programming,network,database);}cout<<"学号 姓名 程序设计 计算机网络 数据库 总分"<<endl;for(i=0;i<COUNT;i++){student[i].Show();} cout<<endl;cout<<"Thanks for using!";return 0;}
- 学生信息类2.0
#include<iostream>#include<cstring>using namespace std;//设置信息函数Set_infoclass Info{private: //我还是喜欢把private打出来 int Id; char Name[20]; int Programming; int Network; int Database; int Total;public:void Set_info(int id,char *name,int programming,int network,int database); int Get_Pro(); int Get_Net(); int Get_Dat(); int Get_Tol(); void Show();};void Info::Set_info(int id,char *name,int programming,int network,int database){Id=id;strcpy(Name,name);Programming=programming;Network=network;Database=database;Total=programming+network+database;} int Info::Get_Pro(){return Programming;}int Info::Get_Net(){return Network;}int Info::Get_Dat(){return Database;}int Info::Get_Tol(){return Total;}//显示信息函数Show void Info::Show(){cout<<Id<<'\t';cout<<Name<<'\t';cout<<Programming<<'\t';cout<<Network<<'\t';cout<<Database<<'\t';cout<<Total<<endl;}int main(){const int MAX_COUNT=100;int count;int i=0,j=0;int id;char name[20];int programming;int network;int database;Info student[MAX_COUNT],tmp;cout<<"请输入学生人数:";cin>>count;while(count>MAX_COUNT){cout<<"学生人数不能超过"<<MAX_COUNT<<"人,请重新输入:";cin>>count;}cout<<"请输入学生成绩的信息:"<<endl;cout<<"学号 姓名 程序设计 计算机网络 数据库"<<endl;for(i=0;i<count;i++){cin>>id>>name>>programming>>network>>database;student[i].Set_info(id,name,programming,network,database);}cout<<"按总分高低排名如下:"<<endl;for(i=0;i<count;i++){for(j=count-1;j>i;j=j-1){if(student[j].Get_Tol()>student[j-1].Get_Tol()){tmp=student[j];student[j]=student[j-1];student[j-1]=tmp;}}}cout<<"学号 姓名 程序设计 计算机网络 数据库 总分"<<endl;for(i=0;i<count;i++){student[i].Show();} cout<<"每门课程成绩都大于85分的学生名单:"<<endl;cout<<"学号 姓名 程序设计 计算机网络 数据库 总分"<<endl;for(i=0;i<count;i++){if(student[i].Get_Pro()>85&&student[i].Get_Net()>85&&student[i].Get_Dat()>85){student[i].Show();}}cout<<endl;cout<<"Thanks for using!";return 0;}
- 日期类的定义(构造函数版)
#include<iostream>using namespace std;class Date{private:int year,month,day;public:Date(int y=1900,int m=1,int d=1) //名字相同,这就是构造函数 {year=y;month=m;day=d;}void init(int,int,int);void print_ymd();void print_mdy();};void Date::init(int yy,int mm,int dd) //有效性检验 {month=(mm>=1&&mm<=12)?mm:1;year=(yy>=1900&yy<=9999)?yy:1900;day=(dd>=1&&dd<=31)?dd:1;}void Date::print_ymd(){cout<<year<<"-"<<month<<"-"<<day<<endl;} void Date::print_mdy(){cout<<month<<"-"<<day<<"-"<<year<<endl;}int main(){Date date1; //创建一个日期类对象,并初始化 cout<<"输出对象date1的数据成员值:"<<endl;date1.print_ymd(); //显示初始化数据的情况system("pause"); date1.init(2006,3,28); //修改数据成员 date1.print_ymd(); //显示修改数据的情况system("pause"); Date date2(2013,11,26); //再创建一个日期类对象,并初始化 cout<<"输出对象date2的数据成员值:"<<endl;date2.print_mdy();system("pause");date2.init(2006,13,38);date2.print_ymd();cout<<endl;cout<<"Thanks for using!"; return 0;}
- 日期类的定义(重载构造函数版)
#include<iostream> #include<string.h>#include<stdlib.h>using namespace std;class Date{private:int year,month,day;public:Date():year(1900),month(1),day(1){}; //无参构造函数 Date(int yy,int mm=1,int dd=1); //默认参数构造函数 Date(Date &d):year(d.year),month(d.month),day(d.day){};Date(char *ps);void print_ymd(); };Date::Date(int yy,int mm,int dd):year(1900),month(1),day(1){if(yy>=1900&&yy<=9999){ //这段还是有效性检验 year=yy;}else{return;}if(mm>=1&&mm<=12){month=mm;}else{year=1900;return;}if(dd>=1&&dd<=31){day=dd;}else{year=1900;month=1;return;} } Date::Date(char *ps):year(1900),month(1),day(1) { char py[5],pm[3],pd[3]; strncpy(py,ps,4); ps=ps+5; strncpy(pm,ps,2); ps=ps+3; strncpy(pd,ps,2); int yy=atoi(py),mm=atoi(pm),dd=atoi(pd); if(yy>=1900&yy<9999){ year=yy;}else{return;} if(mm>=1&&mm<=12){month=mm;}else{year=1900;return;}if(dd>=1&&dd<=31){day=dd;}else{year=1900;month=1;return;} } void Date::print_ymd() { cout<<year<<"-"<<month<<"-"<<day<<endl; } int main() { Date date1; cout<<"date1:"; date1.print_ymd(); system("pause"); Date date2(2006); cout<<"date2:"; date2.print_ymd(); system("pause"); Date date3(2006,4); cout<<"date3;"; date3.print_ymd(); system("pause"); Date date4(2006,4,8); cout<<"date4:";date4.print_ymd();system("pause"); Date date5(2006,14,8);cout<<"date5:";date5.print_ymd();system("pause");Date date6(date4);cout<<"date6:";date6.print_ymd();system("pause");Date date7("2008-08-08");cout<<"date7:";date7.print_ymd();system("pause");cout<<endl;cout<<"Thanks for using!";return 0; }
- 日期类的定义(析构函数版)
#include<iostream>#include<string.h>#include<stdlib.h>using namespace std;class Date{private:int year;int month;int day;public:Date():year(1900),month(1),day(1){}Date(int yy,int mm=1,int dd=1);Date(Date &d):year(d.year),month(d.month),day(d.day){};Date(char *ps); //字符串日期构造函数 void print_ymd();~Date(){static int i=1;cout<<"撤销日期对象"<<i<<'\n';i++;}; };Date::Date(int yy,int mm,int dd):year(1900),month(1),day(1) { if(yy>=1900&&yy<=9999){ //这段还是有效性检验 year=yy; } else{ return; } if(mm>=1&&mm<=12){ month=mm; } else{ year=1900; return; } if(dd>=1&&dd<=31){ day=dd; } else{ year=1900; month=1; return; } } Date::Date(char *ps):year(1900),month(1),day(1) { char py[5],pm[3],pd[3]; strncpy(py,ps,4); ps=ps+5; strncpy(pm,ps,2); ps=ps+3; strncpy(pd,ps,2); int yy=atoi(py),mm=atoi(pm),dd=atoi(pd); if(yy>=1900&yy<9999){ year=yy; } else{ return; } if(mm>=1&&mm<=12){ month=mm; } else{ year=1900; return; } if(dd>=1&&dd<=31){ day=dd; } else{ year=1900; month=1; return; } } void Date::print_ymd() { cout<<year<<"-"<<month<<"-"<<day<<endl; } int main(){Date date1; cout<<"date1:"; date1.print_ymd(); system("pause"); Date date2(2006); cout<<"date2:"; date2.print_ymd(); system("pause"); Date date3(2006,4); cout<<"date3;"; date3.print_ymd(); system("pause"); Date date4(2006,4,8); cout<<"date4:"; date4.print_ymd(); system("pause"); Date date5(2006,14,8); cout<<"date5:"; date5.print_ymd(); system("pause"); Date date6(date4); cout<<"date6:"; date6.print_ymd(); system("pause"); Date date7("2008-08-08"); cout<<"date7:"; date7.print_ymd(); system("pause"); cout<<endl; cout<<"Thanks for using!"; return 0; }
- 能翻译整数的机器人1.0
#include<iostream>#include<cstring>using namespace std;class robot{private:char name[20];char type[20];public:robot(){strcpy(name,"XXXXXX");strcpy(type,"XXXXXX");}void set(char n[],char t[]){strcpy(name,n);strcpy(type,t);}void out(int a);void tran_int(int n);~robot(){};};static char *num1[]={" ","one ","two ","three ","four ","five ","six ","seven ","eight ","nine ","ten ","eleven ","twelve ","thirteen ","fourteen ","fifteen ","sixteen ","seventeen ","eighteen ","nineteen "};static char *num10[]={" ","twenty ","thirty ","forty ","fifty ","sixty ","seventy ","eighty ","ninety "};void robot::out(int a){int b=a%100;if(a/100!=0){cout<<num1[a/100]<<"hundred ";if(b!=0);cout<<"and ";}if(b<20){cout<<num1[b];}else{cout<<num10[b/10];if(b%10!=0){cout<<"\b."<<num1[b%10];}}}void robot::tran_int(int n){if(n>1999999999){cout<<"Dev C++ 平台无法处理大于1999999999位的数字!"<<endl;}else{int a=n/1000000000;int b=(n%1000000000)/1000000;int c=(n%1000000)/1000;int d=n%1000; if(a!=0){ out(a); cout<<"billion "; } if(b!=0){ out(b); cout<<"million "; } if(c!=0){ out(c); cout<<"thousand "; } if(d!=0){ if(d<100&&(a!=0||b!=0||c!=0)){ cout<<"and "; } out(d); } cout<<endl; }}int main(){int n;cout<<"请输入n: ";cin>>n;cout<<n<<endl;robot brown;brown.tran_int(n);cout<<endl;cout<<"Thanks for using!";return 0;}
- 能翻译整数的机器人2.0
#include<iostream>#include<cstring>#include<stdlib.h>using namespace std;class robot{private: char name[20]; char type[20]; int num; char *ps;public:robot() //构造函数 { strcpy(name,"XXXXXX"); strcpy(type,"XXXXXX"); num=0; ps=new char[5]; strcpy(ps,"Zero");}void set(char n[],char t[],int m);char *out(int a);char *tran_int(int n);void print_num();~robot(){ cout<<"堆区中"<<strlen(ps)+1<<"个字节空间释放收回"<<endl;delete[]ps;};};static char *num1[]= { " ","one ","two ","three ","four ","five ","six ","seven ", "eight ","nine ","ten ","eleven ","twelve ","thirteen ", "fourteen ","fifteen ","sixteen ","seventeen ","eighteen ", "nineteen " }; static char *num10[]= { " "," ","twenty ","thirty ","forty ","fifty ","sixty ","seventy ", "eighty ","ninety " }; char *robot::out(int a){char k[1000]="";int b=a%100;if(a/100!=0){strcat(k,num1[a/100]);strcat(k,"hundred ");if(b!=0){strcat(k,"and ");}}if(b<20){strcat(k,num1[b]);}else{strcat(k,num10[b/10]);if(b%10!=0){strcat(k,"\b-");}strcat(k,num1[b%10]);}char *p=new char[strlen(k)+1];strcpy(p,k);return p;}char *robot::tran_int(int n){char *p;char kk[1000]="";if(n>1999999999){//cout<<"dev C++平台无法处理太大的数字!"<<endl;strcpy(kk,"dev C++平台无法处理太大的数字!\n");}else{int a=n/1000000000;int b=(n%1000000000)/1000000;int c=(n%1000000)/1000;int d=n%1000;if(a!=0){p=out(a);strcpy(kk,p);strcat(kk,"billion ");delete[]p;}if(b!=0){p=out(b);strcat(kk,p);strcat(kk,"million ");delete[]p;}if(c!=0){p=out(c);strcat(kk,p);strcat(kk,"thousand ");delete[]p;} if(d!=0){if(d<100&&(a!=0||b!=0||c!=0)){strcat(kk,"and ");}p=out(d);strcat(kk,p);delete[]p;}}//end of if(n>1999999999) elsep=new char[strlen(kk)+1];strcpy(p,kk);return p;}void robot::print_num(){cout<<"整数为:"<<num<<endl;if(ps==NULL){cout<<"zero"<<endl;}else{cout<<ps<<endl;}}void robot::set(char n[],char t[],int m){strcpy(name,n);strcpy(type,t);if(num==m){return;}else{num=m;delete [] ps;}if(num>0){char *tp=tran_int(num);ps=new char[strlen(tp)+1];strcpy(ps,tp);delete[]tp;}else if(num==0){ps=new char[5];strcpy(ps,"Zero");}else{ps=new char[13];strcpy(ps,"负数不能翻译");}}int main(){robot brown;brown.print_num();int n;cout<<"请输入n:";cin>>n;brown.set("brown","800#",n);brown.print_num();cout<<endl;cout<<"Thanks for using!"; return 0;}
- Person类的定义
#include<iostream>#include<cstring>using namespace std;class Person{private:char Name[9];char Sex;int Age;char Pid[19];char *Add;public:Person(){strcpy(Name,"XXX");Age=0;Sex=' ';strcpy(Pid,"xxxxxxxxxxxxxxxxxx");Add=NULL;}Person(char *N,int A,char S,char *P,char *Ad){strcpy(Name,N);Age=A;Sex=S;strcpy(Pid,P);int L=strlen(Ad);Add=new char[L+1];strcpy(Add,Ad);}~Person(){cout<<"Now destroying Person: "<<Name<<endl;delete[]Add;}void Register(char *name,int age,char sex,char *Pid,char *add);void ShowMe();};void Person::Register(char *name,int age,char sex,char *pid,char *add){strcpy(Name,name);Age=age;Sex=sex;strcpy(Pid,pid);if(Add!=NULL){delete[]Add;}int L=strlen(add);Add=new char[L+1];strcpy(Add,add);}void Person::ShowMe(){cout<<Name<<" "<<Age<<" "<<Sex<<" "<<Pid<<" ";if(Add!=NULL){cout<<Add;}cout<<endl;}int main(){Person person1;Person person2("张晨曦",19,'m',"142703199612073333","江苏科技大学西校区3#515");Person person3;cout<<"person1: ";person1.ShowMe();cout<<endl;cout<<"person2: "; person2.ShowMe(); cout<<endl; person3.Register("林丹",32,'m',"610103198409152127","中国新疆乌鲁木齐不知名的山沟沟12号"); cout<<"person3:";person3.ShowMe();cout<<endl;person2.Register("金三胖",28,'w',"610103198409192493","朝鲜大楼");cout<<"person2: ";person2.ShowMe(); cout<<endl;cout<<endl;cout<<"Thanks for using!"<<endl;return 0;}
- this指针使用的例子
#include<iostream>using namespace std;class Test{ private: int x; public: Test(int=0); void print();};Test::Test(int a){x=a;}void Test::print(){int x=13;cout<<" x="<<x<<endl;cout<<" this->x="<<this->x<<endl;cout<<"(*this).x="<<(*this).x<<endl;}int main(){Test testObject(12);testObject.print();return 0;}
- 日期类输出的定义
#include<iostream>using namespace std;class Date{private:int day,month,year;public:Date(int y=1900,int m=1,int d=1); //构造函数设置默认值 void SetDate(int yy,int mm,int dd);bool IsLeapYear();void print_ymd();void print_mdy();};Date::Date(int y,int m,int d){SetDate(y,m,d);}void Date::SetDate(int yy,int mm,int dd){month=(mm>=1&&mm<=12)?mm:1;year=(yy>=1900&&yy<=2100)?yy:1;switch(month){case 4:case 6:case 9:case 11: day=(dd>=1&&dd<=30)?dd:1;break;case 2:if(IsLeapYear()){day=(dd>=1&&dd<=29)?dd:1;}else{day=(dd>=1&&dd<=28)?dd:1;}break;default:day=(dd>=1&&dd<=31)?dd:1;}}bool Date::IsLeapYear(){if(year%400==0||(year%100!=0&&year%4==0)){return true;}else{return false;}}void Date::print_ymd(){cout<<year<<"-"<<month<<"-"<<day<<endl;}void Date::print_mdy(){char *monthName[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};cout<<monthName[month-1]<<" "<<day<<","<<year<<endl;} int main(){Date date;int a,b,c;cout<<"Please input the date: "<<endl;cin>>a>>b>>c;date.SetDate(a,b,c);cout<<"两种输出格式如下:"<<endl;date.print_ymd();date.print_mdy();cout<<endl;cout<<"Thanks for using!";return 0;}
- person类(singer)综合定义
#include<iostream>#include<windows.h>#include<cstring>using namespace std;class person{private:char *name;char sex;char pid[19];int weight;int high;public:person(); //一个可爱的无参构造函数person(char *n,char s,char *p,int w,int h);void change_data(char *n,char s,char *p,int w,int h);void walking(int k,int v);void hearing(char *sentence);void speaking(int n);void writing();void print();void out(int a);~person(); };static char *num1[]= { " ","one ","two ","three ","four ","five ","six ","seven ", "eight ","nine ","ten ","eleven ","twelve ","thirteen ", "fourteen ","fifteen ","sixteen ","seventeen ","eighteen ", "nineteen " };static char *num10[]= { " "," ","twenty ","thirty ","forty ","fifty ","sixty ","seventy ", "eighty ","ninety " }; void person::print(){cout<<name<<" "<<sex<<" "<<pid<<" "<<weight<<" "<<high<<endl;}person::person(){name=new char[strlen("XXXXXX")+1];strcpy(name,"XXXXXX");strcpy(pid,"XXXXXXXXXXXXXXXXXX");sex='X';weight=0;high=0; }person::person(char *n,char s,char *p,int w,int h){name=new char[strlen(n)+1];strcpy(name,n);strcpy(pid,p);sex=s;weight=w;high=h;}void person::change_data(char *n,char s,char *p,int w,int h){if(name!=NULL){delete[]name;}name=new char[strlen(n)];strcpy(name,n);strcpy(pid,p);sex=s;weight=w;high=h;}void person::out(int a){int b=a%100;if(a/100!=0){cout<<num1[a/100]<<"hundred ";if(b!=0){cout<<"and ";}}if(b<20){cout<<num1[b];}else{cout<<num10[b/10];if(b%10!=0){cout<<"\b-"<<num1[b%10];}}}void person::walking(int k,int v){cout<<"\n"<<name<<"水平直线行走"<<k<<"步"<<endl;for(int i=0;i<k;i++){cout<<' '<<"o_o";Sleep(1000/v);cout<<"\b\b\b";}}void person::hearing(char *sentence){cout<<endl<<sentence<<endl;char *p=new char[strlen(sentence)+1];strcpy(p,sentence);char *pp=p;while(*p){if(*p>'a'&&*p<='z'){*p='A'+(*p-'a'+0); //删掉这个+0试试看 }else if(*p>='A'&&*p<='Z'){*p='a'+(*p-'A');}p++;}cout<<pp<<endl;delete []pp;}void person::speaking(int n){if(n>1999999999){cout<<"Dev C++平台无法处理太大的数字!"<<endl;}else{int a=n/1000000000;int b=(n%1000000000)/1000000;int c=(n%1000000)/1000;int d=n%1000;if(a!=0){out(a);cout<<"billion ";}if(b!=0){out(b);cout<<"million ";}if(c!=0){out(c);cout<<"thousand";}if(d!=0){if(d<100&&(a!=0)||b!=0||c!=0){cout<<"and ";} out(d);}}cout<<endl;}void person::writing(){cout<<endl<<" "<<endl;cout<<" # # "<<endl;cout<<" # # "<<endl;cout<<" # # "<<endl;cout<<" # # "<<endl;cout<<" ######################## "<<endl;cout<<" # # # # "<<endl;cout<<" # # # # "<<endl;cout<<" # # # # "<<endl;cout<<" # # # # "<<endl;cout<<" ######################## "<<endl;cout<<" # # # # "<<endl;cout<<" # # # # "<<endl;cout<<" # # # # "<<endl;cout<<" # # # # "<<endl;cout<<" # # # # "<<endl;cout<<" ######################## "<<endl;}person::~person(){delete[]name;}class sing_star:public person{private:float salary;public:sing_star();sing_star(char *n,char s,char *p,int w,int h,float s1);void change_data(char *n,char s,char *p,int w,int h,float s1);void playing(char *ps);void print();};sing_star::sing_star():person(){salary=0.0;}sing_star::sing_star(char *n,char s,char *p,int w,int h,float s1){person::change_data(n,s,p,w,h);salary=s1;}void sing_star::change_data(char *n,char s,char *p,int w,int h,float s1){person::change_data(n,s,p,w,h);salary=s1;}void sing_star::print(){person::print();cout<<salary<<endl;}int main(){person Andre("Andre Christina",'M',"142703199612073333",140,175);Andre.print();system("pause");Andre.walking(20,4);system("pause");Andre.hearing("I Love This World!");system("pause");Andre.speaking(1207);system("pause");cout<<endl;Andre.writing();system("pause");sing_star Annie("Annie",'F',"142703199612073334",90,165,2020);Annie.print(); Annie.walking(10,4);Annie.hearing("You are Simple");Annie.speaking(12070);cout<<endl;Annie.writing();cout<<endl<<"Thanks for using!";return 0;}
- person类继承Date类和Time类
#include<iostream>#include<windows.h>#include<string.h>>using namespace std;class Date{protected: int day,month,year;public: Date(){int year=1900,month=1,day=1;} Date(int yy,int mm,int dd){init(yy,mm,dd);} void init(int,int,int); void print_ymd(); void print_mdy();};void Date::init(int yy,int mm,int dd){ month=(mm>=1&&mm<=12)?mm:1; year=(yy>=1900&&yy<=2100)?yy:1; day=(dd>=1&&dd<=31)?dd:1;}void Date::print_ymd(){cout<<year<<"-"<<month<<"-"<<day<<endl;}void Date::print_mdy(){cout<<month<<"-"<<day<<"-"<<year<<endl;}class Time{protected: int hour,minute,second;public: Time(){int hour=minute=second=0;} Time(int h,int m,int s){init (h,m,s);} void init(int,int,int); void print_time();};void Time::init(int h,int m,int s){ hour=(h>=0&&h<=24)?h:0; minute=(m>=0&&m<=60)?m:0; second=(s>=0&&s<=60)?s:0;}void Time::print_time(){cout<<hour<<":"<<minute<<":"<<second;}class person:public Date,public Time{private: char *name; char sex; char pid[19]; int weight; int high;public: person(); person(char *n,char s,char *p,int w,int h,int hr,int me,int sd); void change_data(char *n,char s,char *p,int w,int h,int hr,int me,int sd); void walking(int k,int v); void hearing(char *sentence); void speaking(int n); void out(int a); void print(); void writing(); ~person();};static char *num1[]={ " ","one ","two ","three ","four ","five ","six ","seven ", "eight ","nine ","ten ","eleven ","twelve ","thirteen ", "fourteen ","fifteen ","sixteen ","seventeen ","eighteen ", "nineteen "};static char *num10[]={ " "," ","twenty ","thirty ","forty ","fifty ","sixty ","seventy ", "eighty ","ninety "};void person::print(){ cout<<"Name:"<<name<<"\tSex:"<<sex<<endl; cout<<"ID Number:"<<pid<<endl; cout<<"High"<<high<<"\tWeight:"<<weight<<endl; cout<<"Birthday:"; print_ymd(); cout<<"Birthdate:"; print_time();}person::person(){ name=new char[strlen("XXXXXX"+1)]; strcpy(name,"XXXXXX"); strcpy(pid,"XXXXXXXXXXXXXXXXXX"); sex='X'; weight=high=0; year=1900; month=day=1; hour=minute=second=0;}person::person(char *n,char s,char *p,int w,int h,int hr,int me,int sd){ change_data(n,s,p,w,h,hr,me,sd);}void person::change_data(char *n,char s,char *p,int w,int h,int hr,int me,int sd){ name=new char[strlen(n)]; strcpy(name,n); strcpy(pid,p); sex=s; weight=w; high=h; char temp[5]; strncpy(temp,p+6,4); year=atoi(temp); strncpy(temp,p+10,2); temp[2]='\0'; month=atoi(temp); strncpy(temp,p+12,2); temp[2]='\0'; day=atoi(temp); hour=hr; minute=me; second=sd;}void person::out(int a){ int b=a%100; if(a/100!=0){ cout<<num1[a/100]<<"hundred "; if(b!=0){ cout<<"and "; } } if(b<20){ cout<<num1[b]; } else{ cout<<num10[b/10]; if(b%10!=0){ cout<<"\b-"<<num1[b%10]; } }}void person::walking(int k,int v){ cout<<"\n"<<name<<"水平直线行走"<<k<<"步"<<endl; for(int i=0;i<k;i++){ cout<<' '<<"o_o"; Sleep(1000/v); cout<<"\b\b\b"; }}void person::hearing(char *sentence){ cout<<endl<<sentence<<endl; char *p=new char[strlen(sentence)+1]; strcpy(p,sentence); char *pp=p; while(*p){ if(*p>'a'&&*p<='z'){ *p='A'+(*p-'a'+0); } else if(*p>='A'&&*p<='Z'){ *p='a'+(*p-'A'); } p++; } cout<<pp<<endl; delete []pp;}void person::speaking(int n){ if(n>1999999999){ cout<<"Dev C++平台无法处理太大的数字!"<<endl; } else{ int a=n/1000000000; int b=(n%1000000000)/1000000; int c=(n%1000000)/1000; int d=n%1000; if(a!=0){ out(a); cout<<"billion "; } if(b!=0){ out(b); cout<<"million "; } if(c!=0){ out(c); cout<<"thousand"; } if(d!=0){ if(d<100&&(a!=0)||b!=0||c!=0){ cout<<"and "; } out(d); } } cout<<endl;}void person::writing(){ cout<<endl<<" "<<endl; cout<<" # # "<<endl; cout<<" # # "<<endl; cout<<" # # "<<endl; cout<<" # # "<<endl; cout<<" ######################## "<<endl; cout<<" # # # # "<<endl; cout<<" # # # # "<<endl; cout<<" # # # # "<<endl; cout<<" # # # # "<<endl; cout<<" ######################## "<<endl; cout<<" # # # # "<<endl; cout<<" # # # # "<<endl; cout<<" # # # # "<<endl; cout<<" # # # # "<<endl; cout<<" # # # # "<<endl; cout<<" ######################## "<<endl;}person::~person(){ delete[]name;}int main(){ person Andre("Andre Christ",'M',"142703199612073333",140,175,9,45,7); Andre.print(); system("pause"); cout<<endl; Andre.walking(10,4); system("pause"); cout<<endl; Andre.hearing("I love this world so much"); system("pause"); cout<<endl; Andre.speaking(1207); system("pause"); cout<<endl; Andre.writing(); system("pause"); cout<<endl<<"Thanks for using!"; return 0;}
- 智能手机类定义
#include<iostream>#include<cstring>using namespace std;class mobile{private: char mynumber[12]; char m_type[40]; float price;public: mobile(){init("00000000000","None_type", 0);} void init(char *number,char *pt,float pri); void dial(); void answer(char othernumber[]); void hangup(); void show();};void mobile::init(char *number,char *pt,float pri){ strcpy(mynumber,number); strcpy(m_type,pt); price=pri;}void mobile::dial(){ cout<<"Dialing number is "<<mynumber<<endl; cout<<"Dialing on..."<<endl;}void mobile::answer(char othernumber[]){ cout<<"Answering number is "<<othernumber<<endl; cout<<"Answering is..."<<endl;}void mobile::hangup(){ cout<<"Hanging up..."<<endl;}void mobile::show(){cout<<mynumber<<'\t'<<m_type<<'\t'<<price<<endl;}class smartphone:public mobile{private: char OS[20]; int memory;public: smartphone() { init("00000000000","None_type",0,"None_OS",0); } void init(char *number,char *pt,float pri,char *os,int mem); void send(char othernumber[],char message[]); void showmemory(); void show();};void smartphone::init(char *number,char *pt,float pri,char *os,int mem){ mobile::init(number,pt,pri); strcpy(OS,os); memory=mem;}void smartphone::send(char othernumber[],char message[]){ cout<<"Sending message is "<<message<<" to "<<othernumber<<endl; cout<<"Sending on..."<<endl;}void smartphone::show(){ mobile::show(); cout<<OS<<'\t'<<memory<<endl;}void smartphone::showmemory(){cout<<"memory is "<<memory<<" GB"<<endl;}int main(){ smartphone newphone; newphone.init("18052848860","Smartisan M1",2499,"Smarisan OS 3.1.5",32); newphone.send("15711583306","Hello darling,i miss u so much"); cout<<endl; cout<<"The information about Smartisan M1 is below:"<<endl; newphone.showmemory(); newphone.show(); return 0;}
1 0
- C++课程学习代码汇总基础
- 黑马IOS基础课程的学习笔记 C语言基础
- C++基础 课程代码
- Netty零基础学习实战课程 视频+笔记+代码
- 学习Java基础课程
- C语言课程部分代码
- 3g零基础学习课程
- jQuery基础课程学习笔记
- C++基础学习课程 前言
- 北理工机器学习课程Project题目汇总
- Andrew Ng机器学习课程笔记--汇总
- C语言学习基础代码记录
- 课程汇总
- 【stanford C++】C++课程学习
- JAVA 代码基础规范汇总
- 理学院C语言基础班课程一
- C语言及程序设计基础 课程主页
- C学习地址汇总
- POJ-1260-Pearls
- 彩虹秒赞7.8源码破解版(去域名授权) 彩虹云任务系统无任何限制
- c++利用winapi实现简单多线程
- RecyclerView中点击事件的添加
- 图形绘制
- C++课程学习代码汇总基础
- Go语言GUI Demo 之 Walk
- redis笔记
- 关于ueditor的配置、图片上传、保存数据库、显示等
- 傅里叶
- Hadoop初学小结
- js获取网页高度(详细整理)
- 【noip2009提高组】 最优贸易 邻接表+SPFA
- 【BJOI】【欧拉函数】Euler