码迷,mamicode.com
首页 > 其他好文 > 详细

d3_2 简单的取款机系统

时间:2014-07-23 16:30:51      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   strong   io   

class Assignment-2
(use data types,constructs, switch, do while, functions, local variables)
  Create a Menu Driven Program for a simple bank operation:-
  consider the Initial balance is 500 (balance variable will be local to main)
  Sample Output:-
 
  1.Deposit
  2.Withdraw
  3.Check Balance
  4.Quit
  Enter Your Choice:- 3

  Your balance is :- 500RMB

  1.Deposit
  2.Withdraw
  3.Check Balance
  4.Quit
  Enter Your Choice:- 2

  Enter the amount to Withdraw :- 1000
   Sorry InSufficient Balance!!!!
   
 
  1.Deposit
  2.Withdraw
  3.Check Balance
  4.Quit
  Enter Your Choice:-4

  Thank YOu for Using my BANK!!!!
 

balance 为 main()的局部变量



#include<stdio.h> #include<stdlib.h> #include<conio.h> void menu(); int deposit(int balance); int withdraw(int balance); void check(int balance); void quit(); void main() { int balance=500; int menuNumber=0; while(menuNumber!=4)//当按菜单4时退出循环 { menu(); scanf("%d",&menuNumber); switch(menuNumber) { case 1:balance=deposit(balance);break; case 2:balance=withdraw(balance);break; case 3:check(balance);break; case 4:quit();break; default :printf("operator wronng!\n"); } } } //0 menu void menu(){ system("color 37"); //change the color of screen and text printf("*****************************\n"); printf("* Menu: *\n"); printf("* 1.Deposit *\n"); printf("* 2.Withdraw *\n"); printf("* 3.Check Balance *\n"); printf("* 4.Quit *\n"); printf("*****************************\n"); printf("Enter Your Choice:"); } //1 deposit int deposit(int balance) { int amount,newbalance; printf("Enter the amount to Deposit:"); scanf("%d",&amount); newbalance=balance+amount; printf("Your balance is %d RMB\n",newbalance); return newbalance; } //2 withdraw int withdraw(int balance) { int amount,newbalance; printf("Enter the amount to withdraw:"); scanf("%d",&amount); newbalance=balance-amount; if(newbalance<0) { printf(" Sorry InSufficient Balance!!!!\n"); return balance; }else { printf("Now your balance is %d RMB\n",newbalance); return newbalance; } } //3 check void check(int balance){ printf("Your balance is %d RMB\n",balance); } //4 quit void quit(){ system("cls"); printf("Thank YOu for Using my BANK!!!!\n"); }

若balance为全局变量,代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void menu();
void deposit();
void withdraw();
void check();
void quit();
int balance=500;

void main()
{
    
    int menuNumber=0;
    while(menuNumber!=4)//当按菜单4时退出循环
    {
      menu();
      scanf("%d",&menuNumber); 
      switch(menuNumber)
     {
     case 1:deposit();break;
     case 2:withdraw();break;
     case 3:check();break;
     case 4:quit();break;
     default :printf("operator wronng!\n");
     }
        
    }
  
}
//0 menu
void menu(){
    system("color 37");  //change the color of screen and text
   printf("*****************************\n");
   printf("* Menu:                     *\n");
   printf("* 1.Deposit                 *\n");
   printf("* 2.Withdraw                *\n");
   printf("* 3.Check Balance           *\n");
   printf("* 4.Quit                    *\n");
   printf("*****************************\n");
   printf("Enter Your Choice:");
   
    
}

//1 deposit
void deposit()
{
  int amount;
  printf("Enter the amount to Deposit:");
  scanf("%d",&amount);
  balance=balance+amount;
  printf("Your balance is %d RMB\n",balance);
  

}

//2 withdraw
void  withdraw()
{
  int amount,newbalance;
  printf("Enter the amount to withdraw:");
  scanf("%d",&amount);
  newbalance=balance-amount;
  if(newbalance<0)
  {
      printf(" Sorry InSufficient Balance!!!!\n");
     
  }else
  {
     balance=newbalance;
     printf("Now your balance is %d RMB\n",balance); 
  }



}
//3 check
  void check(){
      printf("Your balance is %d RMB\n",balance);
  
  }
 //4 quit
  void quit(){
      system("cls"); //clear the screen
   printf("Thank YOu for Using my BANK!!!!\n");
  }

 


2014-07-2316:08:35

d3_2 简单的取款机系统,布布扣,bubuko.com

d3_2 简单的取款机系统

标签:style   blog   color   os   strong   io   

原文地址:http://www.cnblogs.com/thrive/p/3863219.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!