Les reponses ont ete preparees par Jinzhi Xia (jxia@site.uottawa.ca) Thierry Metais va mettre une traduction de la partie ecrite sur sa page web. =========================================================================== #include #include using namespace std; // main method -- "compress.cpp" int main(int argc, char* argv[]) { char ch,temp; unsigned int number = 1;//number of occurence of same character fstream input,output; // input and output streams if ((argc == 2) ||((argc == 3)&&(strcmp(argv[1],"-v")== 0)))//command line format is compress [-v] inputname { char *inputname; // input filename char outputname[20];//output filename bool end = false; int origsize,compressedsize;//original and compressed file size if (argc == 2) inputname = argv[1]; else inputname = argv[2]; strcpy(outputname,inputname); strcat(outputname,".rle"); // open the input file input.open(inputname, ios::in|ios::binary); // Check to make sure that we could open the input file properly! if (input.fail()) { cerr << "Could not open file "; cerr << inputname; cerr << ".\n"; return 1; }//if //get original file size input.seekg(0,ios::end); origsize = input.tellg(); input.seekg(0,ios::beg); // open the output file output.open(outputname,ios::in); if (output.fail()) { output.clear(); output.open(outputname,ios::out|ios::binary);// file doesn't exist; create a new one } else{ output.close(); output.open(outputname,ios::out|ios::binary); } ch = input.get(); // read next character while (1) { if (input.eof()) break; // end of file while (((temp = input.get()) == ch)&&(!input.eof())) number ++;//calculate repeated occurrence of character ch if (number < 4)//occurence is less than 4 times for (unsigned int i = 0; i< number; i++) output.put(ch); else{ unsigned char time = (int)number/255; unsigned char t = (int)number%255; for (int i = 0; i < time; i++) { output.put('*'); output.put(ch); output.put((unsigned char)255); } output.put('*'); output.put(ch); output.put(t); }//else ch = temp; number = 1; }//while if (argc != 2)//command has -v option { output.seekg(0,ios::end); compressedsize = output.tellg();//compressed file size //cerr << origsize << compressedsize; cerr << "Compression ratio is " << (float)compressedsize/origsize << ".\n"; } input.close(); output.close(); }//if else //wrong command line arguments { cout << "\nUsage: " << argv[0] << " \n"; return 1; }//else return 0; } =============================================================================== #include #include #include using namespace std; // main method -- "uncompress.cpp" int main(int argc, char* argv[]) { char ch,temp; int number = 1; int length = 0; if (argc == 2) { fstream input,output; // input and output streams char *inputname; // input filename char outputname[20];//output filename char a[20]; inputname = argv[1]; //Check whether the inputfile name ended with ".rle" string name(inputname); length = (int)name.length(); temp=name.substr(length-4,4); if (temp.compare(".rle") == 0){//ended with ".rle" temp = name.substr(0,length-4); strcpy(outputname,temp.data()); } else{ strcpy(outputname,name.data()); name.append(".rle"); strcpy(a,name.data()); inputname = a; } input.open(inputname, ios::in|ios::binary); // Check to make sure that we could open the input file properly! if (input.fail()) { cerr << "Could not open file "; cerr << inputname; cerr << ".\n"; return 1; } //open the output file output.open(outputname,ios::in); if (output.fail()) { output.clear(); output.open(outputname,ios::out|ios::binary);// file doesn't exist; create a new one } else{ output.close(); output.open(outputname,ios::out|ios::binary); } while (1) { ch = input.get(); // read next character if (input.eof()) break; // end of file if (ch == '*'){//assume * is not included in the original file ch = input.get(); number = (int)input.get(); for (int i =0;i < number; i++) output.put(ch); } else output.put(ch); } // close the files input.close(); output.close(); } else //wrong command line argument { cout << "\nUsage: " << argv[0] << " \n"; return 1; } return 0; } =============================================================================== WRITTEN QUESTIONS ----------------- 1) Magnetic Disks 1-a) Configuration 1: A) Average total time per track = average seek + average rotational delay + transfer time = 2 + 0.5 + 1 = 3.5 (msec) Total time for accessing the file in sequence = Average total time per track x File size/Track capacity = 3.5 x 1,000,000/(100 x 600) = 58.3 (msec) B) Number of tracks = 1,000,000/(600 x 100) = 100/6 = 16.7 Number of records per track = 100 x 600/500 = 120 (records) Among 120 records per track, 1/3 (40) of them are stored in one sector, 2/3 (80) of them span 2 sectors, to access each of these records we need to read two sectors. Average time to read one of 40 records= Average seek time + Average rotational delay + Transfer time = 2 + 0.5 + 0.01 = 2.51 (msec) Average time to read one of 80 records= Average seek time + Average rotational delay + 2 x Transfer time = 2 + 0.5 + 2 x 0.01 = 2.52 (msec) Average time to access the file randomly = 100/6 x (1/3 x 120 x 2.51 + 2/3 x 120 x 2 x 2.52) = 100/6 x (40 x 2.51 + 80 x 2.52) = 100/6 x 302 = 5033.3 (msec) = 5.03 (sec) C) All sectors are used, thus internal fragment is zero. 1-b) 1 records per sector 1 x 100 = 100 records per track 100 x 4 = 400 records per cylinder Number of cylinders = 2000/400 = 5 (cylinders) A) Average total time for the first track per cylinder = Average seek time + Average rotational delay + Average transfer time for a track = 2 + 0.5 + 1 = 3.5 (msecs) Average total time for the rest tracks per cylinder = Average transfer time for a track = 1.5 (msecs) Average total time per cylinder = 3.5 + 3 x 1 = 6.5 (msecs) The file occupies 5 cylinders. Total time for accessing the file in sequence = 5 x 6.5 = 32.5 (msec) B) Average time to read one sector = Average seek time + Average rotational delay + Transfer time = 2 + 0.5 + 0.01 = 2.51 (msec) Average total time to access the entire file randomly = Number of records x Average time to read one sector = 2000 x 2.51 = 5020 (msec) = 5. 02 (sec) C) Unused space in each sector = section size ¨C record size = 600 -500 = 100 (bytes) Unused space presented in the file = Unused space in each sector x number of sectors = 100 x 2000 = 200,000 (bytes) = 200 KB 2) Magnetic Tapes Blocking factor = 50 A) b = length of data block(in inches) = 50 x 30/3000 = 0.5 (inches) n = 1000 records/50 records per block = 20 blocks s = 20 x (0.5 + 0.2) = 14 (inches) = 1.2 feet B) Effective Recording Density = Number of bytes per block/number of inches to store a block = 1500/(0.2 + 0.5) ? 2142.9 (bpi) C) Effective Transmission Rate = Effective Recording Density x tape speed = 1500/0.7 x 200 =418,571.4(bytes/sec) = 418.6 KB/sec Blocking factor = 100 A) b = length of data block(in inches) = 100 x 30/3000 = 1 (inches) n = 1000 records/100 records per block = 10 blocks s = 10 x (1 + 0.2) = 12 (inches) = 1 foot B) Effective Recording Density = Number of bytes per block/number of inches to store a block = 3000/(1 + 0.2) ? 2500 (bpi) C) Effective Transmission Rate = Effective Recording Density x tape speed = 2500 x 200 =500,000(bytes/sec) = 500 KB/sec