Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // 5000 Templates_1.0
- //
- // Created by Berkovich Pavel on 2/24/19.
- // Copyright © 2019 Berkovich Pavel. All rights reserved.
- //
- #include <iostream>
- #include <time.h>
- #include <string>
- using namespace std;
- //template <class T> T lessOnes(int, T *);
- int Binary(int chislo){
- char str[50];
- int a(0), r(1),counter(0),minus(0),buffer(0),i(0);
- int p=2;
- buffer=abs(chislo);
- while (buffer >= 1){
- minus++;
- if(buffer % p){
- counter++;
- str[i]='1';
- }
- else{str[i]='0';}
- a = a + (buffer % p) * r;
- r = r * 10;
- buffer = buffer / p;
- i++;
- }
- if(chislo<0){
- int ost=1;
- for(int l=minus-1;l>=0;l--){
- str[l]=((str[l]=='1') ? '0' : '1');
- }
- for(int l=0;l<minus;l++){
- if (ost==1){
- if(str[l]=='0'){
- str[l]='1';
- ost=0;
- }
- else{
- str[l]='0';
- }
- }
- }
- counter=0;
- for(int l=minus-1;l>=0;l--){
- if(str[l]=='1') { counter++;};
- cout<<str[l];
- }
- cout<<endl;
- }
- else{
- cout<<a<<endl;
- }
- cout<<"Count: "<<counter<<endl;
- return counter;
- }
- int Binary(string slovo){
- int count(0);
- for(int i=0;i<slovo.length();++i){
- char str[9] = "00000000";
- unsigned char symbol = slovo[i];
- for(char i=0; i<8; i++)
- {
- str[i] = (symbol & 0x80) ? '1' : '0';
- symbol = symbol << 1;
- }
- for (int i =0;i<8;i++){
- cout<<str[i];
- if(str[i]=='1'){
- count++;
- }
- }
- cout<<endl<<"Count: "<<count<<endl;
- }
- return count;
- }
- int printArray(string array, int count)
- {
- array += " ";
- int nachalo(0),konec,i(0),count_slov(0),min_ed=999999999;
- string slovo;
- string slova [100];
- while((nachalo = array.find_first_not_of(" ",nachalo))!=-1){
- konec = array.find_first_of(" ",nachalo);
- slovo=array.substr(nachalo,konec-nachalo);
- slova[i]=slovo;
- i++;
- count_slov++;
- nachalo=konec;
- }
- cout << endl;
- for (int i=0;i<count_slov;++i){
- char charstr[30];
- char *ptr;
- char**ptr1=&ptr;
- strcpy(charstr,slova[i].c_str());
- double a=strtod(charstr,ptr1);
- if(strlen(*ptr1)==0){
- cout<<a<<endl;
- int Bin_presentation;
- Bin_presentation=Binary(a);
- if ( Bin_presentation < min_ed){
- min_ed= Bin_presentation;
- }
- }
- else{
- cout<<slova[i]<<endl;
- int sch;
- sch=Binary(slova[i]);
- if (sch < min_ed){
- min_ed=sch;
- }
- }
- cout<<endl<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
- }
- return min_ed;
- }
- // return slova;
- // конец шаблона функции printArray
- #define _rand(min, max) ( rand() % ((max) - (min) + 1) + (min) )
- int main()
- {
- //рандомное заполнение строки класса string;
- int quantity;
- setlocale(LC_ALL, "russian");
- srand(time(NULL));
- cout<<"Введите желаемое число симфолов в последовательности : "<<endl;
- cin >> quantity;
- string chars;
- int* in = new int [quantity];
- cout << "\nСлучайным образом:\n";
- for(int i=0; i<quantity; ++i)
- {
- if(_rand(0,1)){
- in[i]= _rand(33, 128);
- chars+=(char)in[i];
- chars+=" ";
- }
- else{
- in[i]= _rand(-1000, 1000);
- chars+=to_string(in[i]);
- chars+=" ";
- }
- }
- cout<<endl<<(string) chars <<endl;
- cout<<endl<<"--------------------------------------"<<endl;
- int min;
- min = printArray(chars, quantity);
- cout<<" Min : "<<min<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement