黑馬程序員C++學習筆記


1.關於if語句使用的邏輯問題【未解決】

/*這里可以書寫多行注釋
 版權:copyright*/
#include<iostream>
using namespace std;

int main()
{
    //選擇結構 單行if語句
    float score = 0;
    cout << "請輸入你的分數" << endl;
    cin >> score;
    cout << "您輸入的分數是" << score << endl;
    if (score >= 600)
    {
        cout << "恭喜您考上了一本大學" << endl;
    }
    else if(500 <= score < 600)
    {
        cout << "二本" << endl;
    }
    else if(score > 400)
    {
        cout << "專科" << endl;
    }
    else
    {
        cout << "nothing" << endl;
    }
    
    
}

當輸入66時,程序輸出二本,debug時發現程序直接運行了

else if(500 <= score < 600)
    {
        cout << "二本" << endl; }然后跳出。


2.結構體的應用案例引發的錯誤【待排查】

#include<iostream>
#include<string>
using namespace std;


//學生的結構體定義
struct Student
{
    string sName;
    int score;
};

//老師的結構體定義
struct Teacher
{
    string tName;
    struct Student sArray[5];
};

//給老師和學生賦值的函數
void allocateSpace(struct Teacher tArray[], int len)
{
    string nameSeed = "ABCDE";
    //給老師賦值
    for(int i = 0; i < len; i++ )
    {
        tArray[i].tName = "Teacher_";
        tArray[i].tName += nameSeed[i];
        for (int j = 0; j < 5; j++)
        {
            tArray[i].sArray[j].sName = "Student_";
            tArray[i].sArray[j].sName += nameSeed[j];
            tArray[i].sArray[j].score = 60;
        }    
    }
}

//打印所有信息
void printInfo(struct Teacher tArray[], int len)
{
    string nameSeed = "ABCDE";
    //給老師賦值
    for (int i = 0; i < len; i++)
    {
        cout << "老師姓名:" << tArray[i].tName << endl;  //編譯器顯示此行引起程序崩潰
        for (int j = 0; j < 5; j++)
        {
            cout << "學生姓名:" << tArray[i].sArray[j].sName << endl
                << "考試分數:" << tArray[i].sArray[j].score << endl;
        }
    }
}
int main()
{
    //1.創建三名老師的數組
    struct Teacher tArray[3];
    int len = sizeof(tArray) / sizeof(tArray[0]);
    allocateSpace(tArray, len);
    printInfo(tArray, 5);
    system("pause");
    return 0;
}

 

 3.C++實現通訊錄管理系統

#include<iostream>
#include<string>
using namespace std;
#define MAX 1000

struct Person
{
    string m_Name;
    int m_Sex;
    int m_Age;
    string m_Phone;
    string m_Addr;
};

struct Addressbooks
{
    struct Person personArray[MAX];
    int m_Size;
};


//顯示菜單
void showMenu()
{
    cout << "*************************" << endl;
    cout << "*****  1.添加聯系人  *****" << endl;
    cout << "*****  2.顯示聯系人  *****" << endl;
    cout << "*****  3.刪除聯系人  *****" << endl;
    cout << "*****  4.查找聯系人  *****" << endl;
    cout << "*****  5.修改聯系人  *****" << endl;
    cout << "*****  6.清空聯系人  *****" << endl;
    cout << "*****  7.打開python  *****" << endl;
    cout << "*****  0.退出" << endl;
    cout << "*************************" << endl;

}


//添加過程
void add_person(Addressbooks * abs)
{
    string name;
    cout << "請輸入姓名" << endl;
    cin >> name;
    abs->personArray[abs->m_Size].m_Name = name;

    cout << "請輸入性別" << endl;
    cout << "1 --- 男" << endl;
    cout << "2 --- 女" << endl;
    int sex = 0;
    while (true)
    {
        cin >> sex;
        if (sex == 1 || sex == 2)
        {
            abs->personArray[abs->m_Size].m_Sex = sex;
            break;
        }
        cout << "輸入有誤,請重新輸入" << endl;
    }


    cout << "請輸入年齡: " << endl;
    int age = 0;
    cin >> age;
    abs->personArray[abs->m_Size].m_Age = age;

    cout << "請輸入聯系電話: " << endl;
    string phone;
    cin >> phone;
    abs->personArray[abs->m_Size].m_Phone = phone;

    cout << "請輸入家庭住址: " << endl;
    string address;
    cin >> address;
    abs->personArray[abs->m_Size].m_Addr = address;

    //更新通信錄人數
    abs->m_Size++;
    cout << "添加成功" << endl;
}

//1.添加聯系人
void addPreson(Addressbooks* abs)
{
    
    //判斷通信錄是否已滿,如果滿了就不再添加
    if (abs->m_Size == MAX)
    {
        cout << "通信錄已滿,無法添加!" << endl;
        return;
    }
    else
    {
        add_person(abs);
        system("pause");
        system("cls");
    }
}


//2.顯示所有的聯系人
void showPerson(Addressbooks* abs)
{ 
//判斷通信錄中人數是否為0,如果為0,提示記錄為空
//如果不為0,顯示記錄的聯系人信息
if (abs->m_Size == 0)
{
    cout << "當前記錄為空" << endl;
}
else
{
    for (int i = 0; i < abs->m_Size; i++)
    {
        cout << "姓名:" << abs->personArray[i].m_Name << endl;
        cout << "性別:" << (abs->personArray[i].m_Sex == 1 ? "":"")<< endl;
        cout << "年齡:" << abs->personArray[i].m_Age << endl;
        cout << "電話:" << abs->personArray[i].m_Phone<< endl;
        cout << "地址:" << abs->personArray[i].m_Addr << endl;
        cout << endl;
    }
}
    system("pause");
    system("cls");
}


//檢測聯系人是否存在,如果存在,返回聯系人所在數組中的具體位置,不存在返回-1
//參數一 通信錄  參數二 對比姓名
int isExist(Addressbooks * abs, string name)
{
    for (int i = 0; i < abs->m_Size; i++)
    {
        if (abs->personArray[i].m_Name == name)
        {
            return i;
        }
    }
    return -1; //如果遍歷結束都沒有找到,返回-1
}



//3.刪除指定的聯系人
void deletePerson(Addressbooks* abs)
{
    cout << "請輸入您要刪除的聯系人" << endl;
    string name;
    cin >> name;

    //ret == -1 未查到
    //ret != -1 查到了
    int ret = isExist(abs, name);
    if (ret != -1)
    {
        for (int i = ret; i < abs->m_Size; i++)
        {
            //數據前移
            abs->personArray[i] = abs->personArray[i + 1];
        }
        cout << "刪除成功!" << endl;
    }
    else
    {
        cout << "查無此人" << endl;
    }
    system("pause");
    system("cls");
}


//4.查找指定的聯系人信息
void findPerson(Addressbooks* abs)
{
    cout << "請輸入您要查找的聯系人" << endl;
    string name;
    cin >> name;

    //判斷指定的聯系人是否存在通信錄中
    int ret = isExist(abs, name);
    if (ret != -1)
    {
        cout << "姓名:" << abs->personArray[ret].m_Name << endl;
        cout << "性別:" << (abs->personArray[ret].m_Sex == 1 ? "" : "") << endl;
        cout << "年齡:" << abs->personArray[ret].m_Age << endl;
        cout << "電話:" << abs->personArray[ret].m_Phone << endl;
        cout << "地址:" << abs->personArray[ret].m_Addr << endl;
        cout << endl;
    }
    else
    {
        cout << "查無此人" << endl;
    }
    system("pause");
    system("cls");
}


//5.修改指定聯系人
void modifyPerson(Addressbooks * abs)
{
    cout << "請輸入您要修改的聯系人" << endl;
    string name;
    cin >> name;

    int ret = isExist(abs, name);

        if (ret != -1)
        {
            abs->m_Size = ret;
            add_person(abs);
            cout << "修改成功!" << endl;
        }
        else
        {
            cout << "查無此人" << endl;
        }
        system("pause");
        system("cls");

}


//6.清空聯系人
void cleanPerson(Addressbooks* abs)
{
    cout << "該操作會刪除全部聯系人,是否確認清空?(y/n)" << endl;
    while (true)
    {
        int choose;
        cin >> choose;
        if (choose == 1 || choose == 2)
        {
            if (choose == 1)
            {
                abs->m_Size = 0;
                cout << "通訊錄已清空!" << endl;
            }
            else
            {
                cout << "已取消刪除!" << endl;
            }
            break;
        }
        else
        {
            cout << "輸入有誤,請重新輸入" << endl;
        }

    }

    system("pause");
    system("cls");
}




int main()
{
    //創建通信錄結構體變量
    Addressbooks abs;
    //初始化通信錄中當前人員個數
    abs.m_Size = 0;
    int select = 0; //創建用戶選擇輸入變量
    while (true)
    {
        showMenu();
        cin >> select;
        switch (select)
        {
        case 1:
            addPreson(&abs);  //利用地址傳遞,可以修改實參
            break;
        case 2:
            showPerson(&abs);
            break;
        case 3:
            deletePerson(&abs);
            break;
        case 4:
            findPerson(&abs);
            break;
        case 5:
            modifyPerson(&abs);
            break;
        case 6:
            cleanPerson(&abs);
            break;
        case 7:
            system("python");
        case 0:
            cout << "歡迎下次使用" << endl;
            system("pause");
            return 0;
            break;
        default:
            break;
        }
    }


    system("pause");
    return 0;
}

 


免責聲明!

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



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