⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cd3_3u.cpp

📁 C++ Builder程序员学习数据结构第3章
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "cd3_3u.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
#define max 5                 //定义max为5
String queue[max];
int front=-1;                 //定义front,rear为整数型,初始值设置为-1
int rear=-1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
    f0->Visible=false;              //将窗体初始状态中不需要显示的Label隐藏
    r0->Visible=false;
    f1->Visible=false;
    r1->Visible=false;
    f2->Visible=false;
    r2->Visible=false;
    f3->Visible=false;
    r3->Visible=false;
    f4->Visible=false;
    r4->Visible=false;
    full->Visible=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::endClick(TObject *Sender)
{
   Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::einKeyUp(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
   if(Key==13)
   {
      if(rear>=max-1)                     //队列数据已满时的信号,并停止数据输入
         full->Visible=true;
      else
      {
         rear++;                          //数据输入后,后端记数器加1
         queue[rear]=ein->Text;           //将输入值存入队列中,并将Edit控件清空
         ein->Text="  ";
         qsg->Cells[rear][0]=queue[rear]; //显示输入值于字符串表格中

         if(rear!=-1)
            emp->Visible=false;

         switch(rear)                     //后端记数器位置显示决策
         {
            case 0:r_1->Visible=false;
                   r0->Visible=true;
                   break;

            case 1:r0->Visible=false;
                   r1->Visible=true;
                   break;
                   
            case 2:r1->Visible=false; //case后可不加大括号,
                   r2->Visible=true;
                   break;             //break结束指令 跳出决策

            case 3:r2->Visible=false;
                   r3->Visible=true;
                   break;
                   
            case 4:r3->Visible=false;
                   r4->Visible=true;
                   break;                                      
         }                            //段落层次尽可能清楚分明
      }                               //若程序大而复杂,这样的习惯就会减少很多问题
   }                                  //可于段落初设时建立对应的大括号
}
//---------------------------------------------------------------------------
void __fastcall TForm1::delClick(TObject *Sender)
{
   full->Visible=false;         //有取出就不会显示已满的状态
   if(front==rear)              //当前端位置等于后端时表示队列数据已空
   {
      emp->Visible=true;
      cap->Color=clPurple;      //也可以用128+0+128*256*256
   }
   else
   {
      front++;                            //前端记数器加1
      out->Caption=queue[front];          //显示取出数据
      queue[front]="  ";                  //清空取出队列位置
      qsg->Cells[front][0]=queue[front];

      switch(front)                       //前端记数器位置显示决策
      {
         case 0:f_1->Visible=false;
                f0->Visible=true;
                break;
         case 1:f0->Visible=false;
                f1->Visible=true;
                break;
         case 2:f1->Visible=false;
                f2->Visible=true;
                break;
         case 3:f2->Visible=false;
                f3->Visible=true;
                break;
         case 4:f3->Visible=false;
                f4->Visible=true;
                break;
      }                                   //例如switch()
   }                                      //    {
}                                         //    }
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -