//////////////////////////// // // This program reads in information about the geometry // of a hard drive, then prints out the capacity of the drive, // in binary and in decimal notation #include using namespace std; int main (){ //////////// // Variables unsigned long int cylinders = 0; unsigned long int sectors = 0; unsigned long int heads =0; unsigned long int capacity = 0; unsigned long int num_gigabytes, num_megabytes, num_kilobytes, num_gibibytes, num_mebibytes, num_kibibytes; ///////////// // Constants const unsigned long int bytes_per_sector = 512; const unsigned long int bytes_per_gigabyte = 1000000000; const unsigned long int bytes_per_megabyte = 1000000; const unsigned long int bytes_per_kilobyte = 1000; const unsigned long int bytes_per_gibibyte = 1073741824; const unsigned long int bytes_per_mebibyte = 1048576; const unsigned long int bytes_per_kibibyte = 1024; /////////////////// // Explain what the program does cout << "This program takes the number of cylinders, sectors, and heads" << endl << "on a hard drive and returns the capacity of the drive." << endl << endl; //cerr << maxint << endl; // Get the number of cylinders and make sure it is valid cout << "Enter number of cylinders: "; cin >> cylinders; if (cylinders <=0) { cout << "Sorry, that has to be greater than zero" << endl; exit (1); } // Get the number of sectors and make sure it is valid cout << "Enter number of sectors: "; cin >> sectors; if (sectors <=0) { cout << "Sorry, that has to be greater than zero" << endl; exit (1); } // Get the number of heads and make sure it is valid cout << "Enter number of heads: "; cin >> heads; if (heads <=0) { cout << "Sorry, that has to be greater than zero" << endl; exit (1); } // Now determine the size of the drive, in bytes capacity = bytes_per_sector * cylinders * sectors * heads; cout << "Capacity in bytes: " << capacity << endl; cout << "The capacity of the disk is: " << endl; // Now the more trickier part, to figure out how big the drive is // and report the data in a nicer format // First in decimal bytes cout << "Decimal bytes: "; num_gigabytes = capacity / bytes_per_gigabyte; capacity = capacity % bytes_per_gigabyte; cout << num_gigabytes << " GB "; num_megabytes = capacity / bytes_per_megabyte; capacity = capacity % bytes_per_megabyte; cout << num_megabytes << " MB "; num_kilobytes = capacity / bytes_per_kilobyte; capacity = capacity % bytes_per_kilobyte; cout << num_kilobytes << " KB "; cout << "and " << capacity << " B." << endl; // Now in bibytes cout << "Binary bytes: "; capacity = bytes_per_sector * cylinders * sectors * heads; num_gibibytes = capacity / bytes_per_gibibyte; capacity = capacity % bytes_per_gibibyte; cout << num_gibibytes << " GiB "; num_mebibytes = capacity / bytes_per_mebibyte; capacity = capacity % bytes_per_mebibyte; cout << num_mebibytes << " MiB "; num_kibibytes = capacity / bytes_per_kibibyte; capacity = capacity % bytes_per_kibibyte; cout << num_kibibytes << " KiB "; cout << "and " << capacity << " B." << endl; }