C++ 学习笔记 day04

本文介绍了一个简单的C++银行系统的设计与实现,包括账户创建、存款、取款及查询等功能。通过对不同模块如业务逻辑层(biz)和显示层(menu)的划分,展示了良好的软件架构。同时涉及了基本的数据结构如数组的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、程序由函数组成,函数只完成自己特定的功能即可
   把函数声明写在头文件里(想使用函数时,可直接导入头文件,调用函数),把函数实现写在".cc"文件中
   把多个".cc"文件编译成可执行文件 ->分别编译成".o"文件,再连接到一起

2、值传递
   函数中的参数传递是值传递,形参只是实参的一份拷贝数据,在函数中改变形参的值,对实参无影响
  
3、作业分析:显示层(与用户的交互)
             操作数据(完成业务逻辑)biz层
             数据(id ,password , balance )

Bank实现代码

================================================================

           biz.cc

================================================================

//operation

 

/* p : Password of account .

 * b: balance of account .

 *return : id of account .

 */

long create( int p , double b );

void save( double sum ) ;

 

/*

 *return : 0 success , otherwise -1 returned .

 */

int withdraw( int p , double sum ) ;

double query( int p ) ;

long generateId();

================================================================

           biz.cc

================================================================

static long id ;

static int passwd ;

static double balance ;

#include <iostream>

using namespace std;

long generateId(){

   static int id = 1 ;

   return id++ ;

}

long create( int p , double b ){

   id = generateId();

   passwd = p ;

   balance = b ;

   return id ;

}

void save( double sum ){

   balance += sum ;

}

int withdraw( int p , double sum ){

   if( p != passwd ){

       cout<<"invalid password ." << endl;

       return -1 ;

    }

   if( balance < (sum + 10) ){

       cout<<"no enough money ." << endl;

       return -1 ;

    }

   balance -= sum ;

   return 0 ;

}

double query( int p ){

    if( p != passwd ){

       cout<<"invalid password " << endl;

       return -1 ;

   }else{

       return balance ;

    }

}

================================================================

           menu.h

================================================================

int showMenu();

 

void createMenu();

void saveMenu();

void withdrawMenu();

void queryMenu();

================================================================

           menu.cc

================================================================

#include "biz.h"

#include <iostream>

using namespace std;

 

int showMenu(){

   cout<<"create ------> 1 " << endl;

   cout<<"save  ------> 2 " << endl;

   cout<<"withdraw ----> 3 " << endl;

   cout<<"query -------> 4 " << endl;

   cout<<"exit --------> 0 " << endl;

   cout<<"enter your choice >";

   int c ;

   cin>>c ;

   if( !cin ){

       return -1 ;

   }else{

       return c ;

    }

}

 

void createMenu(){

   int passwd ;   

   double balance ;

 

   cout<<"\tenter password >";

   cin>>passwd ;

   cout<<"\tenter balance >";

   cin>>balance ;

 

   long id = create( passwd , balance );

   cout<<"======================"<<endl;

   cout<<"create account ok , id = " << id<<endl;

   cout<<"======================"<<endl;

}

void saveMenu(){

   double sum ;

   cout<<"\tenter sum >";

   cin>>sum ;

   

   save( sum ) ;

 

   cout<<"======================"<<endl;

   cout<<"save money ok "<<endl;

   cout<<"======================"<<endl;

}

void withdrawMenu(){

   int passwd ;

   double sum ;

   cout<<"\tenter password >";

   cin>>passwd ;

   cout<<"\tenter sum >";

   cin>>sum ;

 

   int ret = withdraw( passwd , sum ) ;

 

   if( ret == 0 ){

       cout<<"========================"<<endl;

       cout<<"withdraw successful . "<<endl;

       cout<<"========================"<<endl;

   }   

}

void queryMenu(){

   int passwd ;

   cout<<"\tenter password >";

   cin>>passwd ;

 

   double ret = query( passwd ) ;

   if( ret != -1 ){

      cout<<"============================"<<endl;

      cout<<"BLANCE : $ " << ret << endl;

      cout<<"============================"<<endl;

   }else{

      cout<<"============================"<<endl;

      cout<<"invalid password "<<endl;

      cout<<"============================"<<endl;

    }

}

================================================================

           main.cc

================================================================

#include <iostream>

#include "menu.h"

using namespace std;

int main(){

   int c = 0 ;

   do{

       c = showMenu();

       if( c == -1 ) { break ; }

 

       switch( c ){

         case 1 :

           createMenu();

           break;

         case 2 :

           saveMenu();

           break;

         case 3 :

           withdrawMenu();

           break;

         case 4 :

           queryMenu();

           break;

         case 0 :

           cout<<"===================="<<endl;

           cout<<"Good Bye "<<endl;

           cout<<"===================="<<endl;

           break;

         default :

           cout<<"===================="<<endl;

           cout<<"invalid option, try again.";

           cout<<endl;

           cout<<"===================="<<endl;

           break;

       }   

   }while( c != 0 );

   return 0 ;

}

4、数组
   (1)声明数组    <元素类型> 数组名[元素个数]  int intArray[100]; -->intArray 是个集合,有100个元素,每个元素都是int类型的
   (2)初始化数组 
   (3)使用        通过数组的下标来访问数组中的元素,下标从0开始  intArray[0]=100; -->intArray数组中的第一个元素赋值为100
  
   数组声明时,元素个数必须是常量表达式
   数组声明带有初始化,则可直接为数组赋值
   在数组声明时,必须指明数组长度,若在声明时候初始化,数组长度可省
   int a1[2]={100,200}; 长度2
   int a2[] = {5,6,7}; 长度3

   对于数组中的元素个数,比声明时的多,则会报错,这样的越界访问,对整个程序来说会有很严重的后果!!!
                        比声明少,系统会把余下的没有定义数据的元素初始化为0
                     
   不初始化的数组,其中的元素数据为随机数
  
   下标工作的原理:
           表示编号,还表示当前元素相对于数组起始位置的偏移量
           计算机通过偏移量找到元素在内存中的位置

5、数组的排序
   选择排序:找出最小的放在第一个位置
             数组元素有n个,需要找n-1次,需要比较n-i次(i为开始元素的位置)
            
6、多维数组
    二维数组;一个数组中的每个元素是个数组
            声明: intiA[5][10];  -->“5”代表数组有5行,“10”代表数组有10列
                  声明时,第一维可以省略!
            确定一个元素需要2个下标

7、结构    用户自己定义的一组数据类型   声明结构时,编译器不会分配空间,在声明一个结构的变量的时候才会为其分配空间    结构中的成员是多个简单类型组成的    用 “结构名.成员名”来操作其中的成员变量    strcpy(p.name,"huxz");为char数组赋值   结构类型的变量间也可以相互赋值       结构的大小就是所有成员的大小之和(每个成员的大小必须是int类型的整倍数,当不够时,会自动补齐)    Unix上还要求,结构的大小是结构的最大的成员的整倍数    size(of)  计算结构大小  

作业:把银行系统用结构改写,要求可以开多个账户(定义一个account类型的数组保存)

     struct account{

          long id;

          int password;

           double balance;

     }

     

     create(int password,double balance);

     void save(int id , double sum);

     int withdraw(int id,int password,double sun);

     double query(int id,int password);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值