实验二——函数重载,快速排序,类对象
函數重載:
#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 a1=2,b1=3;
?????????????????? double a2=2.0,b2=3.0;
?????????????????? struct complex num1={2,3},num2={3,4};
?????????????????? cout<<add(a1,b1)<<endl;
?????????????????? cout<<add(a2,b2)<<endl;
?????????????????? complex n=add(num1,num2);
?????????????????? cout<< n.real<<"+"<<n.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){
?????????????????? struct complex m;
?????????????????? m.real=a.real+b.real;
?????????????????? m.imaginary=a.imaginary+b.imaginary;
?????????????????? return m;
???????? }
?
快速排序:
#include<iostream>
using namespace std;
void quicksort(int a[], int low, int high)
{
??? if(low>=high){return;}
??? int num1=low;
??? int num2=high;
??? int sample=a[num1];
??? while(num1<num2)
??? {
??????? while(num1<num2&&a[num2]>=sample)
??????????? --num2;
??????? a[num1]=a[num2];
??????? while(num1<num2&&a[num1]<=sample)
??????????? ++num1;
??????? a[num2]=a[num1];???
??????? a[num1]=sample;
??????? quicksort(a,low,num1-1);
??????? quicksort(a,num1+1,high);
?????? }
}
?int main()
?{
???? int i;
???? int a[]={4,20,15,14,19,7,8};
?????? ?cout<<"before:";
???? for(i=0;i<7;i++)
????????????? ?cout<<a[i]<<" ";
?????? ?cout<<endl;
???? quicksort(a,0,6);
?????? ?cout<<"after:";
???? for(i=0;i<7;i++)
???????? cout<<a[i]<<" ";
?????? ?cout<<endl;
???? return 0;
?}
簡單類對象:
#include<iostream>
#include<string>
using namespace std;
class user
{
public:
?????? void setinfo(string name1,string password1="111111",string email1=" ");
?????? void changepassword();
?????? void printinfo();
private:
?????? string name;
?????? string password;
?????? string email;
};
void user::setinfo(string name1,string password1,string email1)
{
?????? name=name1;
?????? password=password1;
?????? email=email1;
}
void user::changepassword()
{
?????? int i=0;
?????? string mima;
?????? while(1)
?????? {
?????? ? cout<<"enter the old password:";
?????? ? cin>>mima;
????? if(mima==password)
?????? ? {
????????????? cout<<"enter the new password:";
????????????? cin>>password;
????????????? break;
?????? ? }
?????? ? if(mima!=password)
?????? ? {
????????????? i++;
????????????? cout<<"the password is wrong,please resume load!"<<endl;
????????????? if(i==3)
????????????? {
???????????????????? cout<<"please try after a short while!";
???????????????????? break;
????????????? }
?????? ? }
?????? }
}
void user::printinfo()
{
?? cout<<"name:"<<name<<endl;
?? cout<<"password:"<<password<<endl;
?? cout<<"email:"<<email<<endl;
}
int main()
{
?????? cout<<"please enter the first information"<<endl;
?????? user user1;
?????? user1.setinfo("Leonard");
?????? user1.changepassword();
?????? user1.printinfo();
?????? cout<<"please enter the second information"<<endl;
?????? user user2;
?????? user2.setinfo("yfwg","173779","yfwg@bkymail.com");
?????? user2.printinfo();
?????? return 0;
}
實驗總結:
1、對于快速排序的算法依舊很生疏,這里的代碼借助了百度百科。
2、對于類對象的代碼依舊沒有形成充足的認識,這次寫類對象的代碼參考了很多,自己的能力實在不足。要加強這方面練習。
3、對于結構體這一塊依舊很不熟練,在c語言的時候就沒有學的很扎實,以后還要多練習
轉載于:https://www.cnblogs.com/yfwg/p/10594280.html
總結
以上是生活随笔為你收集整理的实验二——函数重载,快速排序,类对象的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对xml操作的主要方法[轉]
- 下一篇: Poetry