1.函数重载

#ifndef HEADER_H
#define HEADER_H
#include<iostream>
using namespace std;

template<class T>
void add(T a, T b) {
    T n;
    n = a + b;
    cout << n << endl;
}
#endif // !HEADER_H
add

 

#ifndef HEADER_2_H
#define HEADER_2_H
#include<iostream>
using namespace std;

template<class T>
void complexadd(T r1, T r2,T i1,T i2) {
    T n,m;
    n = r1 + r2;
    m = i1 + i2;
    cout << n << "+" << m << "i" << endl;
}
#endif // !HEADER_H
complexadd
#include<iostream>
#include"add.h"
#include"complex add.h"
using namespace std;
struct Complex {
    double real;
    double imaginary;
}com1,com2;
int main()
{
    int a, b;
    float x, y;
    cin >> a >> b >> x >> y >> com1.real >> com1.imaginary >> com2.real >> com2.imaginary;
    add(a, b);
    add(x, y);
    complexadd(com1.real, com2.real, com1.imaginary,  com2.imaginary);
    system("pause");
    return 0;
}
View Code

2.快速排序

#ifndef Quicksort
#define Quicksort
template <class T>
void quick(T s[], int low, int high) {
    int a, b, c = 0;
    T f, ex;
    a = low; b = high - 1; f = s[(low+high)/2];
    if (a < b) {
    while (a < b) {
        while (a < b&&f < s[b])
            b--;
        while (a < b&&f > s[a])
            a++;
        if (a >= b) c = b;
        else { ex = s[a]; s[a] = s[b]; s[b] = ex; }
    }
        quick(s, low, c);
        quick(s, c + 1, high);
    }
}
#endif // !Quicksort
quick sort.h
#include <iostream>
#include <iomanip>
#include "quick.h"
using namespace std;
int main (){
    int i;
    int a[5] = { 9,1,6,4,2 };
    double b[5] = { 9.1,1.1,6.1,4.1,2.1 };
    quick(a, 0, 5);
    quick(b, 0, 5);
    for (i = 0; i < 5; i++)
        cout << setw(5) << a[i];
    cout << endl;
    for (i = 0; i < 5; i++)
        cout << setw(5) << b[i];
    return (0);
}
View Code

3.用户类

#include<iostream>
#include<string>
using namespace std;
class User {
public:
    User(string yourname, string yourpasswd, string youremail);
    User() {
        name = "";
        password = "111111";
        email = "";
    }
    void setInfo(string yourname = "", string yourpasswd = "111111", string youremail = "");
    void changePasswd();
    void printInfo();
private:
    string name;
    string password;
    string email;
};
void User::setInfo(string yourname, string yourpasswd, string youremail) {
    if (name == " ") cin >> yourname;
    name = yourname;
    if (email == " ") cin >> youremail;
    email = youremail;
    if (password == " ") cin >> yourpasswd;
    password = yourpasswd;
}
void User::changePasswd() {
    string n, n1, n2;
    int i;
    cout << "Please enter the oldpassword: ";
    for (i = 0; i <3; i++) {
        cin >> n;
        if (password == n) {
            while (1)
                {
                 cout << "Please enter the newpassword: ";
                 cin >> n1;
                 cout << "Please enter the newpassword again: ";
                 cin >> n2;
                 if (n1 == n2) {
                    password = n; break;
                }
                 else {
                       cout << "Two passworld is difficult!Please try again!" << endl; continue;
                       }
                }
        }
         else {
                cout << "Your entered is wrong!" ;
                cout << "Please enter the password again: ";continue;
               }
    }
     if (i >=2 )
         cout << "You input error many times !Please try again afer a few minutes." << endl;
}
void User::printInfo() {
    cout << "Name: " << name << endl;
    cout << "Password: " << "******" << endl;
    cout << "Email: " << email << endl;
}
user.h
#include<iostream>
#include<iomanip>
#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", "92197", "xyz@hotmail.com");
    user2.printInfo();
    system("pause");
    return 0;
}
View Code

实验总结与体会

1.函数重载这个程序把输出写进了头文件里,所以用来两个头文件

2.快速排序自己写不出,就照着老师给的链接模仿着写了一个

3.第三个程序不知道为什么会多做一个循环

4.希望有大佬可以帮忙改正,谢谢

 我的评论

1.https://www.cnblogs.com/KOKODA/p/10566358.html

2.https://www.cnblogs.com/shenqidetao/p/10579872.html

3.https://www.cnblogs.com/sq102217/p/10558477.html

posted on 2019-03-24 12:14  domestic徐婷楠  阅读(145)  评论(1编辑  收藏  举报