尝试在不使用预定义排序函数C ++

时间:2018-04-27 21:07:43

标签: c++

我正在尝试为大学项目设置一个有序列表,按照字母顺序和数字方式将电影标题按顺序添加到按顺序排序。我已经尝试使用我已经使用过的数字的有序列表,但是在使用字符串时,add函数没有对它们进行排序。

任何人都可以帮我添加功能吗?我需要它添加我放入数组的第三个条目作为第一个,因为它应该在一个排序列表中。

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

class OList
{
private:
    string data[3];
    int count; //First empty slot
public:
    OList();
    bool isEmpty();
    bool isFull();
    void Add(string newVal);
    string Remove(string newVal);
    bool LinearSearch(string searchVal);
    void Display();
};

OList::OList()
{
    count = 0;
}

bool OList::isEmpty()
{
    if (count == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool OList::isFull()
{
    if (count == 3)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void OList::Add(string movie)
{
    data[count] = movie;
    int i ;
    if (isFull())
        return;

    //Find the insertion point
    for (i = 0; i < count; i++);

    if (i == count)
        data[i] = movie; //Copy in the new value
    else

        //Make a Space
        for (int j = count - 1; j >= i; j--)
        {
            data[j + 1] = data[j];
        }
    data[i] = movie; //Copy in the new value

    count++;
}

string OList::Remove(string movie)
{
    int i = LinearSearch(movie);
    string temp = movie;

    if (isEmpty())
        return false;
    i++;

    while (i < count - 1)
    {
        data[i] = data[i + 1];
        i++;
    }

    count--;
    return temp;
}

bool OList::LinearSearch(string searchVal)
{
    for (int i = 0; (i < count) || (data[i] > searchVal); i++)
    {
        if (data[i] == searchVal)
            return -1;
    }
    return false;
}

void OList::Display()
{
    for (int i = 0; i < count; i++)
    {
        cout << data[i] << endl;
    }
}



int main()
{
    OList movies;
    string movie;



    movies.Add("Yeh Jawaani Hai Deewani | 1, 790, 000, 000 | Ayan Mukerji");
    movies.Add("Dhoom 3 | 2,840,000,000 | Vijay Krishna Acharya");
    movies.Add("Chennai Express | 2, 275, 000, 000 | Rohit Shetty");

    movies.Display();

    std::cout << endl;

    movies.Remove("Chennai Express | 2, 275, 000, 000 | Rohit Shetty");



    if (!movies.isEmpty())
    {
        std::cout << "The queue is not empty!" << endl;
    }

    std::cout << endl;

    movies.Display();

    system("pause");

    return 0;
}

0 个答案:

没有答案