首页 > 其他 > 详细

实验二

时间:2019-03-26 23:58:27      阅读:223      评论:0      收藏:0      [点我收藏+]

1、函数重载框架练习

#include <iostream>
using namespace std;
struct complex{
double real;
double imaginary;};
int add(int a,int b)
{return a+b;}
double add(double a,double b)
{return a+b;}
complex add(complex a,complex b)
{complex x;
x.real=a.real+b.real;
x.imaginary=a.imaginary+b.imaginary;
return x;}
int main()
{cout<<"1+2="<<add(1,2)<<endl;
cout<<"1.2+3.4="<<add(1.2,3.4)<<endl;
cout<<"(1+3i)+(2+4i)="<<add(1,2)<<"+"<<add(3,4)<<"i"<<endl;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

技术分享图片

 

2、快速排序

#include<iostream>
using namespace std;
void QuickSort(double a[], int left,int right)
{if(left<right)
{int i=left;
int j=right;
double x=a[i];
while(i<j)
{while(i<j&&a[j]>x)
j--;
if(i<j)
{a[i]=a[j];
i++;}
while(i<j&&a[i]<x)
i++;
if(i<j)
{a[j] = a[i];
j--;}}
a[i]=x;
QuickSort(a, left, i-1);
QuickSort(a, i+1, right);
}}
int main()
{int i,k;
double a[100];
char s;
for(i=0;i<100;i++)
{cin>>a[i];
if(getchar()==‘\n‘)break;else k++;}
QuickSort(a,0,i-1);
cout<<"排序后的数为: ";
for(i=0;i<k;i++)
cout<<a[i]<<‘ ‘;
cout<<endl;
return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

技术分享图片

 

3、类的定义、实现与使用

 

#include <iostream>
#include <string>
using namespace std;
class User
{public:
User(string name1, string passwd1, string email1);
User(){
name="";
passwd="111111";
email="";}
void setInfo(string name1="",string passwd1="111111",string email1="");
void changePasswd();
void printInfo();
private:
string name;
string passwd;
string email;};
void User::setInfo(string name1,string passwd1,string email1)
{if (name == " ")cin>>name1;
name=name1;
if(passwd == " ")cin>>passwd1;
passwd=passwd1;
if(email == " ")cin>>email1;
email=email1;
}
void User::changePasswd(){
string oldpasswd;
int n=1;
cout<<"请输入原密码:";
cin>>oldpasswd;
while(oldpasswd!=passwd&&n<3)
{cout<<"错误,请重试:";
cin>>oldpasswd;
n++;}
if(oldpasswd==passwd)
{cout<<"请输入新密码:";
cin>>passwd;}
}
void User::printInfo(){
cout<<"姓名: "<<name<<endl;
cout<<"密码:******"<<endl;
cout<<"email: "<<email<<endl;
cout<<endl;}
int main()
{User user1;
user1.setInfo("Leonard");
user1.printInfo();
user1.changePasswd();
user1.printInfo();
User user2;
user2.setInfo("Jonny","92197","xyz@hotmail.com");
user2.printInfo();
return 0;}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

技术分享图片

 

实验二

原文:https://www.cnblogs.com/yidaoyigexiaopenyou/p/10601954.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!