码迷,mamicode.com
首页 > 其他好文 > 详细

实验二

时间:2019-03-25 23:14:56      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:new   --   快速排序   std   基础   cout   ...   pass   include   

#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,c;
 cin>>a>>b;
 c= add(a,b);
 cout<<c<<endl;
 double n,m,h;
 cin>>n>>m;
 h= add(n,m);
 cout<<h<<endl;
 Complex i,j,k;
 cin>>i.real>>i.imaginary>>j.real>>j.imaginary;
 k= add(i,j);
 cout<<k.real<<"+"<<k.imaginary<<endl;
 return 0;
}
 
 int add(int a,int b)
 { return a+b;
  } 
  
 double add(double n, double m)
  { return n+m;
   }
 Complex add(Complex i,Complex j)
 { Complex x;
   x.real=i.real+j.real;
   x.imaginary=i.imaginary+j.imaginary;
   return x;
 }

技术图片

#include <iostream>
using namespace std;

int partition(int a[],int left,int right)
{
    int i=left;
    int j=right;
    int temp=a[i];
    while(i<j)
    {
        while(i<j && a[j]>=temp)
            j--;
            if(i<j)
                a[i]=a[j];
        while(i<j && a[i]<=temp)
            i++;
            if(i<j)
                a[j]=a[i];
    }
    a[i]=temp;
    return i;
}
double partition(double a[],int left,int right)
{
    int i=left;
    int j=right;
    double temp=a[i];
    while(i<j)
    {
        while(i<j && a[j]>=temp)
            j--;
            if(i<j)
                a[i]=a[j];
        while(i<j && a[i]<=temp)
            i++;
            if(i<j)
                a[j]=a[i];
    }
    a[i]=temp;
    return i;
}
void quickSort(int a[],int left,int right)
{
    int m;
    if(left<right)
    {
        m=partition(a,left,right);
        quickSort(a,left,m-1);
        quickSort(a,m+1,right);
    }
}

void quickSort(double a[],int left,int right)
{
    int m;
    if(left<right)
    {
        m=partition(a,left,right);
        quickSort(a,left,m-1);
        quickSort(a,m+1,right);
    }
}

int main()
{
    int a[10]={12,44,74,86,45,99,64,23,48};
    quickSort(a,0,9);
    for(int i=0;i<10;i++)
    {
        cout<<a[i]<<"    ";
    }
    
    cout<<"\n"<<endl;
    double b[10]={1.2,4.4,7.4,8.6,4.5,9.9,6.4,2.3,4.8};
    quickSort(b,0,9);
    for(int i=0;i<10;i++)
    {
        cout<<b[i]<<"   ";
    }
    return 0;
}

技术图片

#include <iostream>
#include <string> 
using namespace std;
class User {    
           public:
           void    setInfo(string name4,string password4="111111",string email4=" ");
 void changePasswd();       
  void printInfo();    
  private: 
    string name;     
    string passwd;    
    string email;   };
    
         void User::    setInfo(string name4,string password4,string email4)
               {
        name=name4;
        passwd=password4;
        email=email4;
               }
    
   void User::changePasswd(){
        string oldpassword;
        cin>>oldpassword;
        int i=1;
        while(oldpassword!=passwd&&i<3)
        {
            cout<<"wrong,enter your old password anagin:";
            cin>>oldpassword;
            i++;
        }
        if(i>=3)
        cout<<"try later"<<endl;
        if(oldpassword==passwd)
        {
            string newpassword;
            cin>>newpassword;
            passwd=newpassword;
        }
    }
    
    void User::printInfo(){
        cout<<"name:"<<name<<endl;
        cout<<"password"<<"******"<<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; 
  }
   

技术图片

实验总结:

本次实验相比上次难度提升了 但我在会使用快速排序法的基础上学会了用c++编写快速排序法。

实验二

标签:new   --   快速排序   std   基础   cout   ...   pass   include   

原文地址:https://www.cnblogs.com/mzx1999/p/10597319.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!