Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Module 2
- //hydro.cpp
- #include<iostream>
- #include <fstream>
- using namespace std;
- #include "list.cpp"
- #include "hydro.h"
- int main()
- {
- Flowlist list();
- int records;
- displayHeader(list, records);
- int choice;
- while(1){
- choice = menu();
- //Display records
- if(choice == 1){
- display(list, records);
- pressEnter();
- }
- //Add Data to linked list
- if(choice == 2){
- addData(list, records);
- pressEnter();
- }
- //save data to text file
- if(choice == 3){
- saveData(list, records);
- pressEnter();
- }
- //remove data from linked list
- if(choice == 4){
- void removeData(list, records);
- pressEnter();
- }
- if(choice == 5){
- cout<<"program terminated";
- break;
- }
- }
- return 0;
- }
- void displayHeader(Flowlist &list, int &records){
- cout<<"Program: Flow Studies - Fall 2017"<<endl;
- cout<<"Version: 1.0"<<endl;
- cout<<"Lab section: B03"<<endl;
- cout<<"Produced by: "<<endl;
- pressEnter();
- records = readData(list);
- }
- void pressEnter(){
- cout<<"<<< Press Enter to Continue >>>"<<endl;
- while(1){
- if (cin.get() == '\n')
- break;
- }
- }
- int readData(Flowlist &list)
- {
- int rec = 0;
- ifstream flow("flow.txt", ios::in);
- if(flow.fail()){
- cerr<<"file not found"<<endl;
- exit(1);
- }
- Node *item = new Node;
- while(!flow.eof()){
- flow>>item.year>>item.flow;
- list->Flowlist::insert(item);
- rec++;
- }
- flow.close;
- return rec;
- }
- int menu()
- {
- cout<<"Please select on the following operations"<<endl;
- cout<<" 1. Display flow list, average and median"<<endl;
- cout<<" 2. Add data."<<endl;
- cout<<" 3. Save data into the file"<<endl;
- cout<<" 4. Remove data"<<endl;
- cout<<" 5. Quit"<<endl;
- cout<<" Enter your choice (1, 2, 3, 4, of 5): "<<endl;
- int choice = 0;
- while((choice < 1) || (choice > 5))
- cin>>choice;
- return choice;
- }
- //Option 1
- void display(const Flowlist &list, const int &records)
- {
- cout<<"Year Flow (in billions of cubic meters)"<<endl;
- cout<<"---- ----------------------------------"<<endl;
- for(int i = 0; i < records; i++)
- cout<<list.Flowlist::get_year(i)<<" "<<list.Flowlist::get_flow(i);
- double ave = average(list, records);
- double med = median(list, records);
- cout<<"The annual average of the flow is: "<<ave<<" millions cubic meter"<<endl;
- cout<<"The median flow is: "<<med<<" millions cubic meter."<<endl;
- }
- double average(Flowlist &list, int &records)
- {
- double sum = 0;
- for(int i = 0; i < records; i++)
- {
- sum += list.Flowlist::get_flow(records);
- }
- return sum/records;
- }
- double median(Flowlist &list, int &records)
- {
- double median = 0;
- Node *middle;
- if(records%2 = 0)
- {
- middle = Flowlist::read_item(list, records/2);
- median += middle.item.flow;
- middle = Flowlist::read_item(list, ((records/2)+1));
- median += middle.item.flow;
- median = median/2;
- }
- else
- {
- middle = Flowlist::read_item(list, ((records/2)+1));
- median += middle.item.flow;
- }
- return median;
- }
- //Option 2
- void addData(Flowlist &list, int &records)
- {
- Node* n = new Node;
- cout<<"Please enter a year:";
- cin>>n.item.year;
- cout<<endl<<"Please enter the flow";
- cin>>n.item.flow;
- if(!Flowlist::year_exists(n.year))
- {
- cout<<"Error: duplicate data."<<endl;
- pressEnter();
- }
- else
- {
- Flowlist::insert(list, n);
- records++;
- }
- }
- //Option 3
- void saveData(Flowlist &list, int &records)
- {
- ofstream flow("flow.txt", ios::out);
- if(flow.fail()){
- cerr<<"file not found"<<endl;
- return;
- }
- for(int i = 0; i < records; i++)
- flow<<list.Flowlist::get_year(i)<<" "<<list.Flowlist::get_flow(i);
- }
- //Option 4
- void removeData(Flowlist &list, int &records)
- {
- int y;
- cout<<"Please enter the year that you want to remove: ";
- cin>>y;
- cout<<endl;
- if(!list.Flowlist::year_exists(y)){
- cerr<<"Error: year is not in list"<<endl;
- return;
- }
- list.Flowlist::removeyear(y);
- records--;
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement