zoukankan      html  css  js  c++  java
  • c++第二次实验作业

    (1)user类的用法

    #include<iostream> 
    #include<string>
    #include <conio.h> 
    #include <algorithm>
    using namespace std;
    
    class User{
        public:
            User(string name,string email="",string passwd="111111" ):name(name),email(email),passwd(passwd){};
            void emailcin();                            //输入邮箱 
            void changePasswd();                       
            void printInfo();
            int checkemail();                           //检查邮箱是否含有@ 
        private:
            string name;
            string passwd;
            string email;
    };                                                  //user类的定义 
    
    
    int main() {
        
        cout << "testing 1......" << endl;
        User user1("Leonard","178392qq.com");
        user1.printInfo();
        do{
        int defact;        
        defact=user1.checkemail();                  
        if(defact)
            {
            cout<<"please enter your email again:";
            user1.emailcin();
            }
        else 
            break;
        }while(1);                                    //判断是否含@ 
           user1.printInfo();
        user1.changePasswd();
        cout<<endl;
        user1.printInfo();
        cout<<endl;
        
        cout << endl << "testing 2......" << endl ;
        User user2("Jonny","92197","xyz@hotmail.com");
        user2.printInfo();
        return 0;
    }
    
    
    void User::changePasswd(){
        string past;
        char ch; 
        int i=1,k;
        cout<<"please enter the past passwd:";
        while(i++<=3){
            for(k=1;k<=6;k++){
            ch=getch();
            cout<<"*";
            past=past+ch;
        }
            if(past==passwd)
            break;
            else
            cout<<endl<<"please enter the past passwd:";
        }
        if(i<=3){
            cout<<endl<<"now please enter the new passwd:";
            char a[7];
            passwd='';
            for(int k=1;k<=6;k++){
                a[k]=getch();
                passwd=passwd+a[k];
                cout<<"*";
                }
        }    
        else
            cout<<"please do it later"<<endl;
    }
    
    
        void User::printInfo(){
        cout<<"user name:"<<name<<endl;
        cout<<"passwd:"<<"******"<<endl;
        cout<<"email:"<<email<<endl;
    }
    
        int User::checkemail(){
            string c="@";
        string::size_type chec;
        chec=email.find(c);
        if(chec==string::npos)    
        return 1;
        return 0;
    }
    
    void User::emailcin(){
        cin>>email;
    } 

    (2)快速排序

    #include <iostream>
    #include"Quick.h"
    #include"output.h"
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    
    int main(int argc, char** argv) {
        int a[8]={77,77,88,88,668,88,0,77};
        QuickSort(a,8);
        output(a,8);
        return 0;
    } 
    #define OUTPUT_H
    
    template<class T>
    void output(T a[],int n){
        for(int i=0;i<n;i++)
            std::cout<<a[i]<<" ";
    } 
    #define Quick
    
    template<class T>
    void swap(T&a,T&b);
    
    template<class T>
    void Qsort(T a[],int,int);
    
    template<class T>
    void QuickSort(T a[],int n){
        Qsort(a,0,n-1);
    }
    
    template<class T>
    void Qsort(T a[],int low,int high){
        T temp=a[low];
        int x1=low,x2=high;
        while(low!=high){
            while(temp<=a[high]&&low<high)
            high--;
            if(high==low)
            break;
            a[low]=a[high];
            while(temp>=a[low]&&low<high)
            low++;
            if(high==low)
            break;
            a[high]=a[low];
        }
        a[low]=temp;
        if(low-x1>=3)
        Qsort(a,x1,low-1);
        else 
        if(a[x1+1]>a[low+1])
        swap(a[x1],a[low-1]);
        
        if(x2-low>=3)
        Qsort(a,low+1,x2);
        else 
        if(a[low+1]>a[x2])
        swap(a[low+1],a[x2]);
    }
    
    template<class T>
    void swap(T&a,T&b){
        int temp=a;
        a=b;
        b=temp;
    }

     
     
     
     
    (3)重载函数实现相加
    #include<iostream>
    using namespace std;
    
    struct Complex{
        double real;
        double imaginary;
    };
    
    int add(int,int);
    double add(double,double);
    Complex add(Complex,Complex);
    
    int main(){
        Complex a={2.2,3.3};
        Complex b={4.4,7.8};
        Complex c=add(a,b);
        
        cout<<add(3,4)<<endl;
        cout<<add(5.5,6.6)<<endl;
        cout<<c.real<<"+"<<c.imaginary<<"i"<<endl;
        
        return 0;
    }
    
    int add(int a,int b){
        return a+b;
    }
    
    double add(double a,double b){
        return a+b;
    }
    
    Complex add(Complex a,Complex b){
        Complex c;
        c.real=a.real+b.real;
        c.imaginary=a.imaginary+b.imaginary;
        return c;
    }

    emm...在评论别人的时候,发现我快速排序写的不太好。

    总结:在这次实验完成过程中,通过不断修改,查找函数用法,掌握了很多。我觉得这次最大的收获就是学会了创建项目,因为以前一直困惑怎么将一个像sale那种已经编好的代码放在同一个文件夹下。

    https://www.cnblogs.com/sora5934/

    https://www.cnblogs.com/fifi1224/

    https://www.cnblogs.com/xiaobailong123/

  • 相关阅读:
    在纪念中国人民抗日战争暨世界反法西斯战争胜利70周年大会上的讲话
    ConcurrentHashMap 的实现原理
    聊聊并发(四)——深入分析ConcurrentHashMap
    Mybatis 动态 SQL
    Mybatis Mapper XML 文件
    MySQL的语句执行顺序
    Java 集合细节(二):asList 的缺陷
    java中 列表,集合,数组之间的转换
    将java中数组转换为ArrayList的方法实例(包括ArrayList转数组)
    把Java数组转换为List时的注意事项
  • 原文地址:https://www.cnblogs.com/sqcmxg/p/10574927.html
Copyright © 2011-2022 走看看