#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]();
}