写在开头的话:
今天难得一天没课。打算把之前学习过的课程再重新看一遍。写了好多代码,编译运行之后都删掉写下一个了。
久而久之不免觉得浪费,现在把它们都贴在下面把。就跟看着自己的孩子一样。说不定以后有利用的价值,没事儿还可以翻出来看看。
(不定时补充)
发现好像写了蛮多的,在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);