Show toolbar

2014年7月2日 星期三

For statement

標題:for迴圈語句
VC++ (forstatement.cpp):
//Chapter 1 - for statement
#include <iostream>
using namespace std;

int main(void)
{
    int i = 0, j = 0, len = 0;
    const double PI = acos(-1.0), DEGREE = 180.0/PI;

    for(i=0;i<=180;i+=10) {
        len = (int)( sin( i/DEGREE )*30 );
        for(j=0;j<len;j++) {
            cout << '*';
        }
        cout << " " << len << endl;
    }
    
    system("pause");
    return 0;
}
範例結果:

說明:
基本使用for迴圈印出sin波範例

If...else...statements

標題:if else 判斷語句
VC++ (ifstatement.cpp):
//Chapter 1 - if statement
#include <iostream>
#include <string>
using namespace std;

int main(void)
{
    int len = 0;
    string footballer = "";
    
    cout << "Input Name:";
    cin  >> footballer;

    len = footballer.length();

    if(len != 0 && len <= 10) {
        cout << footballer << endl;
    } else {
        cout << "Word length of less than 10" << endl;
    }
    
    system("pause");
    return 0;
}

說明:
基本if else判斷語句的使用

Cin and cout command

標題:輸入與輸出命令
VC++ (variable.cpp):
//Chapter 1 - cin and cout
#include <iostream>
#include <string>
using namespace std;
 
int main(void)
{
    int age = 19, year = 2011;
    string footballer = "";
    
    cout << "Input Name:";
    cin  >> footballer;
    cout << footballer << " is a Brazilian footballer.\n";
    cout << "At the age of " << age << ", " << footballer <<" won the " << year << " South American Footballer\n";

    system("pause");
    return 0;
}

說明:
基本輸入與輸出命令

Variable type and size

標題:變數型態與大小
VC++ (datatype.cpp):
//Chapter 1 - datatype
#include <iostream>
using namespace std;

int main(void)
{
    cout << "type\tbyte" << endl;

    cout << "int\t"    << sizeof(int)    << endl; //4
    cout << "long\t"   << sizeof(long)   << endl; //4
    cout << "float\t"  << sizeof(float)  << endl; //4
    cout << "double\t" << sizeof(double) << endl; //8
    cout << "char\t"   << sizeof(char)   << endl; //1
    cout << "bool\t"   << sizeof(bool)   << endl; //1

    system("pause");
    return 0;
}

說明:
基本的變數型態與大小

Cout example

標題:cout範例
VC++ (cout.cpp):
//Chapter 1 - Part1

//引用函式庫,使用標準函式庫
#include <iostream>

//標準函式庫的命名空間
using namespace std;

//主函式寫法
int main(void)
{
    //印出整數及文字
    cout << 10 << endl;
    cout << "Neymar" << endl;

    //送出Command命令pause
    system("pause");

    //回傳終止
    return 0;
}

說明:
基礎cout範例程式