C++航空系统

/*
*   SHA-256 implementation, Mark 2
*
*   Copyright (c) 2010,2014 Ilya O. Levin, http://www.literatecode.com
*
*   Permission to use, copy, modify, and distribute this software for any
*   purpose with or without fee is hereby granted, provided that the above
*   copyright notice and this permission notice appear in all copies.
*
*   THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
*   WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
*   MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
*   ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
*   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
*   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
*   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "sha256.h"
/* #define MINIMIZE_STACK_IMPACT */

#ifdef __cplusplus
extern "C" {
#endif

#define FN_ inline static

static const uint32_t K[64] = {
    0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
    0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
    0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
    0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
    0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
    0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
    0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
    0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
    0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
    0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
    0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
    0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
    0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
    0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
    0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
    0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};

#ifdef MINIMIZE_STACK_IMPACT
static uint32_t W[64];
#endif

/* -------------------------------------------------------------------------- */
FN_ uint8_t _shb(uint32_t x, uint32_t n)
{
    return ( (x >> (n & 31)) & 0xff );
} /* _shb */

/* -------------------------------------------------------------------------- */
FN_ uint32_t _shw(uint32_t x, uint32_t n)
{
    return ( (x << (n & 31)) & 0xffffffff );
} /* _shw */

/* -------------------------------------------------------------------------- */
FN_ uint32_t _r(uint32_t x, uint8_t n)
{
    return ( (x >> n) | _shw(x, 32 - n) );
} /* _r */

/* -------------------------------------------------------------------------- */
FN_ uint32_t _Ch(uint32_t x, uint32_t y, uint32_t z)
{
    return ( (x & y) ^ ((~x) & z) );
} /* _Ch */

/* -------------------------------------------------------------------------- */
FN_ uint32_t _Ma(uint32_t x, uint32_t y, uint32_t z)
{
    return ( (x & y) ^ (x & z) ^ (y & z) );
} /* _Ma */

/* -------------------------------------------------------------------------- */
FN_ uint32_t _S0(uint32_t x)
{
    return ( _r(x, 2) ^ _r(x, 13) ^ _r(x, 22) );
} /* _S0 */

/* -------------------------------------------------------------------------- */
FN_ uint32_t _S1(uint32_t x)
{
    return ( _r(x, 6) ^ _r(x, 11) ^ _r(x, 25) );
} /* _S1 */

/* -------------------------------------------------------------------------- */
FN_ uint32_t _G0(uint32_t x)
{
    return ( _r(x, 7) ^ _r(x, 18) ^ (x >> 3) );
} /* _G0 */

/* -------------------------------------------------------------------------- */
FN_ uint32_t _G1(uint32_t x)
{
    return ( _r(x, 17) ^ _r(x, 19) ^ (x >> 10) );
} /* _G1 */

/* -------------------------------------------------------------------------- */
FN_ uint32_t _word(uint8_t *c)
{
    return ( _shw(c[0], 24) | _shw(c[1], 16) | _shw(c[2], 8) | (c[3]) );
} /* _word */

/* -------------------------------------------------------------------------- */
FN_ void  _addbits(sha256_context *ctx, uint32_t n)
{
    if ( ctx->bits[0] > (0xffffffff - n) )
        ctx->bits[1] = (ctx->bits[1] + 1) & 0xFFFFFFFF;
    ctx->bits[0] = (ctx->bits[0] + n) & 0xFFFFFFFF;
} /* _addbits */

/* -------------------------------------------------------------------------- */
static void _hash(sha256_context *ctx)
{
    register uint32_t a, b, c, d, e, f, g, h, i;
    uint32_t t[2];
#ifndef MINIMIZE_STACK_IMPACT
    uint32_t W[64];
#endif

    a = ctx->hash[0];
    b = ctx->hash[1];
    c = ctx->hash[2];
    d = ctx->hash[3];
    e = ctx->hash[4];
    f = ctx->hash[5];
    g = ctx->hash[6];
    h = ctx->hash[7];

    for (i = 0; i < 64; i++) {
        if ( i < 16 )
            W[i] = _word(&ctx->buf[_shw(i, 2)]);
        else
            W[i] = _G1(W[i - 2]) + W[i - 7] + _G0(W[i - 15]) + W[i - 16];

        t[0] = h + _S1(e) + _Ch(e, f, g) + K[i] + W[i];
        t[1] = _S0(a) + _Ma(a, b, c);
        h = g;
        g = f;
        f = e;
        e = d + t[0];
        d = c;
        c = b;
        b = a;
        a = t[0] + t[1];
    }

    ctx->hash[0] += a;
    ctx->hash[1] += b;
    ctx->hash[2] += c;
    ctx->hash[3] += d;
    ctx->hash[4] += e;
    ctx->hash[5] += f;
    ctx->hash[6] += g;
    ctx->hash[7] += h;
} /* _hash */

/* -------------------------------------------------------------------------- */
void sha256_init(sha256_context *ctx)
{
    if ( ctx != NULL ) {
        ctx->bits[0]  = ctx->bits[1] = 0;
        ctx->len      = 0;
        ctx->hash[0] = 0x6a09e667;
        ctx->hash[1] = 0xbb67ae85;
        ctx->hash[2] = 0x3c6ef372;
        ctx->hash[3] = 0xa54ff53a;
        ctx->hash[4] = 0x510e527f;
        ctx->hash[5] = 0x9b05688c;
        ctx->hash[6] = 0x1f83d9ab;
        ctx->hash[7] = 0x5be0cd19;
    }
} /* sha256_init */

/* -------------------------------------------------------------------------- */
void sha256_hash(sha256_context *ctx, const void *data, size_t len)
{
    register size_t i;
    const uint8_t *bytes = (const uint8_t *)data;

    if ( (ctx != NULL) && (bytes != NULL) )
        for (i = 0; i < len; i++) {
            ctx->buf[ctx->len] = bytes[i];
            ctx->len++;
            if (ctx->len == sizeof(ctx->buf) ) {
                _hash(ctx);
                _addbits(ctx, sizeof(ctx->buf) * 8);
                ctx->len = 0;
            }
        }
} /* sha256_hash */

/* -------------------------------------------------------------------------- */
void sha256_done(sha256_context *ctx, uint8_t *hash)
{
    register uint32_t i, j;

    if ( ctx != NULL ) {
        j = ctx->len % sizeof(ctx->buf);
        ctx->buf[j] = 0x80;
        for (i = j + 1; i < sizeof(ctx->buf); i++)
            ctx->buf[i] = 0x00;

        if ( ctx->len > 55 ) {
            _hash(ctx);
            for (j = 0; j < sizeof(ctx->buf); j++)
                ctx->buf[j] = 0x00;
        }

        _addbits(ctx, ctx->len * 8);
        ctx->buf[63] = _shb(ctx->bits[0],  0);
        ctx->buf[62] = _shb(ctx->bits[0],  8);
        ctx->buf[61] = _shb(ctx->bits[0], 16);
        ctx->buf[60] = _shb(ctx->bits[0], 24);
        ctx->buf[59] = _shb(ctx->bits[1],  0);
        ctx->buf[58] = _shb(ctx->bits[1],  8);
        ctx->buf[57] = _shb(ctx->bits[1], 16);
        ctx->buf[56] = _shb(ctx->bits[1], 24);
        _hash(ctx);

        if ( hash != NULL )
            for (i = 0, j = 24; i < 4; i++, j -= 8) {
                hash[i     ] = _shb(ctx->hash[0], j);
                hash[i +  4] = _shb(ctx->hash[1], j);
                hash[i +  8] = _shb(ctx->hash[2], j);
                hash[i + 12] = _shb(ctx->hash[3], j);
                hash[i + 16] = _shb(ctx->hash[4], j);
                hash[i + 20] = _shb(ctx->hash[5], j);
                hash[i + 24] = _shb(ctx->hash[6], j);
                hash[i + 28] = _shb(ctx->hash[7], j);
            }
    }
} /* sha256_done */

/* -------------------------------------------------------------------------- */
void sha256(const void *data, size_t len, uint8_t *hash)
{
    sha256_context ctx;

    sha256_init(&ctx);
    sha256_hash(&ctx, data, len);
    sha256_done(&ctx, hash);
} /* sha256 */


}

/*********************************************************************************/


#include<iostream>
#include<algorithm>
#include<windows.h>
#include<string>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<stdio.h>
using namespace std;
class Time {
private:
    int month;
    int day;
    int houor;
    int minute;
public:
    Time();
    Time(int m, int d, int h, int mi);
    int get_month();
    int get_day();
    int get_houor();
    int get_minute();
    void show();
    bool operator<(const Time &t);
    bool check();
};
Time::Time() {
    ;
}
Time::Time(int m, int d, int h, int mi) {
    month = m;
    day = d;
    houor = h;
    minute = mi;
}
int Time::get_month() {
    return month;
}
int Time::get_day(){
    return day;
}
void Time::show() {
    printf("%02d月%02d日 %02d:%02d ", month, day, houor, minute);
}
bool Time::operator<(const Time &t) {
    if (month < t.month)
        return true;
    else if (month==t.month &&  day< t.day)
        return true;
    else if (month==t.month&& day==t.day && houor < t.houor)
        return true;
    else if (month==t.month && day==t.day&& houor==t.houor && minute < t.minute)
        return true;
    else
        return false;
}
bool Time::check(){
    if(!(month>=0 && month<=12 && day>=1 && day<=31 && houor>=0 && houor<=24 && minute>=0 && minute<=60))
        return false;
    return true;
}
class Passenger {
private:
    string name;
    string passport;
    string country;
    string tel_number;
public:
    Passenger();
    Passenger(string n, string p, string c, string t);
    string get_passport();
    void Delete();
    void set(string n, string p, string c, string t);
    void show();
}*TP;
int Count;
Passenger::Passenger() {
    ;
}
Passenger::Passenger(string n, string p, string c, string t) {
    name = n;
    passport = p;
    country = t;
    tel_number = t;
}
string Passenger::get_passport() {
    return passport;
}
void Passenger::Delete() {
    this->name = " ";
    this->passport = " ";
    this->country = " ";
    this->tel_number = " ";
}
void Passenger::set(string n, string p, string c, string t) {
    name = n;
    passport = p;
    country = t;
    tel_number = t;
}

void Passenger::show() {
    cout << name << " " << passport << " " << country << " " << tel_number << endl;
}

struct user_data
{
    string ID;
    int Role;
    int i;
};
class User {
private:
    user_data *u;
    int length;
    int number;
public:
    User(int length=100,int number=0){
        u=new user_data[length];
        this->number=number;
    }
    void add(string ID,int role,int i){
        u[number].ID=ID;
        u[number].Role=role;
        u[number].i=i;
        number++;
    }
    int change_i(string s){
        for(int i=0;i<number;i++){
            if(u[i].ID==s){
                u[i].i=u[i].i-1;
            }
        }
    }
    user_data &get(string s){
        int flag=0;
        for(int i=0;i<number;i++){
            if(u[i].ID==s){
                flag=1;
                return u[i];
            }
        }
        if(flag==0){
            cout<<"用户名错误"<<endl;
            exit(0);
        }
    }
}U;

class Flight {
private:
    string flight_number;
    string type;
    string origin_place;
    string dest_place;
    Time start_time;
    Time end_time;
    int price;
    char S[20][20];
    Passenger P[20][20];
    int seat;
    int x;
    int y;
    int a_seat;
public:
    Flight();
    Flight(string f, string t, string o, string d, Time start, Time end, int p, int x, int y, int a = 0);
    void set(string f, string t, string o, string d, Time start, Time end, int p, int x, int y, int a = 0);
    string get_flight_number();
    string get_origin_place();
    string get_dest_place();
    Time get_start_time();
    bool book(Passenger p);
    bool book(Passenger p, int x1, int y1);
    bool cancel(string s);
    void show();
    void show_seat();
    void show_passenger();
};
Flight::Flight() {
    ;
}
Flight::Flight(string f, string t, string o, string d, Time start, Time end, int p, int x,int y, int a ) {
    this->flight_number = f;
    this->origin_place = o;
    this->dest_place = d;
    this->start_time = start;
    this->end_time = end;
    this->price = p;
    this->type = t;
    this->x = x;
    this->y = y;
    this->a_seat = 0;
    this->seat = x * y;
    for (int i = 0; i < x; i++) {
        for (int j = 0; j < y; j++)
            S[i][j] = '0';
    }
}
void Flight::set(string f, string t, string o, string d, Time start, Time end, int p, int x,int y, int a) {
    this->flight_number = f;
    this->origin_place = o;
    this->dest_place = d;
    this->start_time = start;
    this->end_time = end;
    this->price = p;
    this->type = t;
    this->x = x;
    this->y = y;
    this->a_seat = 0;
    this->seat = x * y;
    for (int i = 0; i < this->x; i++) {
        for (int j = 0; j < this->y; j++)
            S[i][j] = '1';
    }
}
string Flight::get_flight_number() {
    return flight_number;
}
string Flight::get_origin_place() {
    return origin_place;
}
string Flight::get_dest_place(){
    return dest_place;
}
Time Flight::get_start_time(){
    return start_time;
}
bool Flight::book(Passenger p) {
    if (a_seat == seat)
        return false;
    srand((unsigned)time(NULL));
    int x1 = rand() % x;
    int y1 = rand() % y;
    while (S[x1][y1] != '1') {
        x1 = rand() % x;
        y1 = rand() % y;
    }
    P[x1][y1] = p;
    //P[x1][y1].show();
    S[x1][y1] = '*';
    a_seat++;
    TP[Count++] = p;
    return true;
}
bool Flight::book(Passenger p, int x1, int y1) {
    if (a_seat == seat) {
        cout << "该航班已没有剩余座位" << endl;
        return false;
    }
    if (S[x1][y1] == '*'){
        cout << "该座位已被预订" << endl;
        return false;
    }
    S[x1][y1] = '*';
    P[x1][y1] = p;
    //P[x1][y1].show();
    a_seat++;
    TP[Count++] = p;
    return true;

}
bool Flight::cancel(string s) {
    int flag = false;
    for (int i = 0; i < x; i++) {
        for (int j = 0; j < y; j++) {
            if (S[i][j] == '*') {
                    //cout<<P[i][j].get_passport();
                if (P[i][j].get_passport() == s) {
                    flag = true;
                    S[i][j] = '1';
                    P[i][j].Delete();
                }
            }
        }
    }
    if (flag == false)
        cout << "不存在此护照号" << endl;
    return flag;
}
void Flight::show() {
    printf("%-10s%-30s%-10s%-10s ",flight_number.c_str(),type.c_str(),origin_place.c_str(),dest_place.c_str());
    start_time.show();
    cout<<"   ";
    end_time.show();
    printf("%10d%10d\n",seat,a_seat);
}
void Flight::show_seat() {
    cout << "  ";
    for (int i = 0; i < y; i++)
        printf("%d ",i);
    cout << endl;
    for (int i = 0; i < x; i++) {
        printf("%02d ",i);
        for (int j = 0; j < y; j++) {
            cout << S[i][j]<<" ";
        }
        cout << endl;
    }
}
void Flight::show_passenger(){
    for(int i=0;i<x;i++){
        for(int j=0;j<y;j++){
            if(S[i][j]=='*')
                P[i][j].show();
        }
    }
}
class Menu {
private:
    int length;
    int number;
    int max_increase;
    Flight *F;
public:
    Menu(int n = 100, int m = 10);
    void Increase();
    bool Add(string f, string t, string o, string d, Time start, Time end, int p, int x,int y, int a = 0);
    void Delete(string f);
    void Query();
    void Query(string f);
    void Query(string s1, string s2);
    void Query(Time t);
    void Query(string s1, string s2, Time t);
    bool Book(string f,Passenger p);
    bool Book(string f, Passenger p, int x1, int y1);
    void Refund(string f);
    void show_seat(string f);
    void show_passenger(string f);
    bool cancel(string s1, string s2);
};
Menu::Menu(int n, int m) {
    F = new Flight[n];
    length = n;
    max_increase = m;
    number = 0;
}
void Menu::Increase() {
    Flight *tF = new Flight[length + max_increase];
    for (int i = 0; i < length; i++) {
        tF[i] = F[i];
    }
    delete[]F;
    F = tF;
    length += max_increase;
}
bool Menu::Add(string f, string t, string o, string d, Time start, Time end, int p, int x,int y, int a) {
    if (number + 1 <= length) {
        Increase();
    }
    F[number].set(f,t,o,d,start,end,p,x,y,a);
    number = number + 1;
    return true;
}
void Menu::Delete(string f) {
    for (int i = 0; i <= number; i++) {
        if (f == F[i].get_flight_number()) {
            for (int j = i; j <= number; j++)
                F[j] = F[j + 1];
            //delete F[j + 1];
            number--;
        }
    }
}
void Menu::Query() {
    printf("%-10s%-30s%-10s%-10s%-20s%-20s%-10s%-10s\n","航班号","飞机类型","出发地","目的地","出发时间","到达时间","载客量","已预定数");
    for (int i = 0; i <number; i++) {
        F[i].show();
    }
}
void Menu::Query(string f) {
    bool falg = false;
    for (int i = 0; i < number; i++) {
        if (F[i].get_flight_number() == f) {
            F[i].show();
            falg = true;
        }
    }
    if (falg == false)
        cout << "无此航班" << endl;
}
void Menu::Query(Time t) {
    Time t1(t.get_month(), t.get_day(), 0, 0);
    Time t2(t.get_month(), t.get_day() + 1, 0, 0);
    bool flag = false;
    for (int i = 0; i < number; i++) {
        if (t1 < F[i].get_start_time() && F[i].get_start_time() < t2) {
            flag = true;
            F[i].show();
        }
    }
    if (flag == false)
        cout << "无符合条件的航班" << endl;
}
void Menu::Query(string s1, string s2) {
    bool flag = false;
    for (int i = 0; i < number; i++) {
        if (F[i].get_origin_place() == s1 && F[i].get_dest_place() == s2 ) {
            F[i].show();
            flag = true;
        }
    }
    if (flag == false)
        cout << "无符合条件的航班" << endl;
}
void Menu::Query(string s1, string s2, Time t) {
    Time t1(t.get_month(), t.get_day(), 0, 0);
    Time t2(t.get_month(), t.get_day() + 1, 0, 0);
    bool flag = false;
    for (int i = 0; i < number; i++) {
        if (F[i].get_origin_place() == s1 && F[i].get_dest_place() == s2 && t1 < F[i].get_start_time() && F[i].get_start_time() < t2) {
            F[i].show();
            flag = true;
        }
    }
}
bool Menu::Book(string f,Passenger p) {
    for (int i = 0; i <= number; i++) {
        if (f == F[i].get_flight_number()) {
            bool J = F[i].book(p);
            if (J == false)
                return false;
            else
                return true;
        }
    }
}
bool Menu::Book(string f, Passenger p, int x1, int y1) {
    for (int i = 0; i <= number; i++) {
        if (f == F[i].get_flight_number()) {
            bool J = F[i].book(p,x1,y1);
            if (J == false)
                return false;
            else
                return true;
        }
    }
}
void Menu::Refund(string f) {
    bool flag = false;
    for (int i = 0; i < number; i++) {
        if (f == F[i].get_flight_number()) {
            flag == true;
            for (int j = i; j < number-1; j++) {
                F[j] = F[j + 1];
            }
        }
    }
    number--;
    if (flag == false) {
        cout << "无此航班" << endl;
    }
}
void Menu::show_seat(string f) {
    for (int i = 0; i <number; i++) {
        if (f == F[i].get_flight_number())
            F[i].show_seat();
    }
}
bool Menu::cancel(string s1, string s2) {
    bool flag = false;
    for (int i = 0; i < number; i++) {
        if (s1 == F[i].get_flight_number()) {
            flag = true;
            if (F[i].cancel(s2) == false)
                return false;
        }
    }
    if (flag == false) {
        cout << "无此航班";
    }
    return flag;
}
void Menu::show_passenger(string f){
    for(int i=0;i<number;i++){
        if(f==F[i].get_flight_number()){
            F[i].show_passenger();
        }
    }
}
class Check{
public:
    bool time(Time t){
        return t.check();
    }
    bool time(Time t1,Time t2){
        if(t1<t2)
            return true;
        else
            return false;
    }
    bool tel_number(string s){
        int len=s.length();
        for(int i=0;i<len;i++)
            if(!s[i]>='0' && s[i]<='9')
            return false;
        return true;
    }
};
/****************************************************************************************************/
Menu M;
Check C;
void Init() {
    FILE *P;
    if ((P = fopen("data.txt", "r+")) == NULL) {
        cout << "Error" << endl;
        exit(0);
    }
    char f[100];
    char t[100];
    char o[100];
    char d[100];
    Time start;
    Time end;
    int p;
    int x;
    int y;
    int a;
    int m1, d1, h1, mi1;
    int m2, d2, h2, mi2;
    while (fscanf(P, "%s%s%s%s%d%d%d%d%d%d%d%d%d%d%d%d", f, t, o, d, &m1, &d1, &h1, &mi1, &m2, &d2, &h2, &mi2, &p, &x,&y,&a) != EOF) {
        M.Add(f, t, o, d, Time(m1, d1, h1, mi1), Time(m2, d2, h2, mi2), p, x,y,a);
    }
    //M.Query();
}
void Init2(){
    FILE *P;
    TP=new Passenger[10000];
    Count=0;
    if((P=fopen("Passenger.txt","r+"))==NULL){
        cout<<"error"<<endl;
        exit(0);
    }
    char n[100];
    char p[100];
    char c[100];
    char t[100];
    while(fscanf(P,"%s%s%s%s",n,p,c,t)!=EOF){
        M.Book("KN5838",Passenger(n,p,c,t));
    }
}
void p_query(){
    for(int i=0;i<Count;i++)
        TP[i].show();
}
void p_query(string s){
    bool flag=false;
    for(int i=0;i<Count;i++){
        if(s==TP[i].get_passport()){
            TP[i].show();
            flag=true;
        }
    }
    if(flag==false)
        cout<<"无此证件号"<<endl;
}

bool enroll(){
    FILE *P;
    srand((unsigned)time(NULL));
    char s1[100],s2[100];
    uint8_t hash[SHA256_BYTES];
    uint8_t hash1[SHA256_BYTES];
    size_t i, j;
    int judge;
    cout<<"1.管理员注册"<<endl;
    cout<<"2.普通用户注册:"<<endl;
    cin>>judge;
    cout<<"输入用户名:";
    cin>>s1;
    cout<<"输入密码:";
    cin>>s2;
   // judge=rand()%2;
    if(judge==2){
            U.add(s1,judge,0);
            if((P=fopen("ID1.txt","r+"))==NULL){
                cout<<"can not open ID1.txt"<<endl;
                return false;
            }
            char *salt=new char[20];
            for(int i=0;i<16;i++)
                 salt[i]=33+rand()%95;
            salt[16]='\0';
            int l1=strlen(s2);
            int t=l1+16;
            for(int i=l1;i<t;i++)
                s2[i]=salt[i-l1];
            s2[t]='\0';
            //for(int i=0;i<strlen(s2);i++)
              //  cout<<s2[i];
            //cout<<endl;
            sha256(s2,strlen(s2),hash);
            //for(j=0;j<SHA256_BYTES;j++)
            //   printf("%0x",hash[j]);
            fprintf(P,"%s %s\n",s1,salt);
            for(j=0;j<SHA256_BYTES;j++)
                fprintf(P,"%x ",hash[j]);
            fprintf(P,"\n");
            fclose(P);
    }
    else if(judge==1){
            int i=1000;
            U.add(s1,judge,i);
            if((P=fopen("ID2.txt","r+"))==NULL){
                cout<<"can not open ID2.txt"<<endl;
                exit(0);
            }
            fprintf(P,"%s\n",s1);
            for(i=1;i<=1001;i++){
                if(i==1){
                    sha256(s2,strlen(s2),hash);
                   // for(j=0;j<SHA256_BYTES;j++)
                     //   printf("%x",hash[j]);
                    cout<<endl;
                }
                else{
                    for(j=0;j<SHA256_BYTES;j++)
                        hash1[j]=hash[j];
                    sha256(hash1,SHA256_BYTES,hash);
                }
            }
            for(j=0;j<SHA256_BYTES;j++){
                fprintf(P,"%x ",hash[j]);

            }
           // for(j=0;j<SHA256_BYTES;j++)
             //   printf("%x",hash[j]);
            //cout<<endl;
            fclose(P);
    }
    return true;
}
bool land1(){
    FILE *P;
    if((P=fopen("ID1.txt","r"))==NULL){
            cout<<"can not open ID1.txt"<<endl;
            return false;
    }
    uint8_t hash[SHA256_BYTES];
    uint8_t hash1[SHA256_BYTES];
    size_t j;
    char s[100],salt[100];
    cout<<"输入用户名:";
    char s1[100],s2[100];
    cin>>s1;
    cout<<"输入密码:";
    cin>>s2;
    while(fscanf(P,"%s%s",s,salt)!=EOF){
        if(strcmp(s,s1)==0){
             for(j=0;j<SHA256_BYTES;j++)
                fscanf(P,"%x",&hash[j]);
             break;
         }
    }
    int l1=strlen(s2);
    int t=l1+16;
    for(int i=l1;i<t;i++)
        s2[i]=salt[i-l1];
    s2[t]='\0';
   // for(j=0;j<strlen(s2);j++)
     //   cout<<s2[j];
    //cout<<endl;
    //for(j=0;j<SHA256_BYTES;j++)
      //  printf("%0x",hash[j]);
    //cout<<endl;
    sha256(s2,strlen(s2),hash1);
    //for(j=0;j<SHA256_BYTES;j++)
      //  printf("%0x",hash1[j]);
    for(j=0;j<SHA256_BYTES;j++){
        if(hash[j]!=hash1[j])
            return false;
    }
    cout<<"登陆成功"<<endl;
    return true;
}
bool judge(uint8_t h1[],uint8_t h2[]){
    uint8_t h[SHA256_BYTES];
    sha256(h1,SHA256_BYTES,h);
    size_t j;
    for(j=0;j<SHA256_BYTES;j++){
        if(h[j]!=h2[j])
            return false;
    }
    return true;
}
bool land2(){
    char s1[100],s2[100];
    cout<<"请输入用户名:";
    cin>>s1;
    cout<<"请输入密码:";
    cin>>s2;
    FILE *P;
    user_data u=U.get(s1);
    char s[100];
    int i=u.i;
    uint8_t hash[SHA256_BYTES];
    uint8_t hash1[SHA256_BYTES];
    uint8_t thash[SHA256_BYTES];
    size_t j;
    if((P=fopen("ID2.txt","r+"))==NULL){
        cout<<"error"<<endl;
        return false;
    }
    while(fscanf(P,"%s",s)!=EOF){
        if(strcmp(s1,s)==0){
             for(j=0;j<SHA256_BYTES;j++){
                fscanf(P,"%x",&thash[j]);
             }
             break;
         }
    }
    fclose(P);
    //for(j=0;j<SHA256_BYTES;j++)
      //         printf("%x",thash[j]);
    //cout<<endl;
    for(int k=1;k<=i;k++){
        if(k==1){
            sha256(s2,strlen(s2),hash);
        }
    else{
            for(j=0;j<SHA256_BYTES;j++)
                hash1[j]=hash[j];
                sha256(hash1,SHA256_BYTES,hash);
                }
            }
    /*for(j=0;j<SHA256_BYTES;j++){
        if(thash[j]!=hash[j])
            return false;
    }*/
    if(judge(hash,thash)==false)
        return false;
    U.change_i(s1);
    for(j=0;j<SHA256_BYTES;j++)
        fprintf(P,"%0x",hash[j]);
    cout<<"登陆成功"<<endl;
    return true;
}
void Operation() {
    int judge,Role,operation;
    cout<<"请选择 1.注册 2.登陆"<<endl;
    while(cin>>judge){
        if(judge==1){
            if(enroll()==true){
                cout<<"注册成功!"<<endl;
            }
            else{
                cout<<"注册失败!"<<endl;
                continue;
            }
        }
        else if(judge==2){
                cout<<"请选择 1.管理员 2.用户"<<endl;
                cin>>Role;
                if(Role==1){
                    if(land2()==true)
                        break;
                    else{
                        cout<<"登陆失败,请重新登陆"<<endl;
                        continue;
                    }
                }
                else if(Role==2){
                    if(land1()==true)
                        break;
                    else{
                        cout<<"登陆失败,请重新登陆"<<endl;
                        continue;
                    }
                }
                break;
        }
        else{
                cout<<"输入不合法"<<endl;
        }
    }
    cout<<"**************************************************************************************"<< endl;
    cout<<"**                                                                                  **"<<endl;
    cout<<"**                                 欢迎进入航班管理系统                             **"<< endl;
    cout<<"**                                                                                  **"<<endl;
    cout<<"**************************************************************************************"<< endl;
    int way;
    if (Role == 1) {
        cout << "请选择操作" << endl;
        cout << "0.退出"<<endl;
        cout << "1.查询航班信息" << endl;
        cout << "2.预约航班" << endl;
        cout << "3.取消航班" << endl;
        cout << "4.修改航班信息" << endl;
        cout << "5.查询旅客信息" << endl;
    }
    else if (Role == 2) {
        cout << "请选择操作" << endl;
        cout << "0.退出" << endl;
        cout << "1.查询航班信息" << endl;
        cout << "2.预约航班" << endl;
        cout << "3.取消航班" << endl;
    }
    else {
        cout << "输入不合法" << endl;
        exit(0);
    }
    while (cin >> operation) {
        if (operation == 0)
            break;
        else if (operation == 1) {
            int next = 0;
            cout << "请选择" << endl;
            cout << "1.查询所有航班信息" << endl;
            cout << "2.输入航班号查询单个航班信息" << endl;
            cout << "3.输入出发地和目的地查询航班信息" << endl;
            cout << "4.输入出发时间查询航班信息" << endl;
            cout << "5.输入出发地目的地和出发时间查询航班信息" << endl;
            cin >> next;
            if(next==1){
                M.Query();
                continue;
            }
            else if (next == 2) {
                string s;
                cout << "输入航班号" << endl;
                cin >> s;
                M.Query(s);
                continue;
            }
            else if (next == 3) {
                string s1, s2;
                cout << "输入出发地和目的地" << endl;
                cin >> s1 >> s2;
                M.Query(s1, s2);
                continue;
            }
            else if (next == 4) {
                int m, d;
                cout << "输入出发时间" << endl;
                cin >> m >> d;
                M.Query(Time(m, d, 0, 0));
                continue;
            }
            else if (next == 5) {
                string s1, s2;
                int m, d;
                cout << "输入出发地、目的地、出发时间" << endl;
                cin >> s1 >> s2;
                cin >> m >> d;
                M.Query(s1, s2, Time(m, d, 0, d));
                continue;
            }
        }
        else if (operation == 2) {
            string s;
            string s1, s2, s3, s4;
            int i;
            bool flag;
            cout << "输入航班号" << endl;
            cin >> s;
            cout << "请输入个人信息" << endl;
            cout << "请输入您的名字:";
            cin >> s1;
            cout << endl;
            cout << "请输入您的护照号:";
            cin >> s2;
            cout << endl;
            cout << "请输入您的国籍:";
            cin >> s3;
            cout << endl;
            cout << "请输入您的电话号码:";
            cin >> s4;
            while(C.tel_number(s4)==false){
                cout<<"电话号码格式错误,请重新输入"<<endl;
                cin>>s4;
            }
            cout << endl;
            //Passenger p(s1, s2, s3, s4);
            cout << "是否选择座位(1.是 2.否)" << endl;
            cin >> i;
            if (i == 1) {
                M.show_seat(s);
                int x, y;
                cout << "请输入座位号(行坐标+列坐标)" << endl;
                cin >> x >> y;
                flag=M.Book(s,Passenger(s1,s2,s3,s4), x, y);
                while (flag == false){
                    cout<<"订票失败"<<endl;
                    cout<<"请重新选择座位:";
                    cin>>x>>y;
                    flag=M.Book(s,Passenger(s1,s2,s3,s4),x,y);
                }
                cout<<"订票成功"<<endl;
                M.show_seat(s);
            }
            else {
                flag = M.Book(s, Passenger(s1,s2,s3,s4));
                if (flag == false)
                    cout << "订票失败" << endl;
                else
                    cout << "订票成功" << endl;
            }
        }
        else if (operation == 3) {
            string s1, s2;
            cout << "请输入需要取消的航班号 和 你的护照号" << endl;
            cin >> s1 >> s2;
            bool flag=M.cancel(s1, s2);
            if (flag == false)
                cout << "取消航班失败" << endl;
            else{
                cout<<"操作成功"<<endl;
                M.show_seat(s1);
            }
        }
        else if(operation==4){

            if(Role==2){
                cout<<"您没有此权限"<<endl;
                continue;
            }
            else{
                int next;
                cout<<"请选择"<<endl;
                cout<<"1.增加航班"<<endl;
                cout<<"2.删除航班"<<endl;
                cin>>next;
                if(next==1){
                    string s1;
                    cout<<"需要增加的航班信息"<<endl;
                    char f[100];
                    char t[100];
                    char o[100];
                    char d[100];
                    int p;
                    int x;
                    int y;
                    int a;
                    int m1,d1,h1,mi1;
                    int m2,d2,h2,mi2;
                    cout<<"航班号:";
                    cin>>f;
                    cout<<"飞机类型:";
                    cin>>t;
                    cout<<"出发地:";
                    cin>>o;
                    cout<<"目的地:";
                    cin>>d;
                    int flag=0;
                    while(flag==0){
                        cout<<"出发时间:";
                        cin>>m1>>d1>>h1>>mi1;
                        while(C.time(Time(m1,d1,h1,mi1))==false){
                            cout<<endl;
                            cout<<"时间不合法,请重新输入:";
                            cin>>m1>>d1>>h1>>mi1;
                        }
                        cout<<"到达时间:";
                        cin>>m2>>d2>>h2>>mi2;
                        while(C.time(Time(m2,d2,h2,mi2))==false){
                            cout<<endl;
                            cout<<"时间不合法,请重新输入:";
                            cin>>m2>>d2>>h2>>mi2;
                        }
                        if(C.time(Time(m1,d1,h1,mi1),Time(m2,d2,h2,mi2))==false){
                            cout<<"出发时间比到达时间晚,请重新输入"<<endl;
                        }
                        else
                            flag=1;
                    }
                    cout<<"价格:";
                    cin>>p;
                    cout<<"机舱行数 列数:";
                    cin>>x>>y;
                    if(M.Add(f, t, o, d, Time(m1, d1, h1, mi1), Time(m2, d2, h2, mi2), p, x,y,0)==true){
                        M.Query();
                    }
                    else{
                        cout<<"添加失败"<<endl;
                    }
                }
                else{
                    string number;
                    cout<<"需要删除的机舱号:";
                    cin>>number;
                    M.Delete(number);
                    M.Query();
                }
            }
        }
        else if(operation==5){
                if(Role==2){
                    cout<<"您没有此权限:"<<endl;
                    continue;
                }
                else{
                    int next;
                    cout<<"请选择:"<<endl;
                    cout<<"1.查询所有乘客信息:"<<endl;
                    cout<<"2.查询某个航班乘客信息:"<<endl;
                    cout<<"3.查询单个乘客信息:"<<endl;
                    cin>>next;
                    if(next==1)
                        p_query();
                    else if(next==2){
                        string s;
                        cout<<"输入航班号"<<endl;
                        cin>>s;
                        M.show_passenger(s);
                    }
                    else if(next==3){
                        string s;
                        cout<<"输入所要查询乘客额护照号:";
                        cin>>s;
                        p_query(s);
                    }
                }
        }
    }
}
int main()
{
    Init();
    Init2();
    Operation();
    return 0;
}

sha256.h

/*
*   SHA-256 implementation, Mark 2
*
*   Copyright (c) 2010,2014 Ilya O. Levin, http://www.literatecode.com
*
*   Permission to use, copy, modify, and distribute this software for any
*   purpose with or without fee is hereby granted, provided that the above
*   copyright notice and this permission notice appear in all copies.
*
*   THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
*   WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
*   MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
*   ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
*   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
*   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
*   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef SHA256_H_
#define SHA256_H_

#include <stddef.h>
#ifdef _MSC_VER
#ifndef uint8_t
typedef unsigned __int8 uint8_t;
#endif
#ifndef uint32_t
typedef unsigned __int32 uint32_t;
#endif
#else
#include <stdint.h>
#endif

#define SHA256_BYTES    32

#ifdef __cplusplus
extern "C"
{
#endif

typedef struct {
    uint8_t  buf[64];
    uint32_t hash[8];
    uint32_t bits[2];
    uint32_t len;
} sha256_context;

void sha256_init(sha256_context *ctx);
void sha256_hash(sha256_context *ctx, const void *data, size_t len);
void sha256_done(sha256_context *ctx, uint8_t *hash);

void sha256(const void *data, size_t len, uint8_t *hash);

#ifdef __cplusplus
}
#endif

#endif

原文链接: https://www.cnblogs.com/zuoyou151/p/9199347.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/276259

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月15日 上午1:36
下一篇 2023年2月15日 上午1:36

相关推荐