目前分類:C++ (39)

瀏覽方式: 標題列表 簡短摘要
  • Mar 18 Tue 2008 23:13
  • 5-11

//---------------------------------------------------------------------------
#include <iostream>
#include <cstring>
//---------------------------------------------------------------------------
char first[100];
char last[100];
char full_name[100];

int main(int argc, char* argv[])
{
        std::strcpy(first, "Steve");
        std::strcpy(last, "Oualline");

        std::strcpy(full_name, first); // full = "Steve"
        std::strcat(full_name, " ");   // full = "Steve "
        std::strcat(full_name, last);  // full = "Steve Oualline"
        std::cout << "The full name is " << full_name << '\n';
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------

此程式的輸出是:
The full name is Steve Oualline


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

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

//---------------------------------------------------------------------------
#include <iostream>
#include <cstring>
//---------------------------------------------------------------------------
char name[30];

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

或可改寫成

//---------------------------------------------------------------------------
#include <iostream>
#include <cstring>
//---------------------------------------------------------------------------
char name[] = "Sam";

int main(int argc, char* argv[])
{

        std::cout << "The name is " << name << '\n';
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


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

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

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

//---------------------------------------------------------------------------
int array[3][5] = {
   {0, 1, 2, 3, 4 },
   {10, 11, 12, 13, 14 },
   {20, 21, 22, 23, 24 }
};

int main(int argc, char* argv[])
{
        std::cout << "Last element is " << array[2,4] << '\n';
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


Q:為什麼範例5-9中的程式會輸出不正確的答案?

Answer:問題在指定陣列元素的方式:array[2,4]。應該寫成:array[2][4]。


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

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

//---------------------------------------------------------------------------
#include <iostream>
#include <assert.h>

//---------------------------------------------------------------------------
int primes[] = {2, 3, 5, 7, 11, 13, 17};

int main(int argc, char* argv[])
{
        int index = 10;

        assert(index < (sizeof(primes)/sizeof(primes[0])));
        assert(index >= 0);
        std::cout << "The tenth prime is " << primes[index] << '\n';
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


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

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

//---------------------------------------------------------------------------
/****************************************************************************
範例5-5                                                                     *
****************************************************************************/
#include <iostream>
#include <string>

//---------------------------------------------------------------------------
std::string line;

int main(int argc, char* argv[])
{
        std::cout << "Enter a line:";
        std::getline(std::cin, line);

        std::cout << "The length of the line is: " << line.length() << '\n';
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


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

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

****************************************************************************/
#include <iostream>

//---------------------------------------------------------------------------
int height;
int width;
int area;

int main(int argc, char* argv[])
{
        std::cout << "Enter width height: ";
        std::cin >> width >> height;
        area = ( width * height ) / 2;
        std::cout<< "The area is " << area << '\n';
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


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

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

//---------------------------------------------------------------------------
/****************************************************************************
範例5-3                                                                     *
****************************************************************************/
#include <iostream>

//---------------------------------------------------------------------------
int value;

int main(int argc, char* argv[])
{
        std::cout << "Enter a value: ";
        std::cin >> value;
        std::cout << "Twice " << value << " is " << value * 2 << '\n';
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


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

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

//---------------------------------------------------------------------------
/****************************************************************************
範例5-2                                                                     *
****************************************************************************/
#include <iostream>
#include <string>

//---------------------------------------------------------------------------
std::string first_name;
std::string last_name;
std::string full_name;

int main(int argc, char* argv[])
{
        first_name = "Steve";
        last_name = "Oualline";
        full_name = first_name + " " + last_name;
        std::cout << " Full name is " << full_name << '\n';
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


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

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

//---------------------------------------------------------------------------
/****************************************************************************
範例5-1                                                                     *
****************************************************************************/
#include <iostream>

//---------------------------------------------------------------------------
float data[5];
float total;
float average;

int main(int argc, char* argv[])
{
        data[0] = 34.0;
        data[1] = 27.0;
        data[2] = 46.5;
        data[3] = 82.0;
        data[4] = 22.0;

        total = data[0] + data[1] + data[2] + data[3] + data[4];
        average = total / 5.0;
        std::cout << "Total " << total << " Average " << average << '\n';
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


執行結果:
Total  211.5  Average  42.3

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

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

//---------------------------------------------------------------------------
/***************************************************************************
練習4-3:寫一個程式印出"HELLO",其中的每個字母為7個字元高和五個字元寬。    * 
***************************************************************************/
#include <iostream>
//---------------------------------------------------------------------------

int main(int argc, char* argv[])
{
        std::cout << " *      *  ***** *         *         ***** " << "\n"
                       << " *      *  *         *         *         *      * " << "\n"
                       << " *      *  *         *         *         *      * " << "\n"
                       << " *****  ****   *         *         *      * " << "\n"
                       << " *      *  *         *         *         *      * " << "\n"
                       << " *      *  *         *         *         *      * " << "\n"
                       << " *      *  ***** ***** ***** ***** " << "\n";
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


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

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

//---------------------------------------------------------------------------
/***************************************************************************
練習4-3:寫一個程式計算3英吋寬和五英吋寬的長方形面積及周長:如果要改成計算 *
6.8英吋寬和2.3英吋長,該如何修改程式?                                     *
***************************************************************************/
#include <iostream>
//---------------------------------------------------------------------------

int main(int argc, char* argv[])
{
        std::cout << " 長方形面積= " << 3*5 << " 平方英吋 " << "\n"
                       << " 長方形周長= " << 2*(3+5) << "英吋" << "\n";
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------



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

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

//---------------------------------------------------------------------------
/****************************************************************************
練習4-2:寫一個使用星號(*)組合出英文字母E的形狀,而E為7個字元高及5個字元寬  *
****************************************************************************/
#include <iostream>

//---------------------------------------------------------------------------

int main(int argc, char* argv[])
{
        std::cout << "*****" << "\n"
                       << "*        " << "\n"
                       << "*        " << "\n"
                       << "****  " << "\n"
                       << "*        " << "\n"
                       << "*        " << "\n"
                       << "*****" << "\n";
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


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

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

//---------------------------------------------------------------------------
/*********************************************************
練習4-1:寫一個程式印出你的姓名、身分證號碼和出生日期    *
*********************************************************/
#include <iostream>

//---------------------------------------------------------------------------

int main(int argc, char* argv[])
{
        std::cout << "My name is Jumbo " << "\n"
                       << "My ID number is S123456789 " << "\n"
                       << "My birth day is 1986/06/01 " << "\n";
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


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

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

//---------------------------------------------------------------------------
/*********************************************************
範例4-5                                                  *
*********************************************************/
#include <iostream>

//---------------------------------------------------------------------------
char char1;    //第一個字元
char char2;    //第二個字元
char char3;    //第三個字元

int main(int argc, char* argv[])
{
        char1 = 'A';
        char2 = 'B';
        char3 = 'C';
        std::cout << char1 << char2 << char3 << " reversed is "
                  << char3 << char2 << char1 << "\n";
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------


執行後會印出:
    ABC reversed is CBA




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

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

//---------------------------------------------------------------------------
/*********************************************************
範例4-4為什麼會印出"The value of 1/3 is 0?該如何解決?    *
*********************************************************/
#include <iostream>

//---------------------------------------------------------------------------
float answer;   //除法的結果

int main(int argc, char* argv[])
{
        answer = 1/3;
        std::cout << "The value of 1/3 is " << answer
                  << "\n";
        std::system("pause");
        return 0;
}
//---------------------------------------------------------------------------










解答:因為1和3都是整數,因此為整數除法。 
           小數部份會被整數除法所截去,可改寫成:
           answer = 1.0 / 3.0;


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

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

//--------------------------------------------------------------------------
/*************************************************
範例4-2                                                                                    *
*************************************************/
#include <iostream>

//--------------------------------------------------------------------------

int term;
int main(int argc, char* argv[])
{
        term = 3 * 5;
        std::cout<< " Twice " << term << " is " << 2*term << "\n";
        std::cout<< " Three times " << term << " is " << 3*term << "\n";
        std::system("pause");
        return 0;
}
//--------------------------------------------------------------------------


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

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

//---------------------------------------------------------------------------
/**************************************************
 課本範例3-1  "Hello World"                                                    *
**************************************************/

#include <iostream>

//---------------------------------------------------------------------------

int main(int argc, char* argv[])
{
        std::cout << "Hello World\n";
        std::system("pause");
        return 0;
}

//-----------------------------------------------------


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

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

//---------------------------------------------------------------------------
/**************************************************
 課本範例4-1  "簡單的運算式"                                               *
**************************************************/

#include <iostream>

//---------------------------------------------------------------------------

int main(int argc, char* argv[])
{
        std::cout << "(1+2) * 4=" << (1+2)*4 << "\n";
        std::system("pause");
        return 0;
}

//-------------------------------------------------------------------------


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

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

//---------------------------------------------------------------------------
/*******************************************************
 * 作者:Jumbo                                                                                  *
 *                                                                                                          *
 * 目的:判別使用者輸入的數是否為質數                                     *
 *                                                                                                          * 
 ******************************************************/
#include <iostream>

using std::cout;
using std::cin;
using std::system;

//---------------------------------------------------------------------------

int main(int argc, char* argv[])
{
        int integer;

        cout<<"Enter an integer, and I will tell you "
            <<"whether it is an integer:";

        cin>>integer;

        for (int i=2;i<integer;i++)
        {
          if (integer%i==0)
          {
            cout<<integer<<" is not a prime number!";
            system("pause");
            return 0;
          }
        }

        cout<<integer<<" is a prime number!";

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

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

«12