实例三十八:银行卡问题

实例三十八:银行卡问题

问题描述:
某银行共发出 M 张储蓄卡,每张储蓄卡拥有唯一的卡号,每天每张储蓄卡至多允许持卡者进行 N 笔“存款”或“取款”业务。
A bank issued a total of M savings cards, each of which has a unique card number, and each of the savings cards per day allows the cardholder to perform N “deposit” or “withdrawal” services.
当持卡者输入正确的卡号、存款或取款金额后,程序进行相应的处理:若输入不正确的数据,程序会提示持卡者重新输入;当输入的卡号为负数时,银行终止当日业务。
After the cardholder enters the correct card number, deposit or withdrawal amount, the program performs corresponding processing: if incorrect data is entered, the program will prompt the cardholder to re-enter; when the entered card number is negative, the bank terminates the business on the day.

算法思路:

数组 card[M][N+3] 中的每一行存放一张储蓄卡的有关信息,其中:
card[i][0] 存放第 i 张卡的卡号;
card[i][1] 存放第 i 张卡的余额;
card[i][2] 存放第 i 张卡的当日业务实际发生笔数;
card[i][3] ~ card[i][N+2] 存放第 i 张卡的当日某笔存取款金额,正值代表存款,负值代表取款。
首先应初始化二维数组的每一行的前三个元素(分别代表每张储蓄卡的卡号、金额和当日已发生的业务笔数),接着我们就根据一次业务的发生过程编写程序:输入卡号 --> 判断卡号是否正确(为负数则结束当日业务) --> 求输入的卡号是第几张卡的(若不存在这样的卡号就结束本次业务) --> 输入存取钱的金额 --> 若为取钱判断金额是否够(金额不够则结束本次业务) --> 判断此卡的当天业务笔数是否已超过规定的笔数(超过则结束本次业务) --> 完成本次业务。
Each row in the array card[M][N+3] stores information about a savings card, where:
Card[i][0] stores the card number of the i-th card;
Card[i][1] stores the balance of the i-th card;
Card[i][2] The actual number of transactions on the day of the i-th card;
Card[i][3] ~ card[i][N+2] The amount of a deposit and withdrawal on the day of the i-th card, the positive value represents the deposit, and the negative value represents the withdrawal.
First, the first three elements of each row of the two-dimensional array should be initialized (representing the card number, amount, and number of transactions that have occurred on the current day).Then we write the program according to the process of a business.Enter the card number to determine --> whether the card number is correct. (If it is a negative number, end the business on the day) --> and ask for the card number that is the first card (if there is no such card number, the service will be terminated) --> and enter the amount of money to be exchanged. --> If the amount of money is sufficient, the amount of business on the day of the card has exceeded the specified number of pens (more than the end of the business) --> and the business is completed.

/*实例三十八:银行卡问题*/

#include <stdio.h>
#define M 6
#define N 5             /*初始化M张储蓄卡*/
long card[M][N+3]={{200501,2000,0},{200502,2000,2},{200401,3000,1},
{200402,3000,2},{200403,3000,3},{200503,2000,3}};
/*查找m张储蓄卡中哪一张的卡号是no,若都不是no,返回-1*/
int locate(long card[][N+3],int m,long no)
{
    int i;
    for(i=0;i<m;i++)
        if(card[i][0]==no)
            return -1;
    return 0;
}

int main(void)
{
    long cardNo,money;
    int k;
    while(1)
    {
        printf("Please enter the card number:\n");
        scanf("%ld",&cardNo);
        if(cardNo<0)break;
        k=locate(card,M,cardNo);
        if(k==-1)
        {
            printf("There is no savings card with %ld number\n",cardNo);
            continue;
        }
        printf("Please enter the card amount (positive value for deposit, negative value for withdrawal):\n");
        scanf("%ld",&money);
        if(card[k][2]==N)       /*取款大于现有金额*/
        {
            printf("The day business of this card has been completed\n");
            continue;
        }           /*处理一笔业务的数据*/
        card[k][3+card[k][2]]=money;    /*card[k][3+i]表示第k张卡的第i笔业务之后的业务之后的业务金额*/
        card[k][1]+=money;
        card[k][2]++;           /*显示正在处理业务的储蓄卡的信息*/
        printf("The card number is %ld and the current balance of the savings card is %ld.\n",card[k][0],card[k][1]);
        printf("The number of business for this card on the day is%ld\n",card[k][2]);
    }
    return 0;
}

程序心得:

M 代表发卡数量(即用户数),N 代表当天允许操作的业务笔数。
M represents the number of cards issued (ie, the number of users), and N represents the number of services allowed to operate on the day.
程序中数组 card 的每一行的第 4 个元素表示第 1 笔业务,第 5 个元素表示第 2 笔业务,即 card[k][3] 表示第 k 张卡当天的第 1 笔业务存取金额,card[k][4] 表示第 2 笔业务的存取金额,card[k][3+i] 表示第 k 张卡当天的第 i+1 笔业务,所以 card[k][3+card[k][2]] 表示第 k 张卡的第 card[k][2]+1 笔业务,也就是当日的第 card[k][2]+1 笔业务。
The fourth element of each line of the array card in the program represents the first business, and the fifth element represents the second business, that is, card[k][3] represents the first business access amount of the kth card on the day. , card[k][4] indicates the access amount of the second business, and card[k][3+i] indicates the i+1th business of the kth card day, so card[k][3+card [k][2]] indicates the card[k][2]+1 service of the kth card, that is, the card[k][2]+1 service on the current day.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值