ATM项目

atm.h(头文件)

#pragma once

typedef struct BankCard {//银行卡类型设计
    int id;
    int password;
    double money;
}BankCard;

typedef struct Array {//数组封装  int*arr=(int*)malloc(sizeof(int)*n) ;
    //arr[0] = 10;《 ==》  *(arr+0)=10      int arr[10]={0};
    BankCard *cards;//用户群体不确定,需要时可以动态开辟
    int size;//记录有效数据个数
    int length;//记录当前数组的cards的大小
}Array,*PArr;

//函数声明
void init(PArr parray,int len);//初始化Array   属性初始化
int  search_card(PArr parray,int id, int password);//查找卡
bool login_card(PArr parray,int id, int password);//登录功能
void after_login_success(PArr parray,int index);//登录成功后 存款 ,取款...

bool if_full(PArr parray);//对数组进行判满操作
void grow(PArr parray);//若数组已满,扩容1.5倍
bool register_card(PArr parray,int id,int password);//注册功能  -> 给数组中添加卡

void destory(PArr parray);//销毁函数->释放init()函数 malloc的堆内存

atm.cpp

#define  _CRT_SECURE_NO_WARNINGS
#include"atm.h"
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>

//函数声明
//初始化Array
void init(PArr parray,int len)
{
    //(*parray).cards
    assert(parray != NULL);
    parray->cards = (BankCard*)malloc(sizeof(BankCard));
    assert(parray->cards != NULL);
    parray->size = 0;//有效卡的个数  最初是0
    parray->length = len;//初始化  数组本身大小 len
}

//查找卡:卡在数组中,遍历数组
//返回卡所在的下标 若不存在  返回-1
int search_card(PArr parray, int id, int password)
{    
    int i = 0;
    for (;i < parray->size;i++) {
        BankCard curcard = parray->cards[i];//指针指向数组的i号下标数据
        if (curcard.id == id && curcard.password == password) {
            return i;
        }
    }
    return  -1;
}

//登录功能
bool login_card(PArr parray, int id, int password)
{
    assert(parray != NULL);
    int index = search_card(parray, id, password);
    return index;//< 0 ? false:true;
}

//登录成功后的操作
void after_login_success(PArr parray, int index)

{
    int choice,id,password,user_index;
    double money;
    while (true) {
        printf("1.存款  2.取款  3.转账 4.查询余额  5.退卡");
        scanf("%d", &choice);
        if (choice == 5) {
            break;
        }
        switch (choice) {
        case 1:
            printf("请输入存款金额:");
            scanf("%lf", &money);//当前登录成功的卡 cards[index].money+=money
            parray->cards[index].money += money;
            printf("存款成功\n");
            break;
        case 2:
            printf("请输入取款金额:");
            scanf("%lf", money);
            if (parray->cards[index].money >= money) {
                (*parray).cards[index].money -= money;
                printf("取款成功");
            }
            else {
                printf("取款失败\n");   
            }
            break;
        case 3://转账
            printf("请输入对方账号:");
            scanf("%d", &id);
            printf("请输入对方密码:");
            scanf("%d", &password);//验证是否存在该用户
            user_index=search_card(parray, id, password);
            if (user_index == -1) {
                printf("不存在该用户信息\n");
            }
            else {//存在该卡,是当前用户user_index
                printf("请输入转账金额:");
                scanf("%lf", &money);
                //当前登录成功的卡index-=money  对方卡user_index+=money
                if (parray->cards[index].money < money) {
                    printf("余额不足,转账失败\n");
                }
                else {
                    parray->cards[index].money -= money;
                    parray->cards[user_index].money += money;
                    printf("转账成功\n");
                }
            }
            break;
        case 4:
            printf("当前卡的余额为: %lf\n", parray->cards[index].money);
            break;
        }
    }
}

//for (int i = 0;i < parray->size;i++) //指向符优先级比算术运算符高{
//if (parray->cards[i].id == id &&parray-> cards[i].password == password) {
//    printf("登录成功!");
//    continue;
//}
//}
//printf("登录失败,请重新输入账号或密码");
//exit(0);
//}

//对数组进行判满操作
bool is_full(PArr parray)
{
    assert(parray != NULL);
    return parray->size == parray->length;
    /*if (parray->length == parray->size)
        return true;
    return false;*/
}

//若数组已满,扩容1.5倍
void grow(PArr parray)
{
    assert(parray != NULL);
    int newlength = parray->length +( parray->length>>1);
    parray->cards = (BankCard*)realloc(parray->cards, newlength);
    assert(parray->cards != NULL);// 思考:若扩容失败 parray->c
    /*if (if_full) {
        int* PArr = (int*)malloc(sizeof(int) * 1.5);
        assert(PArr != NULL);
    }
        */
}

//注册功能  -> 给数组中添加卡
bool register_card(PArr parray, int id, int password)
{
    assert(parray != NULL);
    //数组容量是否已满,看size是否等于length
    if (is_full(parray)) {//若数组满,扩容 1.5倍
        grow(parray);
    }
    //若数组中存在id,password 卡  ,注册失败
    if (search_card(parray, id, password)) {
        return false;
    }
    //数组length 足够使用
    BankCard card = { id,password,0.0 };
    parray->cards[parray->size] = card;
    parray->size++;
    return true;
}

    /*BankCard cards = new BankCard(id,password);
    if (search_card != NULL) {
        printf("注册失败!");
        return NULL;
    }
    return card;*/


//销毁函数->释放init()函数 malloc的堆内存
void destory(PArr parray)
{
    if (parray == NULL) {
        return;
    }
    free(parray->cards);//释放内存
    parray->cards = NULL;//置空
}

tset.cpp(测试文件)

#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include"atm.h"
#define INITCAPACITY 10

void menu() {
    printf("-------欢迎进入ATM机系统-------\n");
    printf("请选择:(1—3\n");
    printf("-------1.登录----------\n");
    printf("-------2.注册----------\n");
    printf("-------3.退出----------\n");
}

void show() {
    
        struct Array array;//结构体变量{size,length}
        init(&array, INITCAPACITY);//对结构体变量中的属性做初始化
        int choice, id, password,index;
        
        while (true) {
            menu();
            //从键盘输入  选择1-3
        scanf("%d", &choice);
        if (choice == 3) {
            break;
        }
        switch (choice)
        {
        case 1:
            printf("请输入登录的账号和密码:");
            scanf("%d", &id);
            scanf("%d", &password);
            index = login_card(&array, id, password);//传指针  解引用  通过形参修改实参
            if (index != -1) {
                printf("登录成功\n");
                //此时登录成功后的操作:
                //存款、取款、转账、查询余额、退卡
                after_login_success(&array,index);//存款 对登录成功的卡.money+=1000
            }
            else {
                printf("登录失败\n");
            }
            break;
        case 2:
            printf("请输入登录的账号和密码:");
            scanf("%d", id);
            scanf("%d", password);
            if (register_card(&array, id, password)) {
                printf("注册成功\n");
            }
            else {
                printf("注册失败\n");
            }
            break;
        default:
            break;
        }
    }
    destory(&array);//释放malloc内存
}

int main() {
    show();
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sweep-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值