2015-02-23 28 views
0

我對C++還很陌生,以及編程和使用的術語。我正在學習「編程:使用C++的原理和實踐」(就像天賦給我的那樣),第四章結束時我遇到了一個問題。鑽頭被分成12個練習,其中前五個如下:一個練習要求我改變循環的主體。這是什麼意思?

  1. 編寫一個程序,由一while循環的那個(圍繞每次循環)在兩個整數和然後打印讀取他們。當終止'|'時退出 程序被輸入。

  2. 改寫程序寫出來的較小值是:後面的數字越小越大的值是:後面跟着 的較大值。

  3. 擴大該程序,以便它寫入的行數相等(只),如果他們是相等的。

  4. 更改程序,使其使用雙打而不是整數。

  5. 更改程序,使其寫出後寫出的數字幾乎相等,如果兩個 的數字相差小於1.0/100,則該數字越大越小。

我已經處理了這些工作,但現在我不完全得到什麼,在接下來的練習做:

  • 現在變化循環的主體,使其每次只讀取一倍。定義兩個變量來跟蹤哪一個是最小的,哪一個是迄今爲止所看到的最大值 。每次通過循環寫出輸入的值。如果它是迄今爲止最小的,那麼在數字後面寫下最小的。如果它是迄今爲止最大的,那麼在編號之後寫下目前最大的。
  • 我不明白。我該怎麼處理循環? 練習6實際上是什麼?


    我的代碼,我已經取得了迄今爲止從第五個步驟如下:

    #include <iostream> 
    #include <vector> 
    #include <algorithm> 
    //Name 
    int main() 
    { 
    char terminate = ' '; 
    double one = 0.0; 
    double two = 0.0; 
    int one_i = one; 
    int two_i = two; 
    while (terminate != '|') 
    { 
        std::cout << "Input two numbers, follow each one by enter: " << std::endl; 
        std::cin >> one; 
        std::cin >> two; 
        if (one == two) 
        { 
         std::cout << "The two numbers are equal to each other." << std::endl; 
         std::cout << "To terminate this program, type \"|\" into the system followed by pressing enter twice." << std::endl; 
         std::cin >> terminate; 
         if (terminate == '|') 
         { 
          break; 
         } 
        } 
        std::cout << "Here is the larger value: "; 
        if (one > two) 
        { 
         std::cout << one << std::endl; 
        } 
        else 
        { 
         if (two > one) 
         { 
          std::cout << two << std::endl; 
         } 
        } 
        std::cout << "Here is the smaller value: "; 
        if (one < two) 
        { 
         std::cout << one << std::endl; 
         if (one_i == two_i || two_i == one_i) 
         { 
          std::wcout << "The numbers are almost equal." << std::endl; 
         } 
        } 
        else 
        { 
         if (two < one) 
         { 
          std::cout << two << std::endl; 
          if (one_i == two_i || two_i == one_i) 
          { 
           std::wcout << "The numbers are almost equal." << std::endl; 
          } 
         } 
        } 
    
        std::cout << "To terminate this program, type \"|\" into the system followed by pressing enter twice." << std::endl; 
        std::cin >> terminate; 
    } 
    } 
    

    我試圖找出問題,如果這個代碼可以幫助任何你看到的到什麼程度我對此感到困惑。

    #include <iostream> 
    #include <vector> 
    #include <algorithm> 
    //Name 
    int main() 
    { 
    char terminate = ' '; 
    std::vector<double>num_size; 
    
    while (terminate != '|') 
    { 
        std::cout << "Type in a number: " << std::endl; 
        for (double num; std::cin >> num;) 
        { 
         num_size.push_back(num); 
         std::sort(num_size.begin(), num_size.end()); 
    
        } 
        std::cout << "To terminate this program, type \"|\" into the system followed by pressing enter twice." << std::endl; 
        std::cin >> terminate; 
    } 
    } 
    
    +6

    那麼,什麼是你的問題? – 2015-02-23 12:59:00

    +0

    *「我不太瞭解我的C++ Drill的語句」*。你究竟在哪裏遇到問題?您提供了兩個代碼片段,但尚不清楚問題出在哪裏或哪裏。你期望的行爲是什麼?你遇到什麼行爲? – Zeta 2015-02-23 12:59:46

    +0

    步驟6要求我做什麼? – Episha 2015-02-23 13:00:11

    回答

    0

    那麼,你沒有完成第5步。999和1000幾乎相等(差異< 1%)。

    忽略這一點,你的第二個片段是產生第6步中想要的行爲的良好開端,但忽略了規定的方法。是的,已排序的向量具有.front().back(),它們分別是最小值和最大值,但第6步特別告訴您使用兩個變量而不是整個向量。

    所以double max = std::numeric_limits<double>::max(); double min = -max;從那裏你應該能夠弄明白。

    0

    你想要的東西,如:

    double my_max = numeric_limits<double>::max(); 
    double my_min = -1 * numeric_limits<double>::max(); 
    while (...) { 
        ... 
        my_min = min(my_min, one); 
        my_min = min(my_min, two); 
        my_max = max(my_max, one); 
        my_max = max(my_max, two); 
    
    相關問題