板凳——————————————————c++(74)

    技术2022-07-10  92

    #include <iostream> void BubbleSort (int list[], int n){     for(int i = 0; i < n -1; i ++){         for(int j = 0; j < n-i-1; j++){              if(list[j] > list[j+1])    // 生序  0 1 2 3 4 5 6 7 8 9               //if(list[j] < list[j+1])  // 逆序列  9 8 7 6 5 4 3 2 1 0              std::swap(list[j], list[j+1]);         }     }         } //Data structures and Algorithm Analysis in c++ 4th edition  p14 class IntCell{     public:        explicit IntCell(int initialValue = 0);        int read() const;        void write(int x);     private:        int storedValue; };

    IntCell::IntCell(int initialValue) : storedValue{initialValue}{ }

    int IntCell::read() const{     return storedValue; }

    void IntCell::write(int x){     storedValue = x; }

    int main(){     int a [] {0, 2, 4, 6, 8, 1, 3, 5 ,7, 9};     BubbleSort(a, 10);     for(int k = 0; k < 10; k++){         std::cout << a[k] << ' ';             }         std::cout << std::endl;              IntCell m;     m.write(5);     std::cout << "Cell contents: " << m.read() <<std::endl;     return 0; }

    */ #include <iostream> #include <chrono> #include <iomanip> #include <ctime> #include <ratio> #include <cmath> using namespace std::chrono; int getRandom(int min, int max) {     return (rand() % static_cast<int>(max + 1 - min)) + min; }

    int main() { //professional c++ 4th edition p466     duration<long, std::ratio<60>> d1(123);     std::cout << d1.count() << std::endl;          duration<double> d2;     d2 = d2.max();     std::cout << d2.count() << std::endl;          duration<long, std::ratio<60>> d3(10);  // = 10 minutes     duration<long, std::ratio<1>> d4(14);   // = 14 seconds

        // Compare both durations     if (d3 > d4)         std::cout << "d3 > d4" << std::endl;     else         std::cout << "d3 <= d4" << std::endl;

        // Increment d4 with 1 resulting in 15 seconds     ++d4;

        // Multiply d4 by 2 resulting in 30 seconds     d4 *= 2; //    duration<double, std::ratio<1>> d5 = d3 + d4; //    10 minutes + 30 seconds = 630 minutes or 630 seconds

        duration<double, std::ratio<60>> d5 = d3 + d4;     duration<long, std::ratio<1>> d6 = d3 + d4;     std::cout << d3.count() << " minutes + " << d4.count() << " seconds = "         << d5.count() << " minutes or "         << d6.count() << " seconds" << std::endl;     duration<long> d7(30);

        // Convert the seconds of d7 to minutes     duration<double, std::ratio<60>> d8(d7);     std::cout << d7.count() << " seconds = " << d8.count() << " minutes" << std::endl;

        // Convert the seconds of d7 to integral minutes using duration_cast()     auto d8_ = duration_cast<duration<long, std::ratio<60>>>(d7);    // minutes     std::cout << d8_.count() << std::endl;

        // Convert integral minutes to integral seconds     duration<long, std::ratio<60>> d9(10);    // minutes     //minutes d9(10);                      // minutes     duration<long> d10(d9);              // seconds     std::cout << d10.count() << std::endl;

        // Use predefined durations     auto t1 = hours(1) + minutes(23) + seconds(45);     std::cout << seconds(t1).count() << " seconds" << std::endl;

        // Standard user-defined literals     auto myDuration = 42min;    // 42 minutes //professional c++ 4th edition p468     // Get current time as a time_point     system_clock::time_point tpoint = system_clock::now();     // Convert to a time_t     time_t tt = system_clock::to_time_t(tpoint);     // Convert to local time     tm* t2 = std::localtime(&tt);     // Write the time to the console     std::cout << std::put_time(t2, "%H:%M:%S") << std::endl;          // Convert to readable format     char buffer[80] = { 0 };     strftime(buffer, sizeof(buffer), "%H:%M:%S", t2);     // Write the time to the console     std::cout << buffer << std::endl; //professional c++ 4th edition p469         auto start = high_resolution_clock::now();     // Execute code that you want to time     double d = 0;     for (int i = 0; i < 1000000; ++i) {         d += sqrt(sin(i) * cos(i));     }     // Get the end time and calculate the difference     auto end = high_resolution_clock::now();     auto diff = end - start;     // Convert the difference into milliseconds and output to the console     std::cout << duration<double, std::milli>(diff).count() << "ms" << std::endl;

        // Output d, because otherwise a compiler might optimize away the whole loop above.     std::cout << d << std::endl;          time_point<steady_clock> tp1;     tp1 += minutes(10);     // Store the duration between epoch and time_point     auto d11 = tp1.time_since_epoch();     // Convert the duration to seconds and output to the console     duration<double> d12(d11);     std::cout << d12.count() << " seconds" << std::endl;          {         time_point<steady_clock, seconds> tpSeconds(42s);         // Convert seconds to milliseconds implicitly.         time_point<steady_clock, milliseconds> tpMilliseconds(tpSeconds);

            std::cout << tpMilliseconds.time_since_epoch().count() << " ms" << std::endl;     }

        {         time_point<steady_clock, milliseconds> tpMilliseconds(42'424ms);         // Convert milliseconds to seconds explicitly.         time_point<steady_clock, seconds> tpSeconds(             time_point_cast<seconds>(tpMilliseconds));

            // Convert seconds back to milliseconds and output the result.         milliseconds ms(tpSeconds.time_since_epoch());         std::cout << ms.count() << " ms" << std::endl;     } //professional c++ 4th edition p470         srand(static_cast<unsigned int>(time(nullptr)));          std::cout << rand() << std::endl;

        std::cout << getRandom(1, 6) << std::endl;

        return 0; }

    /* wannian07@wannian07-PC:~$ g++ -std=c++17 -o c13 c13.cpp wannian07@wannian07-PC:~$ ./c13 123 1.79769e+308 d3 > d4 10 minutes + 30 seconds = 10.5 minutes or 630 seconds 30 seconds = 0.5 minutes 0 600 5025 seconds

    21:07:05 21:07:05

    108.155ms -nan

    600 seconds

    42000 ms 42000 ms

    1647843756 3 */

    Processed: 0.016, SQL: 9