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


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

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

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

  還有兩種方法

  1.   第一在代碼的第一行加上“#define _CRT_SECURE_NO_WARNINGS”。

   2.         或者修改文件屬性也可以做到和上面一樣的效果

   右鍵點擊源文件,

       

 

 

   點擊屬性

  

 

依次選中: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;
    
        
    }

    給自己提個醒吧,重視基礎,出來混總是要還的,所以還是好好學習,打牢自己的基礎吧

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM