Linked lists

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

Linked lists

 
0
  #1
Dec 2nd, 2008
I'm having trouble making this program with linked lists work. Where I am having a problem is in printarea()

  1. //****************************************************************
  2. // This program tests creating dynamic linked list of
  3. // several Dynamic (descendents)
  4. // It uses: shape1.h header file
  5. // Filename: prtshape.cpp
  6. //****************************************************************
  7. #include <iomanip>
  8. #include <iostream>
  9. #include "shape1.h"
  10.  
  11. using namespace std;
  12.  
  13. struct Shapelink
  14. {
  15. char type;
  16. Shape *item;
  17. Shapelink *next;
  18. };
  19.  
  20. class Dynamic
  21. {
  22. protected:
  23. Shapelink *lastone;
  24. void linkup (Shape *, char);
  25. public:
  26. Dynamic (void);
  27. void createspace (int);
  28. void printarea (void);
  29. int menu(void);
  30. };
  31.  
  32. //-------------------------- Dynamic methods ----------------------------
  33.  
  34. Dynamic::Dynamic()
  35. {
  36. lastone = NULL;
  37. }
  38.  
  39. void
  40. Dynamic::linkup(Shape *newshape, char type)
  41. {
  42. Shapelink *another;
  43.  
  44. another = new Shapelink;
  45. another->type = type;
  46. another->item = newshape;
  47. another->next = lastone;
  48. lastone = another;
  49. }
  50.  
  51. void
  52. Dynamic::createspace (int type)
  53. {
  54. Circle *cir;
  55. Rectangle *rect;
  56. Triangle *tri;
  57. Square *sq;
  58. float x, y, z;
  59.  
  60. switch (type)
  61. {
  62. case 1:
  63. cir = new Circle;
  64. cout << "Enter the circle's radius ";
  65. cin >> x;
  66. cir->store_radius(x);
  67. linkup(cir,'c');
  68. break;
  69. case 2:
  70. rect = new Rectangle;
  71. cout << "Enter the rectangle's length:" << endl;
  72. cin >> x;
  73. cout << "Enter the retangle's width:" << endl;
  74. cin >> y;
  75. rect->store_sides(x,y);
  76. linkup(rect,'r');
  77. break;
  78. case 3:
  79. tri = new Triangle;
  80. cout << "Enter the triangle's three sides:" << endl;
  81. cin >> x >> y >> z;
  82. tri->store_sides(x,y,z);
  83. linkup(tri,'t');
  84. break;
  85. case 4:
  86. sq = new Square;
  87. cout << "Enter the square's side:" << endl;
  88. cin >> x;
  89. sq->store_side(x);
  90. linkup(sq,'t');
  91. break;
  92. case 5:
  93. printarea();
  94. break;
  95. case 6:
  96. system("exit");
  97. default:
  98. cout << "Error: wrong choice!" << endl;
  99. break;
  100. }
  101. }
  102.  
  103. int
  104. Dynamic::menu()
  105. {
  106. int x;
  107. cout << "Choose one:" << endl;
  108. cout << "1: Circle" << endl;
  109. cout << "2: Rectangle" << endl;
  110. cout << "3: Triangle" << endl;
  111. cout << "4: Square" << endl;
  112. //cout << "5: Print area's" << endl;
  113. cout << "6: Exit" << endl;
  114. cin >> x;
  115. return x;
  116. }
  117.  
  118. void
  119. Dynamic::printarea()
  120. {
  121. Dynamic start;
  122. Shape *local = new Shape;
  123. Shapelink *shape;
  124. shape = lastone;
  125.  
  126. while(lastone != NULL)
  127. {
  128. shape = lastone;
  129. lastone = lastone->next;
  130. shape->local.calc_area();
  131. }
  132. }
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139. //-------------------------- normal functions --------------------------
  140.  
  141.  
  142. //-------------------------- main() ----------------------------
  143.  
  144. int
  145. main()
  146. {
  147. Dynamic start;
  148. int choice;
  149.  
  150. choice = start.menu();
  151. while (choice >= 1 && choice <= 4)
  152. {
  153. start.createspace(choice);
  154. choice = start.menu();
  155. }
  156.  
  157.  
  158. start.printarea();
  159.  
  160. system("pause");
  161. return 0;
  162.  
  163. }

Here is the class that does all of the calculations:
  1. //******************************************************************
  2. // This is a header file with a class hierarchy as follows
  3. // Shape
  4. // Circle Rectangle Triangle
  5. // Cylinder Sphere Square
  6. //
  7. // Filename: shape1.h
  8. //******************************************************************
  9.  
  10. #include <cmath>
  11. #include <iostream>
  12.  
  13. using namespace std;
  14.  
  15. class Shape
  16. {
  17. public:
  18. float return_area ();
  19. float return_perimeter ();
  20. void print_area();
  21. void print_perimeter();
  22. virtual float calc_area ();
  23. virtual float calc_perimeter ();
  24. };
  25.  
  26. class Circle : public Shape
  27. {
  28. protected:
  29. float radius;
  30. public:
  31. void store_radius(float);
  32. float return_radius ();
  33. virtual float calc_perimeter(void);
  34. virtual float calc_area(void);
  35. };
  36.  
  37. class Rectangle : public Shape
  38. {
  39. protected:
  40. float length, width;
  41. public:
  42. void store_sides (float, float);
  43. void return_sides (float &, float &);
  44. virtual float calc_area ();
  45. virtual float calc_perimeter ();
  46. };
  47.  
  48. class Triangle : public Shape
  49. {
  50. protected:
  51. float side1, side2, side3;
  52. public:
  53. void store_sides (float, float, float);
  54. void return_sides (float &, float &, float &);
  55. virtual float calc_area ();
  56. virtual float calc_perimeter ();
  57. };
  58.  
  59. class Cylinder : public Circle
  60. {
  61. protected:
  62. float length;
  63. public:
  64. void store_length(float);
  65. float return_length();
  66. virtual float calc_area ();
  67. float calc_volume ();
  68. };
  69.  
  70. class Sphere : public Circle
  71. {
  72. public:
  73. virtual float calc_area ();
  74. float calc_volume ();
  75. };
  76.  
  77. class Square : public Rectangle
  78. {
  79. public:
  80. void store_side (float);
  81. float return_side (void);
  82. };
  83.  
  84. //------------------ Shape's methods --------------------
  85.  
  86. float
  87. Shape::return_area ()
  88. {
  89. return calc_area();
  90. }
  91.  
  92. float
  93. Shape::return_perimeter ()
  94. {
  95. return calc_perimeter();
  96. }
  97.  
  98. void
  99. Shape::print_area()
  100. {
  101. cout << "The area is : " << calc_area() << endl;
  102. }
  103.  
  104. void
  105. Shape::print_perimeter()
  106. {
  107. cout << "The perimeter is : " << calc_perimeter() << endl;
  108. }
  109.  
  110. float
  111. Shape::calc_area ()
  112. {
  113. return -1;
  114. }
  115.  
  116. float
  117. Shape::calc_perimeter ()
  118. {
  119. return -1;
  120. }
  121.  
  122. //----------------- Circle's methods --------------------
  123.  
  124. void
  125. Circle::store_radius(float value)
  126. {
  127. radius = value;
  128. }
  129.  
  130. float
  131. Circle::return_radius()
  132. {
  133. return radius;
  134. }
  135.  
  136.  
  137. float
  138. Circle::calc_perimeter(void)
  139. {
  140. float circum;
  141. circum = 3.14*2*radius;
  142. return circum;
  143. }
  144.  
  145. float
  146. Circle::calc_area(void)
  147. {
  148. return (3.14*radius*radius);
  149. }
  150.  
  151. //----------------- Rectangle's methods -----------------
  152.  
  153. void
  154. Rectangle::store_sides(float l, float w)
  155. {
  156. length = l;
  157. width = w;
  158. }
  159.  
  160. void
  161. Rectangle::return_sides(float &l, float &w)
  162. {
  163. l = length;
  164. w = width;
  165. }
  166.  
  167.  
  168. float
  169. Rectangle::calc_perimeter(void)
  170. {
  171. return (2 * length + 2 * width);
  172. }
  173.  
  174. float
  175. Rectangle::calc_area(void)
  176. {
  177. return (length * width);
  178. }
  179.  
  180. //----------------- Triangle's methods ------------------
  181.  
  182. void
  183. Triangle::store_sides(float s1, float s2, float s3)
  184. {
  185. side1 = s1;
  186. side2 = s2;
  187. side3 = s3;
  188. }
  189.  
  190. void
  191. Triangle::return_sides(float &s1, float &s2, float &s3)
  192. {
  193. s1 = side1;
  194. s2 = side2;
  195. s3 = side3;
  196. }
  197.  
  198.  
  199. float
  200. Triangle::calc_perimeter(void)
  201. {
  202. return (side1 + side2 + side3);
  203. }
  204.  
  205. float
  206. Triangle::calc_area(void)
  207. {
  208. float s, area;
  209.  
  210. s = (side1 + side2 + side3)/2;
  211. area = sqrt(s * (s - side1) * (s - side2) * (s - side3) );
  212. return area;
  213. }
  214.  
  215. //----------------- Cylinder's methods ------------------
  216.  
  217. void
  218. Cylinder::store_length(float l)
  219. {
  220. length = l;
  221. }
  222.  
  223. float
  224. Cylinder::return_length()
  225. {
  226. return length;
  227. }
  228.  
  229. float Cylinder::calc_area ()
  230. {
  231. float base, side;
  232.  
  233. base = Circle::calc_area();
  234. side = length * calc_perimeter();
  235. return (2 * base + side);
  236. }
  237.  
  238. float Cylinder::calc_volume ()
  239. {
  240. return (Circle::calc_area() * length);
  241. }
  242.  
  243. //----------------- Sphere's methods --------------------
  244.  
  245. float
  246. Sphere::calc_area ()
  247. {
  248. return (4.0 * 3.14 * radius * radius);
  249. }
  250.  
  251. float
  252. Sphere::calc_volume ()
  253. {
  254. return (4.0 / 3.0 * 3.14 * radius * radius * radius);
  255. }
  256.  
  257. //----------------- Square's methods --------------------
  258.  
  259. void
  260. Square::store_side (float side)
  261. {
  262. length = side;
  263. width = side;
  264. }
  265.  
  266. float
  267. Square::return_side (void)
  268. {
  269. return length;
  270. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Linked lists

 
0
  #2
Dec 3rd, 2008
You have invented a rather strange one-time list where new members are inserted in the head and the first traversal is at the same time the last possible one. After the 1st call of (incorrect) printarea the list becomes unusable. It losts all elements.

Well, non-destructive Dynamic::printarea looks like
  1. void Dynamic::printarea()
  2. {
  3. for (Shapelink* plink = lastone;
  4. plink != 0;
  5. plink = plink->next) {
  6. plink->item->print_area();
  7. }
  8. }
There are lots of other defects in your code but it's another story ...
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

Re: Linked lists

 
0
  #3
Dec 3rd, 2008
The prof wrote most of..well pretty much all of the code. We just had to write the printarea() function
Thanks, I'll try what you said!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC