zoukankan      html  css  js  c++  java
  • C++实验二

    实验内容:

    1.编写重载函数add(),实现对int型,double型,Complex型数据的加法。在main()函数中定义不同类型 数据,调用测试

    #include<iostream>
    using namespace std;
    
    int add(int x,int y){
        return x+y;
    }
    
    double add(double x,double y){
        return x+y;
    } 
    
    struct Complex{
        double real;
        double imaginary;
    };
    
    void add(Complex c1,Complex c2){
        Complex c;
        c.real=c1.real+c2.real;
        c.imaginary=c1.imaginary+c2.imaginary;
        if(c.imaginary<0)
        cout<<c.real<<"-"<<c.imaginary<<"i"<<endl;
        else 
        cout<<c.real<<"+"<<c.imaginary<<"i"<<endl;
        cout<<endl;
    }
    
    int main(){
        int x,y;
        double a,b;
        double r1,i1,r2,i2;
        
        cout<<"Enter two integers to add: "<<endl;//瞎j8写的英文,心意到了    
        cin>>x>>y;
        cout<<add(x,y)<<endl;
        cout<<"Enter two decimal numbers to add: "<<endl;
        cin>>a>>b;
        cout<<add(a,b)<<endl;;
        cout<<"Enter two complexs to add: "<<endl;
        cin>>r1>>i1>>r2>>i2;
        add({r1,i1},{r2,i2});//注意结构体的表示,还有cout不要写重了 
        
        return 0;
    }
    View Code

     2.编写实现快速排序函数模板,并在main()函数中,定义不同类型数据,调用测试。

    关于快速排序算法,这篇博客写得很棒棒:https://blog.csdn.net/code_ac/article/details/74158681

    #include<iostream>
    using namespace std;
    #define N 100
    
    /*
    void quicksort(int s[],int l,int r){
        if(l<r){
            int i=1,j=r,x=s[l];
            while(i<j){
            while((i<j)&&s[j]>=x)
            j--;
            if(i<j)
            s[i++]=s[j];
            while((i<j)&&s[i]<x)
            i++;
            if(i<j)
            s[j--]=s[i];
        }
        s[i]=x;
        quicksort(s,l,i-1);
        quicksort(s,i+1,r);
    }
    }
    */
    
    template <typename T> 
    T FindPos(T a[],T low,T high){
        T t=a[low];
        while(low<high){
            while(low<high && a[high]>=t)
                high--;
            a[low]=a[high];
            while(low<high && a[low]<=t)
                low++;
            a[high]=a[low];
        }
        a[low]=t;
        return low;
    }//将第一个数放到数组的中间,使得它的前面均是比它小的数,后面的数均比它大 
    
    template <typename T>
    void QuickSort(T a[],T low,T high) {
        if(low>high)
            return ;
        T pos=FindPos(a, low, high);
        QuickSort(a, low, pos-1);//在前半段进行快速排序 
        QuickSort(a, pos+1, high);//在后半段进行快速排序 
    }
    
    int main(){
        int a[N];
        int high;
        cout<<"Enter the total number u want to sort: "<<endl;
        while(cin>>high){
            cout<<"Enter a set of numbers that u want to sort: "<<endl;
            for(int i=1;i<=high;i++)
            {cin>>a[i];}
            QuickSort(a,1,high);
            cout<<"After sorting is: "<<endl;
            for(int i=1;i<=high;i++)
            cout<<a[i]<<' '; 
            cout<<endl;
        }
        return 0;
    }
    View Code

    3.设计并实现一个用户类User,并在主函数中使用和测试这个类。

    #include<iostream>
    #include<string>
    #include<conio.h>
    using namespace std;
    
    class User{
        public:
            void setinfo(string iname,string ipassword="111111",string imail=" ");
            void printinfo();
            void changepassed();
        private:
            string name;
            string password;
            string mail;     
    };
    
    int main(){
        cout<<"testing 1……"<<endl;
        User user1;
        user1.setinfo("Lenoard");
        user1.printinfo(); 
        user1.changepassed();
        
        cout<<"testing 2……"<<endl;
        User user2;
        user2.setinfo("jonny","92197","xyz@hotmail.com");
        user2.printinfo();
        
        return 0;
    }
    
    void User::setinfo(string iname,string ipassword,string imail){
        name=iname;
        password=ipassword;
        mail=imail;
    }
    /*
    void User::hiding(char ipassword[6]){
        string a;
        while(password=getch()!=13){///13是回车符的ASCII码
            a+=password[6];
            cout<<"*";
        }
    }//网上搜的,用来把密码隐藏,但因为c学得不好,所以暂时还不会用 
    */
    void User::printinfo(){
        cout<<"Name: "<<name<<endl;
        cout<<"Password: "<<"******"<<endl;
        cout<<"Mail: "<<mail<<endl;
    }
    
    void User::changepassed(){
        string password0,password1;
        cout<<"Enter the original password: "<<endl;    
        int count=0;
    do{
        cin>>password0;
        count++;
    if((password0==password)&&(count<4)){
        cout<<"Now set the new password: "<<endl;
        cin>>password1;
        password=password1;
        cout<<"Change completed"<<endl; }
    else if((password0!=password)&&(count<4))
        cout<<"Try again..."<<endl;
    else
        cout<<"U will quit temporarily,please try again later..."<<endl;
    }while(count<4);
    }
    View Code

    实验总结:

    1.这次实验我做得不好,理解得也不是很透彻。像User类那题,只是一个很简陋的框框,可以说什么功能都不能实现,changepasswd那个函数也是唬人用的,改完密码后什么也其实不能干,老师建议的那些选做我也是一个都不会,看到大佬们的博客,就 觉得哇!好详细!好全面!再捯饬捯饬就是个课设了啊!(我有...失败啊)。

    我觉得有以下几个方面的原因:1)C的知识学得确实不好,浮光掠影地,想查漏补缺的,发现好么大一个洞???!!有种补都补不了的感觉。2)之前的习惯不好,不独立思考,主动性也不够,依赖性过强,题目不会一般就百度或者抱大佬舍友的大腿了。4)缺乏自信,不相信自己能看懂,不相信自己看懂之后能做出来,不相信自己做出来是没bug的。就...一点点补上吧,也没说不聪明到不可救药的地步(膨胀)。

    2.在进行编程之前,应该对整体有一个大概的设计,不然看起来会很乱。

  • 相关阅读:
    小工具之文件整合
    [JavaWeb基础] 031.dom4j写入xml的方法
    AES128_CBC模式加密
    eatwhatApp开发实战(九)
    [Objective-C] 021 KVC、KVO
    eatwhatApp开发实战(八)
    eatwhatApp开发实战(七)
    [Objective-C] 020_ Block
    eatwhatApp开发实战(六)
    年终总结--我的2019
  • 原文地址:https://www.cnblogs.com/kori/p/10562179.html
Copyright © 2011-2022 走看看