C++课程学习代码汇总基础

写在开头的话:

         今天难得一天没课。打算把之前学习过的课程再重新看一遍。写了好多代码,编译运行之后都删掉写下一个了。

         久而久之不免觉得浪费,现在把它们都贴在下面把。就跟看着自己的孩子一样。说不定以后有利用的价值,没事儿还可以翻出来看看。

(不定时补充



发现好像写了蛮多的,在1楼做一个目录吧。方便以后的查阅。

  1. 文字信息统计
  2. 冒泡排序
  3. 简易计算器
  4. 使用string字符串替换拼接字符中的单词
  5. 三位逆序数
  6. 使用string取子字符串
  7. 词频统计
  8. 求三个数中的最大数
  9. Hanoi汉诺塔问题
  10. 利用指针实现三个数的排序
  11. 利用字符指针删除字符首部的空格
  12. 钟表类的完整定义
  13. 日期类的完整定义
  14. 分数运算函数的完整定义
  15. 可鸣笛、运动和播放MP3的汽车类定义
  16. 设计平面上的几何图形:圆
  17. 学生信息类1.0
  18. 学生信息类2.0
  19. 日期类的定义(构造函数版)
  20. 日期类的定义(重载构造函数版)
  21. 日期类的定义(析构函数版)
  22. 能翻译整数的机器人1.0
  23. 能翻译整数的机器人2.0
  24. Person类的定义
  25. this指针使用的例子
  26. 日期类输出定义
  27. person类(singer)综合定义
  28. person类继承Date类和Time类
  29. 智能手机类定义

下面是代码的集合:

  1. 文字信息统计
    #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;
    }

  2. 冒泡排序
    #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;
    }

  3. 简易计算器
    #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;
    }

  4. 使用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;
    }
    

  5. 三位逆序数
    #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;
    }

  6. 使用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;
    }

  7. 词频统计
    #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;
    }

  8. 求三个数中的最大数
    #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; 
    }

  9. 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;
    }

  10. 利用指针实现三个数的排序
    #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;
    }

  11. 利用字符指针删除字符首部的空格
    #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;
    }

  12. 钟表类的完整定义
    #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);  
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值