1. 重载函数
- #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(){
- int a,b,s1;
- double c,d,s2;
- Complex e,f,s3;
- cin>>a>>b>>c>>d>>e.real>>e.imaginary>>f.real>>f.imaginary;
- s1=add(a,b);
- s2=add(c,d);
- s3=add(e,f);
- cout<<s1<<endl;
- cout<<s2<<endl;
- cout<<s3.real<<"+"<<s3.imaginary<<"i"<<endl;
- return 0;
- }
- int add(int a,int b)
- {
- return a+b;
- }
- double add(double c,double d)
- {
- return c+d;
- }
- Complex add(Complex e,Complex f)
- {
- Complex s3;
- s3.imaginary=e.imaginary+f.imaginary;
- s3.real=e.real+f.real;
- return s3;
- }
2. 快速排序
- #include<iostream>
- using namespace std;
- template <class T>
- void output(T a[],int n)
- {int i;
- for (i=0;i<n;i++)
- cout<<a[i]<<" ";}
- template <class T>
- void QuickSort (T a[],int left,int right)
- {int z,y,m=0;
- T o,t;
- z=left;y=right-1;o=a[(left+right)/2];
- if(z>=y)
- m=y;
- else
- {while(z<y)
- {while (o<a[y])
- y--;
- while(o>a[z])
- z++;
- t=a[z];a[z]=a[y];a[y]=t;
- }
- }
- QuickSort(a,left,m);
- QuickSort(a,m+1,right);
- }
- template <class T>
- void QuickSort (T ,int,int);
- int main()
- { int a[6]={5,2,95,30,78,100};
- double b[6]={3.6,4.2,1.9,2.8,96.5,7.2};
- QuickSort(a,0,6);
- output(a,6);
- QuickSort(b,0,6);
- output(b,6);
- return 0;
- }
3.USER 类
- #include<iostream>
- #include<string>
- using namespace std;
- class User{
- public:
- User(string yourname,string yourpasswd,string youremail);
- User(){
- name=" ";
- passwd="111111";
- email=" ";}
- void setInfo(string yourname="",string yourpasswd="111111",string youremail=" ");
- void changePasswd();
- void printInfo();
- private:
- string name;
- string passwd;
- string email;
- };
- void User::setInfo(string yourname,string yourpasswd,string youremail)
- { name=yourname;passwd=yourpasswd;email=youremail;}
- void User::changePasswd(){
- string a,b;
- int i;
- cout<<"请输入密码:";
- cin>>a;
- if(a!=passwd)
- {for(i=0;i<2;i++)
- {cout<<"请再次输入密码:";
- cin>>a;
- if(a==passwd)
- break;}
- cout<<"请稍后再试"<<endl;}
- if(a==passwd)
- {cin>>a;cout<<"请再输入一次:";cin>>b;
- if(a==b)
- passwd=a;
- else
- cout<<"密码错误";
- }}
- void User::printInfo(){
- cout<<"Name:"<<name<<endl;
- cout<<"Passwd:"<<"******"<<endl;
- cout<<"Email"<<email<<endl;}
- int main()
- {cout<<"testing 1......"<<endl;
- User user1;
- user1.setInfo("Leonard");
- user1.printInfo();
- user1.changePasswd();
- user1.printInfo();
- cout<<endl<<"testing 2......"<<endl<<endl;
- User user2;
- user2.setInfo("Jonny","92197","xyz@hotmail.com");
- user2.printInfo();
- return 0;}
在做快速排序法的时候, 刚开始用了项目做, 三段但是不知道怎么合成...... 就用了一段, 然后格式没错运行出来就是这样, 改了好多次还是不知道问题出在哪儿, 求解.
实验二
来源: http://www.bubuko.com/infodetail-3001116.html