用C写ATM机

    技术2024-06-26  62

     

    #include<stdio.h>

    #include<stdlib.h>

    #include<string.h>

    #include<conio.h>

    #include<time.h>

    #define N 10

    struct inform                //取款记录  

    {

    int year,mon,day,hour,min,sec;

    int amount;

    char place[20];

    struct inform *next;

    };

     

    struct account             //帐号信息

    {

    int no;

    char name[9];

    char password[N];

    float remainder;        //余额

    struct account *next;

    struct inform *mnext;

    };

     

    void Creatlist(struct account **head,FILE *fp);

    void access(struct account *head,struct account **p);

    void menu(struct account *p);

    int Draw(struct account **p,int *s);

    int Changepass(struct account *p);

    int Save(struct account **p);

    void Inquiry(struct account *p);

    void sum(struct account **p,int *s);

    void Save_Data(FILE *fp,struct account *head);//存盘

     

    int main()

    {

    FILE *fp;

    struct account *head=NULL,*p=NULL;

    int flag=1,select,s=0;

     

    if((fp=fopen("E://VC&汇编//编译程序//ATM作业//account.dat","r+"))==NULL)

    {

    printf("系统出错,即将退出!/a/n");

    exit (0);

    }

     

    Creatlist(&head,fp);

    access(head,&p);

     

    while(flag)

    {

    system("cls");

    menu(p);

    scanf("%d",&select);

    switch(select)

    {

    case 1:Draw(&p,&s);break;   //取款

    case 2:Save(&p);break;         //存款

    case 3:Inquiry(p);break;       //查询余额

    case 4:Changepass(p);break;    //修改密码

    case 0:flag=0;break;           //退出

    }

    }

     

    sum(&p,&s);

    rewind(fp);

    fflush(fp);

    Save_Data(fp,head);

     

    fclose(fp);

    return 1;

    }

     

    void Creatlist(struct account **head,FILE *fp)

    {

    struct inform *q;

    struct account *p;

    int c;

     

    fscanf(fp,"%d",&c);

    while(!feof(fp))

    {

    p=(struct account *)malloc(sizeof(struct account));

    p->no=c;

    fscanf(fp,"%s",p->name);

    fscanf(fp,"%s",p->password);

    fscanf(fp,"%f",&p->remainder);

     

    p->mnext=NULL;

    fscanf(fp,"%d",&c);

    while(c<10000&&!feof(fp))

    {

    q=(struct inform*)malloc(sizeof(struct inform));

    q->year=c;

    fscanf(fp,"%d",&q->mon);

    fscanf(fp,"%d",&q->day);

    fscanf(fp,"%d",&q->hour);

    fscanf(fp,"%d",&q->min);

    fscanf(fp,"%d",&q->sec);

    fscanf(fp,"%d",&q->amount); 

    fscanf(fp,"%s",q->place);

    q->next=p->mnext;

    p->mnext=q;

    fscanf(fp,"%d",&c);

    }

     

    p->next=*head;

    *head=p;

    }

    }

     

    void access(struct account *head,struct account **p)

    {

    char user[N],ch;

    int t=0,i,n;

     

    printf("请输入账户号码:"); //模拟读卡

    scanf("%d",&n);

    *p=head;

    while((*p)!=NULL)           //查找账户

    if((*p)->no==n)

    break;

    else

    (*p)=(*p)->next; 

    while(1)

    {

    i=0;

    printf("请输入密码:");

    while((ch=getch())!='/r')

    {

    user[i++]=ch;

    putchar('*');

    }

    user[i]='/0';

    t++;

    if(strcmp((*p)->password,user)==0)

    break;

    else

    {

    if(t<3)

    printf("/n密码错误,请重新输入!/n");

    else

    {

    printf("/n对不起,你无权使用本卡! 退出系统!/a/n");

    exit (-1);

    }

    }

    }

    }

     

    void menu(struct account *p)

    {

    printf("/t/t/t   欢迎使用ATM取款机!/n/n");

    printf("/t/t/t您的帐户名是:   %s/n",p->name); 

    printf("/t/t/t您的帐户号码是:%d/n/n",p->no);

    printf("/t/t/t请输入所需办理业务的序号:/n");

    printf("/t/t/t/t1 取款/n");

    printf("/t/t/t/t2 存款/n");

    printf("/t/t/t/t3 余额查询/n");

    printf("/t/t/t/t4 修改密码/n");

    printf("/t/t/t/t0 退出/n");

    }

     

    int Draw(struct account **p,int *s)

    {

    int n,t=0;

     

    while(1)

    {

    system("cls");

    printf("本机仅提供面值100元人民币/n一次取款金额不得超过5000/n");

    printf("返回上级菜单请输0/n");

    if(t==1)

    printf("金额输入错误!/n");

    if(t==2)

    printf("余额不足!/n");

    printf("请输入取款金额:");

    scanf("%d",&n);

    if(n>(*p)->remainder)

    {

    t=2;

    continue;

    }

    if(n%100||n>5000)

    {

    t=1;

    continue;

    }

    if(!n)

    return 0;

    if(!(n%100)&&n<=5000)

    {

    (*p)->remainder-=n;

    *s+=n;

    t=0;

    }

    }

    }

     

    void sum(struct account **p,int *s)

    {

    struct inform *q=NULL;

    time_t rawtime;

    struct tm *timeinfo;

     

    if(*s>50000)

    {

    q=(struct inform*)malloc(sizeof(struct inform));

    q->next=(*p)->mnext;

    (*p)->mnext=q; 

    time(&rawtime);

    timeinfo=localtime(&rawtime);

    q->year=1900+timeinfo->tm_year;

    q->mon=1+timeinfo->tm_mon;

    q->day=timeinfo->tm_mday;

    q->hour=timeinfo->tm_hour;

    q->min=timeinfo->tm_min;

    q->sec=timeinfo->tm_sec;

    q->amount=*s;

    scanf("武汉",q->place);

    }

    }

     

    int Changepass(struct account *p)

    {

    int i,t=0;

    char user[N],ch,m[N],n[N];

     

    while(1)

    {

    i=0;

    printf("请输入原始密码:");

    while((ch=getch())!='/r')      

    {

    user[i++]=ch;

    putchar('*');

    }

    user[i]='/0';

    t++;

    printf("%s",p->password);

    if(strcmp(p->password,user)==0)

    break;

    else

    {

    if(t<3)

    printf("/n密码错误,请重新输入!/n");

    else

    {

    printf("/n对不起,你无权修改密码! 退出系统!/a/n");

    return 0;

    }

    }

    }

    while(1)

    {

    printf("/n请输入新密码:");

    scanf("%s",m);

    if(strcmp(m,"0")==0)

    return 1;

    system("cls");

    printf("请确认新密码:");

    scanf("%s",n);

    if(strcmp(m,n)==0)

    {

    strcpy(p->password,m); 

    printf("密码修改成功!");

    printf("输任意数返回上级菜单/n");

    scanf("%d",&i);

    return 1;

    }

    else

    {

    printf("密码输入不一致,请重新输入/n");

    printf("返回上级菜单请输0/n");

    }

    }

    }

     

    int Save(struct account **p)

    {

    int n;

     

    while(1)

    {

    system("cls");

    printf("本机仅提供面值100元人民币/n");

    printf("返回上级菜单请输0/n");

    printf("请输入存款金额:");

    scanf("%d",&n);

    if(!n)

    return 0;

    if(n%100)

    printf("输入错误/n");

    else

    {

    printf("存款成功/n");

    (*p)->remainder+=n; 

    printf("输任意数返回上级菜单/n");

    scanf("%d",&n);

    return 1;

    }

    }

    }

     

    void Inquiry(struct account *p)

    {

    int n;

     

    printf("您的余额为:%.2f/n/n",p->remainder);

    printf("输任意数返回上级菜单/n");

    scanf("%d",&n);

    }

     

    void Save_Data(FILE *fp,struct account *p)//存盘

    {

    struct inform *q;

     

    while(p!=NULL)   

    {

    fprintf(fp,"%-8d",p->no);

    fprintf(fp,"%s",p->name);

    fprintf(fp,"%10s",p->password);

    fprintf(fp,"%10.2f/n",p->remainder);

     

    q=p->mnext;     //存入取款记录

    while(q!=NULL)

    {

    fprintf(fp,"/t%4d",q->year);

    fprintf(fp,"%3d",q->mon);

    fprintf(fp,"%3d",q->day);

    fprintf(fp,"%4d",q->hour);

    fprintf(fp,"%3d",q->min);

    fprintf(fp,"%3d",q->sec);

    fprintf(fp,"%7d",q->amount);

    fprintf(fp,"/t武汉/n");

    q=q->next;

    }

    p=p->next;

    }

    }

     

     

     

    最新回复(0)