proste działania matematyczne edytuj

  // Arithmetic - proste działania matematyczne
 #include <iostream>

 int main()
 {
    using namespace std;

    int Liczba1 = 0;
    int Liczba2 = 0;
    int Wynik = 0;

    // koniec linii to '\n'
    cout << "Podaj pierwsza liczbe calkowita\n";
    cin >> Liczba1;

    // czasem mozna linie konczyc uzywajac ::std::endl
    // ale nalezy pamietac, ze endl oczyszcza bufor
    // co wiaze sie ze spadkiem wydajnosci 
    // kiedy wykonywanych jest duzo takich operacji
    cout << "Podaj druga liczbe calkowita" << endl;
    cin >> Liczba2;

    Wynik = Liczba1 + Liczba2;
    cout << '\n' << Liczba1 << '+' << Liczba2 << '=' << Wynik << '\n';

    return 0;
 }

Łańcuchy edytuj

Łańcuchy znaków korzystające z biblioteki standardowej, w której zaimplementowano uogólnioną klasę napisów zwaną string

#include <iostream>
#include <string>


using namespace std;

int main(void) {
  string greeting = "Hello";
  cout << greeting<< std::endl;
  return 0;
}

Kompilacja

g++ s.cpp -Wall -Wextra

Uruchomienie

./a.out


Wynik:

Hello


konwersja liczby dziesiętnej do dwójkowej edytuj

Liczba dwójkowa ( binarna) jest w postaci łańcucha

/*
g++ b.cpp -Wall -Wextra -lm

https://stackoverflow.com/questions/8222127/changing-integer-to-binary-string-of-digits

*/

#include <iostream> // cout
#include <cmath>   // log
#include <string>   // 
#include <bitset>   //

using namespace std;


#define LENGTH 
int main(void) {

	int number = 2356;
	int length = floor(log(number)/log(2)) + 1; // https://www.exploringbinary.com/number-of-bits-in-a-decimal-integer/
	string binary_string = "";
	
	const int c_length = 12; // floor(3.4* length_of_decimal_number )
	
	binary_string = bitset< c_length > (number).to_string(); // string conversion
	cout << "decimal number = "<< number << " is binary base = " << binary_string << endl;
	cout << "length of binary digit is  = "<< length << endl;
	return 0;
}


Kompilacja

g++ b.cpp -Wall -Wextra -lm

Uruchomienie

./a.out


Wynik:

decimal number = 2356 is binary base = 100100110100
length of binary digit is  = 12


vector bool edytuj

Vector bool zachowuje się jak dynamiczny bitset. Długość jest zmienną[1]

/*

g++ v.cpp -Wall -Wextra

*/

#include <iostream>
#include <vector>

using namespace std;

int main(){

    vector<bool> tab;
    int n;
    cout << "Podaj długość tablicy " << endl;
    cin >> n;

    for( int i=0; i<n; ++i )
    {
       int element;
       cout << "Podaj element (0 lub 1) " << endl ;
       cin >> element;
       tab.push_back(element);
    }
    
    cout << "wyświetl elementy tablicy  " << endl;
    
    // https://www.kompikownia.pl/index.php/2019/04/18/poznaj-nowoczesna-tablice-vector-w-c/
    for(auto i : tab) { // range-based loop od  C++11

    	cout<<i<<endl;
	}
    
    return 0;
}

Przekroczenie limitu liczb całkowitych edytuj

#include <iostream>
#include <math.h>       /* pow */

int main()
{	unsigned long long int r;
	unsigned long long int t;

	 for ( r = 2; r < 70; ++r){
	 
	 	t = pow(2,r);
	 	std::cout << r << "\t 2^r = " << t << "\n"; //
	 }

	return 0;
}

wynik:

./a.out
2	 2^r = 4
3	 2^r = 8
4	 2^r = 16
5	 2^r = 32
6	 2^r = 64
7	 2^r = 128
8	 2^r = 256
9	 2^r = 512
10	 2^r = 1024
11	 2^r = 2048
12	 2^r = 4096
13	 2^r = 8192
14	 2^r = 16384
15	 2^r = 32768
16	 2^r = 65536
17	 2^r = 131072
18	 2^r = 262144
19	 2^r = 524288
20	 2^r = 1048576
21	 2^r = 2097152
22	 2^r = 4194304
23	 2^r = 8388608
24	 2^r = 16777216
25	 2^r = 33554432
26	 2^r = 67108864
27	 2^r = 134217728
28	 2^r = 268435456
29	 2^r = 536870912
30	 2^r = 1073741824
31	 2^r = 2147483648
32	 2^r = 4294967296
33	 2^r = 8589934592
34	 2^r = 17179869184
35	 2^r = 34359738368
36	 2^r = 68719476736
37	 2^r = 137438953472
38	 2^r = 274877906944
39	 2^r = 549755813888
40	 2^r = 1099511627776
41	 2^r = 2199023255552
42	 2^r = 4398046511104
43	 2^r = 8796093022208
44	 2^r = 17592186044416
45	 2^r = 35184372088832
46	 2^r = 70368744177664
47	 2^r = 140737488355328
48	 2^r = 281474976710656
49	 2^r = 562949953421312
50	 2^r = 1125899906842624
51	 2^r = 2251799813685248
52	 2^r = 4503599627370496
53	 2^r = 9007199254740992
54	 2^r = 18014398509481984
55	 2^r = 36028797018963968
56	 2^r = 72057594037927936
57	 2^r = 144115188075855872
58	 2^r = 288230376151711744
59	 2^r = 576460752303423488
60	 2^r = 1152921504606846976
61	 2^r = 2305843009213693952
62	 2^r = 4611686018427387904
63	 2^r = 9223372036854775808
64	 2^r = 0
65	 2^r = 0
66	 2^r = 0
67	 2^r = 0
68	 2^r = 0
69	 2^r = 0



  1. poznaj-nowoczesna-tablice-vector-w-c Autor Karol Ślusarz