订单管理系统,大学生数据结构期末作业/C语言实践作业

任务:订单管理系统的设计与实现

设计并实现一个订单管理系统

界面分成两部分,分别是管理员和用户的界面

主要功能:

用户:

        (1)用户的登录及注册

        (2)用户信息修改

        (3)购买物品

        (4)充值

        (5)升序排序(按金额)物品

管理员:

        (1)显示所有订单

        (2)插入订单信息

        (3)删除订单信息

        (4)排序订单(快排按编号)

        (5)统计订单信息

        (6)添加物品


注意事项:
一共需要建立4个文件(key.txt、物品清单.txt、用户信息.txt、用户账号.txt),其中只有key.txt需要提前输入东西,其他的都可以通过后面的输入添加到文件中,代码部分需要提前将文件的存储路径进行修改

密码:密码统一是6位的,并且直接输入6位就可以,不需要输入回车或者空格

物品清单的输入:先进入管理员界面,输入7,根据提示输入即可

用户信息的输入:这个是和用户账号相对应的,当你注册一个新账号时 会自动保存到用户信息里

用户账号的输入:先输入用户,然后选择注册,输入账号及密码,输入完成后,会提示继续完善信息,注意请输入姓名必须和之前的账号一致,其他的随意。

学校的学期末大作业感觉好水啊......虽然小蒟蒻自己写的代码没啥健壮性而且代码重复率比较高......

#define _CRT_SECURE_NO_WARNINGS // 解决scanf不安全问题
#include <stdio.h>
#include <windows.h>
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <conio.h>
#include <map>
#include <string>
#define MAX_SIZE 100
using namespace std;
struct item
{
    char name[100];
    int id;
    int me;
};
int itemlen;
typedef struct User
{                     // 用户信息
    char name[100];   // 姓名
    char phnum[20];   // 手机号
    char address[50]; // 地址
    int num;
    int f;
    char itemname[100];
} User, *user;
struct node
{
    char name[100];
    char password[100];
    int m;
};
void meun1() // 用户界面
{
    printf("\t\t\t\t 1、修改用户人的信息\n");
    printf("\t\t\t\t---------------\t\t\t---------------\n");
    printf("\t\t\t\t 2、查看物品\n");
    printf("\t\t\t\t---------------\t\t\t---------------\n");
    printf("\t\t\t\t 3、充值\n");
    printf("\t\t\t\t---------------\t\t\t---------------\n");
    printf("\t\t\t\t 4、排序物品\n");
    printf("\t\t\t\t---------------\t\t\t---------------\n");
    printf("\t\t\t\t 0、退出 \n");
}
void insert_sort()//插入排序
{
    FILE *fp;
    struct item p[MAX_SIZE];
    fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\物品清单.txt", "r");
    int i = 0;
    while (fscanf(fp, "%d %s %d", &p[i].id, p[i].name, &p[i].me) != EOF)
        i += 1;
    fclose(fp);
    int n = i;
    for (i = 1; i < n; i++)
    {
        auto u = p[i];
        int op = 0;
        for (int j = i - 1; j >= 0; j--)
        {
            if (u.me >= p[j].me)
            {
                op = j + 1;
                break;
            }
            p[j + 1] = p[j];
        }
        p[op] = u;
    }

    cout << "已完成排序\n";
    for (int j = 0; j < i; j++)
    {
        printf("%-5d %-8s %-5d\n", p[j].id, p[j].name, p[j].me);
        // cout <<p[j].id << p[j].name << " " << p[j].me << endl;
    }
    fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\物品清单.txt", "w");
    for (int j = 0; j < i; j++)
    {
        fprintf(fp, "%d %s %d\n", p[j].id, p[j].name, p[j].me);
    }
    fclose(fp);
}
void meun1_1(char name[]) // 用户信息及修改
{
    /*cout << name << endl;*/
    FILE *fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户信息.txt", "r"),
         *fp2 = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户账号.txt", "r");
    struct User user[MAX_SIZE];
    struct node pass[MAX_SIZE];
    char password[100];

    int i = 0, j, flag = 0, k = 0, ppp;
    while (fscanf(fp2, "%s %s", pass[k].name, pass[k].password) != EOF)
    {
        k += 1;
    }
    while (fscanf(fp, "%d %s %s %s %s", &user[i].num, user[i].name, user[i].phnum, user[i].address, user[i].itemname) != EOF)
    {
        i += 1;
    }

    for (ppp = 0; ppp <= k; ppp++)
    {
        if (strcmp(name, pass[ppp].name) == 0)
            break;
    }

    for (j = 0; j <= i; j++)
    {
        if (strcmp(name, user[j].name) == 0)
            break;
    }

    fclose(fp);
    fclose(fp2);

    // cout << "11111 " << i << endl;
    fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户信息.txt", "w");
    fp2 = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户账号.txt", "w");
    char se[10];

    printf("密码是否更改 Y/N\n");
    scanf("%s", se);
    if (se[0] == 'Y' || se[0] == 'y')
    {
        printf("请输入密码:");
        int i = 0;
        while (i < 6)
        {
            char c = _getch();
            pass[ppp].password[i++] = c;
            printf("*");
        }
        pass[ppp].password[6] = '\0';
        printf("是否显示密码(Y/N):");
        cin >> se;
        if (se[0] == 'Y' || se[0 == 'y'])
            cout << pass[ppp].password << endl;
        // scanf("%s", pass[ppp].password);
    }

    printf("\n电话是否更改 Y/N\n");
    scanf("%s", se);
    if (se[0] == 'Y' || se[0] == 'y')
    {
        printf("请输入电话号码\n");
        while (true)
        {
        scanf("%s", user[j].phnum);
        if (strlen(user[j].phnum) != 11 || user[j].phnum[0] != '1')
            printf("输入电话格式错误,请重新输入\n");
        else
            break;
        }
    }

    printf("\n住址是否更改 Y/N\n");
    scanf("%s", se);
    if (se[0] == 'Y' || se[0] == 'y')
    {
        printf("请输入地址\n");
        scanf("%s", user[j].address);
    }
    // printf("修改成功\n");

    for (j = 0; j < i; j++)
    {
        fprintf(fp, "%d %s %s %s %s\n", user[j].num, user[j].name, user[j].phnum, user[j].address, user[j].itemname);
    }
    for (j = 0; j < k; j++)
    {
        fprintf(fp2, "%s %s\n", pass[j].name, pass[j].password);
    }
    // for (j = 0; j < i; j++)
    //{
    //     printf("%s %s %s\n", p[j].name, p[j].phnum, p[j].address);
    // }

    printf("修改成功\n");
    fclose(fp);
    fclose(fp2);
}
void meun1_2(char name[]) // 物品清单及购买
{
    FILE *fp, *fp3;
    struct item p[MAX_SIZE];
    struct node p3[MAX_SIZE];
    int i = 0, ioi = 0;
    ;
    fp3 = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户账号.txt", "r");

    while (fscanf(fp3, "%s %s %d", p3[ioi].name, p3[ioi].password, &p3[ioi].m) != EOF)
        ioi++;
    fclose(fp3);

    int curid = 0, cur = 0;
    for (int ui = 0; ui <= ioi; ui += 1)
    {
        if (strcmp(p3[ui].name, name) == 0)
        {
            cur = p3[ui].m;
            curid = ui;
            break;
        }
    }

    fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\物品清单.txt", "r");
    while (fscanf(fp, "%d %s %d", &p[i].id, p[i].name, &p[i].me) != EOF)
        i++;
    for (int j = 0; j < i; j += 1)
        printf("%-8d\t |%-8s\t |%-8d元 \t\n", p[j].id, p[j].name, p[j].me);
    fclose(fp);
    printf("\n\n请输入要购买的商品的编号(退出请按0):\n\n");
    int s;
    scanf("%d", &s);

    fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户信息.txt", "r");
    struct User p1[MAX_SIZE];
    int k = 0;
    while (fscanf(fp, "%d %s %s %s %s", &p1[k].num, p1[k].name, p1[k].phnum, p1[k].address, p1[k].itemname) != EOF)
    {
        k += 1;
    }
    int people = 0;
    for (int ppp = 0; ppp <= k; ppp++)
    {
        if (strcmp(p1[ppp].name, name) == 0)
        {
            people = ppp;
            break;
        }
    }
    fclose(fp);
    if (s == 0)
        return;
    char op[100];
    int j = 0;
    int opcurm = 0;
    for (j = 0; j < i; j++)
    {
        if (p[j].id == s)
        {
            char se[100];
            printf("确定购买该商品吗(Y/N):");
            scanf("%s", se);

            if (se[0] == 'N')
                return;
            if (cur < p[j].me)
            {
                cout << "余额不足,已退出\n";
                return;
            }
            else
            {
                if (strcmp(p1[people].name, p1[people].itemname) != 0)
                {
                    int mp = 0;
                    for (int l = 0; l < i; l++)
                    {
                        if (strcmp(p[l].name, p1[people].itemname) == 0)
                        {
                            p3[curid].m += p[l].me;
                            break;
                        }
                    }
                }
                p3[curid].m = p3[curid].m - p[j].me;
            }
            strcpy(op, p[j].name);
            break;
        }
    }
    if (j == i)
    {
        cout << "不存在该商品,请下次选择\n";
        return;
    }

    for (int ppp = 0; ppp < k; ppp++)
    {
        if (strcmp(p1[ppp].name, name) == 0)
        {
            strcpy(p1[ppp].itemname, op);
            break;
        }
    }
    fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户信息.txt", "w");

    for (int l = 0; l < k; l++)
    {
        fprintf(fp, "%d %s %s %s %s\n", p1[l].num, p1[l].name, p1[l].phnum, p1[l].address, p1[l].itemname);
    }
    printf("购买成功\n");
    fp3 = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户账号.txt", "w");
    for (int ui = 0; ui < ioi; ui++)
    {
        fprintf(fp3, "%s %s %d\n", p3[ui].name, p3[ui].password, p3[ui].m);
    }
    fclose(fp3);
    fclose(fp);
}
bool login(char mn[])//用户登录
{
    struct node p[MAX_SIZE];
    char name[100], password[100];
    printf("输入账号:");
    scanf("%s", name);
    strcpy(mn, name);
    printf("输入密码:");
    int pppp = 0;
    while (pppp < 6)
    {
        char c = _getch();
        password[pppp++] = c;
        printf("*");
    }
    password[6] = '\0';
    // scanf("%s", password);
    FILE *fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户账号.txt", "r");
    int i = 0, j;
    while (fscanf(fp, "%s %s %d", p[i].name, p[i].password,&p[i].m) != EOF)
        i += 1;
    fclose(fp);
    for (j = 0; j < i; j++)
    {
        if (strcmp(p[j].name, name) == 0 && strcmp(p[j].password, password) == 0)
        {
            printf("密码正确,已成功登录\n\n");
            return 1;
        }
    }
    printf("账号或密码不正确\n\n");
    return 0;
}
bool sign(char mn[])//用户注册
{
    struct node p[MAX_SIZE];
    char name[100], password[100];
    printf("输入账号:");
    scanf("%s", name);
    strcpy(mn, name);
    printf("输入密码:");
    int pppp = 0;
    while (pppp < 6)
    {
        char c = _getch();
        password[pppp++] = c;
        printf("*");
    }
    password[6] = '\0';
    // scanf("%s", password);
    FILE *fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户账号.txt", "r");
    int i = 0, j;
    while (fscanf(fp, "%s %s %d", p[i].name, p[i].password, &p[i].m) != EOF)
        i += 1;
    fclose(fp);
    for (j = 0; j <= i; j++)
    {
        if (strcmp(p[j].name, name) == 0)
        {
            printf("\n\n该账号已存在\n");
            fclose(fp);
            return 0;
        }
    }
    fclose(fp);
    strcpy(p[i].name, name);
    strcpy(p[i].password, password);
    p[i].m = 0;
    fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户账号.txt", "w");
    for (j = 0; j <= i; j++)
        fprintf(fp, "%s %s %d\n", p[j].name, p[j].password, p[j].m);
    fclose(fp);
    printf("\n\n已成功创建\n");
    return 1;
}
void meun1_3(char name[])//用户充值
{
    struct node p[MAX_SIZE];
    FILE *fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户账号.txt", "r");
    int i = 0, j;
    while (fscanf(fp, "%s %s %d", p[i].name, p[i].password, &p[i].m) != EOF)
    {
        i += 1;
    }
    fclose(fp);
    printf("请输入要充值的金额,退出请按0\n");
    int n;
    cin >> n;
    if (n == 0)
        return;

    for (j = 0; j < i; j++)
    {
        if (strcmp(p[j].name, name) == 0)
        {
            int cnt = 0;
            while (cnt < 3)
            {
                cnt += 1;
                char op[100];
                printf("请输入密码:");
                int i = 0;
                while (i < 6)
                {
                    char c = _getch();
                    op[i++] = c;
                    printf("*");
                }
                op[6] = '\0';
                // scanf("%s", op);
                if (strcmp(p[j].password, op) == 0)
                {
                    p[j].m += n;
                    break;
                }
                else
                {
                    printf("当前密码错误,请重试\n");
                }
                
            }
            if (cnt == 3)
            {
                printf("密码测试失败,未充值\n");
            }
            else
                printf("\n\n当前共有%d元\n\n", p[j].m);
            break;
        }
    }
    fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户账号.txt", "w");
    for (j = 0; j < i; j++)
    {
        fprintf(fp, "%s %s %d\n", p[j].name, p[j].password, p[j].m);
    }
    fclose(fp);
}
int meoney(char name[])//用户金额实时显示
{
    struct node p[MAX_SIZE];
    FILE *fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户账号.txt", "r");
    int i = 0, j;
    while (fscanf(fp, "%s %s %d", p[i].name, p[i].password, &p[i].m) != EOF)
    {
        if (strcmp(name, p[i].name) == 0)
        {
            fclose(fp);
            return p[i].m;
        }
        i += 1;
    }
}
void itemcout(char name[])//用户当前购买产品
{
    struct User p[MAX_SIZE];
    FILE *fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户信息.txt", "r");
    int i = 0;
    while (fscanf(fp, "%d %s %s %s %s", &p[i].num, p[i].name, p[i].phnum, p[i].address, p[i].itemname) != EOF)
    {
        if (strcmp(p[i].name, name) == 0)
        {
            if (strcmp(p[i].name, p[i].itemname) == 0)
                cout << "当前没有购买物品\n";
            else
                printf("当前购买了:%s\n\n", p[i].itemname);
            fclose(fp);
            break;
        }
        i++;
    }
    fclose(fp);
}
void MEUN11() // 用户主程序
{
    char name[100];
    printf("1.注册\t\t\t2.登录\n");
    int op;
    int flag = 0;
    int cur = 0;
    while (cin >> op)
    {
        if (op == 1)
        {
            flag = sign(name);
            if (flag == 1)
                flag += 1;
            break;
        }
        else if (op == 2)
        {
            flag = login(name);
            break;
        }
        else
        {
            printf("请重新输入\n");
        }
    }
    printf("\n按回车键继续\n");
    getchar();
    getchar();
    system("cls");
    if (flag == 0)
        return;
    if (flag == 2)
    {
        printf("请您继续完善身份信息\n");

        char op1[100], op2[100], op3[100];

        printf("请输入姓名\n");
        scanf("%s", op1);
        printf("请输入电话\n");
        while (true)
        {
        scanf("%s", op2);
        if (strlen(op2) != 11 || op2[0] != '1')
            printf("不正确的电话号码,请重新输入\n");
        else
            break;
        }
        printf("请输入住址\n");
        scanf("%s", op3);

        struct User p[MAX_SIZE];

        FILE *fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户信息.txt", "r");
        // cout <<"$#$#$#$ " << endl;
        int i = 0, j;
        int m = 10000;
        while (fscanf(fp, "%d %s %s %s %s", &p[i].num, p[i].name, p[i].phnum, p[i].address, p[i].itemname) != EOF)
        {

            m = max(m, p[i].num);
            i += 1;
        }

        fclose(fp);
        p[i].num = m + 1;
        strcpy(p[i].name, op1);

        strcpy(p[i].phnum, op2);

        strcpy(p[i].address, op3);

        strcpy(p[i].itemname, op1);

        fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户信息.txt", "w");
        for (j = 0; j <= i; j++)
        {

            fprintf(fp, "%d %s %s %s %s\n", p[j].num, p[j].name, p[j].phnum, p[j].address, p[j].itemname);
        }
        fclose(fp);
    }

    while (true)
    {
        meun1();
        cur = meoney(name);
        itemcout(name);
        printf("当前余额:%d元\n", cur);
        int n;
        cin >> n;
        switch (n)
        {
        case 1:
            meun1_1(name);
            break;
        case 2:
            meun1_2(name);
            break;
        case 3:
            meun1_3(name);
            break;
        case 4:
            insert_sort();
            break;
        case 0:
            return;
        default:
            printf("错误输入\n");
        }
        printf("按回车键继续\n");
        getchar();
        getchar();
        system("cls");
    }
}
int meun22_1lenth() // 用户的长度统计
{
    struct User p[MAX_SIZE];
    FILE *fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户信息.txt", "r");
    int i = 0, j;
    while (fscanf(fp, "%d %s %s %s %s", &p[i].num, p[i].name, p[i].phnum, p[i].address, p[i].itemname) != EOF)
        i += 1;
    fclose(fp);
    return i;
}
void q_sort(struct User p[], int l, int r)//快速排序
{
    int i = l, j = r;
    if (l >= r)
        return;
    int mid = (l + r) / 2;
    do
    {
        while (p[i].num < p[mid].num)
            i += 1;
        while (p[j].num > p[mid].num)
            j -= 1;
        if (i <= j)
        {
            swap(p[i], p[j]);
            i++;
            j--;
        }
    } while (i <= j);
    q_sort(p, l, j);
    q_sort(p, i, r);
}
void MEUN22() // 管理员界面
{
    printf("\t\t\t\t\t1.显示订单信息\n");
    printf("\t\t\t\t\t--------------\n");
    printf("\t\t\t\t\t2.插入订单信息\n");
    printf("\t\t\t\t\t--------------\n");
    printf("\t\t\t\t\t3.删除订单信息\n");
    printf("\t\t\t\t\t--------------\n");
    printf("\t\t\t\t\t4.排序订单信息(利用快速排序按照订单号排序)\n");
    printf("\t\t\t\t\t--------------\n");
    printf("\t\t\t\t\t5.统计当前表中的订单个数\n");
    printf("\t\t\t\t\t--------------\n");
    printf("\t\t\t\t\t6.查找订单信息\n");
    printf("\t\t\t\t\t--------------\n");
    printf("\t\t\t\t\t7.添加物品\n");
    printf("\t\t\t\t\t--------------\n");
    printf("\t\t\t\t\t0.退出 \n");
}
void meun22_1() // 显示订单信息
{
    struct User p[MAX_SIZE];
    FILE *fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户信息.txt", "r");
    int i = 0, j;
    while (fscanf(fp, "%d %s %s %s %s", &p[i].num, p[i].name, p[i].phnum, p[i].address, p[i].itemname) != EOF)
        i += 1;
    if (i == 0)
    {
        cout << "当前文件中并未保存信息\n";
        return;
    }
    //cout << i << endl;
    for (j = 0; j < i; j++)
    {
        printf("%-8d %-8s %-13s %-13s ", p[j].num, p[j].name, p[j].phnum, p[j].address);
        if (strcmp(p[j].name, p[j].itemname) == 0)
            printf("%-13s", "该用户未购买商品");
        else
            printf("%-13s", p[j].itemname);
        cout << endl;
    }

    printf("\n\n\n");
    fclose(fp);
}
void save_1(struct User p[])//快排前置操作
{
    int i = 0, j;
    FILE *fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户信息.txt", "r");
    while (fscanf(fp, "%d %s %s %s %s", &p[i].num, p[i].name, p[i].phnum, p[i].address, p[i].itemname) != EOF)
        i += 1;
    fclose(fp);
}
void save(struct User p[], int cnt)//快排后置操作
{
    FILE *fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户信息.txt", "w");
    for (int i = 0; i <= cnt; i += 1)
    {
        fprintf(fp, "%d %s %s %s %s\n", p[i].num, p[i].name, p[i].phnum, p[i].address, p[i].itemname);
    }
    fclose(fp);
}
void meun22_3() // 删除订单
{
    int i = 0, j;
    struct User p[MAX_SIZE];
    FILE *fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户信息.txt", "r");
    while (fscanf(fp, "%d %s %s %s %s", &p[i].num, p[i].name, p[i].phnum, p[i].address, p[i].itemname) != EOF)
        i += 1;
    fclose(fp);

    meun22_1(); // 显示订单
    printf("输入要删除第几条订单\n");
    int x, flag = 0;
    scanf("%d", &x);
    x--;
    fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户信息.txt", "w");

    for (int k = 0; k < i; k++)
    {
        if (k == x)
            continue;
        fprintf(fp, "%d %s %s %s %s\n", p[k].num, p[k].name, p[k].phnum, p[k].address, p[k].itemname);
    }

    fclose(fp);
}
void meun22_2() // 插入订单信息
{
    int i = 0, j;
    struct User p[MAX_SIZE];
    FILE *fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户信息.txt", "r");
    while (fscanf(fp, "%d %s %s %s %s", &p[i].num, p[i].name, p[i].phnum, p[i].address, p[i].itemname) != EOF)
        i += 1;
    fclose(fp);

    int x;
    printf("输入要插入的位置");
    scanf("%d", &x);
    x--;
    for (int k = i; k >= x; k -= 1)
        p[k + 1] = p[k];

    i += 1;

    printf("输入订单编号,客户姓名,客户电话号码,客户地址,客户订单\n");

        int a1;
        char s2[100], s3[100], s4[100], s5[100];
    while (true)
    {


        scanf("%d %s %s %s %s", &a1, s2, s3, s4, s5);
        int flag = 0;
        for (j = 0; j < i; j++)
        {
            if (a1 == p[j].num)
            {
                flag = 1;
                cout << "订单号重复\n";
            }
            if (strcmp(p[j].name, s2) == 0)
            {
                flag = 1;
                cout << "用户姓名重复\n";
            }
            if (strcmp(p[j].phnum, s2) == 0)
            {
                flag = 1;
                cout << "用户电话重复\n";
            }
        }
        if (!flag)
        {
            cout << "\n\n已成功添加\n";
            break;
        }
    
    }
    p[x].num = a1;
    strcpy(p[x].name,s2);
    strcpy(p[x].phnum, s3);
    strcpy(p[x].address, s4);
    strcpy(p[x].itemname, s5);

    //scanf("%d %s %s %s %s", &p[x].num, p[x].name, p[x].phnum, p[x].address, p[x].itemname);



    fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户信息.txt", "w");
    for (int k = 0; k < i; k++)
        fprintf(fp, "%d %s %s %s %s\n", p[k].num, p[k].name, p[k].phnum, p[k].address, p[k].itemname);

    printf("完成插入\n");
    fclose(fp);
}
void findd()
{
    cout << "当前使用顺序查找\n\n";

    int i = 0, j;
    struct User p[MAX_SIZE];
    FILE* fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户信息.txt", "r");
    while (fscanf(fp, "%d %s %s %s %s", &p[i].num, p[i].name, p[i].phnum, p[i].address, p[i].itemname) != EOF)
        i += 1;
    fclose(fp);
    printf("输入订单的编号\n");
    int id;
    scanf("%d", &id);
    for (j = 0; j < i; j++)
    {
        if (p[j].num == id)
        {
            printf("%d %s %s %s %s\n", p[j].num, p[j].name, p[j].phnum, p[j].address, p[j].itemname);
            return;
        }
    }
    cout << "查找失败";
}
void heap()//哈希查找(备用方案)
{
    map<string, int > heap;
    int i = 0, j;
    struct User p[MAX_SIZE];
    FILE* fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户信息.txt", "r");
    while (fscanf(fp, "%d %s %s %s %s", &p[i].num, p[i].name, p[i].phnum, p[i].address, p[i].itemname) != EOF)
        i += 1;
    fclose(fp);

    for (j = 0; j < i; j++)
    {
        heap[to_string(p[i].num)] = j;
        heap[p[j].name] = j;
        heap[p[j].phnum] = j;
        heap[p[j].address] = j;
        heap[p[j].itemname] = j;
    }

    cout << "请输入要查找的信息";
    char se[100];
    cin >> se;
    if (heap[se] == 0)
    {
        cout << "请检查输入,并未查找到该用户的相关信息";
        return;
    }
    else
    {
        int l = heap[se];
        printf("%d %s %s %s %s\n", p[l].num, p[l].name, p[l].phnum, p[l].address, p[l].itemname);
    }
    return;
}
void meun22_6()//二分查找订单编号
{
    printf("共支持以下查找方式\n");
    printf("1. 按编号查找、2. 按姓名查找、3. 按手机号查找、4. 按地址查找、5. 按购买物查找\n\n");



    printf("请输入要查找的内容的编号:");
    int i = 0, j;
    struct User p[MAX_SIZE];
    FILE *fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\用户信息.txt", "r");
    while (fscanf(fp, "%d %s %s %s %s", &p[i].num, p[i].name, p[i].phnum, p[i].address, p[i].itemname) != EOF)
        i += 1;
    fclose(fp);
    int u;
    while (true)
    {
        cin >> u;
        if (u == 1)
        {
            break;
        }
        else if (u == 2)
        {
            printf("\n\n请输入用户姓名:");
            char c[100];
            scanf("%s", c);
            for (j = 0; j < i; j++)
            {
                if (strcmp(p[j].name, c) == 0)
                {
                    int l = j;
                    printf ("\n\n该用户的信息为:\n");
                    printf("%-8d %-10s %-10s %-10s", p[l].num, p[l].name, p[l].phnum, p[l].address);
                    if (strcmp(p[j].name, p[j].itemname) == 0)
                        printf("%-13s\n", "该用户未购买商品");
                    else
                        printf("%-13s\n", p[j].itemname);
                    return;
                }
            }
            printf("未查找到该用户,请检查是否输入正确\n");
            return;
        }
        else if (u == 3)
        {
            printf("\n\n请输入用户电话号码:");
            char c[100];
            int flag = 0;
            scanf("%s", c);
            for (j = 0; j < i; j++)
            {
                if (strcmp(p[j].phnum, c) == 0)
                {
                    int l = j;
                    if (!flag)
                    printf("该用户的信息为:\n");
                    flag = 1;
                    printf("%-8d %-10s %-10s %-10s", p[l].num, p[l].name, p[l].phnum, p[l].address);
                    if (strcmp(p[j].name, p[j].itemname) == 0)
                        printf("%-13s\n", "该用户未购买商品");
                    else
                        printf("%-13s\n", p[j].itemname);
                }
            }
            if (!flag)
            printf("未查找到该用户,请检查是否输入正确\n");
            return;
        }
        else if (u == 4)
        {
            printf("\n\n请输入用户住址:");
            char c[100];
            int flag = 0;
            scanf("%s", c);
            for (j = 0; j < i; j++)
            {
                if (strcmp(p[j].address, c) == 0)
                {
                    int l = j;
                    if (!flag)
                        printf("该用户的信息为:\n");
                    flag = 1;
                    printf("%-8d %-10s %-10s %-10s", p[l].num, p[l].name, p[l].phnum, p[l].address);
                    if (strcmp(p[j].name, p[j].itemname) == 0)
                        printf("%-13s\n", "该用户未购买商品");
                    else
                        printf("%-13s\n", p[j].itemname);
                }
            }
            if (!flag)
                printf("未查找到该用户,请检查是否输入正确\n");
            return;
        }
        else if (u == 5)
        {
            printf("\n\n对于未购买物品的用户,请直接输入用户名\n\n");
            printf("请输入物品名:");
           
            char c[100];
            int flag = 0;
            scanf("%s", c);
            for (j = 0; j < i; j++)
            {
                if (strcmp(p[j].itemname, c) == 0)
                {
                    int l = j;
                    if (flag == 0)
                        printf("用户的信息为:\n");
                    flag = 1;
                    printf("%-8d %-10s %-10s %-10s %-10s\n", p[l].num, p[l].name, p[l].phnum, p[l].address,"用户未购买物品");
                }
            }
            if (!flag)
                printf("未查找到该用户,请检查是否输入正确\n");
            return;
        }
        else if (u == 0)
        {
            return;
        }
        else
        {
           cout << "不存在该方式\n";
        }
    }

    int flag = 0;
    for (j = 0; j < i-1; j++)
    {
        if (p[j].num > p[j + 1].num)
        {
            flag = 1;
            break;
        }
    }
    if (flag)
    {
        cout << "当前编号并不是有序的,是否进行排序(Y/N):";
        char se[10];
        cin >> se;
        if (se[0] == 'y' || se[0] == 'Y')
        {
            int cnt = meun22_1lenth() - 1;
            save_1(p);
            q_sort(p, 0, cnt);
            save(p, cnt);
        }
        else
        {
            findd();
            return;
        }

    }

    printf("输入订单的编号\n");
    int id;
    scanf("%d", &id);
    

    int l = 0, r = i;
    while (l < r)
    {

        int mid = l + r >> 1;
        if (p[mid].num >= id)
            r = mid;
        else
            l = mid + 1;
    }
    if (p[l].num == id)
    {
        printf("%d %s %s %s %s\n", p[l].num, p[l].name, p[l].phnum, p[l].address, p[l].itemname);
    }
    else
    {
        printf("查找失败\n");
    }
}

void meun22_7()//管理员添加物品
{
    struct item p[MAX_SIZE];
    int i = 0;
    FILE *fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\物品清单.txt", "r");
    while (fscanf(fp, "%d %s %d", &p[i].id, p[i].name, &p[i].me) != EOF)
        i += 1;
    fclose(fp);

    printf("输入物品的编号:");
    cin >> p[i].id;
    printf("输入物品的名称:");
    cin >> p[i].name;
    printf("输入物品的金额:");
    cin >> p[i].me;
    fp = fopen("C:\\Users\\25127\\Desktop\\cccc\\物品清单.txt", "w");
    for (int j = 0; j <= i; j++)
        fprintf(fp, "%d %s %d\n", p[j].id, p[j].name, p[j].me);
    fclose(fp);
}
void meun22() // 管理员界面
{
    struct User p[MAX_SIZE];
    int cnt = 0;
    while (true)
    {
        MEUN22();
        int n;
        cin >> n;
        switch (n)
        {
        case 1:
            meun22_1();
            break;

        case 2:
            meun22_2();
            break;

        case 3:
            meun22_3();
            break;

        case 4:
            cnt = meun22_1lenth() - 1;

            save_1(p);
            q_sort(p, 0, cnt);
            save(p, cnt);
            break;

        case 5:
            cnt = meun22_1lenth();
            printf("当前共有%d个订单\n", cnt);
            break;

        case 6:
            meun22_6();
            break;
        case 7:
            meun22_7();
            break;
        case 0:
            return;
            break;
        default:
            printf("错误输入\n");
        }
        printf("按回车键继续\n");
        getchar();
        getchar();
        system("cls");
    }
}
void fun1()//登录主程序
{
    FILE *pf;
    pf = fopen("C:\\Users\\25127\\Desktop\\cccc\\key.txt", "r");
    char p[100];
    fscanf(pf, "%s", p);
    char identity[100], password[100];
    printf("请输入您的身份(管理员 / 用户):");
    scanf("%s", identity);
    getchar();
    if (strcmp(identity, "用户") == 0)
    {
        MEUN11();
    }

    else if (strcmp(identity, "管理员") == 0 || strcmp(identity, "管理") == 0)
    {
        printf("请输入管理员密码:");
        char c;
        int i = 0;
        while (i < 6)
        {
            c = _getch();
            password[i++] = c;
            printf("*");
        }
        password[6] = '\0';

        if (strcmp(password, p) == 0)
        {
            printf("\n\n\n密码正确\n\n\n");
            printf("按回车进入下一界面\n\n");
            getchar();
            system("cls");
            meun22();
        }
        else
        {
            printf("密码错误,已退出\n");
        }
    }
    else
        printf("错误身份,已退出\n");
    fclose(pf);
}
int main()
{
    fun1();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值