Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Collections;
  5. using System.Diagnostics;
  6.  
  7. // -----------------------------------------------------------------------------
  8. // A Drug object holds information about one fee-for-service outpatient drug
  9. // reimbursed by Medi-Cal (California's Medicaid program) to pharmacies.
  10. class Drug : IComparable
  11. {
  12. string name; // brand name, strength, dosage form
  13. string id; // national drug code number
  14. double size; // package size
  15. string unit; // unit of measurement
  16. double quantity; // number of units dispensed
  17. double lowest; // price Medi-Cal is willing to pay
  18. double ingredientCost; // estimated ingredient cost
  19. int numTar; // number of claims with a treatment auth. request
  20. double totalPaid; // total amount paid
  21. double averagePaid; // average paid per prescription
  22. int daysSupply; // total days supply
  23. int claimLines; // total number of claim lines
  24.  
  25. // Properties providing read-only access to every field.
  26. public string Name { get { return name; } }
  27. public string Id { get { return id; } }
  28. public double Size { get { return size; } }
  29. public string Unit { get { return unit; } }
  30. public double Quantity { get { return quantity; } }
  31. public double Lowest { get { return lowest; } }
  32. public double IngredientCost { get { return ingredientCost; } }
  33. public int NumTar { get { return numTar; } }
  34. public double TotalPaid { get { return totalPaid; } }
  35. public double AveragePaid { get { return averagePaid; } }
  36. public int DaysSupply { get { return daysSupply; } }
  37. public int ClaimLines { get { return claimLines; } }
  38.  
  39. public Drug ( string name, string id, double size, string unit,
  40. double quantity, double lowest, double ingredientCost, int numTar,
  41. double totalPaid, double averagePaid, int daysSupply, int claimLines )
  42. {
  43. this.name = name;
  44. this.id = id;
  45. this.size = size;
  46. this.unit = unit;
  47. this.quantity = quantity;
  48. this.lowest = lowest;
  49. this.ingredientCost = ingredientCost;
  50. this.numTar = numTar;
  51. this.totalPaid = totalPaid;
  52. this.averagePaid = averagePaid;
  53. this.daysSupply = daysSupply;
  54. this.claimLines = claimLines;
  55. }
  56.  
  57. // Simple string for debugging purposes, showing only selected fields.
  58. public override string ToString( )
  59. {
  60. return string.Format(
  61. "{0}: {1}, {2}", id, name, size );
  62. }
  63.  
  64. public int CompareTo( object obj )
  65. {
  66. // TODO : Implement this method, replacing the line below
  67. if (obj is Drug)
  68. {
  69. Drug drug = (Drug) obj;
  70.  
  71. if (String.Compare(drug.Name, this.Name) > 0)
  72. return 1;
  73.  
  74. else if (String.Compare(drug.Name, this.Name) < 0)
  75. return -1;
  76.  
  77. else
  78. return 0;
  79. }
  80. else
  81. {
  82. throw new ArgumentException("no comparison is possible");
  83. }
  84. }
  85.  
  86. }
  87.  
  88. // -----------------------------------------------------------------------------
  89. // Linked list of Drugs. A list object holds references to its head and tail
  90. // Nodes and a count of the number of Nodes.
  91. class DrugList
  92. {
  93. // Nodes form the singly linked list. Each node holds one Drug item.
  94. class Node : IComparable
  95. {
  96. Node next;
  97. Drug data;
  98.  
  99. public Node( Drug data ) { next = null; this.data = data; }
  100.  
  101. public Node Next{ get { return next; } set { next = value; } }
  102. public Drug Data{ get { return data; } }
  103.  
  104. public int CompareTo( object obj )
  105. {
  106. // TODO 1: Implement this method, replacing the line below
  107.  
  108. if (obj is Node)
  109. {
  110. Node node = (Node) obj;
  111.  
  112. if (String.Compare(node.Data.Name, this.Data.Name) > 0)
  113. return 1;
  114.  
  115. else if (String.Compare(node.Data.Name, this.Data.Name) < 0)
  116. return -1;
  117.  
  118. else
  119. return 0;
  120. }
  121.  
  122. else
  123. {
  124. throw new ArgumentException("no comparison is possible");
  125. }
  126. }
  127. }
  128.  
  129.  
  130. Node tail;
  131. Node head;
  132. int count;
  133.  
  134. public int Count { get { return count; } }
  135.  
  136. // Constructors:
  137. public DrugList( ) { tail = null; head = null; count = 0; }
  138. public DrugList( string drugFile ) : this( ) { AppendFromFile( drugFile ); }
  139.  
  140. // Methods which add elements:
  141. // Build this list from a specified drug file.
  142. public void AppendFromFile( string drugFile )
  143. {
  144. using( StreamReader sr = new StreamReader( drugFile ) )
  145. {
  146. while( ! sr.EndOfStream )
  147. {
  148. string line = sr.ReadLine( );
  149.  
  150. // Extract drug information
  151. string name = line.Substring( 7, 30 ).Trim( );
  152. string id = line.Substring( 37, 13 ).Trim( );
  153. string temp = line.Substring( 50, 14 ).Trim( );
  154. double size
  155. = double.Parse( temp.Substring( 0 , temp.Length - 2 ) );
  156. string unit = temp.Substring( temp.Length - 2, 2 );
  157. double quantity = double.Parse( line.Substring( 64, 16 ) );
  158. double lowest = double.Parse( line.Substring( 80, 10 ) );
  159. double ingredientCost
  160. = double.Parse( line.Substring( 90, 12 ) );
  161. int numTar = int.Parse( line.Substring( 102, 8 ) );
  162. double totalPaid = double.Parse( line.Substring( 110, 14 ) );
  163. double averagePaid = double.Parse( line.Substring( 124, 10 ) );
  164. int daysSupply
  165. = ( int ) double.Parse( line.Substring( 134, 14 ) );
  166. int claimLines = int.Parse( line.Substring( 148 ) );
  167.  
  168. // Put drug onto this list of drugs.
  169. Append( new Drug( name, id, size, unit, quantity, lowest,
  170. ingredientCost, numTar, totalPaid, averagePaid,
  171. daysSupply, claimLines ) );
  172. }
  173. }
  174. }
  175.  
  176. // Add a new Drug item to the end of this linked list.
  177. public void Append( Drug data )
  178. {
  179. if (data == null )
  180. {
  181. return;
  182. }
  183.  
  184. if (head == null)
  185. {
  186. head = tail = new Node(data);
  187. count++;
  188. }
  189.  
  190. else
  191. {
  192. Node newDrug = new Node(data);
  193. tail.Next = newDrug;
  194. tail = newDrug;
  195. count++;
  196. }
  197. }
  198.  
  199. // Remove the minimal Drug
  200. public Drug RemoveMin( )
  201. {
  202. // TODO 3: Complete this method, replacing the following statement.
  203. Node current = head;
  204. Node minimum = head;
  205.  
  206. if (head == null)
  207. {
  208. return null;
  209. }
  210.  
  211.  
  212. while (current != null)
  213. {
  214. if (current.CompareTo(minimum) > 0)
  215. {
  216. minimum = current;
  217. current = current.Next;
  218. }
  219.  
  220. else
  221. current = current.Next;
  222. }
  223.  
  224. current = head;
  225.  
  226. if (head == minimum)
  227. {
  228. count--;
  229. head = head.Next;
  230. return minimum.Data;
  231. }
  232.  
  233. while (current.Next != minimum)
  234. {
  235. current = current.Next;
  236. }
  237.  
  238. if (tail == minimum)
  239. {
  240. count--;
  241. current.Next = null;
  242. tail = current;
  243. return minimum.Data;
  244. }
  245.  
  246. else
  247. {
  248. count--;
  249. current.Next = (current.Next).Next;
  250. return minimum.Data;
  251. }
  252.  
  253. }
  254.  
  255. // Methods which sort the list:
  256. // Sort this list by selection sort.
  257. public void SelectSort( )
  258. {
  259. int length = count;
  260. DrugList sorted = new DrugList();
  261.  
  262. if (head == null)
  263. {
  264. return;
  265. }
  266.  
  267. int i = 0;
  268. while(i < length)
  269. {
  270. sorted.Append(RemoveMin());
  271. i++;
  272. }
  273.  
  274. head = sorted.head;
  275. tail = sorted.tail;
  276. count = sorted.count;
  277. }
  278.  
  279. // Methods which extract the Drugs:
  280. // Return, as an array, references to all the Drug objects on the list.
  281. public Drug[ ] ToArray( )
  282. {
  283. Drug[ ] result = new Drug[ count ];
  284. int nextIndex = 0;
  285. Node current = head;
  286. while( current != null )
  287. {
  288. result[ nextIndex ] = current.Data;
  289. nextIndex ++;
  290. current = current.Next;
  291. }
  292. return result;
  293. }
  294.  
  295. // Return a collection of references to the Drug items on this list which
  296. // can be used in a foreach loop. Understanding enumerations and the
  297. // 'yield return' statement is beyond the scope of the course.
  298. public IEnumerable< Drug > Enumeration
  299. {
  300. get
  301. {
  302. Node current = head;
  303. while( current != null )
  304. {
  305. yield return current.Data;
  306. current = current.Next;
  307. }
  308. }
  309. }
  310. }
  311.  
  312. // -----------------------------------------------------------------------------
  313. // Test the linked list of Drugs.
  314. static class Program
  315. {
  316. static void Main( )
  317. {
  318. Stopwatch timer = new Stopwatch();
  319. timer.Start();
  320. DrugList drugs = new DrugList( "RXQT1402.txt" );
  321. drugs.SelectSort( );
  322. foreach( Drug d in drugs.ToArray( ) ) Console.WriteLine( d );
  323. timer.Stop();
  324. Console.WriteLine(timer.Elapsed);
  325. }
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement