/* lab12.cpp CIS 150 7/7/2005 David Klick This program demonstrates the use of classes. */ #include "dgkio.h" #include #include #include #include #include #include using std::cout; using std::cin; using std::setw; using std::ifstream; using std::ofstream; using std::ios; using dgk::getInt; using dgk::getDouble; using dgk::getChoice; using dgk::processMenu; class Circle { public: char name[30]; int x; int y; double radius; double area() { return radius * radius * 3.1415926536; } double distFromOrigin() { return pow(x * x + y * y, .5); } void display() { if (strlen(name)==0) cout << "[No Name]: "; else cout << name << ": "; cout << '(' << x << ',' << y << "), r = " << radius; } }; enum sorttype { BYNAME, BYSIZE, BYORIGIN }; void listCircles(Circle c[], int len); bool addCircle(Circle c[], int len); bool deleteCircle(Circle c[], int len); bool saveToFile(Circle c[], int len); int readFromFile(Circle c[]); void sortCircles(Circle c[], int len, sorttype type=BYNAME); void swap(Circle c[], int x, int y); const int MAXSIZE = 20; int main(void) { Circle circles[MAXSIZE]; int numCircles = 0; char choice; char* menu = "Main menu\n" "1. (A)dd circle\n" "2. (L)ist circles\n" "3. (D)elete circle\n" "4. (R)ead from file\n" "5. (S)ave to file\n" "6. Sort by (N)ame\n" "7. Sort by (O)rigin\n" "8. Sort by si(z)e\n" "9. E(x)it\n\n"; char* prompt = "Enter choice: "; char* allowed = "123456789AaLlDdRrSsNnOoZzXx"; do { choice = processMenu(menu, prompt, allowed, true); switch (choice) { case 'A': case '1': if (numCircles >= MAXSIZE) { cout << "Error: Array full.\n\n"; } else { if (addCircle(circles, numCircles)) { numCircles++; } } break; case 'L': case '2': if (numCircles == 0) { cout << "Error: There are no Circles to display\n\n"; } else { cout << "List of circles:\n"; listCircles(circles, numCircles); cout << '\n'; } break; case 'D': case '3': if (numCircles == 0) { cout << "Error: There are no Circles to delete\n\n"; } else { if (deleteCircle(circles, numCircles)) { numCircles--; } } break; case 'R': case '4': numCircles = readFromFile(circles); break; case 'S': case '5': saveToFile(circles, numCircles); break; case 'N': case '6': sortCircles(circles, numCircles, BYNAME); break; case 'O': case '7': sortCircles(circles, numCircles, BYORIGIN); break; case 'Z': case '8': sortCircles(circles, numCircles, BYSIZE); } } while (choice != '9' && choice != 'X'); cout << "\nNormal program termination\n"; return 0; } void listCircles(Circle c[], int len) { for (int i=0; i= MAXSIZE) { cout << "Error: Array full.\n\n"; return false; } // get info for Circle from user cout << "Enter name: "; cin.getline(c[len].name, 30, '\n'); c[len].x = getInt("Enter integer x coordinate: "); c[len].y = getInt("Enter integer y coordinate: "); c[len].radius = getDouble("Enter radius (0-20000): ", 0.0, 20000.0); // confirm add ans = getChoice("Do you really want to add this circle? (Y/N) ", "YyNn", true); // return true id add succeeded if (ans == 'N') return false; return true; } bool deleteCircle(Circle c[], int len) { int ans; int i; char prompt[80]; char reply; // return if no circles in list if (len == 0) { cout << "Error: There are no Circles to delete\n\n"; return false; } // display circles available for deletion cout << "List of circles:\n"; listCircles(c, len); // construct prompt sprintf(prompt, "Which circle would you like to delete? (1-%d, 0 to cancel): ", len); // find out which circle the user wants to delete ans = getInt(prompt, 0, len); if (ans == 0) return false; // confirm deletion cout << "This circle will be deleted: "; c[ans-1].display(); reply = getChoice("\nReally delete it? (Y/N) ", "YyNn", true); if (reply == 'N') return false; // delete circle from array for (i=ans; i MAXSIZE) { cout << "Error: Too many records in file. Input truncated.\n\n"; numrecs = MAXSIZE; } // read records from file into array for (i=0; i0) swap(c, i, j); break; case BYSIZE: if (c[i].area() > c[j].area()) swap(c, i, j); break; case BYORIGIN: if (c[i].distFromOrigin() > c[j].distFromOrigin()) swap(c, i, j); } } } }