Сommit
						3c5eb2a58c
					
				| @ -0,0 +1,4 @@ | |||||||
|  | /bin | ||||||
|  | /obj | ||||||
|  | /cs-lab34.layout | ||||||
|  | /cs-lab34.depend | ||||||
| @ -0,0 +1,40 @@ | |||||||
|  | <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> | ||||||
|  | <CodeBlocks_project_file> | ||||||
|  | 	<FileVersion major="1" minor="6" /> | ||||||
|  | 	<Project> | ||||||
|  | 		<Option title="cs-lab34" /> | ||||||
|  | 		<Option pch_mode="2" /> | ||||||
|  | 		<Option compiler="gcc" /> | ||||||
|  | 		<Build> | ||||||
|  | 			<Target title="Debug"> | ||||||
|  | 				<Option output="bin/Debug/cs-lab34" prefix_auto="1" extension_auto="1" /> | ||||||
|  | 				<Option object_output="obj/Debug/" /> | ||||||
|  | 				<Option type="1" /> | ||||||
|  | 				<Option compiler="gcc" /> | ||||||
|  | 				<Compiler> | ||||||
|  | 					<Add option="-g" /> | ||||||
|  | 				</Compiler> | ||||||
|  | 			</Target> | ||||||
|  | 			<Target title="Release"> | ||||||
|  | 				<Option output="bin/Release/cs-lab34" prefix_auto="1" extension_auto="1" /> | ||||||
|  | 				<Option object_output="obj/Release/" /> | ||||||
|  | 				<Option type="1" /> | ||||||
|  | 				<Option compiler="gcc" /> | ||||||
|  | 				<Compiler> | ||||||
|  | 					<Add option="-O2" /> | ||||||
|  | 				</Compiler> | ||||||
|  | 				<Linker> | ||||||
|  | 					<Add option="-s" /> | ||||||
|  | 				</Linker> | ||||||
|  | 			</Target> | ||||||
|  | 		</Build> | ||||||
|  | 		<Compiler> | ||||||
|  | 			<Add option="-Wall" /> | ||||||
|  | 			<Add option="-fexceptions" /> | ||||||
|  | 		</Compiler> | ||||||
|  | 		<Unit filename="main.cpp" /> | ||||||
|  | 		<Extensions> | ||||||
|  | 			<lib_finder disable_auto="1" /> | ||||||
|  | 		</Extensions> | ||||||
|  | 	</Project> | ||||||
|  | </CodeBlocks_project_file> | ||||||
| @ -0,0 +1,146 @@ | |||||||
|  | //main.cpp  -- the program gets number count, numbers and bin size, then builds a histogram.
 | ||||||
|  | //Task 15   -- make a scale under the histogram.
 | ||||||
|  | 
 | ||||||
|  | #include <iostream> | ||||||
|  | #include <vector> | ||||||
|  | #include <math.h> | ||||||
|  | using namespace std; | ||||||
|  | 
 | ||||||
|  | double find_ext(vector <double> array, short key) | ||||||
|  | { | ||||||
|  |     double ext = array[0];                                                  //Key "1" keeps the comparison the same.
 | ||||||
|  |     for(double element : array)                                             //Key "-1" reverses the comparison.
 | ||||||
|  |     { | ||||||
|  |         if(key * element > key * ext) {ext = element;} | ||||||
|  |     } | ||||||
|  |     return ext; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | size_t find_ext(vector <size_t> array, short key) | ||||||
|  | { | ||||||
|  |     size_t ext = array[0];                                                  //Key "1" keeps the comparison the same.
 | ||||||
|  |     for(double element : array)                                             //Key "-1" reverses the comparison.
 | ||||||
|  |     { | ||||||
|  |         if(key * element > key * ext) {ext = element;} | ||||||
|  |     } | ||||||
|  |     return ext; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | int main() { | ||||||
|  |     const size_t screen_width = 80; | ||||||
|  |     const size_t max_asterisk = screen_width - 3 - 1; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     size_t number_count;                                                    //Input variable.
 | ||||||
|  |     cerr << "Enter number count: "; | ||||||
|  |     cin >> number_count; | ||||||
|  |     vector<double> numbers(number_count); | ||||||
|  |     for (size_t i = 0; i < number_count; ++i) { cin >> numbers[i]; } | ||||||
|  |     size_t bin_count; | ||||||
|  |     cerr << "Enter bin count: "; | ||||||
|  |     cin >> bin_count; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     double max = find_ext(numbers, 1);                                      //Find min, max and bin size.
 | ||||||
|  |     double min = find_ext(numbers, -1);                                     //Key 1 stands for maximum, key -1 stands for minimum.
 | ||||||
|  |     double bin_size = static_cast<double>(max - min) / (bin_count); | ||||||
|  | 
 | ||||||
|  |     vector <size_t> bins(bin_count); | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     for(size_t i = 0; i < number_count; ++i)                                //Checking if a number is in a bin.
 | ||||||
|  |     { | ||||||
|  |         bool found = false; | ||||||
|  |         for(size_t j = 0; (j < bin_count - 1) && !found; ++j) | ||||||
|  |         { | ||||||
|  |             if( (numbers[i] >= (min + j * bin_size)) &&                     //Where (min + j * bin_size) is equal to the lower border
 | ||||||
|  |             (numbers[i] < (min + (j + 1) * bin_size)) )                     //and (min + (j + 1) * bin_size) is equal to the higher border.
 | ||||||
|  |             { | ||||||
|  |                 ++bins[j]; | ||||||
|  |                 found = true; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         if(!found) {++bins[bin_count - 1];}                                 //A special case when current number is equal to the maximum.
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     size_t max_bin = find_ext(bins, 1);                                     //Finds a bin with the maximum size. Key 1 stands for maximum.
 | ||||||
|  |     double modifier; | ||||||
|  | 
 | ||||||
|  |     /*
 | ||||||
|  |      * In case when maximum bin > 76, histogram will be scaled according to | ||||||
|  |      * the formula "max_asterisk * (static_cast<double>(bins[i]) / max_bin)" | ||||||
|  |      *          or "bins[i] * (static_cast<double>(max_asterisk) / max_bin)". | ||||||
|  |      * In other case, histogram won't be scaled, i.e, asterisk count depends on the current bin number. | ||||||
|  |      */ | ||||||
|  | 
 | ||||||
|  |     if(max_bin > 76) {modifier = static_cast<double>(max_asterisk) / (max_bin);} | ||||||
|  |     else {modifier = 1;} | ||||||
|  | 
 | ||||||
|  |     for(long unsigned int i = 0; i < bin_count; ++i)                                      //Histogram output with alignment, if necessary.
 | ||||||
|  |     { | ||||||
|  |         if(bins[i] >= 10) | ||||||
|  |         { | ||||||
|  |             if(bins[i] >= 100) {cout << bins[i] << '|';}                    //Output a three-digit number.
 | ||||||
|  |             else {cout << ' ' << bins[i] << '|';}                           //Output a two-digit number with alignment.
 | ||||||
|  |         } | ||||||
|  |         else {cout << "  " << bins[i] << '|';}                              //Output a single-digit number with alignment.
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |         size_t height = modifier * bins[i];                                 //Height stands for the number of output asterisks.
 | ||||||
|  |         for(long unsigned int k = 0; k < height; ++k) | ||||||
|  |             cout << '*'; | ||||||
|  |         cout << "\n"; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     //Task 15.
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     size_t interval_task; | ||||||
|  |     cerr << "Enter interval size: "; | ||||||
|  |     cin >> interval_task; | ||||||
|  | 
 | ||||||
|  |     if((interval_task >= 4) && (interval_task <= 9))                        //The scale under the histogram.
 | ||||||
|  |     { | ||||||
|  |         size_t times; | ||||||
|  |         if(modifier == 1) {times = static_cast<size_t>(ceil(max_bin / interval_task) + 1);} | ||||||
|  |         else {times = static_cast<size_t>(ceil(max_asterisk / interval_task) + 1);} | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |         cout << "   |";                                                     //1st row output.
 | ||||||
|  |         for(long unsigned int i = 0; i < times; ++i) | ||||||
|  |         { | ||||||
|  |             for(long unsigned int k = 0; k < interval_task - 1; ++k) | ||||||
|  |                 cout << '*'; | ||||||
|  |             cout << '|'; | ||||||
|  |         } | ||||||
|  |         cout << '\n'; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |         cout << "   " << 0;                                                 //2nd row output.
 | ||||||
|  |         for(long unsigned int i = 0; i < (interval_task - 1); ++i)                        //Distance from the first axis to the second.
 | ||||||
|  |             cout << ' '; | ||||||
|  |         cout << interval_task; | ||||||
|  | 
 | ||||||
|  |         //  (Number of intervals between the second and last * Interval size) - Last axis.
 | ||||||
|  | 
 | ||||||
|  |         if(times > 1) | ||||||
|  |         { | ||||||
|  |             for(long unsigned int i = 0; i < (times - 1) * (interval_task) - 1; ++i) | ||||||
|  |                 cout << ' '; | ||||||
|  |             cout << times * interval_task; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     else {cout << "ERROR";} | ||||||
|  | 
 | ||||||
|  |     return 0; | ||||||
|  | } | ||||||
					Загрузка…
					
					
				
		Ссылка в новой задаче
	
	 NikitkinTY
						NikitkinTY