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; 的=不是數學的"等於"的意思,而是把右值賦予給左值的意思
(程式的等於是 ==,兩個等號)

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