c++实验二

(1)

#include <iostream>
using namespace std;
typedef struct
{
double real;
double imaginary;
} Complex;

int add(int, int);
double add(double, double);
Complex add(Complex, Complex);

int main()
{
int a=1, b=2;
double c=1.1, d=1.2;
Complex m, n;
m.real=2;
m.imaginary=3;
n.real=1;
n.imaginary=2;
cout << add(a, b) << endl;
cout << add(c, d) << endl;
cout << add(m, n).real << "+" << add(m, n).imaginary <<"i"<< endl;
cin.get();
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)
{
Complex c;
c.real = a.real + b.real;
c.imaginary = a.imaginary + b.imaginary;
return c;
}

(2)

#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] = {8,3,23,17,26,30,53,45,67};
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;
}

 

(3)

#include <iostream>
#include <string>
using namespace std;
class User
{
public:
void setInfo(string name0,string passwd0="111111",string email0="");
void changePasswd();
void printInfo();
private:
string name;
string passwd;
string email;
};
void User::setInfo(string name0,string passwd0,string email0)
{
name=name0;
passwd=passwd0;
email=email0;
}
void User::changePasswd()
{
string passwd1;
int count=1;
cout<<"Please enter the oled password first and verify it correctly before allowing modification:";
cin>>passwd1;
while(passwd1!="111111"&&count<3)
{
count++;
cout<<"Wrong!Please re-enter again:";
cin>>passwd1;
}
if(count>=3)
{
cout<<"Please try again later and temporarily quit the program."<<endl;
}
else
{
cout<<"The password is modified successfully!"<<endl;
passwd=passwd1;
}
}
void User::printInfo()
{
cout<<"name:\t"<<name<<endl;
cout<<"passwd:\t"<<"******"<<endl;
cout<<"email:\t"<<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;
}

 

转载于:https://www.cnblogs.com/csc13813017371/p/10584971.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值