实验二

1.函数重载编程练习

编写重载函数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 main(){
    int a,b,c;
    cin>>a>>b;
    c=add(a,b);
    cout<<"sum:"<<c<<endl;
    
    double x,y,z;
    cin>>x>>y;
    z=add(x,y);
    cout<<"sum:"<<z<<endl;
    
    Complex j,k,l;
    cin>>j.real>>j.imaginary>>k.real>>k.imaginary;
    l=add(j,k);
    cout<<"sum:"<<l.real<<"+"<<l.imaginary<<"i"<<endl;
    
    return 0;
}
int add(int m,int n)
{
    return m+n;
}
double add(double m,double n)
{
    return m+n;
}
Complex add(Complex a,Complex b)
{   Complex c;
    c.real=a.real+b.real;
    c.imaginary=a.imaginary+b.imaginary;
    return c;
}
重载函数

 

2.函数模板编程练习
编写实现快速排序函数模板,并在main()函数中,定义不同类型数据,调用测试。
#ifndef Sort
#define Sort
template <class T>
void quick(T s[], int low, int high) {
    int  x, y, z = 0;
    T f, ex;
    x = low; y = high; f = s[(low + high) / 2];
    if (x < y) {
        while (x< y) {
            while (f < s[y])
                y--;
            while (f > s[x])
                x++;
            if (x >= y) z = y;
            else { ex = s[x];s[x] = s[y];s[y] = ex; }
        }
        quick(s, low, z);
        quick(s, z+ 1, high);
    }
}
#endif 
quick sort.h
#include <iostream>
#include <iomanip>
#include "quick sort.h"
using namespace std;
int main() {
    int i;
    int x[5] = { 57,49,61,16,39};
    double y[5] = { 2.5,1.1,9.8,4.7,2.6};
    quick(x, 0, 4);
    quick(y, 0, 4);
    for (i = 0; i < 5; i++)
        cout << setw(5) << x[i];
    cout << endl;
    for (i = 0; i < 5; i++)
        cout << setw(5) << y[i];
    system("pause");
    return 0;
}
main

 

 
3.类的定义、实现和使用编程练习
设计并实现一个用户类User,并在主函数中使用和测试这个类。具体要求如下:
每一个用户有用户名(name), 密码(passwd),联系邮箱(email)三个属性。
支持设置用户信息setInfo()。允许设置信息时密码默认为6个1,联系邮箱默认为空串。
支持打印用户信息printInfo()。打印用户名、密码、联系邮箱。其中,密码以6个*方式显示。
支持修改密码changePasswd(),。在修改密码前,要求先输入旧密码,验证无误后,才允许修改。
如果输入旧密码时,连续三次输入错误,则提示用户稍后再试,暂时退出修改密码程序。
在main()函数中创建User类实例,测试User类的各项操作(设置用户信息,修改密码,打印用户信
息)
 
!!!这题编错了! ! !(看到了一楼的评论,前来改正) !!!
#ifndef HEADER
#define HEADER
#include <iostream>
#include <string>
using namespace std;// User类的声明
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) {
    name = name1;
    passwd = passwd1;
    email = email1;
}
void User::changePasswd() {
    string old, n;
    int i;
    cout << "Please enter the oldpassword: ";cin >> old;
    if (old != passwd) {
        for (i = 0; i < 2; i++) {
            cout << "Please enter the newpassword again: ";
            cin >> old;
            if (old == passwd)  break;
        }
        cout << "Please try again afer a while." << endl;
    }
    if (old == passwd)
    {
        cin >> old;cout << "Please enter the password again:";cin >> n;
        if (old == n)
            passwd = old;
        else
            cout << "Your entered is wrong!" << endl;

    }
}
void User::printInfo() {
    cout << "Name:" << name << endl;
    cout << "Passwd:" << "******" << endl;
    cout << "Email:" << email << endl;
}
#endif // !HEADER
user.h
#include<iostream>
#include"user.h"
using namespace std;
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", "568945", "xyz@hotmail.com");
    user2.printInfo();
    system("pause");
    return 0;
}
mian

 

 

修改后的程序:
#include <iostream>
#include <string>
using namespace std;// User类的声明
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) {
    name = name1;
    passwd = passwd1;
    email = email1;
}
void User::changePasswd(){
    string old;
    int i;
    cout<<"Please enter the oldpassword:";
    for(i=0;i<3;i++){
    cin>>old;
    if(old=="111111"){
      cout<<"Please enter the newpassword again:"; 
      cin>>old;
      passwd=old;
      cout<<"Success!"<<endl;
      break;
    }
    else
        cout<<"Please enter the password again:"; 

}
    if(i==3){
        cout<<"Please try again afer a while."<<endl; 
    }
}
void User::printInfo() {
    cout << "Name:" << name << endl;
    cout << "Passwd:" << "******" << 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", "568945", "xyz@hotmail.com");
    user2.printInfo();
    system("pause");
    return 0;
}
user

正确密码输入:

 错误密码输入:
这个错误密码的输出还是感觉有点怪怪的,希望有大佬指点。
 总结:这次实验对于类的用法比较难,有些地方我还是请教了同学才弄明白。第一题和第三题都有套用公式比较好上手,但是没有格式就感觉无从下手,程序中可能存在一些错误希望老师同学可以指导点评一下。谢谢!
互评:
1.https://www.cnblogs.com/mxueyyqx/p/10585933.html
2.https://www.cnblogs.com/q1831726125/p/10597633.html
posted @ 2019-03-26 00:04  星星会打烊  阅读(240)  评论(1编辑  收藏  举报