Advertisement
Guest User

Untitled

a guest
May 24th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // 5000 Templates_1.0
  4. //
  5. // Created by Berkovich Pavel on 2/24/19.
  6. // Copyright © 2019 Berkovich Pavel. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <time.h>
  11. #include <string>
  12. using namespace std;
  13.  
  14. //template <class T> T lessOnes(int, T *);
  15. int Binary(int chislo){
  16. char str[50];
  17. int a(0), r(1),counter(0),minus(0),buffer(0),i(0);
  18. int p=2;
  19. buffer=abs(chislo);
  20.  
  21. while (buffer >= 1){
  22. minus++;
  23. if(buffer % p){
  24. counter++;
  25. str[i]='1';
  26. }
  27. else{str[i]='0';}
  28.  
  29. a = a + (buffer % p) * r;
  30. r = r * 10;
  31. buffer = buffer / p;
  32. i++;
  33. }
  34. if(chislo<0){
  35. int ost=1;
  36. for(int l=minus-1;l>=0;l--){
  37. str[l]=((str[l]=='1') ? '0' : '1');
  38. }
  39. for(int l=0;l<minus;l++){
  40. if (ost==1){
  41. if(str[l]=='0'){
  42. str[l]='1';
  43. ost=0;
  44. }
  45. else{
  46. str[l]='0';
  47. }
  48. }
  49. }
  50. counter=0;
  51. for(int l=minus-1;l>=0;l--){
  52. if(str[l]=='1') { counter++;};
  53. cout<<str[l];
  54. }
  55.  
  56. cout<<endl;
  57. }
  58. else{
  59. cout<<a<<endl;
  60. }
  61. cout<<"Count: "<<counter<<endl;
  62. return counter;
  63. }
  64. int Binary(string slovo){
  65. int count(0);
  66. for(int i=0;i<slovo.length();++i){
  67. char str[9] = "00000000";
  68. unsigned char symbol = slovo[i];
  69. for(char i=0; i<8; i++)
  70. {
  71. str[i] = (symbol & 0x80) ? '1' : '0';
  72. symbol = symbol << 1;
  73. }
  74. for (int i =0;i<8;i++){
  75. cout<<str[i];
  76. if(str[i]=='1'){
  77. count++;
  78. }
  79. }
  80. cout<<endl<<"Count: "<<count<<endl;
  81. }
  82. return count;
  83. }
  84.  
  85.  
  86.  
  87. int printArray(string array, int count)
  88. {
  89. array += " ";
  90. int nachalo(0),konec,i(0),count_slov(0),min_ed=999999999;
  91. string slovo;
  92. string slova [100];
  93. while((nachalo = array.find_first_not_of(" ",nachalo))!=-1){
  94. konec = array.find_first_of(" ",nachalo);
  95. slovo=array.substr(nachalo,konec-nachalo);
  96. slova[i]=slovo;
  97. i++;
  98. count_slov++;
  99. nachalo=konec;
  100. }
  101. cout << endl;
  102. for (int i=0;i<count_slov;++i){
  103. char charstr[30];
  104. char *ptr;
  105. char**ptr1=&ptr;
  106. strcpy(charstr,slova[i].c_str());
  107. double a=strtod(charstr,ptr1);
  108. if(strlen(*ptr1)==0){
  109. cout<<a<<endl;
  110. int Bin_presentation;
  111. Bin_presentation=Binary(a);
  112. if ( Bin_presentation < min_ed){
  113. min_ed= Bin_presentation;
  114. }
  115. }
  116. else{
  117. cout<<slova[i]<<endl;
  118. int sch;
  119. sch=Binary(slova[i]);
  120. if (sch < min_ed){
  121. min_ed=sch;
  122. }
  123. }
  124. cout<<endl<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
  125.  
  126. }
  127. return min_ed;
  128. }
  129.  
  130.  
  131.  
  132.  
  133.  
  134. // return slova;
  135. // конец шаблона функции printArray
  136.  
  137. #define _rand(min, max) ( rand() % ((max) - (min) + 1) + (min) )
  138.  
  139. int main()
  140. {
  141. //рандомное заполнение строки класса string;
  142. int quantity;
  143. setlocale(LC_ALL, "russian");
  144. srand(time(NULL));
  145. cout<<"Введите желаемое число симфолов в последовательности : "<<endl;
  146. cin >> quantity;
  147. string chars;
  148. int* in = new int [quantity];
  149. cout << "\nСлучайным образом:\n";
  150. for(int i=0; i<quantity; ++i)
  151. {
  152. if(_rand(0,1)){
  153. in[i]= _rand(33, 128);
  154. chars+=(char)in[i];
  155. chars+=" ";
  156. }
  157. else{
  158. in[i]= _rand(-1000, 1000);
  159. chars+=to_string(in[i]);
  160. chars+=" ";
  161. }
  162. }
  163. cout<<endl<<(string) chars <<endl;
  164. cout<<endl<<"--------------------------------------"<<endl;
  165. int min;
  166. min = printArray(chars, quantity);
  167. cout<<" Min : "<<min<<endl;
  168.  
  169. return 0;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement