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

实验二

时间:2019-03-22 22:52:58      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:real   lse   重载函数   类型   struct   http   i+1   com   eal   

编写重载函数add(),实现对int型,double型,Complex型数据的加法。在main()函数中定义不同类型 数据,调用测试。

#include<iostream>
using namespace std;
struct complex {
double real;
double imaginary;
};
int add(int, int);
double add(double,double);
complex add(complex, complex);
int add (int x,int y){
   return x+y;
} 
double add(double x,double y){
   return x+y;
}
complex add(complex x,complex y){
complex s;
s.imaginary=x.imaginary+y.imaginary;
s.real=x.real+y.real; 
return s;}
int main() {
 int a1=3,b1=5;
 double a2=2.6,b2=6.7;
 complex a3={4.3,2};
 complex b3={5.9,6};
 complex x;
 x=add(a3,b3);
 cout<<"3+5等于"<<add(a1,b1)<<endl;
 cout<<"2.6+6.7等于"<<add(a2,b2)<<endl;
 cout<<"a3+b3="<<x.real<<"+"<<x.imaginary<<"i"<<endl;
 
return 0;
}

 

【运行截图】

 技术图片

 

 

 

 

 

 

 编写实现快速排序函数模板,并在main()函数中,定义不同类型数据,调用测试。

#include <iostream> 
using namespace std;
 void Qsort(int a[], int low, int high)
{
    if(low >= high)
    { return;
    }
    
    int i= low;
    int j= high;
    int k= a[i];
    while(i < j)
    {
        while(i < j && a[j] >= k)
        {
         --j;
        } 
        a[i] = a[j];
        while(i < j && a[i] <= k)
        {
            ++i;
                 
        a[j] = a[i];    
    }
    a[i] = k;
    Qsort(a, low, i-1);
    Qsort(a, i+1, high);
}
}
int main()
{   int i;
    int a[9] = {52, 38, 23, 57, 62, 28, 69, 33, 74};
    cout<<"未排序前"<< endl;
    for(i=0;i<9;i++)
    cout<<a[i]<<" ";
    cout<<endl;
    cout<<"排序后"<< endl;
    Qsort(a,0,9);
    for(i=0;i<9;i++)
    cout<<a[i]<<" ";

      return 0;
} 

 

 

【运行截图】

 技术图片

 

 

 

类的定义、实现和使用编程练习 设计并实现一个用户类User,并在主函数中使用和测试这个类。具体要求如下: 每一个用户有用户名(name), 密码(passwd),联系邮箱(email)三个属性。 支持设置用户信息setInfo()。允许设置信息时密码默认为6个1,联系邮箱默认为空串。 支持打印用户信息printInfo()。打印用户名、密码、联系邮箱。其中,密码以6个*方式显示。 支持修改密码changePasswd(),。在修改密码前,要求先输入旧密码,验证无误后,才允许修改。 如果输入旧密码时,连续三次输入错误,则提示用户稍后再试,暂时退出修改密码程序。 在main()函数中创建User类实例,测试User类的各项操作(设置用户信息,修改密码,打印用户信 息)

#include<iostream>
#include<string>
using namespace std;
class user{
    public:
        void setInfo(string n,string pwd="111111",string em=" ");
        void changePasswd();
        void printfInfo();
    private:
        string name;
        string passwd;
        string email;
        
};
void user::setInfo(string n,string pwd,string em) 
{name=n;
 passwd=pwd;
 email=em;
}

void user::changePasswd()
{int i=1;
 string passwd;
 cout<<"请输入原密码";
 cin>>passwd;
 i++; 
 while(passwd!="111111"&&i<=4)
 {cout<<"密码错误,请重新输入!";
  i++;
  cin>>passwd;
 }
 if(i>4)
 {cout<<"密码错误次数过多,请稍后重试"<<endl;
 return; 
 }
 else
 {cout<<"密码输入正确!欢迎回来!" <<endl;
  cout<<"请输入新密码"<<endl; 
  cin>>passwd;
  cout<<"密码修改成功!"<<endl; 
 }
}
void user::printfInfo()
{
    cout<<"name:\t"<<name<<endl;
    cout<<"passwd:\t"<<"******"<<endl;
    cout<<"email:\t"<<email<<endl;}

int main()
{cout <<"testing 1....."<<endl;
 user user1;
 user1.setInfo("Senility","thunderstruck","shixdkanwo@gmail.com");
 user1.printfInfo();
 user1.changePasswd();
 user1.printfInfo();
 
 cout<<endl<< "testing 2 ...."<<endl<<endl;
 user user2;
 user2.setInfo("Jonny","92197","XYZ@gmail.com");
 user2.printfInfo();
 return 0;
}

 

 

【运行截图】

技术图片

 

实验二

标签:real   lse   重载函数   类型   struct   http   i+1   com   eal   

原文地址:https://www.cnblogs.com/1499978329f/p/10581354.html

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