Advertisement
Iain_Barkley

lab 8 code

Nov 19th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. //Module 2
  2. //hydro.cpp
  3.  
  4. #include<iostream>
  5. #include <fstream>
  6. using namespace std;
  7. #include "list.cpp"
  8. #include "hydro.h"
  9.  
  10.  
  11.  
  12. int main()
  13. {
  14. Flowlist list();
  15. int records;
  16. displayHeader(list, records);
  17.  
  18.  
  19.  
  20. int choice;
  21. while(1){
  22. choice = menu();
  23.  
  24. //Display records
  25. if(choice == 1){
  26. display(list, records);
  27. pressEnter();
  28. }
  29.  
  30. //Add Data to linked list
  31. if(choice == 2){
  32. addData(list, records);
  33. pressEnter();
  34. }
  35.  
  36. //save data to text file
  37. if(choice == 3){
  38. saveData(list, records);
  39. pressEnter();
  40. }
  41.  
  42. //remove data from linked list
  43. if(choice == 4){
  44. void removeData(list, records);
  45. pressEnter();
  46. }
  47.  
  48. if(choice == 5){
  49. cout<<"program terminated";
  50. break;
  51. }
  52. }
  53.  
  54. return 0;
  55. }
  56.  
  57. void displayHeader(Flowlist &list, int &records){
  58. cout<<"Program: Flow Studies - Fall 2017"<<endl;
  59. cout<<"Version: 1.0"<<endl;
  60. cout<<"Lab section: B03"<<endl;
  61. cout<<"Produced by: "<<endl;
  62. pressEnter();
  63. records = readData(list);
  64. }
  65.  
  66. void pressEnter(){
  67. cout<<"<<< Press Enter to Continue >>>"<<endl;
  68. while(1){
  69. if (cin.get() == '\n')
  70. break;
  71. }
  72. }
  73.  
  74. int readData(Flowlist &list)
  75. {
  76. int rec = 0;
  77. ifstream flow("flow.txt", ios::in);
  78. if(flow.fail()){
  79. cerr<<"file not found"<<endl;
  80. exit(1);
  81. }
  82.  
  83. Node *item = new Node;
  84. while(!flow.eof()){
  85. flow>>item.year>>item.flow;
  86. list->Flowlist::insert(item);
  87. rec++;
  88. }
  89. flow.close;
  90. return rec;
  91. }
  92.  
  93. int menu()
  94. {
  95. cout<<"Please select on the following operations"<<endl;
  96. cout<<" 1. Display flow list, average and median"<<endl;
  97. cout<<" 2. Add data."<<endl;
  98. cout<<" 3. Save data into the file"<<endl;
  99. cout<<" 4. Remove data"<<endl;
  100. cout<<" 5. Quit"<<endl;
  101. cout<<" Enter your choice (1, 2, 3, 4, of 5): "<<endl;
  102. int choice = 0;
  103. while((choice < 1) || (choice > 5))
  104. cin>>choice;
  105.  
  106. return choice;
  107. }
  108.  
  109. //Option 1
  110. void display(const Flowlist &list, const int &records)
  111. {
  112.  
  113. cout<<"Year Flow (in billions of cubic meters)"<<endl;
  114. cout<<"---- ----------------------------------"<<endl;
  115.  
  116.  
  117. for(int i = 0; i < records; i++)
  118. cout<<list.Flowlist::get_year(i)<<" "<<list.Flowlist::get_flow(i);
  119.  
  120. double ave = average(list, records);
  121. double med = median(list, records);
  122. cout<<"The annual average of the flow is: "<<ave<<" millions cubic meter"<<endl;
  123. cout<<"The median flow is: "<<med<<" millions cubic meter."<<endl;
  124. }
  125.  
  126. double average(Flowlist &list, int &records)
  127. {
  128. double sum = 0;
  129.  
  130. for(int i = 0; i < records; i++)
  131. {
  132. sum += list.Flowlist::get_flow(records);
  133. }
  134.  
  135. return sum/records;
  136. }
  137.  
  138. double median(Flowlist &list, int &records)
  139. {
  140. double median = 0;
  141. Node *middle;
  142. if(records%2 = 0)
  143. {
  144. middle = Flowlist::read_item(list, records/2);
  145. median += middle.item.flow;
  146.  
  147. middle = Flowlist::read_item(list, ((records/2)+1));
  148. median += middle.item.flow;
  149.  
  150. median = median/2;
  151. }
  152. else
  153. {
  154. middle = Flowlist::read_item(list, ((records/2)+1));
  155. median += middle.item.flow;
  156. }
  157.  
  158. return median;
  159. }
  160.  
  161. //Option 2
  162. void addData(Flowlist &list, int &records)
  163. {
  164. Node* n = new Node;
  165. cout<<"Please enter a year:";
  166. cin>>n.item.year;
  167. cout<<endl<<"Please enter the flow";
  168. cin>>n.item.flow;
  169. if(!Flowlist::year_exists(n.year))
  170. {
  171. cout<<"Error: duplicate data."<<endl;
  172. pressEnter();
  173. }
  174. else
  175. {
  176. Flowlist::insert(list, n);
  177. records++;
  178. }
  179. }
  180.  
  181. //Option 3
  182. void saveData(Flowlist &list, int &records)
  183. {
  184. ofstream flow("flow.txt", ios::out);
  185. if(flow.fail()){
  186. cerr<<"file not found"<<endl;
  187. return;
  188. }
  189.  
  190. for(int i = 0; i < records; i++)
  191. flow<<list.Flowlist::get_year(i)<<" "<<list.Flowlist::get_flow(i);
  192.  
  193. }
  194.  
  195. //Option 4
  196. void removeData(Flowlist &list, int &records)
  197. {
  198. int y;
  199. cout<<"Please enter the year that you want to remove: ";
  200. cin>>y;
  201. cout<<endl;
  202.  
  203. if(!list.Flowlist::year_exists(y)){
  204. cerr<<"Error: year is not in list"<<endl;
  205. return;
  206. }
  207.  
  208. list.Flowlist::removeyear(y);
  209. records--;
  210. return;
  211.  
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement