任务三:

complex.h:

#include<math.h>
#include<iostream>
class complex
{
private:double r,i;
public:complex(){};
       complex(double x,double y=0.0):r(x),i(y){};
       complex(complex const &p){
           i=p.i;
           r=p.r;
       }
       double get_real()const{return r;};
       double get_imag()const{return i;};
       void show()const{
           if(i>=0)
           std::cout<<r<<"+"<<i<<"i"<<std::endl;
           else
               std::cout<<r<<i<<"i"<<std::endl;
       }
       void add(complex const &c1){r+=c1.r;i+=c1.i;};
       friend complex add(complex const &c1,complex const &c2)
       {
           complex c3;
           c3.r=c1.r+c2.r;
           c3.i=c1.i+c2.i;
           return c3;
       }
       friend bool is_equal(complex const &c1,complex const &c2)
       {
           if(c1.r==c2.r&&c1.i==c2.i)
               return true;
           else
               return false;

       }
       friend double abs(complex const &c1)
       {
           double m,n;
           m=c1.i*c1.i;
           n=c1.r*c1.r;
           return sqrt(m+n);
       }
};

task3.cpp:

#include "complex.h"
#include <iostream>
int main()
{
    using namespace std;

    complex c1(2, -6);
    const complex c2(7.5);
    complex c3(c1);

    cout << "c1 = ";
    c1.show();
    cout << endl;

    cout << "c2 = ";
    c2.show();
    cout << endl;
    cout << "c2.imag = " << c2.get_imag() << endl;

    cout << "c3 = ";
    c3.show();
    cout << endl;

    cout << "abs(c1) = ";
    cout << abs(c1) << endl;

    cout << boolalpha;
    cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
    cout << "c1 == c2 : " << is_equal(c1, c2) << endl;

    complex c4;
    c4 = add(c1, c2);
    cout << "c4 = c1 + c2 = ";
    c4.show();
    cout << endl;

    c1.add(c2);
    cout << "c1 += c2, " << "c1 = ";
    c1.show();
    cout << endl;
}

运行结果:

面向对象实验一:类与对象_main函数

 

任务四:

user.hpp:

#include<iostream>
#include<string>
using namespace std;
class user
{
private:string name,passwd,email;
        static int users;
public:user(string a,string b="111111",string c=""){
           if(c!="")
               fixe(c);
           email=c;
           for(;a.length()>16;)
           {
               cout<<"your name is illegal"<<endl;
               cout<<"Please set again ..."<<endl;
               cin>>a;
           }
           name=a;
           cout<<"your name is set successfully!"<<endl;
           fixp(b);
           passwd=b;
           cout<<"your passwd is set successfully!"<<endl;
           users++;
       };
       ~user(){users--;};
       void fixe(string c){
           string s1="@",s2=".com";
           string::size_type idx,idy;
           idx=c.find(s1);
           idy=c.find(s2);
           for(;idx==string::npos||idy==string::npos;)
           {
               cout<<"your email is nonstandard ..."<<endl;
               cout<<"Please set your email again."<<endl;
               cout<<"Enter email address:";
               cin>>c;
idx=c.find(s1);
idy=c.find(s2); } cout
<<"email is set successfully"<<endl; } void fixp(string b) { for(;b.length()>16;) { cout<<"your passwd is illegal"<<endl; cout<<"Please set again ..."<<endl; cin>>b; } } void set_email(){ cout<<"Enter email address:"; string c; cin>>c; fixe(c); email=c; }; void change_passwd(){ int i; string s; cout<<"Enter old passwd:"; cin>>s; for(i=1;i<=2;i++) { if(s!=passwd) { cout<<"passwd input error."<<endl; cout<<"Plesae re-enter again:"; cin>>s; } if(passwd==s)
break; } if(i==3) cout<<"Please try after a while."<<endl; else { cout<<"Enter new passwd:"; cin>>s; fixp(s); passwd=s; cout<<"new passwd is set successfully..."<<endl; } }; void print_info(){ cout<<"name:\t"<<name<<endl; cout<<"passwd:******"<<endl; cout<<"email:\t"<<email<<endl; }; static void print_n(){cout<<"there are "<<users<<" users."<<endl;}; }; int user::users=0;

task4.cpp:

#include "user.hpp"
#include <iostream>

int main()
{
    using namespace std;

    cout << "testing 1......" << endl;
    user user1("Jonny", "92197", "xyz@hotmail.com");
    user1.print_info();

    cout << endl
         << "testing 2......" << endl
         << endl;
    user user2("Leonard");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();

    user::print_n();
}

运行结果:

示例1、

面向对象实验一:类与对象_main函数_02

 示例2、

 面向对象实验一:类与对象_main函数_03

 task3小结:

惨痛的教训,对于class的定义时结尾处花括号(})后一定有结束符(;),否则会导致编译器认为main函数也是定义的对象,这点因为之前主要打if,for语句,对于“;”很少在意。

然后就是const的限定,由于权限只能减小,导致主函数中有一个对象带有const,你的程序就要进行一系列的限定,这点虽然是为了保护数据,但const的限定会导致一些数据功能无法实现

 

task4小结:

这个应该是目前做的最有实际意义的小程序了,除了老师要求的必要条件外,还增加了对名字,密码,邮箱部分合法性(长度、邮箱是否含有(@、.com))的检测

之所以是部分,主要是能力不够,有idea做不出,比如对名字敏感词的屏蔽,这个如果一个个输入是不现实的,应该要有一个数据库进行检测,

之后是密码复杂度的检索,这个要求程序员对string要有熟练的运用,很遗憾,我就是不熟练的那个。

还有对于重名用户的处理,不知道对于大量数据该如何解决等等,只能说程序员的路还很长。

 

然后其他的感悟就是对这个网站的使用吧,感觉很古朴,而且有一些不好用,比如不能直接粘贴图片,代码框不能删除,只能自己手动修改之类的(也许可以快进到自己编写网站?)

 

反馈(?):希望老师能多出关于软件功能的实验,一学期之后拼拼凑凑说不定软件工程的软件都做出来了。