c语言图形化编程(模拟ATM机)

 本程序运行环境TC 2.0 ,界面采用c语言中的图形化编程!数据操作其中包括链表,以及文件的写入(用一个dat文件代替数据库).

 

/* Note:Your choice is C IDE */
#include "stdio.h"
#include "conio.h"
#include "graphics.h"
#include "string.h"
#include "stdlib.h"

typedef struct userinfo 
{
 int money;
 int state;
 char name[13];
 char pwd[7];
 struct userinfo *next; 
}user;

user *head;
user *cur;
struct userinfo rd[999];

int login();
void readdata();
void writedata();
void changec();
void changef();
void intilize();
void waring();
void modifypwd();
void mds(int);
int pwd_list();
void start();
void caozuo();
void changes();
void czmon(int);
void success();
void lock();
void cover(int ,int);
void create_list();
int insert_list_from_head();
void save_list(int);
int change_list(int);
void get_list(int);
void lock_list();

char ido[13];
char id[13];
char pwd[7];
char npwd[7];
int mon;

void writedata()
{
 int i;
 FILE *fp;
 user *p;
 
 if((fp=fopen("D://data.dat","wb"))==NULL)
 {
  printf("NO");
 }
 p=head;
 for(i=0;i<999;i++)
 {
   p=p->next;
   rd[i].state=p->state;
   rd[i].money=p->money;
   strcpy(rd[i].name,p->name);
   strcpy(rd[i].pwd,p->pwd);
 }
 fwrite(rd,sizeof(struct userinfo),999,fp);
 fclose(fp);
}

void readdata()
{
 FILE *fp;
 int i;
 user *p,*q,*s;
 
 if((fp=fopen("D://data.dat","rb"))==NULL)
 {
  printf("NO");
 }
 fread(rd,sizeof(struct userinfo),999,fp);
 for(i=0;rd[i].state==2||rd[i].state==1;i++)
 {
   p=head;
   s=malloc(sizeof(user));
   q=head;
   p=head->next;
   q->next=s;
   
   s->next=p;
   s->state=rd[i].state;
   s->money=rd[i].money;
   strcpy(s->name,rd[i].name);
   strcpy(s->pwd,rd[i].pwd);
 } 
 fclose(fp);   
}

void create_list()
{
 head=malloc(sizeof(user));
 if(head==NULL)
  printf("内存空间不足!/07/n");
 head->next=NULL;
}

int login()
{
 user *p;
 p=head;
 while(p!=NULL && strcmp(p->name,id)) p=p->next;
 if(p!=NULL)
 {
  if(strcmp(p->pwd,pwd)==0)
  {
   cur=p;
   mon=p->money;
   return 1;
  }
  else return 0;
 }
 else return 0;
}

int insert_list_from_head()
{
 user *p,*q,*s;
 
 p=head;
 while(p!=NULL && strcmp(p->name,id)) p=p->next;
 if(p!=NULL) return 0;
 else
 {
  s=malloc(sizeof(user));
  if(s==NULL) 
  {
   printf("内存空间不足/07/n") ;
   return 0;
  }
  q=head;
  p=head->next;
  q->next=s;
  s->next=p;
  
  s->state=2;
  s->money=0;
  strcpy(s->name,id);
  strcpy(s->pwd,pwd);
  
  mon=s->money;
  writedata();
  return 1;
 } 
}

void save_list(int money)
{
 user *p;
 p=head;
 while(p!=NULL && strcmp(p->name,id)) p=p->next;
 if(p!=NULL)
 {
  p->money=p->money+money;
  mon=p->money;
  writedata();
 }
}

int pwd_list()
{
 user *p;
 p=head;
 while(p!=NULL && strcmp(p->name,id)) p=p->next;
 if(p!=NULL)
 {
  strcpy(p->pwd,npwd);
  writedata();
  return 1;
 }
 else return 0;
}

int change_list(int money)
{
 user *p;
 p=head;
 while(p!=NULL && strcmp(p->name,ido)) p=p->next;
 if(p!=NULL)
 {
  p->money=p->money+money;
  p=head;
  while(p!=NULL && strcmp(p->name,id)) p=p->next;
  {
   p->money=p->money-money;
   mon=p->money;
   writedata();
  }
  return 1;
 }
 else return 0;
}

void get_list(int money)
{
 user *p;
 p=head;
 while(p!=NULL && strcmp(p->name,id)) p=p->next;
 if(p!=NULL)
 {
  p->money=p->money-money;
  mon=p->money;
  writedata();
 }
}

void lock_list()
{
 user *p;
 p=head;
 while(p!=NULL && strcmp(p->name,id)) p=p->next;
 if(p!=NULL) p->state=1;
 writedata();
}

void changes()
{
 int i=0;
 char str;
 int c=0;
 int p;
 int x;
 setcolor(7);
   rectangle(20,20,620,400); 
   
     setcolor(10);
    rectangle(220,155,450,180);
    
    setcolor(7); 
    settextstyle(DEFAULT_FONT,HORIZ_DIR,2); 
    outtextxy(290,40,"CHANGE");
    
    setcolor(7); 
    settextstyle(DEFAULT_FONT,HORIZ_DIR,1); 
    outtextxy(200,162,"ID");
    setcolor(7);
    outtextxy(250,308,"OK");
    outtextxy(375,308,"BACK");
    
    setcolor(7);
    rectangle(220,300,320,320);
    rectangle(350,300,450,320);
    
    while(1)
  {
   if(kbhit())
   {
    str=getch();
    if(str>47&&str<58)
    {
     if(i<12)
     {
      ido[i]=str;
      i=i+1;
      if(i==12)
      {
       ido[i]='/0';
       p=1;
      }
     }
    }
    if(str==8)
    {
     if(i>0)
     {
      cover(220,155);
      setcolor(10);
         rectangle(220,155,450,180);
      i=i-1; 
     }
    }
    
    for(x=0;x<i;x++)
    {
     gotoxy(30+2*x,11);
     printf("%c",ido[x]);
    }
    
    if(p==1&&str==13)
    {
     setcolor(10);
       outtextxy(250,308,"OK");
     break; 
    }
   }
  }
  while(1)
  {
   if(kbhit())
   {
    str=getch();
    if(str=='a')
    {
     c=0;
     setcolor(10);
        outtextxy(250,308,"OK");
        setcolor(7);
        outtextxy(375,308,"BACK"); 
    }
    if(str=='d')
    {
     c=1;
     setcolor(7);
        outtextxy(250,308,"OK");
        setcolor(10);
        outtextxy(375,308,"BACK"); 
    }
    if(str==13&&c==0)
    {
     closegraph();
     intilize();
        czmon(2);
        break; 
    }
    if(str==13&&c==1)
    {
     closegraph();
     intilize();
     caozuo();
       break;
    }
   }
  }
}
void modifypwd()
{
 int i=0;
 char str;
 int c=0;
 int p;
 int x;
 setcolor(7);
   rectangle(20,20,620,400); 
   
     setcolor(10);
    rectangle(220,155,450,180);
    
    setcolor(8); 
    settextstyle(DEFAULT_FONT,HORIZ_DIR,2); 
    outtextxy(290,40,"Modify");
    
    setcolor(7); 
    settextstyle(DEFAULT_FONT,HORIZ_DIR,1); 
    outtextxy(110,162,"NEW");
    outtextxy(150,162,"POSSWORD");
    setcolor(7);
    outtextxy(250,308,"OK");
    outtextxy(375,308,"BACK");
    
    setcolor(7);
    rectangle(220,300,320,320);
    rectangle(350,300,450,320);
    
    while(1)
  {
   if(kbhit())
   {
    str=getch();
    if(str>47&&str<58)
    {
     if(i<6)
     {
      npwd[i]=str;
      i=i+1;
      if(i==6)
      {
       npwd[i]='/0';
       p=1;
      }
     }
    }
    if(str==8)
    {
     if(i>0)
     {
      cover(220,155);
      setcolor(10);
         rectangle(220,155,450,180);
      i=i-1; 
     }
    }
    
    for(x=0;x<i;x++)
    {
     gotoxy(30+2*x,11);
     printf("%c",'*');
    }
    
    if(p==1&&str==13)
    {
     setcolor(10);
       outtextxy(250,308,"OK");
     break; 
    }
   }
  }
  while(1)
  {
   if(kbhit())
   {
    str=getch();
    if(str=='a')
    {
     c=0;
     setcolor(10);
        outtextxy(250,308,"OK");
        setcolor(7);
        outtextxy(375,308,"BACK"); 
    }
    if(str=='d')
    {
     c=1;
     setcolor(7);
        outtextxy(250,308,"OK");
        setcolor(10);
        outtextxy(375,308,"BACK"); 
    }
    if(str==13&&c==0)
    {
     int a;
     writedata();
     closegraph();
     intilize();
     a=pwd_list();
        mds(a);
        break; 
    }
    if(str==13&&c==1)
    {
     closegraph();
     intilize();
     caozuo();
       break;
    }
   }
  }
}

void success()
{
 char str;
  printf("/n/n/n/n/n/n/n/n/n/n/n/n/n/tYOUR REIGER HAS BEEN SUCCESSFUL PLEASE PRESS 'ENTER' TO LOGIN SYSTEM");
  while(1)
  {
   if(kbhit())
   {
    str=getch();
    if(str==13)
    {
     closegraph();
     intilize();
        caozuo();
        break; 
    }
   }
  }
}

void changef()
{
 char str;
  printf("/n/n/n/n/n/n/n/n/n/n/n/n/n/tFailed PLEASE PRESS 'ENTER' TO BACK(DEFIND THIS ID)");
  while(1)
  {
   if(kbhit())
   {
    str=getch();
    if(str==13)
    {
     closegraph();
     intilize();
        caozuo();
        break; 
    }
   }
  }
}

void changec()
{
 char str;
  printf("/n/n/n/n/n/n/n/n/n/n/n/n/n/tSUCCESSFUL PLEASE PRESS 'ENTER' TO BACK");
  while(1)
  {
   if(kbhit())
   {
    str=getch();
    if(str==13)
    {
     closegraph();
     intilize();
        caozuo();
        break; 
    }
   }
  }
}

void waring()
{
 char str;
  printf("/n/n/n/n/n/n/n/n/n/n/n/n/n/tYOUR Card HAS BEEN Locked PLEASE PRESS 'ENTER' TO EXIT SYSTEM");
  while(1)
  {
   if(kbhit())
   {
    str=getch();
    if(str==13)
    {
     closegraph();
     intilize();
        start();
        break; 
    }
   }
  }
}

void mds(int a)
{
 char str;
 if (a==1)
 {
  printf("/n/n/n/n/n/n/n/n/n/n/n/n/n/tYOUR POSSWORD HAS BEEN MODIFIED PLEASE PRESS 'ENTER' TO EXIT SYSTEM");
  while(1)
  {
   if(kbhit())
   {
    str=getch();
    if(str==13)
    {
     closegraph();
     intilize();
        start();
        break; 
    }
   }
  }
 }
 else
 {
  printf("/n/n/n/n/n/n/n/n/n/n/n/n/n/tYOUR POSSWORD HASN't BEEN MODIFIY PLEASE PRESS 'ENTER' TO BACK");
  while(1)
  {
   if(kbhit())
   {
    str=getch();
    if(str==13)
    {
     closegraph();
     intilize();
        caozuo();
        break; 
    }
   }
  } 
 }
}
void lock()
{
 char str;
 lock_list();
 printf("/n/n/n/n/n/n/n/n/n/n/n/n/n/tYOUR ID HAS BEEN LOCKED PLEASE PRESS 'ENTER' TO EXIT SYSTEM"); 
 while(1)
  {
   if(kbhit())
   {
    str=getch();
    if(str==13)
    {
     closegraph();
     intilize();
        start();
        break; 
    }
   }
  }
}

void main()
{
 create_list();
 readdata();
    intilize();
    start();
}

void intilize()
{
 int driver,mode;
    driver=VGA;
    mode=VGAHI;
    initgraph(&driver,&mode,""); 
}

void czmon(int k)
{
 char str;
 int h=100;
 int x=40;
 int y=70;
   setcolor(7);
   rectangle(20,20,600,400); 
   
   setcolor(10);
   rectangle(40,70,90,100);
   setcolor(7);
   rectangle(40,130,90,160);
   rectangle(40,190,90,220);
   rectangle(40,250,90,280);
   rectangle(40,310,90,340);
   
   rectangle(530,70,580,100);
   rectangle(530,130,580,160);
   rectangle(530,190,580,220);
   rectangle(530,250,580,280);
   rectangle(530,310,580,340);
   
   printf("/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/t/tYour Money: %d",mon);
   
   if (cur->state==1)
   printf("/t/t/tCard State: Locked");
   else
   printf("/t/t/tCard State: Nomal");
   setcolor(7); 
   settextstyle(DEFAULT_FONT,HORIZ_DIR,2);  
   if(0==k)
   outtextxy(290,40,"GET");
   if(1==k)
   outtextxy(290,40,"SAVE");
   if(2==k)
   outtextxy(290,40,"CHANGE");
   
   gotoxy(8,6);
   printf("100");
   gotoxy(8,10);
   printf("200");
   gotoxy(8,13);
   printf("300");
   gotoxy(8,17);
   printf("400");
   gotoxy(8,21);
   printf("500");
   gotoxy(69,6);
   printf("600");
   gotoxy(69,10);
   printf("700");
   gotoxy(69,13);
   printf("800");
   gotoxy(69,17);
   printf("900");
   gotoxy(69,21);
   printf("1000");
   while(1)
  {
   if(kbhit())
   {
    str=getch();
    if(str==27)
    {
     closegraph();
     intilize();
        caozuo(); 
    }
    if(str==13)
    {
     if(cur->state==2)
     {
      if(0==k&&mon>=h) 
      {
       get_list(h);
       closegraph();
       intilize();
          caozuo();
          break;
      }
      if(1==k) 
      {
       save_list(h);
       closegraph();
       intilize();
          caozuo();
          break;
      }
      if(2==k&&mon>=h) 
      {
       int a;
       a=change_list(h);
       if(0==a)
       {
        closegraph();
        intilize();
        changef();
        break;
       }
       else
       {
        closegraph();
        writedata();
        intilize();
           changec();
           break; 
       }
      }
      if(mon<h)
      {
       setcolor(5); 
          settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
          outtextxy(200,450,"YOU");
          outtextxy(240,450,"HAVE");
          outtextxy(280,450,"NOT");
          outtextxy(310,450,"ENOUGH");
          outtextxy(370,450,"MONEY");
      }
     }
     else
     {
      closegraph();
      intilize();
         waring();
         break;  
     }
    }
    if(str=='a')
    {
     if(h>500)
     {
      h=h-500;
      setcolor(7);
      rectangle(x,y,x+50,y+30);
      x=x-490;
      setcolor(10);
      rectangle(x,y,x+50,y+30);
     } 
    }
    if(str=='d')
    {
     if(h<600)
     {
      h=h+500;
      setcolor(7);
      rectangle(x,y,x+50,y+30);
      x=x+490;
      setcolor(10);
      rectangle(x,y,x+50,y+30);
     } 
    }
    if(str=='w')
    {
     if(h>100)
     {
      h=h-100;
      setcolor(7);
      rectangle(x,y,x+50,y+30);
      if(h==500)
      {
       y=y+240;
       x=x-490;
      }
      else
      {
       y=y-60;
      }
      setcolor(10);
      rectangle(x,y,x+50,y+30);
     } 
    }
    if(str=='s')
    {
     if(h<1000)
     {
      h=h+100;
      setcolor(7);
      rectangle(x,y,x+50,y+30);
      if(h==600)
      {
       y=y-240;
       x=x+490;
      }
      else
      {
       y=y+60;
      }
      setcolor(10);
      rectangle(x,y,x+50,y+30);
     } 
    }
   }
  }
}
void caozuo()
{
 char str;
 int w=0;
 int x=40;
 int y=70;
 
   setcolor(7);
   rectangle(20,20,600,400); 
   
   setcolor(10); 
   rectangle(40,70,90,100);
   setcolor(7); 
   rectangle(40,190,90,220);
   rectangle(40,310,90,340);
   
   rectangle(530,70,580,100);
   rectangle(530,190,580,220);
   rectangle(530,310,580,340);
   printf("/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/t/tYour Money: %d",mon);
   
   if (cur->state==1)
   printf("/t/t/tCard State: Locked");
   else
   printf("/t/t/tCard State: Nomal");
   setcolor(7); 
   setcolor(7); 
   settextstyle(DEFAULT_FONT,HORIZ_DIR,2);   
   outtextxy(290,40,"ATM");
   
   setcolor(7);
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);   
   outtextxy(50,85,"GET");  
   outtextxy(50,205,"SAVE");  
   outtextxy(42,325,"CHANGE");  
   outtextxy(540,85,"LOCK");  
   outtextxy(540,205,"MOD");   
   outtextxy(540,325,"END");
   while(1)
  {
   if(kbhit())
   {
    str=getch();
    if(str==13)
    {
     if(w==5)
     {
      writedata();
      closegraph();
      intilize();
         start(); 
     }
     if(w==0)
     {
      closegraph();
      intilize();
         czmon(0); 
     }
     else if(w==1)
     {
      closegraph();
      intilize();
         czmon(1); 
     }
     else if(w==2)
     {
      closegraph();
      intilize();
         changes(); 
     }
     else if(w==3)
     {
      closegraph();
      intilize();
         lock(); 
     }
     else if(w==4)
     {
      closegraph();
      intilize();
         modifypwd(); 
     }
    }
    if(str=='a')
    {
     if(w>=3)
     {
      w=w-3;
      setcolor(7);
      rectangle(x,y,x+50,y+30);
      x=x-490;
      setcolor(10);
      rectangle(x,y,x+50,y+30);
     } 
    }
    if(str=='d')
    {
     if(w<3)
     {
      w=w+3;
      setcolor(7);
      rectangle(x,y,x+50,y+30);
      x=x+490;
      setcolor(10);
      rectangle(x,y,x+50,y+30);
     } 
    }
    if(str=='w')
    {
     if(w>=1)
     {
      w=w-1;
      setcolor(7);
      rectangle(x,y,x+50,y+30);
      if(w==2)
      {
       y=y+240;
       x=x-490;
      }
      else
      {
       y=y-120;
      }
      setcolor(10);
      rectangle(x,y,x+50,y+30);
     } 
    }
    if(str=='s')
    {
     if(w<5)
     {
      w=w+1;
      setcolor(7);
      rectangle(x,y,x+50,y+30);
      if(w==3)
      {
       y=y-240;
       x=x+490;
      }
      else
      {
       y=y+120;
      }
      setcolor(10);
      rectangle(x,y,x+50,y+30);
     } 
    }
   }
  }
}

void start()
{
 int i=0;
 int m=0;
 int n=0;
 int p1=0;
 int p2=0;
 char str;
 int t=0;
 int x;
 int cf;
 
    setcolor(7);
   rectangle(20,20,620,400); 
   
     setcolor(10);
    rectangle(220,155,450,180);
    setcolor(7);
    rectangle(220,205,450,230);
    
    setcolor(7); 
    settextstyle(DEFAULT_FONT,HORIZ_DIR,2);  
    outtextxy(290,40,"ATM");
    
    setcolor(7); 
    settextstyle(DEFAULT_FONT,HORIZ_DIR,1.5);  
    outtextxy(200,162,"ID");
    outtextxy(150,212,"PASSWORD");
    setcolor(7);
    outtextxy(245,308,"LOGIN");
    outtextxy(375,308,"REIGER");
    
    setcolor(7);
    rectangle(220,300,320,320);
    rectangle(350,300,450,320);
   
    while(1)
  {
   if(kbhit())
   {
    str=getch();
    if(str>47&&str<58)
    {
     if(t==0&&i<12)
     {
      id[i]=str;
      i=i+1;
      if(i==12)
      {
       id[i]='/0';
       p1=1;
      }
     }
     else if(t==1&&m<6)
     {
      pwd

=str;
      m=m+1;
      if(m==6)
      {
       pwd

='/0';
       p2=1;
      }
     }
    }
    if(str==27)
    {
     writedata();
     exit(1);
    }
    if(str==8)
    {
     if(t==0&&i>0)
     {
      cover(220,155);
      setcolor(10);
         rectangle(220,155,450,180);
      i=i-1; 
     }
     if(t==1&&m>0)
     {
      cover(220,205);
      setcolor(10);
         rectangle(220,205,450,230);
      m=m-1;
     }
    }
    if(t==0)
    {
     for(x=0;x<i;x++)
      {
       gotoxy(30+2*x,11);
       printf("%c",id[x]);
      }
    }
    else if(t==1)
    {
     for(x=0;x<m;x++)
      {
       gotoxy(30+2*x,14);
       printf("%c",'*');
      }
    }
    if(str==13&&p1==1&&p2==1)
    {
     setcolor(10);
       outtextxy(245,308,"LOGIN");
       setcolor(7);
        rectangle(220,155,450,180);
        rectangle(220,205,450,230);
     break;
    }
    if(str=='w'&&t==1)
    {
       setcolor(10);
          rectangle(220,155,450,180);
          setcolor(7);
          rectangle(220,205,450,230);
          t=0;
    }
    if(str=='s'&&t==0)
    {
       setcolor(7);
          rectangle(220,155,450,180);
          setcolor(10);
          rectangle(220,205,450,230);
          t=1;
    }
   }
  }
  
  while(1)
  {
   if(kbhit())
   {
    str=getch();
    if(str==27)
    exit(1);
    if(str=='a')
    {
     n=0;
     setcolor(10);
        outtextxy(245,308,"LOGIN");
        setcolor(7);
        outtextxy(375,308,"REIGER"); 
    }
    if(str=='d')
    {
     n=1;
     setcolor(7);
        outtextxy(245,308,"LOGIN");
        setcolor(10);
        outtextxy(375,308,"REIGER"); 
    }
    if(str==13&&n==0)
    {
     int a;
     a=login();
     if(a==1)
     {
      closegraph();
      intilize();
         caozuo();
         break;
     }
     else
     {
      intilize();
      printf("/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/t/t/tLogin Falid ! Please Input Again"); 
      start();
      break;
     }
    }
    if(str==13&&n==1)
    {
     cf=insert_list_from_head();
     if(cf==1)
     {
      closegraph();
      intilize();
      success();
        break;
     }
     else
     {
      intilize();
      printf("/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/t/t/tReiger Falid ! Please Input Again");
      start();
        break; 
     }
    }
   }
  }
}

void cover(int x,int y)
{
 int i;
 for(i=0;i<25;i++)
 {
  setcolor(0);
  rectangle(x,y,x+230,y+1);
  y=y+1;
 }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值