C++day04 学习笔记

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

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

Bank实现代码

复制代码
View Code
================================================================
            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();
复制代码
复制代码
View Code
================================================================
            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 ;
    }
}
复制代码
复制代码
View Code
================================================================
            menu.h
================================================================
int showMenu();

void createMenu();
void saveMenu();
void withdrawMenu();
void queryMenu();
复制代码
复制代码
View Code
================================================================
            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;
    }
}
复制代码
复制代码
View Code
================================================================
            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、多维数组
    二维数组;一个数组中的每个元素是个数组
            声明: int iA[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);
复制代码

 

 

 

posted @   唐小喵  阅读(202)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示