用循環隊列解決舞伴配對問題發現本身的問題

1.首先是對vs2017這款軟件的使用

1.VS中的scanf()這個函數的使用問題

  直到此次寫代碼我才知道VS中用scanf是會被警告的,VS中正規的相似於scanf()函數的輸入函數是scanf_s()只有使用這個函數你纔不會報錯,它有三個參分別是數據類型,地址,最大存儲量,ios

  還有兩種方法c++

  1.   第一在代碼的第一行加上「#define _CRT_SECURE_NO_WARNINGS」。

   2.         或者修改文件屬性也能夠作到和上面同樣的效果算法

   右鍵點擊源文件,windows

       

 

 

   點擊屬性數組

  

 

依次選中:C/C++ >> 預處理器,在右側預處理器定義右側添加上:_CRT_SECURE_NO_DEPRECATE函數

 2.第二就是個人知識了,真的好菜

  1. while()括號中的是循環條件,而不是中止條件,請必定要想好循環條件是啥
  2. 我原本覺得%s輸入有一個特色就是遇到空格就中止,其實這是函數scanf()函數的特色而不是%s的特色,若是想把空格也吞了,那就用gets(),還有兩個函數就是getchar()和getch()
  3. 還有就是寫代碼的習慣很很差,老是思路混亂,不知道接下來幹啥,其實應該,想着寫着,就像翻譯同樣,把你的想法,思路,用代碼翻譯下來
  4. 對算法原理思想理解的不夠,不重視思想原理,循環列表的原理最重要的就兩個(front + 1)% maxsize 和   (rear + 1) % maxsize,我感受
  5. 只是太薄弱,尤爲是在數組的形參表那裏,要去補補了,傳遞的是一個地址,怎麼寫纔好,是 status inqueue(queue all[],&man)仍是 status inqueue(queue all,&man)呢?我不是很清楚,最後我用了前者對了,但我不知道爲啥
  6. void inqueue(person all[], queue &man, queue &woman,int n) {//根據性別分別如男隊和女隊
        for (int i = 0; i < n; i++) {
            if (all[i].sex == 1) {
                //strcpy(man.elem[man.number], all[i].name);
                strcpy(man.elem[man.rear], all[i].name);
                man.rear = (man.rear + 1) % 100;
                man.number++;
            }
            else {
                //strcpy(woman.elem[woman.number], all[i].name);
                strcpy(woman.elem[woman.rear], all[i].name);
                woman.rear = (woman.rear + 1) % 100;
                woman.number++;
            }
        }
    }

    下面就是我此次寫的代碼,很low,很菜,哎,我太菜了。學習

  7. #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<malloc.h>
    #include<math.h>
    #include<windows.h>
    using namespace std;
    
    typedef int status;
    
    int n;
    typedef struct {
        char name[20];
        int sex;
    }person;
    
    typedef struct {
        int front;
        int rear;
        char elem[100][20];
        int number;//人的數量
    }queue;
    
    void inperson(person &all) {
        cout << "請輸這位同窗的姓名和性別:";
        scanf("%s", all.name);//在c和c++語言中輸入字符串向字符數組中是不須要加&的,
        /*輸入參數是已經定義好的「字符數組名」, 不用加&, 由於在C語言中數組名就表明該數組的起始地址*/
        //cout << "請輸入這位同窗的性別:";
        scanf("%d", &all.sex);
    }
    
    void inqueue(person all[], queue &man, queue &woman,int n) {//根據性別分別如男隊和女隊
        for (int i = 0; i < n; i++) {
            if (all[i].sex == 1) {
                //strcpy(man.elem[man.number], all[i].name);
                strcpy(man.elem[man.rear], all[i].name);
                man.rear = (man.rear + 1) % 100;
                man.number++;
            }
            else {
                //strcpy(woman.elem[woman.number], all[i].name);
                strcpy(woman.elem[woman.rear], all[i].name);
                woman.rear = (woman.rear + 1) % 100;
                woman.number++;
            }
        }
    }
    
    status initqueue(queue &man) {
    
        /*int n,i;
        cout<<"請輸入隊中的人員數量:";
        cin>>n;
        man.number = n + 1;
        cout<<"輸入隊中人員的姓名";
        for(i = 0;i < n;i ++){
            scanf("%s",&man.elem[i]);
        }
        man.rear = n;
        */
        man.rear = man.front = 0;
        man.number = 0;
        return 0;
    }
    int emptyqueue(queue man) {//用於判斷隊列中還有沒有人
        if (man.front == man.rear)
            return 0;
        else
            return 1;
    }
    int dequeue(queue &man, char *str) {//刪除隊首元素
        if (man.front == man.rear)
            return 0;
        else {
            strcpy(str, man.elem[man.front]);
            man.front = (man.front + 1) % 100;
            man.number--;
            return 1;
        }
    }
    
    
    int main() {
        person all[100];
        int n;//總人數
        queue man;
        queue woman;
        initqueue(man);
        initqueue(woman);//初始化兩個隊列,使兩個隊列的首和尾都爲零
        cout << "請輸入本班的人數    :";
        cin >> n;
        for (int i = 0; i < n; i++) {//把全部人都存入一個隊列
            inperson(all[i]);
        }
    
        //根據性別入隊列
    
        inqueue(all, man, woman,n);//按性別分別入隊
    
        char str[20];
        while (emptyqueue(man) && emptyqueue(woman)) {//配對,改這裏沒有想好中止條件,我原本寫的是「||」應該是當兩個都不爲空時才中止
            dequeue(man, str);
            dequeue(woman, str);
        }
        if (man.front == man.rear&&woman.front == woman.rear) {
            cout << "徹底配對";
            cout << endl;
        }
        else if (woman.front != woman.rear) {
            cout << "下一位配對的女性是:";
            printf("%s", woman.elem[woman.front]);
            cout << endl;
        }
        else
            printf("下一位配對的男性是:%s", man.elem[man.front]);
        Sleep(50000);
        return 0;
    
        
    }

    給本身提個醒吧,重視基礎,出來混老是要還的,因此仍是好好學習,打牢本身的基礎吧spa

相關文章
相關標籤/搜索