home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD 24 / PCPLUS115.iso / pcplus / wilf / shapes.cpp
Encoding:
C/C++ Source or Header  |  1996-02-26  |  9.3 KB  |  529 lines

  1. // SHAPES - an OOP program to handle properties of shapes
  2. //          as in Wilf's Programmers' Workshop (issue 115)
  3.  
  4. #include <stdio.h>
  5. #include <conio.h>
  6. #include <stdlib.h>
  7. #include <dos.h>
  8. #include <ctype.h>
  9. #include <math.h>
  10.  
  11. //////////////////////////////////////////////////////////////////////////////
  12. int lastitem;
  13.  
  14. class shape{
  15.     int Exists;
  16.     int Xlocation;
  17.     int Ylocation;
  18.     int Colour;
  19.     float Size;
  20.     int NumberOfPoints;
  21.  
  22. // the following characteristics are "hidden":
  23.  
  24. //    float Area;
  25. //    float Lateral;
  26. //    float Perimeter;
  27. //    int AngleAtPoint;    // output only
  28.  
  29.     shape(void);    //constructor
  30.  
  31. public:
  32.     int GetExistence(void);
  33.     void SetExistence(int);
  34.  
  35.     void ShowColour(void);
  36.     void ShowShape(void);
  37.     void ShowLocation(void);
  38.  
  39.     int GetXlocation(void);
  40.     void PutXlocation(int);
  41.     int GetYlocation(void);
  42.     void PutYlocation(int);
  43.     int GetColour(void);
  44.     void PutColour(int);
  45.     float GetSize(void);
  46.     void PutSize(int);
  47.     int GetNumberOfPoints(void);
  48.     void PutNumberOfPoints(int);
  49.     float GetArea(void);
  50.     void PutArea(int);
  51.     float GetLateral(void);
  52.     void PutLateral(int);
  53.     float GetPerimeter(void);
  54.     void PutPerimeter(int);
  55.     int GetAngleAtPoint(void);
  56. };
  57.  
  58. shape::shape(void)        // constructor
  59. {
  60.     Exists = 0;
  61.     PutXlocation( 1 + random(9) );
  62.     PutYlocation( 1 + random(9) );
  63.     PutColour ( 1 + random(4) );
  64.     PutSize ( 1 + random(9) );
  65.     NumberOfPoints = 3 + random(3);
  66. }
  67. int shape::GetExistence(void)
  68. {
  69.     return( Exists );
  70. }
  71. void shape::SetExistence(int Setting)
  72. {
  73.     Exists = Setting;
  74. }
  75. void shape::ShowColour(void)
  76. {
  77.     switch( Colour )
  78.     {
  79.     case 1:
  80.         printf("red ");
  81.         break;
  82.     case 2:
  83.         printf("green ");
  84.         break;
  85.     case 3:
  86.         printf("blue ");
  87.         break;
  88.     case 4:
  89.         printf("yellow ");
  90.         break;
  91.     }
  92. }
  93. void shape::ShowShape(void)
  94. {
  95.     switch( NumberOfPoints )
  96.     {
  97.     case 3:
  98.         printf("triangle.\n");
  99.         break;
  100.     case 4:
  101.         printf("square.\n");
  102.         break;
  103.     case 5:
  104.         printf("pentagon.\n");
  105.         break;
  106.     case 6:
  107.         printf("hexagon.\n");
  108.         break;
  109.     }
  110. }
  111. void shape::ShowLocation(void)
  112. {
  113.     printf("\nLocation: (%d,%d)\n",Xlocation,Ylocation);
  114. }
  115. int shape::GetXlocation(void)
  116. {
  117.     return( Xlocation );
  118. }
  119. void shape::PutXlocation(int newXlocation)
  120. {
  121.     Xlocation = newXlocation;
  122. }
  123. int shape::GetYlocation(void)
  124. {
  125.     return( Ylocation );
  126. }
  127. void shape::PutYlocation(int newYlocation)
  128. {
  129.     Ylocation = newYlocation;
  130. }
  131. int shape::GetColour(void)
  132. {
  133.     return( Colour );
  134. }
  135. void shape::PutColour(int newcolour)
  136. {
  137.     Colour = newcolour;
  138. }
  139. float shape::GetSize(void)
  140. {
  141.     return( Size );
  142. }
  143. void shape::PutSize(int newsize)
  144. {
  145.     Size = newsize;
  146. }
  147. int shape::GetNumberOfPoints(void)
  148. {
  149.     return( NumberOfPoints );
  150. }
  151. void shape::PutNumberOfPoints(int newnumberofpoints)
  152. {
  153.     NumberOfPoints = newnumberofpoints;
  154. }
  155. float shape::GetArea(void)
  156. {
  157.  float Area;
  158.     switch( NumberOfPoints )
  159.     {
  160.     case 3:
  161.         Area = Size * Size * 0.324759;
  162.         break;
  163.     case 4:
  164.         Area = Size * Size * 0.5;
  165.         break;
  166.     case 5:
  167.         Area = Size * Size * 0.59441;
  168.         break;
  169.     case 6:
  170.         Area = Size * Size * 0.649519;
  171.         break;
  172.     }
  173.     return(Area);
  174. }
  175. void shape::PutArea(int Area)
  176. {
  177.     switch( NumberOfPoints )
  178.     {
  179.     case 3:
  180.                    Size = sqrt( Area / 0.324759 );    
  181.         break;
  182.     case 4:
  183.         Size = sqrt( Area / 0.5 );
  184.         break;
  185.     case 5:
  186.         Size = sqrt( Area / 0.59441 );
  187.         break;
  188.     case 6:
  189.         Size = sqrt( Area / 0.649519 );
  190.         break;
  191.     }
  192. }
  193. float shape::GetLateral(void)
  194. {
  195.  float Lateral;
  196.     switch( NumberOfPoints )
  197.     {
  198.     case 3:
  199.         Lateral = Size * 0.866025;
  200.         break;
  201.     case 4:
  202.         Lateral = Size * 0.707107;
  203.         break;
  204.     case 5:
  205.         Lateral = Size * 0.587785;
  206.         break;
  207.     case 6:
  208.         Lateral = Size * 0.5;
  209.         break;
  210.     }
  211.     return(Lateral);
  212. }
  213. void shape::PutLateral(int Lateral)
  214. {
  215.     switch( NumberOfPoints )
  216.     {
  217.     case 3:
  218.         PutSize( Lateral * 1.1547 );
  219.         break;
  220.     case 4:
  221.         PutSize( Lateral * 1.414213 );
  222.         break;
  223.     case 5:
  224.         PutSize( Lateral * 1.701302 );
  225.         break;
  226.     case 6:
  227.         PutSize( Lateral * 2 );
  228.         break;
  229.     }
  230. }
  231. float shape::GetPerimeter(void)
  232. {
  233.     return( NumberOfPoints * GetLateral() );
  234. }
  235. void shape::PutPerimeter(int Perimeter)
  236. {
  237.     PutLateral( Perimeter / NumberOfPoints );
  238. }
  239. int shape::GetAngleAtPoint(void)
  240. {
  241. int temp;
  242.     temp = GetNumberOfPoints();
  243.     return( (temp - 2) * 180 / temp );
  244. }
  245. //////////////////////////////////////////////////////////////////////////////
  246. #define BEEP sound(300); delay(500); nosound();
  247.  
  248. ////////////////////////////////////
  249. void pause(void)
  250. {
  251.     printf("\n\nPress a key to continue ...\n" );
  252.     while( kbhit() == 0 )
  253.         ;
  254.     (void)getch();
  255. }
  256.  
  257. ////////////////////////////////////
  258. void showmenu(void)
  259. {
  260.     clrscr();
  261.     printf("Press...\n");
  262.     printf("P ... Properties\n");
  263.     printf("B ... Build\n");
  264.     printf("D ... Destroy\n");
  265.     printf("\nQ ... quit\n");
  266. }
  267.  
  268. ////////////////////////////////////
  269. void showmenu2(int choice)
  270. {
  271.     printf("\nChange...\n");
  272.     printf("X ... X-location\n");
  273.     printf("Y ... Y-location\n");
  274.     printf("C ... Colour\n");
  275.     if (choice != 4 )
  276.         printf("S ... Size\n");
  277.     printf("P ... Points (shape)\n");
  278.     printf("\nN ... no change\n");
  279. }
  280.  
  281. ////////////////////////////////////
  282. int choices(int maxitem)
  283. {
  284. int kpress;
  285.     printf("\n\nKey item number ... 1 to %d\n", maxitem);
  286.     while(1)
  287.       {
  288.         kpress = getch();
  289.         if ( (kpress > 48) && (kpress < 49 + maxitem) )
  290.         {
  291.           break;
  292.         }
  293.         else
  294.         {
  295.           BEEP;
  296.         }
  297.       }
  298.     kpress -= 48;
  299.     printf("\n\nItem number %d ",kpress);
  300.     return( kpress );
  301. }
  302. ////////////////////////////////////
  303. int actionStatus(int maxitem)
  304. {
  305.     clrscr();
  306.     printf("Properties of items\n");
  307.     return( choices(maxitem) );
  308. }
  309.  
  310. ////////////////////////////////////
  311. int actionBuild(int maxitem)
  312. {
  313.     clrscr();
  314.     printf("Build item ...\n");
  315.     return( choices(maxitem) );
  316. }
  317.  
  318. ////////////////////////////////////
  319. int actionDestroy(int maxitem)
  320. {
  321.     clrscr();
  322.     printf("Destroy item ...\n");
  323.     return( choices(maxitem) );
  324. }
  325.  
  326. ////////////////////////////////////
  327. int GetNumber(void)
  328. {
  329. int num;
  330.     printf("\nKey a number 1 to 9\n");
  331.     while( 1 )
  332.     {
  333.         num = getch();
  334.         if ( ( num > 48 ) && ( num < 58 ) )
  335.           return( num - 48 );
  336.         BEEP;
  337.     }
  338. }
  339. ////////////////////////////////////
  340. int GetRGBY(void)
  341. {
  342. int num;
  343.     printf("\nKey: R(ed), G(reen), B(lue), Y(ellow)\n");
  344.     while( 1 )
  345.     {
  346.         num = getch(); num = toupper(num);
  347.         switch( num )
  348.         {
  349.         case 'R':
  350.             num = 1;
  351.             break;
  352.         case 'G':
  353.             num = 2;
  354.             break;
  355.         case 'B':
  356.             num = 3;
  357.             break;
  358.         case 'Y':
  359.             num = 4;
  360.             break;
  361.         default:
  362.             num = 0;
  363.             BEEP;
  364.         }
  365.         if (num > 0)
  366.             return(num);
  367.     }
  368. }
  369. ////////////////////////////////////
  370. int Get3456(void)
  371. {
  372. int num;
  373.     printf("\nKey: T(riangle), S(quare), P(entagon), H(exagon)\n");
  374.     while( 1 )
  375.     {
  376.         num = getch(); num = toupper(num);
  377.         switch( num )
  378.         {
  379.         case 'T':
  380.             num = 3;
  381.             break;
  382.         case 'S':
  383.             num = 4;
  384.             break;
  385.         case 'P':
  386.             num = 5;
  387.             break;
  388.         case 'H':
  389.             num = 6;
  390.             break;
  391.         default:
  392.             num = 0;
  393.             BEEP;
  394.         }
  395.         if (num > 0)
  396.             return(num);
  397.     }
  398. }
  399. ////////////////////////////////////
  400. int main(void)
  401. {
  402. int kpress;
  403. int choice;
  404. int FullArea;
  405. int i;
  406.  
  407. clrscr(); randomize(); printf("Randomising ..."); pause();
  408.  
  409. shape item[5];                // remember arrays include "0"
  410. lastitem = 4;
  411. item[lastitem].SetExistence( 1 );    // make sure last item exists
  412.  
  413.     while( 1 )
  414.         {
  415.         showmenu();
  416.         kpress=getch();
  417.         kpress=toupper(kpress);
  418.         switch( kpress )
  419.         {
  420.         case 'P':
  421.             choice = actionStatus(lastitem);
  422.             if (item[choice].GetExistence() == 0)
  423.             {
  424.                 printf("does not exist\n");
  425.                 pause();
  426.                 break;
  427.             }
  428.             clrscr();
  429.             printf("Item %d is a ", choice);
  430.             item[choice].ShowColour();
  431.             item[choice].ShowShape();
  432.             item[choice].ShowLocation();
  433.  
  434.             if (choice == lastitem)
  435.             {
  436.                 FullArea = 200;
  437.                 for (i = 1; i < choice; i++)
  438.                 {
  439.                     if ( item[i].GetExistence() == 1)
  440.                     {
  441.                         FullArea -= item[i].GetArea();
  442.                     }
  443.                 }
  444.                 item[choice].PutArea( FullArea );
  445.             }
  446.  
  447.             printf("Size:    %f\n", item[choice].GetSize() );
  448.             printf("Area:    %f\n", item[choice].GetArea() );
  449.             printf("Lateral: %f\n", item[choice].GetLateral() );
  450.             printf("Perim:   %f\n", item[choice].GetPerimeter() );
  451.             printf("Angle:   %d\n", item[choice].GetAngleAtPoint() );
  452.  
  453.             showmenu2(choice);
  454.             while( 1 )
  455.             {
  456.             kpress=getch();
  457.             kpress=toupper(kpress);
  458.             if ( (kpress == 'S') && (choice == 4) )
  459.               kpress = 'Z';
  460.  
  461.             switch( kpress )
  462.             {
  463.             case 'X':
  464.                 item[choice].PutXlocation( GetNumber() );
  465.                 break;
  466.              case 'Y':
  467.                 item[choice].PutYlocation( GetNumber() );
  468.                 break;
  469.             case 'C':
  470.                 item[choice].PutColour( GetRGBY() );
  471.                 break;
  472.             case 'S':
  473.                 item[choice].PutSize( GetNumber() );
  474.                 break;
  475.             case 'P':
  476.                 item[choice].PutNumberOfPoints( Get3456() );
  477.                 break;
  478.             case 'N':
  479.                 break;
  480.             default:
  481.                 kpress = 'Z';
  482.                 BEEP;
  483.                 break;
  484.             }
  485.             if(kpress != 'Z')
  486.                 break;
  487.             }
  488.             break;
  489.         case 'B':
  490.             choice = actionBuild(lastitem - 1);
  491.             if (item[choice].GetExistence() == 0)
  492.             {
  493.                 item[choice].SetExistence( 1 );
  494.                 printf("OK\n");
  495.             }
  496.             else
  497.             {
  498.                 printf("already exists\n");
  499.                 BEEP;
  500.             }
  501.             pause();
  502.             break;
  503.  
  504.         case 'D':
  505.             choice = actionDestroy(lastitem - 1);
  506.             if (item[choice].GetExistence() == 1)
  507.             {
  508.                 item[choice].SetExistence( 0 );
  509.                 printf("OK\n");
  510.             }
  511.             else
  512.             {
  513.                 printf("does not exist\n");
  514.                 BEEP;
  515.             }
  516.             pause();
  517.             break;
  518.  
  519.         case 'Q':
  520.             exit(0);
  521.  
  522.         default:
  523.             BEEP
  524.         }
  525.     }
  526.    }
  527. ;
  528.  
  529.