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