如何从 csv 文件中读取数据并使用快速排序进行排序

问题描述 投票:0回答:0

我能知道解决这个问题的最佳方法是什么吗,因为我目前是 c++ 的新手

我期待的结果是这样的

3   ,Volkswagen Golf 1.9 TDI 11 Months MOT  ,650    ,2000   ,155822 Diesel  ,Manual ,1.9    ,4  ,Silver ,Hatchback  ,https://www.ebay.co.uk/itm/124907646705?hash=item1d15136ef1:g:8dsAAOSwb-thRawR  ,25 Sep 2021

但是我得到的结果是这样的

3.  - 0 - -858993460 - -858993460 -  -  -  - -858993460 -  -  -  -
4.  - 1999 - 0 - -858993460 -  -  -  - -858993460 -  -  -  -
5.  - 0 - 0 - -858993460 -  -  -  - -858993460 -  -  -  -
6.  - 5 - 0 - -858993460 -  -  -  - -858993460 -  -  -  -

我已经尝试了很多不同的方法来做到这一点,但我仍在努力寻找一种方法来正确打印 csv 文件中的所有内容。因为它可以根据id读取,但它不能打印特定行的所有数据

这是我的代码


    #include <iostream>
    #include <string>
    #include <fstream>
    #include <sstream>
    using namespace std;
    
    int sizeofLinkedList = 0;
    struct Car
    {
    
        int id;
        string title, fuel_type, transmission, engine_size,  colour, body_type, url, sale_date;
        int price;
        int year;
        int mileage;
        int doors;
    
        Car* nextAddress;
        Car* prevAddress;
    } *head, * tail;
    
    Car* CreateNewNnode(int id, string title, int price, int year, int mileage ,string fuel_type, string transmission,
        string engine_size, int doors, string colour, string body_type, string url, string sale_date) //create a newnode - use for any insert
    {
    
    
        Car* newnode = new Car;
        newnode->id = id;
        newnode->title = title;
        newnode->price = price;
        newnode->year = year;
        newnode->mileage = mileage;
        newnode->fuel_type = fuel_type;
        newnode->transmission = transmission;
        newnode->engine_size = engine_size;
        newnode->doors = doors;
        newnode->colour = colour;
        newnode->body_type = body_type;
        newnode->url = url;
        newnode->sale_date = sale_date;
        newnode->nextAddress = NULL;
        newnode->prevAddress = NULL;
        return newnode;
    }
    
    Car* partition(Car* head, Car* end, Car** newHead, Car** newEnd)
    {
        Car* pivot = end;
        Car* prev = NULL, * cur = head, * tail = pivot;
    
        while (cur != pivot)
        {
    
            if (cur->id < pivot->id)
            {
                if ((*newHead) == NULL)
                    (*newHead) = cur;
    
                prev = cur;
                cur = cur->nextAddress;
            }
            else
            {
                if (prev)
                    prev->nextAddress = cur->nextAddress;
    
                Car* tmp = cur->nextAddress;
                cur->nextAddress = NULL;
                tail->nextAddress = cur;
                tail = cur;
                cur = tmp;
            }
        }
    
        if ((*newHead) == NULL)
            (*newHead) = pivot;
    
        (*newEnd) = tail;
    
        return pivot;
    }
    
    
    
    Car* getTail(Car* cur)
    {
        while (cur != NULL && cur->nextAddress != NULL)
            cur = cur->nextAddress;
    
        return cur;
    }
    
    Car* quickSortRecur(Car* head, Car* end)
    {
        if (!head || head == end)
            return head;
    
        Car* newHead = NULL, * newEnd = NULL;
        Car* pivot = partition(head, end, &newHead, &newEnd);
    
        if (newHead != pivot)
        {
            Car* tmp = newHead;
            while (tmp->nextAddress != pivot)
                tmp = tmp->nextAddress;
    
            tmp->nextAddress = NULL;
            newHead = quickSortRecur(newHead, tmp);
            tmp = getTail(newHead);
            tmp->nextAddress = pivot;
        }
    
        pivot->nextAddress = quickSortRecur(pivot->nextAddress, newEnd);
    
        return newHead;
    }
    
    void quickSort(Car** headRef)
    {
        (*headRef) = quickSortRecur(*headRef, getTail(*headRef));
    }
    
    // Modified insertIntoASortedList to use quicksort
    void insertIntoASortedList(Car* newnode)
    {
        // Insert new node at the end of the list
        if (head == NULL)
        {
            head = tail = newnode;
        }
        else
        {
            tail->nextAddress = newnode;
            newnode->prevAddress = tail;
            tail = newnode;
        }
    
        // Sort the list using quicksort
        quickSort(&head);
    }
    
    void readCsvFile() {
        ifstream file("carlist(1).csv");
        string line;
        int id;
        string title, fuel_type, transmission, engine_size, colour, body_type, url, sale_date;
        int price;
        int year;
        int mileage;
        int doors;
    
        while (getline(file, line)) {
            stringstream ss(line);
            int id;
            string title, fuel_type, transmission, engine_size, colour, body_type, url, sale_date;
            int price;
            int year;
            int mileage;
            int doors;
    
            ss >> id;
            getline(ss, title, ',');
            ss >> price;
            ss >> year;
            ss >> mileage;
            getline(ss, fuel_type, ',');
            getline(ss, transmission, ',');
            getline(ss, engine_size, ',');
            ss >> doors;
            getline(ss, colour, ',');
            getline(ss, body_type, ',');
            getline(ss, url, ',');
            getline(ss, sale_date, ',');
    
    
            Car* newnode = CreateNewNnode(id, title, price, year, mileage, fuel_type, transmission, 
                engine_size, doors, colour, body_type, url, sale_date);
            insertIntoASortedList(newnode);
        }
    }
    
    
    
    void SearchBasedOnPrice(int price1, int price2)
    {
        Car* current = head;
    
        while (current != NULL)
        {
            if (current->id >= price1 && current->id <= price2)
            {
                cout << current->id << ". " << current->title << " - " << current->price << " - "
                    << current->year << " - " << current->mileage << " - " << current->fuel_type << " - "
                    << current->transmission << " - " << current->engine_size << " - " << current->doors << " - "
                    << current->colour << " - " << current->body_type << " - " << current->url << " - "
                    << current->sale_date << endl;
            }
            current = current->nextAddress;
        }
        cout << "List ended here!" << endl;
    }
    
    int main()
    {
        head = NULL;
    
        srand(time(0));
        int noOfcar, choice = 1;
        int CarID, Year;
        string Brand, Type, color;
        int p1;
        int p2;
    
        cout << "Enter your searching p1: ";
        cin >> p1;
        cout << "Enter your searching p2: ";
    
        cin >> p2;
        readCsvFile();
        SearchBasedOnPrice(p1, p2);
    
    
        cout << endl;
        int answer; string word;
        cout << "Do you want to search anything from the list or not? 1 - Yes, 0 - No: ";
        cin >> answer;
        cin.ignore();
    
    
        while (answer == 1)
        {
    
    
            cout << "Enter your searching p1: ";
            cin >> p1;
            cout << "Enter your searching p2: ";
    
            cin >> p2;
            readCsvFile();
            SearchBasedOnPrice(p1, p2);
    
            cout << "Do you want to edit anything? 1 - Yes, 0 - No: ";
            cin >> answer;
    
            int CarID;
            if (answer == 1)
            {
                cout << "Enter your car ID: ";
                cin >> CarID;
                
            }
    
            cout << "Do you still want to search anything from the list or not? 1 - Yes, 0 - No: ";
            cin >> answer;
            cin.ignore();
            system("pause");
            system("cls");
        }
        return 0;
    }

c++ visual-studio csv data-structures read.csv
© www.soinside.com 2019 - 2024. All rights reserved.