目前分類:C++ (39)

瀏覽方式: 標題列表 簡短摘要

//---------------------------------------------------------------------------
#include <iostream>
/*
 * 這個程式可進行 0 到 100 之間攝氏和華氏溫度的轉換
 *
 * 條件:
 *    這個程式僅能處理整數,計算的結果可能不是十分精確
 */
//---------------------------------------------------------------------------
int celsius;     //要處理的攝氏溫度

int main(int argc, char* argv[])
{
        for (celsius = 0; celsius <= 100; ++celsius);
          std::cout << "celsius: " << celsius <<
                   " Fahrenheit: " << ((celsius * 9) / 5 + 32) << '\n';

        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------

執行時,這個程式會輸出:
    Celsius: 101    Fahrenheit: 213
就沒有其它的東西了,為什麼?

Answer: 問題是出在 for 敘述後的分號(;)。 for 主體是在右括號及分號之間,之間沒
有任何敘述,雖然 std::cout 敘述有縮排,但仍非 for 敘述的一部份。這個內縮格式
很容易誤導你,C++ 編譯器並不認得縮排。這個程式什麼都不做,直到運算式 
celsius <= 100 變成 false ( celsius == 101 )。接著才執行 std::cout。

題目來源:C++風格與藝術 第二版 Steve Oualline 著 黃吉霈 編譯

jumbowind 發表在 痞客邦 留言(0) 人氣()

//---------------------------------------------------------------------------
#include <iostream>
//---------------------------------------------------------------------------
int total;     // 全部的組合
int current;   // 使用者現在輸入的數值
int counter;   // 迴圈計數器

int main(int argc, char* argv[])
{
        total = 0;
        for (counter = 0;counter < 5; counter++) {
          std::cout << "Number? ";

          std::cin >> current;
          total += current;
        }
        std::cout << "The grand total is " << total << '\n';
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


題目來源:C++風格與藝術 第二版 Steve Oualline 著 黃吉霈 編譯

jumbowind 發表在 痞客邦 留言(0) 人氣()

//---------------------------------------------------------------------------
#include <iostream>
//---------------------------------------------------------------------------
int total;     // 全部的組合
int current;   // 使用者現在輸入的數值
int counter;   // 迴圈計數器

int main(int argc, char* argv[])
{
        total = 0;

        counter = 0;
        while (counter < 5){
          std::cout << "Number? ";

          std::cin >> current;
          total += current;

          ++counter;
        }
        std::cout << "The grand total is " << total << '\n';
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------
 
這個程式可用 for 敘述重寫,結果如範例 8-2。


題目來源:C++風格與藝術 第二版 Steve Oualline 著 黃吉霈 編譯

jumbowind 發表在 痞客邦 留言(0) 人氣()

//---------------------------------------------------------------------------
#include <iostream>
//---------------------------------------------------------------------------
int result;  // 計算的結果
char oper_char;  // 使用者指定的運算子
int value;  // 在運算子後面的數值

int main(int argc, char* argv[])
{
        result = 0; //設定結果的初值

        //無窮迴圈(遇到break敘述才停止)
        while (true){
          std::cout << " Result: " << result <<'\n';

          std::cout << " Enter operator and number: ";
          std::cin >> oper_char >> value;

          if (oper_char = '+') {
            result += value;
          } else {
              std::cout << " Unknown operator " << oper_char << '\n';
          }
        }
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


題目來源:C++風格與藝術 第二版 Steve Oualline 著 黃吉霈 編譯

jumbowind 發表在 痞客邦 留言(0) 人氣()

//---------------------------------------------------------------------------
/****************************************************************************
練習6-5:寫一個程式可輸入工作時數和每小時薪資,算出每周的薪水。若是超過40小 *
         時的部份,則應乘上1.5倍(算是加班的鐘點費)。                 *
****************************************************************************/
#include <iostream>
#include <string>
//---------------------------------------------------------------------------
int workHour;
int hourSalary;
int weekSalary;

int main(int argc, char* argv[])
{
        std::cout << "Enter work hours and salary per/hour, system will tell "
                  << "you how much \nsalary you get this week: \n";

        std::cin >> workHour;
        std::cin >> hourSalary;

        if (workHour <= 40)
        {
          weekSalary = workHour * hourSalary;
          std::cout << "You get NT $" << weekSalary << " this week";
        }

        else
        {
          weekSalary = 40 * hourSalary + (workHour-40) * 1.5 * hourSalary;
          std::cout << "You can get NT $" << weekSalary << " this week";
        }

        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


題目來源:C++風格與藝術 第二版 Steve Oualline 著 黃吉霈 編譯
答案來源:自我撰寫(不保證正確喔)

jumbowind 發表在 痞客邦 留言(0) 人氣()

//---------------------------------------------------------------------------
/****************************************************************************
練習6-4:閏年的定義為可被4整除而不可被100整除的年份,或者能被400整除的年份。*
         請寫一個程式,來判斷某一年是否為閏年。                           *
****************************************************************************/
#include <iostream>
#include <string>
//---------------------------------------------------------------------------
int year;


int main(int argc, char* argv[])
{
        std::cout << "Enter year,system will tell you whether it is bissextile "
                  << "or not: \n";

        std::cin >> year;
        if (year%4000==0)
        {
          std::cout << year << " is not bissextile\n";
          std::system("pause");
          return 0;
        }
        else if (year%400==0)
        {
          std::cout << year << " is bissextile\n";
          std::system("pause");
          return 0;
        }
        else if (year%4==0 && year%100!=0)
            std::cout << year << " is bissextile\n";
        else
            std::cout << year << " is not bissextile\n";

        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


題目來源:C++風格與藝術 第二版 Steve Oualline 著 黃吉霈 編譯
答案來源:自我撰寫(不保證正確喔)

jumbowind 發表在 痞客邦 留言(0) 人氣()

最後一位數字修飾符號
1-3-
4-7(空白)
8-0+








//---------------------------------------------------------------------------
/****************************************************************************
練習6-2:修改前一個程式,依分數的個位數印出+或-的符號。參考上表             *
****************************************************************************/
#include <iostream>
#include <string>
//---------------------------------------------------------------------------
std::string grades;
char last_char;
int n=1;


int main(int argc, char* argv[])
{
        std::cout << "Enter grades,system will change last number to the "
                  << "symbols as the upper form \n";

        for (int i=1;i<=n;i){

          std::cin >> grades;

          j = grades.size()-1;
          last_char = grades.at(j);

          if ( last_char == '1' || last_char == '2' || last_char == '3')
            std::cout << "-\n";

          else if ( last_char == '4' || last_char == '5' || last_char == '6'
               || last_char == '7')
            std::cout << " \n";

          else if ( last_char == '8' || last_char == '9' || last_char == '0')
            std::cout << "+\n";
        }
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


題目來源:C++風格與藝術 第二版 Steve Oualline 著 黃吉霈 編譯
答案來源:自我撰寫(不保證正確喔)

jumbowind 發表在 痞客邦 留言(0) 人氣()

有位教授的成績評等使用下表:

分數(%)評等
0-60F
61-70D
71-80C
81-90B
91-100A











//---------------------------------------------------------------------------
/****************************************************************************
練習6-1:輸入分數,然後印出成績                                             *
****************************************************************************/
#include <iostream>
//---------------------------------------------------------------------------
int grades;
int n=1;

int main(int argc, char* argv[])
{
        std::cout << "Enter grades,system will change it to A~F rank: "
                  << "(not 0-100 = exit) \n";

        for (int i=1;i<=n;i){
          std::cin >> grades;

          if ( grades <= 60 && grades >=0)
            std::cout << "F\n";

          else if ( grades >60 && grades <=70 )
            std::cout << "D\n";

          else if ( grades >70 && grades <=80 )
            std::cout << "C\n";

          else if ( grades >80 && grades <=90 )
            std::cout << "B\n";

          else if ( grades >90 && grades <=100 )
            std::cout << "A\n";

          else
            {std::cout << "You enter the wrong grades";
             break;}
        }
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


題目來源:C++風格與藝術 第二版 Steve Oualline 著 黃吉霈 編譯
答案來源:自我撰寫(不保證正確喔)

jumbowind 發表在 痞客邦 留言(0) 人氣()

//---------------------------------------------------------------------------
#include <iostream>
//---------------------------------------------------------------------------
int balance_owed;  //帳戶餘額

int main(int argc, char* argv[])
{
        std::cout << "Enter number of dollars owed:";
        std::cin >> balance_owed;

        if (balance_owed = 0)
          std::cout << "You owe nothing.\n";
        else
          std::cout << "You owe " << balance_owed << " dollars.\n";
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


輸出結果:
       Enter number of dollars owed: 12
       You owe 0 dollars.

Q:為什麼每個人的帳戶會變成0?

Ans:if (balance_owed = 0) 用的是一個等號而不是兩個等號。
          C++ 會將 balance_owed 的值設為0,並且測試該值  (0) ; 
         若結果不是 0 (條件成立),就會執行 if 子句。
          但因為結果為 0 (條件不成立) ,所以會執行 else 子句。
          
          該行敘述:
                 if (balance_owed = 0)
         等於:
                 balance_owed = 0;
                 if ( balance_owed != 0)
        應更正為:
                if (balance_owed == 0)


題目來源:C++風格與藝術 第二版 Steve Oualline 著 黃吉霈 編譯

jumbowind 發表在 痞客邦 留言(0) 人氣()

//---------------------------------------------------------------------------
#include <iostream>
//---------------------------------------------------------------------------
int total;         //目前數字的總和
int item;          //下一個加總的數字
int minus_items;   //負數

int main(int argc, char* argv[])
{
        total = 0;
        minus_items = 0;
        while (true){
          std::cout << "請輸入欲加總的數字 \n";
          std::cout << " 或 0 表示結束: ";
          std::cin >> item;

          if (item == 0)
            break;

          if (item < 0){
            ++minus_items;
            continue;
          }
          total += item;
          std::cout << " 小計: " << total << '\n';
        }
        std::cout << " 總和: " << total << '\n';
        std::cout << " 共有: " << minus_items << " 個負數被忽略 \n";
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


題目來源:C++風格與藝術 第二版 Steve Oualline 著 黃吉霈 編譯

jumbowind 發表在 痞客邦 留言(0) 人氣()

//---------------------------------------------------------------------------
#include <iostream>
//---------------------------------------------------------------------------
int total;    //目前數字的總和
int item;     //下一個加總的數字

int main(int argc, char* argv[])
{
        total = 0;
        while (true){
          std::cout << "請輸入欲加總的數字 \n";
          std::cout << " 或 0 表示結束: ";
          std::cin >> item;

          if (item == 0)
            break;

          total += item;
          std::cout << " 小計: " << total << '\n';
        }
        std::cout << " 總和: " << total << '\n';
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


 題目來源:C++風格與藝術 第二版 Steve Oualline 著 黃吉霈 編譯

jumbowind 發表在 痞客邦 留言(0) 人氣()

//---------------------------------------------------------------------------
/****************************************************************************
範例6-1:計算小於100的費式數。費式數列(Fibonacci sequence)如下:            *
   1 1 2 3 5 8                                                              *
   計算方式如下:                                                           *
     1                                                                      *
     1                                                                      *
     2 = 1 + 1                                                              *
     3 = 1 + 2                                                              *
     5 = 3 + 2                                                              *
     ...依此類推。                                                          *
                                                                            *
     它的公式為:                                                           *
        fn = fn-1 + fn-2                                                   *
****************************************************************************/
#include <iostream>
//---------------------------------------------------------------------------
int old_number;      // 前一個費式數
int current_number;  // 現在的費式數
int next_number;     // 下一個費式數

int main(int argc, char* argv[])
{
        //起始時的設定
        old_number = 1;
        current_number = 1;

        std::cout << current_number << '\n'; //印出第一個數

        while (current_number < 100) {
          std::cout << current_number << '\n';
          next_number = current_number + old_number;

          old_number = current_number;
          current_number = next_number;
        }
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


題目來源:C++風格與藝術 第二版 Steve Oualline 著 黃吉霈 編譯

jumbowind 發表在 痞客邦 留言(0) 人氣()

//---------------------------------------------------------------------------
/****************************************************************************
練習5-5:寫一個程式讀取代表分鐘數的整數,然後輸出相等的小時和分鐘數。       *
      (例如:90分鐘 = 1小時30分)                                            *
****************************************************************************/
#include <iostream>
//---------------------------------------------------------------------------
int time;
int hour;
int minute;

int main(int argc, char* argv[])
{
        std::cout << "Enter the number of minutes:";
        std::cin >> time;
        hour = time / 60;
        minute = time % 60;
        std::cout << time << " 分鐘 = " << hour << " 時 "<< minute << " 分 "
                  << '\n';
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


題目來源:C++風格與藝術 第二版 Steve Oualline 著 黃吉霈 編譯
答案來源:自我撰寫(不保證正確喔)

jumbowind 發表在 痞客邦 留言(0) 人氣()

//---------------------------------------------------------------------------
/****************************************************************************
練習5-5:寫一個程式讀取代表小時與分鐘數的整數,然後輸出相等的分鐘數。       *
      (例如:1小時30分 = 90分鐘)                                            *
****************************************************************************/
#include <iostream>
//---------------------------------------------------------------------------
int hour;
int minute;

int main(int argc, char* argv[])
{
        std::cout << "Enter the numbers of hour and minute:";
        std::cin >> hour >> minute;
        std::cout << hour << " 小時 " << minute << " 分 = "<< hour*60+minute
                  << " 分鐘" << '\n';
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


題目來源:C++風格與藝術 第二版 Steve Oualline 著 黃吉霈 編譯
答案來源:自我撰寫(不保證正確喔)

jumbowind 發表在 痞客邦 留言(0) 人氣()

//---------------------------------------------------------------------------
/****************************************************************************
練習5-4:寫一個程式將公里(kilometer)轉換為英哩(mile)。                               *
                英哩 = 公里 X 0.6213712                                                     *
****************************************************************************/
#include <iostream>
//---------------------------------------------------------------------------
float number;

int main(int argc, char* argv[])
{
        std::cout << "Enter a number of kilometer, and the system will change "
             << "it to the mile:";
        std::cin >> number;
        std::cout << number << " kilometer = " << number*0.6213712 << " mile";
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


題目來源:C++風格與藝術 第二版 Steve Oualline 著 黃吉霈 編譯
答案來源:自我撰寫(不保證正確喔)

jumbowind 發表在 痞客邦 留言(0) 人氣()

//---------------------------------------------------------------------------
/****************************************************************************
練習5-3:寫一個程式可以輸入四方形的寬和高後,求出其周長。                   *
         周長 = 2 * ( 寬 + 高 )                                             *
****************************************************************************/
#include <iostream>

//---------------------------------------------------------------------------
float width;
float height;

int main(int argc, char* argv[])
{
        std::cout << "Enter the width and height of quadrangularly in a row:";
        std::cin >> width >> height;
        std::cout << "The quadrangularly's circumference is:"
                  << 2 * ( width + height ) << '\n';
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


題目來源:C++風格與藝術 第二版 Steve Oualline 著 黃吉霈 編譯
答案來源:自我撰寫(不保證正確喔)

jumbowind 發表在 痞客邦 留言(0) 人氣()

//---------------------------------------------------------------------------
/****************************************************************************
練習5-2:寫一個程式計算球體的體積,4/3πr3。                                *
****************************************************************************/
#include <iostream>

//---------------------------------------------------------------------------
float r;
const float PI = 3.1415926;

int main(int argc, char* argv[])
{
        std::cout << "Enter the radius value of ball:";
        std::cin >> r;
        std::cout << "The ball's valume is(4/3πr3):" << 4.0/3.0 * PI * (r*r*r)
                  << '\n';
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------



題目來源:C++風格與藝術 第二版 Steve Oualline 著 黃吉霈 編譯
答案來源:自我撰寫(不保證正確喔)

jumbowind 發表在 痞客邦 留言(0) 人氣()

//---------------------------------------------------------------------------
/****************************************************************************
練習5-1:寫一個程式進行攝氏溫度轉換成華氏溫度。                             *
****************************************************************************/
#include <iostream>

//---------------------------------------------------------------------------
float C_temparature;
float F_tempature;

int main(int argc, char* argv[])
{
        std::cout << "Enter the degrees of centigrade temparature, and system "
                  << '\n' <<"will changeit it to Fahrenheit tempararture: ";
        std::cin >> C_temparature;
        F_tempature = ( 9/5 * C_temparature ) + 32;
        std::cout << "The Fahrenheit temparature: " << F_tempature << '\n';
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


這樣印出的答案會是錯的,Why?

因為 9/5 是整數除法=1,所以要將它改成 9.0/.50 ~


題目來源:C++風格與藝術 第二版 Steve Oualline 著 黃吉霈 編譯
答案來源:自我撰寫(不保證正確喔)

jumbowind 發表在 痞客邦 留言(0) 人氣()

//---------------------------------------------------------------------------
#include <iostream>


//---------------------------------------------------------------------------
long int zip;

int main(int argc, char* argv[])
{
        zip = 02137L;

        std::cout << "New York's zip code is: " << zip << '\n';
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------

Q:為什麼上面的程式無法印出正確的郵遞區號?印出的結果是什麼?


A:因為郵遞區碼02137的起始值為0。C++認為02137是一個八進位的常數,
      但印出時則適用十進位。由於021378是111910,程式會印出:
              New York's zip code is: 1119


題目來源:C++風格與藝術 第二版 Steve Oualline 著 黃吉霈 編譯

jumbowind 發表在 痞客邦 留言(0) 人氣()

最短整數的讀/寫有些複雜。如果你在輸出敘述中使用char變數,
它會當作是「字元」來處理。你必須告訴C++:char 型態的變數
是一個整數,這可以使用 static_cast 運算子來達成。

//---------------------------------------------------------------------------
#include <iostream>
//---------------------------------------------------------------------------
signed char ch;

int main(int argc, char* argv[])
{
        ch=37;
        std::cout << "The name is " << static_cast<int>(ch) << '\n';
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------

輸出結果:
The name is 37


題目來源:C++風格與藝術 第二版 Steve Oualline 著 黃吉霈 編譯


jumbowind 發表在 痞客邦 留言(0) 人氣()

1 2