第二次实验
#include<iostream> using namespace std; struct complex{ double real; double imaginary; }; int myadd(int,int); double myadd(double,double); complex myadd(complex,complex); int main() { int a=1,b=2; double c=1.1,d=5.5; complex e={1.1,2},f={3.3,4.4},j; cout<<myadd(a,b)<<endl; cout<<myadd(c,d)<<endl; j=myadd(e,f); cout<<j.real<<"+"<<j.imaginary<<"i"<<endl; return 0; } int myadd(int x,int y) { return x+y; } double myadd(double x,double y) { return x+y; } complex myadd(complex x,complex y) { complex i; i.real=x.real+y.real; i.imaginary=x.imaginary+y.imaginary; return i;
}
重载函数比较简单,理解也不是太困难,故写的也比较简单。
#include<iostream> using namespace std; void fastpx(int a[],int high,int low); int main() { int s[10]={55,22,14,36,98,54,40,98,82,2},i; fastpx(s,9,0); for(i=0;i<10;i++) { cout<<s[i]<<" "; } cout<<endl; return 0; } void fastpx(int a[],int high,int low) { if(high-low<=0) return ; int b=a[low],i=high,j=low,k=i,c=low; while(i!=j) { if(k==i) { if(b<a[i]) { i--; k=i; } else { k=j; a[c]=a[i]; c=i; a[c]=b; i--; } } else if(k==j) { if(b>a[j]) { j++; k=j; } else { k=i; a[c]=a[j]; c=j; a[c]=b; j++; } } } if(a[j-1]==b) { if(a[j-1]>a[j]) { k=a[j]; a[j]=a[j-1]; a[j-1]=k; c=j; } } if(a[i+1]==b) { if(a[i+1]<a[i]) { k=a[i]; a[i]=a[i+1]; a[i+1]=k; c=i; } } fastpx(a,c-1,low); fastpx(a,high,c+1); }
快速排序理解还可以,但是不能和老师讲的同步,只能写成这样,然后后期经常出错,这是调试好多次后的结果。
#include<iostream> #include<string> using namespace std; class user{ public: void setInfo(string xingming,string mima="111111",string youjian=" "); void printInfo(); void changePasswd(); private: string name; string passwd; string email; }; void user::setInfo(string xingming,string mima,string youjian) { name=xingming; passwd=mima; email=youjian; } void user::printInfo() { cout<<"用户名:"<<name<<endl; cout<<"密码:"<<"******"<<endl; cout<<"邮箱:"<<email<<endl; } void user::changePasswd() { cout<<"请输入密码:"<<endl; string m; cin>>m; if(m==passwd) { cout<<"请输入新密码"<<endl; cin>>passwd; } else { cout<<"密码不正确,请重新输入:"<<endl; cin>>m; if(m==passwd) { cout<<"请输入新密码"<<endl; cin>>passwd; } else { cout<<"密码不正确,请重新输入:"<<endl; cin>>m; if(m==passwd) { cout<<"请输入新密码"<<endl; cin>>passwd; } else { cout<<"密码不正确,请稍后再输入:"<<endl; } } } } int main() { user user1; user1.setInfo("miaoxinyu","13852039350laoshi","17996548@qq.com"); user1.printInfo(); user1.changePasswd(); return 0; }
对类的理解不太深,这是和同学讨论后的结果,以后要多加复习。