2014年10月24日 星期五

浮動標籤

#include <iostream>
int main()
{
    int a;  //宣告變數a
    int* b; //宣告浮動標籤 名稱b
    std::cin>>a; //輸入a值
    b=&a; //把a的記憶體位子copy到b的記憶體裡
    std::cout<<"address of a \n";
    std::cout<<b<<"="<<&a<<"\n"; //b裡存的是a的位子
    std::cout<<"a is: \t"<<*b<<"="<<a; //*b是a記憶體的別名
    return 0;
}

2014年10月13日 星期一

判斷質數

#include <iostream>
#include <cmath>
int main()
{
float N;
if(N==2)
{
std::cout<<"prime \n";
return 0;
}
else
{
if((N/2)==int(N/2))
{
std::cout<<"not prime \n";
return 0;
}
else
{
for(int D=3;D<=sqrt(N);++++D)
{
if((N/D)==int(N/D))
std::cout<<"not prime \n";
return 0;
}
std::cout<<"prime \n";
return 0;
}
}
}

2014年10月10日 星期五

2014/10/10 作業比大小三種寫法

第一種

#include <iostream>
#include <ctype.h>
int main()
{
    char a,b,c;
    std::cout<<"PLEASE ENTER THREE WORDS \n";
    std::cout<<"first \t"; std::cin>>a;
    std::cout<<"\n second \t"; std::cin>>b;
    std::cout<<"\n third \t"; std::cin>>c;
    if(isdigit(a) && isdigit(b) && isdigit(c))
    {
        std::cout<<"sort big to small \n";
        if(a>=b && b>=c)
        {
            std::cout<<a<<","<<b<<","<<c<<std::endl;
        }
        else if(a>=c && c>=b)
        {
            std::cout<<a<<","<<c<<","<<b<<std::endl;
        }
        else if(b>=a && a>=c)
        {
            std::cout<<b<<","<<a<<","<<c<<std::endl;
        }
        else if(b>=c && c>=a)
        {
            std::cout<<b<<","<<c<<","<<a<<std::endl;
        }
        else if(c>=a && a>=b)
        {
            std::cout<<c<<","<<a<<","<<b<<std::endl;
        }
        else if(c>=b && b>=a)
            std::cout<<c<<","<<b<<","<<a<<std::endl;
    }
    else
    {
        std::cout<<"you are not enter a digit \n";
    }
    return 0;
}

第二種

#include <iostream>
#include <ctype.h>
using namespace std;

int main()
{
    char a,b,c;
    std::cout<<"PLEASE ENTER THREE WORDS \n";
    std::cout<<"first \t"; std::cin>>a;
    std::cout<<"\n second \t"; std::cin>>b;
    std::cout<<"\n third \t"; std::cin>>c;
    if(isdigit(a) && isdigit(b) && isdigit(c))
    {
        std::cout<<"sort big to small \n";
        if(a>=b)
        {
            if(b>=c)
            {
                std::cout<<a<<","<<b<<","<<c<<std::endl;
            }
            else
            {
                if(a>=c)
                {
                    std::cout<<a<<","<<c<<","<<b<<std::cout;
                }
                else
                {
                    std::cout<<c<<","<<a<<","<<b<<std::endl;
                }
            }
        }
        else
        {
            if(a>=c)
            {
                std::cout<<b<<","<<a<<","<<c<<std::endl;
            }
            else
            {
                if(b>=c)
                {
                    std::cout<<b<<","<<c<<","<<a<<std::endl;
                }
                else
                {
                    std::cout<<c<<","<<b<<","<<a<<std::endl;
                }
            }
        }
    }
    else
    {
        std::cout<<"your not enter digit \n";
    }
    return 0;
}

2014年10月7日 星期二

指定字串為數字

#include <iostream>
#include <ctype.h>
using namespace std;
int main()
{
char a;
cout<<"請輸入 \n";
cin>>a;
if(isdigit(a))
cout<<"你輸入的是數字 \n";
else
cout<<"你輸入的不是數字 \n";
return 0;
}

--------------------------------------------------------------------------------------------------------------------
我目前只是個大學一年級的資工生,以上有何不對的地方歡迎指教!!

2014年10月1日 星期三

運算子

#include <iostream>

using namespace std;

int main()
{
     int a,b;
     cin >> a,b;
     if(a > 5 & b > 5)
       {
           cout << "hello!! \n";
        }
      else
        {
            cout<<"hello world!! \n";
         }
     return 0;
}

"hello!! \n" 的\n有跟 endl 幾乎一樣的功用

--------------------------------------------------------------------------------------------------------------------------
我目前只是個大學一年級的資工生,以上有何不對的地方歡迎指教!!

變數替換

#include <iostream>

using namespace std;

int main()
{
     int a,b,temp;
     cin >> a>>b;
     cout << "原來的a,b值"<< a<<"\t"<< b<<endl;
     temp = a;
     a = b;
     b = temp;
     cout <<"後來的a,b值"<<a<<"\t"<<b<<endl;
     return 0;
}

temp = a; 的=不是數學的"等於"的意思,而是把右值賦予給左值的意思
(程式的等於是 ==,兩個等號)

--------------------------------------------------------------------------------------------------------------------------
我目前只是個大學一年級的資工生,以上有何不對的地方歡迎指教!!

2014年9月25日 星期四

重複執行

#include <iostream>

using namespace std;

int main()
{
start:
int a;
cout << "請輸入"<<endl;
cin >> a;
cout << a<<endl;
goto start;
}

start 可改成其他的東西 goto start 的意思是重新執行start以下的程式

--------------------------------------------------------------------------------------------------------------------------
我目前只是個大學一年級的資工生,以上有何不對的地方歡迎指教!!

基本運算

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
 int a,b;
cin >> a >> b;
cout << a+b<<endl /*相加*/
cout << a-b<<endl;   /*相減*/
cout << a*b<<endl;  /*相乘*/
cout << a/b<<endl;  /*相除*/
cout << sqrt(a)<<endl;  /*根號*/
}

--------------------------------------------------------------------------------------------------------------------------
我目前只是個大學一年級的資工生,以上有何不對的地方歡迎指教!!

數字大小排列

#include <iostream>
#include <cmath>

using namespace std;

 int main()
{
int a,b;
cin >> a;
cin >> b;
if(a>=b)
{
cout<< a<< ","<< b<<endl;
}
else
{
cout<< a<< ","<< b<<endl;
}
}

if是條件句如果符合條件則執行if內的程式

--------------------------------------------------------------------------------------------------------------------------
我目前只是個大學一年級的資工生,以上有何不對的地方歡迎指教!!

基本輸入,輸出

#include <iostream>

using namespace std;

int main()
{
     int a;
     cin >> a;
     cout << a<<endl;
}

int 是data type 資料型態 宣告函數,變數用
cin則是可由鍵盤輸入的變數
endl = end-L (End Line)用於換行

--------------------------------------------------------------------------------------------------------------------------
我目前只是個大學一年級的資工生,以上有何不對的地方歡迎指教!!

顯示文字

#include <iostream>

using namespace std;

int main()
{
      cout  << "Hello world"<<endl;      /*Hello world 可改為其他字*/
      return 0;
}

#include是把程式庫導入,導入iostream才可用cout,cin...之類的指令
cout則是把訊息傳到螢幕上顯示


--------------------------------------------------------------------------------------------------------------------------
我目前只是個大學一年級的資工生,以上有何不對的地方歡迎指教!!