读取CSV文件并使用快速排序对数据进行排序,然后显示结果。

huangapple go评论66阅读模式
英文:

how to read data from csv file by using quick sort and display it

问题

以下是您提供的代码的翻译部分:

#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)
{

	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

<details>
<summary>英文:</summary>

Can I know what is the best possible way to solve this issue, as I&#39;m currently new to C++


The result that I expected was something like this 

id, title, price, date, mileage, fuel_type, transmission, engine_size, doors, colour, body_type, url, sale_date


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
4 ,1999 Volkswagen Golf GTi 1.8 turbo AUM engine V20 British racing green 3 door ,650 ,1999 ,178000 ,Petrol ,Manual ,1.8 ,3 ,Green ,Hatchback ,https://www.ebay.co.uk/itm/114998243091?hash=item1ac66def13:g:7y4AAOSwSEdhL~1m ,25 Sep 2021


But the result I got is like this
    • 0 - -858993460 - -858993460 - - - - -858993460 - - - -
    • 1999 - 0 - -858993460 - - - - -858993460 - - - -
    • 0 - 0 - -858993460 - - - - -858993460 - - - -
    • 5 - 0 - -858993460 - - - - -858993460 - - - -
I have tried a lot of different way to do it, but I&#39;m still struggling to find a way to print all the input from the csv file properly. As it can read according to the id, yet it can&#39;t print all the data from the row specifically.
This is my code
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;fstream&gt;
#include &lt;sstream&gt;
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-&gt;id = id;
newnode-&gt;title = title;
newnode-&gt;price = price;
newnode-&gt;year = year;
newnode-&gt;mileage = mileage;
newnode-&gt;fuel_type = fuel_type;
newnode-&gt;transmission = transmission;
newnode-&gt;engine_size = engine_size;
newnode-&gt;doors = doors;
newnode-&gt;colour = colour;
newnode-&gt;body_type = body_type;
newnode-&gt;url = url;
newnode-&gt;sale_date = sale_date;
newnode-&gt;nextAddress = NULL;
newnode-&gt;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-&gt;id &lt; pivot-&gt;id)
{
if ((*newHead) == NULL)
(*newHead) = cur;
prev = cur;
cur = cur-&gt;nextAddress;
}
else
{
if (prev)
prev-&gt;nextAddress = cur-&gt;nextAddress;
Car* tmp = cur-&gt;nextAddress;
cur-&gt;nextAddress = NULL;
tail-&gt;nextAddress = cur;
tail = cur;
cur = tmp;
}
}
if ((*newHead) == NULL)
(*newHead) = pivot;
(*newEnd) = tail;
return pivot;
}
Car* getTail(Car* cur)
{
while (cur != NULL &amp;&amp; cur-&gt;nextAddress != NULL)
cur = cur-&gt;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, &amp;newHead, &amp;newEnd);
if (newHead != pivot)
{
Car* tmp = newHead;
while (tmp-&gt;nextAddress != pivot)
tmp = tmp-&gt;nextAddress;
tmp-&gt;nextAddress = NULL;
newHead = quickSortRecur(newHead, tmp);
tmp = getTail(newHead);
tmp-&gt;nextAddress = pivot;
}
pivot-&gt;nextAddress = quickSortRecur(pivot-&gt;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-&gt;nextAddress = newnode;
newnode-&gt;prevAddress = tail;
tail = newnode;
}
// Sort the list using quicksort
quickSort(&amp;head);
}
void readCsvFile() {
ifstream file(&quot;carlist(1).csv&quot;);
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 &gt;&gt; id;
getline(ss, title, &#39;,&#39;);
ss &gt;&gt; price;
ss &gt;&gt; year;
ss &gt;&gt; mileage;
getline(ss, fuel_type, &#39;,&#39;);
getline(ss, transmission, &#39;,&#39;);
getline(ss, engine_size, &#39;,&#39;);
ss &gt;&gt; doors;
getline(ss, colour, &#39;,&#39;);
getline(ss, body_type, &#39;,&#39;);
getline(ss, url, &#39;,&#39;);
getline(ss, sale_date, &#39;,&#39;);
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-&gt;id &gt;= price1 &amp;&amp; current-&gt;id &lt;= price2)
{
cout &lt;&lt; current-&gt;id &lt;&lt; &quot;. &quot; &lt;&lt; current-&gt;title &lt;&lt; &quot; - &quot; &lt;&lt; current-&gt;price &lt;&lt; &quot; - &quot;
&lt;&lt; current-&gt;year &lt;&lt; &quot; - &quot; &lt;&lt; current-&gt;mileage &lt;&lt; &quot; - &quot; &lt;&lt; current-&gt;fuel_type &lt;&lt; &quot; - &quot;
&lt;&lt; current-&gt;transmission &lt;&lt; &quot; - &quot; &lt;&lt; current-&gt;engine_size &lt;&lt; &quot; - &quot; &lt;&lt; current-&gt;doors &lt;&lt; &quot; - &quot;
&lt;&lt; current-&gt;colour &lt;&lt; &quot; - &quot; &lt;&lt; current-&gt;body_type &lt;&lt; &quot; - &quot; &lt;&lt; current-&gt;url &lt;&lt; &quot; - &quot;
&lt;&lt; current-&gt;sale_date &lt;&lt; endl;
}
current = current-&gt;nextAddress;
}
cout &lt;&lt; &quot;List ended here!&quot; &lt;&lt; endl;
}
int main()
{
head = NULL;
srand(time(0));
int noOfcar, choice = 1;
int CarID, Year;
string Brand, Type, color;
int p1;
int p2;
cout &lt;&lt; &quot;Enter your searching p1: &quot;;
cin &gt;&gt; p1;
cout &lt;&lt; &quot;Enter your searching p2: &quot;;
cin &gt;&gt; p2;
readCsvFile();
SearchBasedOnPrice(p1, p2);
cout &lt;&lt; endl;
int answer; string word;
cout &lt;&lt; &quot;Do you want to search anything from the list or not? 1 - Yes, 0 - No: &quot;;
cin &gt;&gt; answer;
cin.ignore();
while (answer == 1)
{
cout &lt;&lt; &quot;Enter your searching p1: &quot;;
cin &gt;&gt; p1;
cout &lt;&lt; &quot;Enter your searching p2: &quot;;
cin &gt;&gt; p2;
readCsvFile();
SearchBasedOnPrice(p1, p2);
cout &lt;&lt; &quot;Do you want to edit anything? 1 - Yes, 0 - No: &quot;;
cin &gt;&gt; answer;
int CarID;
if (answer == 1)
{
cout &lt;&lt; &quot;Enter your car ID: &quot;;
cin &gt;&gt; CarID;
}
cout &lt;&lt; &quot;Do you still want to search anything from the list or not? 1 - Yes, 0 - No: &quot;;
cin &gt;&gt; answer;
cin.ignore();
system(&quot;pause&quot;);
system(&quot;cls&quot;);
}
return 0;
}

</details>
# 答案1
**得分**: 1
这段代码是不正确的
ss >> id;
getline(ss, title, ',');
ss >> price;
ss >> year;
ss >> mileage;
考虑一下这种格式 `id,title,price,year,mileage`。这里有四个逗号,但上面的代码只读取了一个逗号,所以是错误的。
这里是一些可能更好的代码
string tmp;
getline(ss, tmp, ',');
id = stoi(tmp);
getline(ss, title, ',');
getline(ss, tmp, ',');
price = stoi(tmp);
getline(ss, tmp, ',');
year = stoi(tmp);
getline(ss, tmp, ',');
mileage = stoi(tmp);
这段代码读取了所有的逗号,但在需要整数的地方,它将数据读入一个字符串 `tmp`,然后使用 `stoi` 将字符串转换为整数。
<details>
<summary>英文:</summary>
This code is incorrect
ss &gt;&gt; id;
getline(ss, title, &#39;,&#39;);
ss &gt;&gt; price;
ss &gt;&gt; year;
ss &gt;&gt; mileage;
Think about this format `id,title,price,year,mileage etc`. There are four commas there but the above code only reads one comma, so it cannot be right.
Here&#39;s some code that should work better
string tmp;
getline(ss, tmp, &#39;,&#39;);
id = stoi(tmp);
getline(ss, title, &#39;,&#39;);
getline(ss, tmp, &#39;,&#39;);
price = stoi(tmp);
getline(ss, tmp, &#39;,&#39;);
year = stoi(tmp);
getline(ss, tmp, &#39;,&#39;);
mileage = stoi(tmp);
This code reads all the commas, but where an integer is required it reads the data into a string `tmp` and then uses `stoi` to convert the string into an integer.
</details>

huangapple
  • 本文由 发表于 2023年2月19日 04:06:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定