天天看點

輸入一段字元串,字母入隊,數字出隊

有問題留言

// 實驗四.cpp : 此檔案包含 "main" 函數。程式執行将在此處開始并結束。
//

#include <stdio.h>
#define QueueSize 100
typedef char ElemType;
typedef struct
{
    ElemType data[QueueSize];
    int front;//頭
    int rear;//尾
}SqQueue;

//初始化
void InitQueue(SqQueue& qu)
{
    qu.rear = qu.front = 0;
}
//入隊操作
int EnQueue(SqQueue& qu, ElemType e)
{
    if ((qu.rear + 1) % QueueSize == qu.front)
    {
        return 0;
    }
    qu.data[qu.rear] = e;
    qu.rear = (qu.rear + 1) % QueueSize;
    return 1;
}
//出隊操作
int Dequeue(SqQueue& qu, ElemType& e)
{
    if (qu.rear == qu.front)
    {
        return 0;
    }
    e = qu.data[qu.front];
    qu.front = (qu.front + 1) % QueueSize;
    return 1;
}
//取對頭操作
int GetHead(SqQueue qu, ElemType& e)
{
    if (qu.rear == qu.front)
    {
        return 0;
    }
    e = qu.data[qu.front];
    return 1;
}
//判斷隊列空
int QueueEmpty(SqQueue qu)
{
    return qu.rear == qu.front;
}
//判斷隊滿
int QueueFull(SqQueue qu)
{
    return (qu.rear + 1) % QueueSize == qu.front;
}


int main()
{
    ElemType  e;
    SqQueue qu;
    InitQueue(qu);//初始化
    printf_s("請輸入一串字元序列(以換行結束):\n");
    for (int i = 0; i < QueueSize; i++)
    {
        scanf_s("%c", &e);
        if (e == '\n')
        {
            break;
        }
        else if ('a' <= e && e <= 'z' || 'A' <= e && e <= 'Z')
        {
            if (QueueFull(qu))
            {
                printf_s("隊滿,不能再進隊了\n");
            }
            else
            {
                EnQueue(qu, e);
                printf_s("%c入隊\n", e);
            }
        }
        else if ('0' <= e && e <= '9')
        {
            if (QueueEmpty(qu))
            {
                printf_s("隊空,不能再出隊\n");
            }
            else
            {
                Dequeue(qu, e);//出隊
                printf_s("%c出隊\n", e);
            }
        }
        else
        {
            printf_s("其它字元忽略%c", e);
        }
    }
}

// 運作程式: Ctrl + F5 或調試 >“開始執行(不調試)”菜單
// 調試程式: F5 或調試 >“開始調試”菜單

// 入門使用技巧: 
//   1. 使用解決方案資料總管視窗添加/管理檔案
//   2. 使用團隊資料總管視窗連接配接到源代碼管理
//   3. 使用輸出視窗檢視生成輸出和其他消息
//   4. 使用錯誤清單視窗檢視錯誤
//   5. 轉到“項目”>“添加新項”以建立新的代碼檔案,或轉到“項目”>“添加現有項”以将現有代碼檔案添加到項目
//   6. 将來,若要再次打開此項目,請轉到“檔案”>“打開”>“項目”并選擇 .sln 檔案
      

繼續閱讀