代码改变世界

c++实验二

2019-03-24 10:04  陈仕成  阅读(288)  评论(3编辑  收藏  举报

(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;
}