C++ as to linux 乔林(1-4)

Chapter1 C++ basic grammar element

#include 
using namespace std;
int main()
{
     
    cout << "Hello World!" << endl;
    return 0;
}
#include 
using namespace std;
int main()
{
     
    int a,b,sum;
    cout << "a:";
    cin >> a;
    cout << "b:";
    cin >> b;
    sum = a + b;
    cout << a << "+" << b << "=" << sum << endl;
    return 0;
}

linux:

:~$ cd projects/practice
:~/projects/practice$ ls
test  test.cpp
:~/projects/practice$ test
:~/projects/practice$ text/a/out
bash: text/a/out: No such file or directory
:~/projects/practice$ g++ test.cpp
:~/projects/practice$ ls
a.out  test  test.cpp
:~/projects/practice$ ./a.out
a:1
b:2
1+2=3
:~/projects/practice$ rm ./a.out
:~/projects/practice$ ls
test  test.cpp
:~/projects/practice$ ./hello
bash: ./hello: No such file or directory
:~/projects/practice$ ./test
a:1
b:2
1+2=3

1.2

#include 
using namespace std;
int main()
{
     
    double cash,r;
    cout << "cash:";
    cin >> cash;
    cout << "r:";
    cin >> r;
    cash *= 1 + 0.01 * r;
    cout << cash << endl;
    return 0;
}

Chapter2 Programming Control Structure

#include 
using namespace std;
int main()
{
     
    enum MONTH{
     JAN=1,FEB,MAR};
    MONTH month;
    month=FEB;
    cout << month << endl;
    return 0;
}
#include 
using namespace std;
int main()
{
     
    bool modified;
    modified = false;
    cout << modified << endl;
    return 0;
}
#include 
#include 

using namespace std;

typedef enum {
     SUN,MON,TUE,WED,THU,FRI,SAT} WEEKDAY;

int main()
{
     
    int date;
    const WEEKDAY date_1=FRI;
    WEEKDAY weekday;
    cout << "The program gets a date(1~31), and prints a calendar.\n";
    cout << "The date:\n";
    cin >> date;
    if( date < 1 || date > 31 )
    {
     
        cout << "Date error!\n";
        return 1;
    };
    weekday = (WEEKDAY)((date + int(date_1) -1) % 7);
    cout << "Calendar 2006-12\n";
    cout << "Su  Mo  Tu  We  Th  Fr  Sa\n";
    switch(weekday)
    {
     
    case SUN:
      cout << setw(2) << date;
      break;
    case MON:
      cout << setw(6) << date;
      break;
    case TUE:
      cout << setw(10) << date;
      break;
    case WED:
      cout << setw(14) << date;
      break;
    case THU:
      cout << setw(18) << date;
      break;
    case FRI:
      cout << setw(22) << date;
      break;
    case SAT:
      cout << setw(26) << date;
      break;
    default:
      ;
    }
    return 0;
}  

2.1

#include 
#include 

using namespace std;

int main()
{
     
    int m,n;
    cout << "print volume :\n";
    cin >> n;
    cout << "space :\n";
    cin >> m;
    int i,j;
    for(i=1;i<=n;++i)
    {
     
        cout << setw(n-i+1);
        for(j=1;j<=(2*i-1);++j)
        {
     
            cout << "*";
        }
        cout << setw(m+1);
        for(j=2*n-(2*i-1);j>=1;--j)
        {
     
            cout << "*";
        }
        cout << endl;
    }
    return 0;
}  
~/projects/practice$ ./test
print volume :
8
space :
5
       *     ***************
      ***     *************
     *****     ***********
    *******     *********
   *********     *******
  ***********     *****
 *************     ***
***************     *

2.2 etc.

Chapter3 function

3.1

#include 

using namespace std;

bool IsPrime(int a);
int main()
{
     
    int x;
    bool y;
    cout << "print number:\n";
    cin >> x;
    y = IsPrime(x);
    if(y)
        cout << "It is Prime.\n";
    else
        cout << "It is not Prime.\n";   
    return 0;
}

bool IsPrime(int a)
{
     
    int i;
    for(i=2;i<a;i++)
    {
     
        if(a%i==0)
        {
     
            return false;
            break;
        }
    return true;         
    }
}

3.2

#include 

using namespace std;

int gcd(int x,int y);
int lcm(int x,int y);
int main()
{
     
    int x,y,m,n;
    cout << "print first positive integer\n";
    cin >> x;
    cout << "print second positive integer\n";
    cin >> y;
    m = gcd(x,y);
    cout << "greatest commom divisor is " << m << endl;
    n = lcm(x,y);
    cout << "lowest commom multiple is " << n << endl;
}

int gcd(int x,int y)
{
     
    int temp;
    
    if( x < y )
    {
     
        temp = x;
        x = y;
        y = temp;
    }

    while( y != 0 )
    {
     
        temp = y;
        y = y % x;
        x = temp; 
    }
    return x;
}

int lcm(int x,int y)
{
     
    int temp;
    temp = gcd(x,y);
    return x * y / temp;
}
:~/projects/practice$ ./test
print first positive integer
50
print second positive integer
25
greatest commom divisor is 25
lowest commom multiple is 50

Chapter4 Algorithm

4.1

#include 
#include 

using namespace std;

void decompose(int temp);
int IsPrime(int a);
int main()
{
     
    int temp,a,b;
    cout << "Please print natural number (>1) :\n";
    cin >> temp;
    cout << temp << "=";
    if((temp <= 1) || (temp%1!=0))
    {
     
        cout << "Please print natural number (>1) :\n";
        cin >> temp;
    }    
    else
        decompose(temp);
    return 0;
}

int IsPrime(int temp)
{
     
    int i,j;
    for(i=1;i<=sqrt(double(temp));i++)
    {
     
        if(temp%i==0 && i!=1)
        {
     
            j = i;
            break;
        }
        else
        {
     
            j = 1;
        }
               
    }
    return j;
}

void decompose(int temp)
{
     
    int a,b;
    a = temp;
    if(IsPrime(temp)==1)
    {
     
        cout << temp <<endl;
    }
    else
    {
     
        temp = IsPrime(temp);
        cout << temp << "x";
        temp = a/temp;
        a = temp;
        decompose(temp);
    }
}
:~$ cd ./projects/practice
:~/projects/practice$ ./test
Please print natural number (>1) :
100
100=2x2x5x5

4.2

#include 
#include 

using namespace std;

int BinomialN(int n);
int BinomialNK(int n,int k);
int main()
{
     
    int n,k;
    cout << "Please print n :\n";
    cin >> n;
    cout << "Please print k :\n";
    cin >> k;
    if(n<k)
    {
     
        cout << "Please print again!" << endl;
        cout << "Please print n :\n";
        cin >> n;
        cout << "Please print k :\n";
        cin >> k;
    }
    else
    {
     
        cout << "C(n,k)=" << BinomialNK(n,k) << endl;
    }
    return 0;   
}

int BinomialN(int n)
{
     
    if (n==1)
    return 1;
    else
    {
     
        return n*BinomialN(n-1);
    }
}
int BinomialNK(int n,int k)
{
     
    if(k==0)
    return 1;
    else
    {
     
        return BinomialN(n)/BinomialN(k)/BinomialN(k);
    }
    
}
:~/projects/practice$ ./test
Please print n :
6
Please print k :
3
C(n,k)=20

See you next time !

你可能感兴趣的:(Das,schmeckt,mich,c++,linux)