基于c语言的图形化atm(服务器与客户端)

第一次学习easyx,用easyx和c语言写了一个atm的模拟实现,使用了服务器与客户端发送数据,实现了以下功能:

1.服务器使用tcp发送数据至客户端,将存放数据的结构体数组以顺序的方式发送给客户端,客户端顺序接收。(数据存储在服务器的结构体里,并未设置文件存储)
2.atm实现功能有普通用户的存取,转账,余额显示,有管理员用户的增加、修改、注销、遍历用户。(管理员用户名 爸爸爸爸 密码 爸爸爸爸)
3.用户账户与密码长度为20个字符,存款为int型。

程序仍有许多bug,例如在增加账户时我加了判定账户是否与其他普通用户重名,却未判断是否与管理员账户重名;又例如在输入账户密码时并未判断是否超出数组长度,等等等等…….

但因为程序仅作为学习使用,便不再增加相关错误处理,请规范输入-。-

服务器:

#include <stdio.h> 
#define WIN32_LEAN_AND_MEAN 
#include <windows.h>  
#include <graphics.h>  
#include <stdlib.h>  
#include <time.h>  
#include <conio.h>
#include <winsock2.h>

#pragma comment(lib,"ws2_32.lib")  
struct yonghu
{
char zh[20];
char mm[20];
int money;
int cunzai;
};

struct yonghu yonghu1[100]; 


int main(void)  
{  
    int i,j;
/*
    scanf("%s",yonghu1[0].zh);
    scanf("%s",yonghu1[0].mm);
    yonghu1[0].money=2000;
    yonghu1[0].cunzai=1;
    scanf("%s",yonghu1[1].zh);
    scanf("%s",yonghu1[1].mm);
    yonghu1[1].money=2000;
    yonghu1[1].cunzai=1;
    scanf("%s",yonghu1[50].zh);
    scanf("%s",yonghu1[50].mm);
    yonghu1[50].money=2000;
    yonghu1[50].cunzai=1;
    printf("0=%s\n",yonghu1[4].zh);
    printf("0=%s\n",yonghu1[0].mm);
    printf("0=%d\n",yonghu1[4].money);
    printf("0=%s\n",yonghu1[1].zh);
    printf("0=%s\n",yonghu1[1].mm);
    printf("0=%d\n",yonghu1[1].money);*/

L:
    //初始化wsa  
    WORD sockVision = MAKEWORD(2,2);  
    WSADATA wsadata;  
    //其他变量  
    SOCKET slisten;  
    SOCKET sClient;  
    struct sockaddr_in remoteAddr;  
    struct sockaddr_in sin;  
    int ret = -1;  
    int nAddrlen = sizeof(remoteAddr);  
    char revdata[20];  
    char senddata[20];  

    if( WSAStartup(sockVision,&wsadata) != 0 )  
    {  
        printf("WSA初始化失败\n");  
        return 0;  
    }  

    //创建套接字  
    slisten = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);  
    if(slisten == INVALID_SOCKET)  
    {  
        printf("socket监听者创建失败\n");  
        return 0;  
    }  

    //绑定IP和端口  
    sin.sin_family = AF_INET;  
    sin.sin_port = htons(8888);  
    sin.sin_addr.S_un.S_addr = INADDR_ANY;  
    if( bind(slisten,(LPSOCKADDR)&sin,sizeof(sin)) == SOCKET_ERROR )  
    {  
        printf("绑定IP和端口\n");  
        return 0;  
    }  

    //监听  
    if(listen(slisten,5) == SOCKET_ERROR)  
    {  
        printf("监听失败\n");  
        return 0;  
    }  

    //循环接收数据  
    while(1)  
    {  
        printf("等待连接.........\n");  
        sClient = accept(slisten,(SOCKADDR *)&remoteAddr,&nAddrlen);  
        if(sClient == INVALID_SOCKET)  
        {  
            printf("接受客户端失败,正在重试.........\n");  
            continue;  
        }  
        printf("接受客户端成功:%s\n",inet_ntoa(remoteAddr.sin_addr));  
        //接收数据
        for(i=0;i<100;i++)//发送结构体
        {
            strcpy(senddata,yonghu1[i].zh);
            send(sClient,senddata,20,0);

            Sleep(1);
            strcpy(senddata,yonghu1[i].mm);
            send(sClient,senddata,20,0);

            Sleep(1);
            itoa(yonghu1[i].money,senddata,10);
            send(sClient,senddata,strlen(senddata),0);

            Sleep(1);
            itoa(yonghu1[i].cunzai,senddata,10);
            send(sClient,senddata,strlen(senddata),0);

            Sleep(1);
        }
        strcpy(senddata,"itsoverlook");
        send(sClient,senddata,strlen(senddata),0);



        break;
    }
    j=1;
    i=0;
    while(1)  
        {  
            ret = recv(sClient,revdata,20,0);  
            if(ret > 0)  
            {  
                if(strcmp(revdata,"itsoverlook") == 0)  
                break;
                revdata[ret] = 0x00;
                if(j==1)
                {
                    strcpy(yonghu1[i].zh,revdata);
                }
                if(j==2)
                {
                    strcpy(yonghu1[i].mm,revdata);
                }
                else if(j==3)
                {
                    yonghu1[i].money=atoi(revdata);
                }
                else if(j==4)
                {
                    yonghu1[i].cunzai=atoi(revdata);
                }
                j++;
    //          printf ("%s",revdata);
                if (j==5)
                {
                    j=1;
                    i++;
                }


            }
                if(strcmp(revdata,"itsoverlook") == 0)  
                break;

    }
    closesocket(sClient);
    WSACleanup();
    goto L;

    closesocket(sClient);  
    WSACleanup(); 
    return 0;  
}  

客户端:

#include <stdio.h> 
#define WIN32_LEAN_AND_MEAN 
#include <windows.h>  
#include <graphics.h>  
#include <stdlib.h>  
#include <time.h>  
#include <conio.h>
#include <winsock2.h> 
struct yonghu
{
char zh[20];
char mm[20];
int money;
int cunzai;
}; 

struct yonghu yonghu1[100]; 

#pragma comment(lib,"ws2_32.lib") 

int denglu() ;
void bianli();
void xiugai();
void zengjia();
void zhuxiao();
void main123();

int main()  
{  
    int i,j;
    initgraph(640, 480);
    settextstyle(30, 0, _T("宋体"));
    outtextxy(200, 180, _T("正在连接服务器"));

    WORD sockVision = MAKEWORD(2,2);  
    WSADATA wsadata;  
    SOCKET sclient ;  
    struct sockaddr_in serAddr;  
    char senddata[20];  
    char revdata[20];  
    int ret = -1;  
    if(WSAStartup(sockVision,&wsadata) != 0)  
    {  
        printf("WSA初始化失败\n");  
        return 0;  
    }  

    sclient = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);  
    if(sclient == INVALID_SOCKET)  
    {  
        printf("socket客户端创建失败\n");  
        return 0;  
    }  

    serAddr.sin_family = AF_INET;  
    serAddr.sin_port = htons(8888);  
    serAddr.sin_addr.S_un.S_addr = inet_addr("192.168.1.112");  //输入你的ip
    if( connect(sclient,(SOCKADDR *)&serAddr,sizeof(serAddr)) == SOCKET_ERROR )  
    {  
        printf("socket客户端连接失败\n");  
        return 0;  
    } 
    j=1;
    i=0;
    while(1)  
        {  
            ret = recv(sclient,revdata,20,0);  
            if(ret > 0)  
            {  
                if(strcmp(revdata,"itsoverlook") == 0)  
                break;
                revdata[ret] = 0x00;
                if(j==1)
                {
                    strcpy(yonghu1[i].zh,revdata);
                }
                if(j==2)
                {
                    strcpy(yonghu1[i].mm,revdata);
                }
                else if(j==3)
                {
                    yonghu1[i].money=atoi(revdata);
                }
                else if(j==4)
                {
                    yonghu1[i].cunzai=atoi(revdata);
                }
                j++;
                if (j==5)
                {
                    j=1;
                    i++;
                }


            }
                if(strcmp(revdata,"itsoverlook") == 0)  
                break;

    }
    main123();
    initgraph(640, 480);
    settextstyle(30, 0, _T("宋体"));
    outtextxy(260, 180, _T("正在退出"));
//  system("pause");
    for(i=0;i<100;i++)//发送结构体
        {
            strcpy(senddata,yonghu1[i].zh);
            send(sclient,senddata,20,0);
    //      printf("%s",senddata);

            Sleep(1);
            strcpy(senddata,yonghu1[i].mm);
            send(sclient,senddata,20,0);
    //      printf("%s",senddata);

            Sleep(1);
            itoa(yonghu1[i].money,senddata,10);
            send(sclient,senddata,strlen(senddata),0);
    //      printf("%s",senddata);

            Sleep(1);
            itoa(yonghu1[i].cunzai,senddata,10);
            send(sclient,senddata,strlen(senddata),0);
    //      printf("%s",senddata);

            Sleep(1);
        }
        strcpy(senddata,"itsoverlook");
        send(sclient,senddata,strlen(senddata),0);
//      printf("%s",senddata);


    closesocket(sclient);  
    WSACleanup();  


    return 0;  
}   

int denglu() {
    int a,b,d;
    int i;
    char c=0;
    a=0;
    char zh2[20],mm2[20];
    char s[20];
    initgraph(640, 480);
    setfillcolor(BLUE);
    solidrectangle(120,40,520,440);
    setfillcolor(GREEN);
    solidrectangle(120,140,520,440);
    settextstyle(30, 0, _T("宋体"));
    setbkmode(TRANSPARENT);
    outtextxy(238, 75, _T("欢迎登陆ATM"));
    outtextxy(283, 240, _T("1.登录"));
    outtextxy(283, 280, _T("2.退出"));
    for(;;)
    {
        c=getch();
T:
        switch(c)
        {
        case'1':
            {
                InputBox(s, 10, "请输入卡号:");
                sscanf(s,"%s",&zh2);
                InputBox(s, 10, "请输入密码:");
                sscanf(s,"%s",&mm2);
                if (strcmp(zh2,"爸爸爸爸")==0&&strcmp(mm2,"爸爸爸爸")==0)
                {
                    return 999;
                }
                for (i=0;i<100;i++)
                {
                    if(strcmp(zh2,yonghu1[i].zh)==0&&strcmp(mm2,yonghu1[i].mm)==0&&yonghu1[i].cunzai==1)
                    {
                        b=1;
                        d=i;
                    }
                }
                if (b==1)
                    return  d;
                else
                {
                    a++;
                    if (a<3)
                    goto T;
                }
                if(a==3)
                {
                    cleardevice();
                    settextstyle(30, 0, _T("宋体"));
                    outtextxy(70, 180, _T("账户密码匹配错误已达到3次,程序结束"));
                    Sleep(3000);
                    return 10000;
                }
            }
        case'2':return 10000;
        }
    }
}


void bianli()
{
    int i,in,x;
    x=0;
    char money[10];

    cleardevice();
    setfillcolor(BLUE);
    solidrectangle(0,0,1000,100);
    setfillcolor(GREEN);
    solidrectangle(0,100,1000,1000);
    settextstyle(60, 0, _T("宋体"));
    setbkmode(TRANSPARENT);
    outtextxy(270, 20, _T("欢迎登陆管理员界面"));
    for(i=-1;i<52;i++)
    {
        in=125+17*i;
        line(40,in,465,in);
        line(535,in,960,in);
    }
    line(40,108,40,992);
    line(210,108,210,992);
    line(380,108,380,992);
    line(465,108,465,992);
    line(535,108,535,992);
    line(705,108,705,992);
    line(875,108,875,992);
    line(960,108,960,992);

    settextstyle(16, 0, _T("宋体"));
    setbkmode(TRANSPARENT);
    outtextxy(109, 109, _T("账户"));
    outtextxy(279, 109, _T("密码"));
    outtextxy(402, 109, _T("money"));
    outtextxy(604, 109, _T("账户"));
    outtextxy(774, 109, _T("密码"));
    outtextxy(897, 109, _T("money"));
    for(i=0;i<100;i++)
    {
        if(yonghu1[i].cunzai==1&&x<50)
        {
            itoa(yonghu1[i].money,money,10);
            outtextxy(50,126+17*x,yonghu1[i].zh);
            outtextxy(220,126+17*x,yonghu1[i].mm);
            outtextxy(382,126+17*x,money);
            x++;
        }
        else if(yonghu1[i].cunzai==1&&x>=50)
        {
            itoa(yonghu1[i].money,money,10);
            outtextxy(545,126+17*(x-50),yonghu1[i].zh);
            outtextxy(715,126+17*(x-50),yonghu1[i].mm);
            outtextxy(877,126+17*(x-50),money);
            x++;
        }

    }
    system("pause");
}

void xiugai()
{
    int i,j,x,y;
    char s[20];
    char zh[20];
    y=0;
    InputBox(s, 10, "请输入需要修改的账户名:");
    sscanf(s,"%s",&zh);
    x=0;
    for(;;)
    {
        for(i=0;i<100;i++)
        {
            if(strcmp(yonghu1[i].zh,zh)==0 && yonghu1[i].cunzai==1)
            {
                j=i;
                x=1;
            }
        }
        if (x==1)
        break;
        else
        {
            InputBox(s, 10, "账户输入错误,请重新输入需要修改的账户名:");
            sscanf(s,"%s",&zh);
        }
        y++;
        if (y==4)
            return;
    }
    InputBox(s, 10, "账户输入成功,请输入修改后的账户密码:");
    sscanf(s,"%s",&yonghu1[j].mm);
    InputBox(s, 10, "请输入修改后的账户余额:");
    sscanf(s,"%d",&yonghu1[j].money);
    system("pause");

}

void zengjia()
{
    int i,j,cunzai;
    int x,y;
    char zh[20];
    char s[20];
    cunzai=0;
    y=0;
    for(i=0;i<100;i++)
        cunzai=cunzai+yonghu1[i].cunzai;
    if (cunzai==100)
    {
        cleardevice();
        settextstyle(60, 0, _T("宋体"));
        outtextxy(270, 400, _T("用户已满,不能添加"));
        system("pause");
        return;
    }
    InputBox(s, 10, "请输入要增加的账户名:");
    sscanf(s,"%s",&zh);
    for(;;)
    {
        x=0;
        for(i=0;i<100;i++)
        {
            if(strcmp(yonghu1[i].zh,zh)==0)
            x=1;
        }
        if(x==1)
        {
            InputBox(s, 10, "账户名不能重复,请重新输入要增加的账户名:");
            sscanf(s,"%s",&zh);
            y++;
        }
        else break;
        if(y==4)
            return;
    }
    for(i=0;i<100;i++)
    {
        if (yonghu1[i].cunzai==0)
        {
            j=i;
            break;
        }
    }
    strcpy(yonghu1[j].zh,zh);
    yonghu1[j].cunzai=1;
    InputBox(s, 10, "请输入密码:");
    sscanf(s,"%s",&yonghu1[j].mm);
    InputBox(s, 10, "请输入初始余额:");
    sscanf(s,"%d",&yonghu1[j].money);
    cleardevice();
    settextstyle(60, 0, _T("宋体"));
    outtextxy(270, 400, _T("用户添加成功"));
    system("pause");
}

void zhuxiao()
{
    int x,y,i,j;
    char s[20];
    char zh[20];
    char ax[20];
    y=0;
    InputBox(s, 10, "请输入需要修改的账户名:");
    sscanf(s,"%s",&zh);
    x=0;
    for(;;)
    {
        for(i=0;i<100;i++)
        {
            if(strcmp(yonghu1[i].zh,zh)==0 && yonghu1[i].cunzai==1)
            {
                j=i;
                x=1;
            }
        }
        if (x==1)
        break;
        else
        {
            InputBox(s, 10, "账户输入错误,请重新输入需要修改的账户名:");
            sscanf(s,"%s",&zh);
        }
        y++;
        if (y==4)
            return;
    }
    strcpy(yonghu1[j].zh,ax);
    strcpy(yonghu1[j].mm,ax);
    yonghu1[j].money=0;
    yonghu1[j].cunzai=0;
    cleardevice();
    settextstyle(60, 0, _T("宋体"));
    outtextxy(270, 400, _T("用户注销成功"));
    system("pause");
}

void main123()
{
    int i,j,v,a,k,p,w;
    int x=0;
    char c;
    char s[20];
    char z[20];
H:
    v=denglu();
    for (j=0;j<100;j++)
    {
        if(v==j)
        {
L:
            cleardevice();
            setfillcolor(BLUE);
            solidrectangle(120,40,520,440);
            setfillcolor(GREEN);
            solidrectangle(120,140,520,440);
            settextstyle(30, 0, _T("宋体"));
            setbkmode(TRANSPARENT);
            outtextxy(238, 75, _T("欢迎登陆ATM"));
            outtextxy(260, 190, _T("1.存款"));
            outtextxy(260, 230, _T("2.取款"));
            outtextxy(260, 270, _T("3.查询余额"));
            outtextxy(260, 310, _T("4.转账"));
            outtextxy(260, 350, _T("5.退出"));
            for(;;)
            {
                c=getch();
                switch(c)
                {
                case'1':
                    {
                        InputBox(s, 10, "请输入存款金额:");
                        sscanf(s,"%d",&k);
                        yonghu1[v].money=yonghu1[v].money+k;
                        cleardevice();
                        settextstyle(30, 0, _T("宋体"));
                        outtextxy(200, 180, _T("已经成功存入"));
                        system("pause");
                        goto L;
                    }
                case'2':
                    {
                        InputBox(s, 10, "请输入取款金额:");
                        sscanf(s,"%d",&p);
                        if(p>yonghu1[v].money)
                        {
                            cleardevice();
                            settextstyle(30, 0, _T("宋体"));
                            outtextxy(200, 180, _T("账户余额不足"));
                            system("pause");
                            goto L;
                        }
                        else
                        {
                            yonghu1[v].money=yonghu1[v].money-p;
                            cleardevice();
                            settextstyle(30, 0, _T("宋体"));
                            outtextxy(200, 180, _T("您已成功取款"));
                            system("pause");
                            goto L;
                        }
                    }
                case'3':
                    {
                        cleardevice();
                        settextstyle(30, 0, _T("宋体"));
                        sprintf(s, "您的余额为%d元",yonghu1[v].money );
                        outtextxy(200,180,s);
                        system("pause");
                        goto L;
                    }
                case'4':
                    {
                        InputBox(s, 10, "请输入转账账户:");
                        sscanf(s,"%s",&z);
                        x=0;
                        k=0;
                        for(;;)
                        {
                            for (j=0;j<100;j++)
                            {
                                if(strcmp(z,yonghu1[j].zh)==0 && j!=v)
                                {
                                x=1;
                                w=j;
                                }
                            }
                            if (x==1) break;
                            else
                            {
                                InputBox(s, 10, "账户错误,请重新输入转账账户:");
                                sscanf(s,"%s",&z);
                            }
                        }
                            InputBox(s, 10, "账户验证正确,请输入转账金额:");
                            sscanf(s,"%d",&k);
                            for(;;)
                            {
                                if(k>yonghu1[v].money)
                                {
                                    InputBox(s, 10, "账户余额不足,请重新输入转账金额:");
                                    sscanf(s,"%d",&k);
                                }
                                else
                                {
                                    yonghu1[v].money=yonghu1[v].money-k;
                                    yonghu1[w].money=yonghu1[w].money+k;
                                    cleardevice();
                                    settextstyle(30, 0, _T("宋体"));
                                    outtextxy(200, 180, _T("您已成功转账"));
                                    system("pause");
                                    goto L;
                                }
                            }

                    }
                case'5':goto H;
                }
            }
        }
        else if(v==999)
        {
            initgraph(1000, 1000);
O:
            cleardevice();
            setfillcolor(BLUE);
            solidrectangle(0,0,1000,100);
            setfillcolor(GREEN);
            solidrectangle(0,100,1000,1000);
            settextstyle(60, 0, _T("宋体"));
            setbkmode(TRANSPARENT);
            outtextxy(270, 20, _T("欢迎登陆管理员界面"));
            settextstyle(40, 0, _T("宋体"));
            setbkmode(TRANSPARENT);
            outtextxy(400, 360, _T("1.遍历账户"));
            outtextxy(400, 410, _T("2.修改账户"));
            outtextxy(400, 460, _T("3.增加账户"));
            outtextxy(400, 510, _T("4.注销账户"));
            outtextxy(400, 560, _T("5.退出"));
            for(;;)
            {
                c=getch();
                switch(c)
                {
                case'1':bianli();goto O;
                case'2':xiugai();goto O;
                case'3':zengjia();goto O;
                case'4':zhuxiao();goto O;
                case'5':goto H;
                }
            }
        }
        else if(v==10000)
        {
            closegraph();
        }
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值