0% found this document useful (0 votes)
7 views3 pages

Laptop Sorting Program in C++

The document is a C++ program that manages a list of laptops using a struct to store their details. It includes functions to read laptop data from a file, sort the laptops by price using bubble sort, and output the sorted list to another file. The program utilizes pointers for passing the number of laptops and demonstrates basic file handling and sorting techniques.

Uploaded by

rovlasw
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Laptop Sorting Program in C++

The document is a C++ program that manages a list of laptops using a struct to store their details. It includes functions to read laptop data from a file, sort the laptops by price using bubble sort, and output the sorted list to another file. The program utilizes pointers for passing the number of laptops and demonstrates basic file handling and sorting techniques.

Uploaded by

rovlasw
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <iostream>

#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

//Lab1 with full comments


//version 3.0
//Wang Buqing
//X00192835
//use pointer, struct,bubble sroting

using namespace std; //declare standard function used below, same as std::xxx

struct Laptop //create a laptop stuct with 4 elements


{
string make;
string model;
int year;
float price;
};

// Declare function
void getLaptops(Laptop LaptopArray[], int* P_numLaptop); //use laptop struct to create a array
with 4 vectors as laptop struct. belows are same as it
//P_numLaptop is a pointer to numLaptop, aims to operate the variable stored in main
//P_numLaptop passed by reference
void sortLaptopsByPrice(Laptop LaptopArray[], int numLaptop);//pass by value, because it only
uses value of numLaptop

void outLaptops(const Laptop LaptopArray[], int numLaptop); //pass by value

int main() {
Laptop LaptopArray[10]; // Declare array of Laptops to store laptop data, this array has 4
vectors as same as struct Laptop
int numLaptop; // Declare number of laptop, and later the function getLaptops can use this
variable by pointer

getLaptops(LaptopArray, &numLaptop); //find the address of numLaptop and input it into


function getLaptops, in order to operate the variable stored in main
sortLaptopsByPrice(LaptopArray, numLaptop); //pass by value

outLaptops(LaptopArray, numLaptop); //the same as over

return 0;
}

//input
void getLaptops(Laptop LaptopArray[], int* P_numLaptop) { //use pointer to get real value of it in
main stored area
ifstream ip("[Link]"); // read all message from file and input all of them in to ip

// Check if file opening failed


if (!ip) {
cout << "Error opening file." << endl;
return;
}

// Main function
ip >> *P_numLaptop; // get number of laptops and put it into numLaptop in main function

for (int i = 0; i < *P_numLaptop && i < 10; i++) { //set limit as 10
string make; // declare variable
string model;
int year;
float price;
ip >> make >> model >> year >> price; //intput as order
LaptopArray[i].make = make; //transfer message into each laptoparray vectors
LaptopArray[i].model = model;
LaptopArray[i].year = year;
LaptopArray[i].price = price;
}
[Link]();
}

//bubble sorting
void sortLaptopsByPrice(Laptop LaptopArray[], int numLaptop) {
for (int i = 0; i < numLaptop - 1; i++) { //this loop 1 controls total number of sorting rounds
for (int j = 0; j < numLaptop - i - 1; j++) { //this loop 2 can swap each elements nearby, put
the better one back. finall can find the biggest one at last
//and in next loop, due to loop1, the swap wont operate the bigest one which sorted last
round
if (LaptopArray[j].price > LaptopArray[j + 1].price) { // if previous one is bigger than later
one. then swap them use std::swap function
swap(LaptopArray[j], LaptopArray[j + 1]);
}
}
}
}

//output
void outLaptops(const Laptop LaptopArray[], int numLaptop) {
ofstream op("[Link]"); //output data from op into file

// Check if file opening failed


if (!op) {
cout << "Error opening file." << endl;
return;
}

// Main function
op << numLaptop << endl;

for (int i = 0; i < numLaptop; i++) { //input each vector of araay into op file as order
op << LaptopArray[i].make << " " << LaptopArray[i].model << " " << LaptopArray[i].year << " "
<< LaptopArray[i].price << endl;
}

[Link]();
}

You might also like