Sabtu, 27 September 2014

Kursus Bahasa C atau C++


KURSUS BAHASA C

Kategori : Bahasa C atau C++
Durasi : Lama Belajar 15 jam

SILABUS

+Pemahaman Perintah2 Umum Bahasa Pemrograman, deklarasi, tipe data, karakter control, tandaoperasi, binary operator, relational operator, string operator, komentar program, stateman program, pembuatan prosedur dan fungsi.

+Penerapan Input dan Output pada pemrograman (output terformat, letak di layar, bentuk pointer, bentuk di printer).

+Praktik formulasi perulangan : for-loop, while-do, do-loop, repeat-until, fungsi tersarang.

+Praktik formulasi penyeleksian kondisi : if , if tersarang, case-of, case-of else dll

+Praktik operasi-operasi String : del, insert, toString, copy, concat, length dll

+Praktik membuat prosedur : tanpa / dengan parameter, prosedur tersarang, prosdur rekursif dll

+Praktik membuat function: tanpa / dengan parameter, fungsi tersarang, fungsi rekursif dll

+Praktik dan implementasi fungsi Rekursif: Faktorial, pencarian, quick short dll.


+ Penerapan fungsi Create Update delete append pada .txt file

+Praktik dan Implementasi studi kasus


 jika berminat langsung aja hub :
http://lp2maray.com/detail.php?page=kursusdetail&id=A1211034

Koleksi Bahasa C++

Loop Example
#include <iostream>
int main (){
   int a = 10;
   while( a < 20 ) {
       cout << "value of a: " << a << endl;
       a++;
   }

   return 0;
}
====================================
menggunakan for;
#include <iostream>
int main(){
  for ( int x = 0; x < 10; x++ ) {
    cout<< x <<endl;
  }
  cin.get();
}
====================================
menggunakan while;

#include <iostream>
int main(){
  int x = 0;  // Don't forget to declare variables
  while ( x < 10 ) { // While x is less than 10
    cout<< x <<endl;
    x++;             // Update x so the condition can be met eventually
  }
  cin.get();
}
====================================
menggunakan do;

#include <iostream>
int main(){
  int x;
  x = 0;
  do {
    cout<<"Hello, world!\n";
  } while ( x != 0 );
  cin.get();
}
====================================
menggunakan if:
#include <iostream>
int main (){
  for (int n=10; n>0; n--){
    cout << n << ", ";
    if (n==3) {
      cout << "countdown aborted!";
      break;
    }
  }
}
====================================
menggunakan label
#include <iostream>
int main (){
  int n=10;
mylabel:
  cout << n << ", ";
  n--;
  if (n>0) goto mylabel;
  cout << "liftoff!\n";
}



====================================
menggunakan while dan good:
#include <iostream>
int main (){
int x;
while(true){
  cout << "Enter an integer: ";  // prompt the user for input
  cin >> x; // get input

  // check and see if the input statement succeeded
  if (cin.good() == false) {
    cout << "That was not an integer." << endl;
    return -1;
  }

  cout << x << endl;
  }
  return 0;
}

====================================
menggunakan getch:
#include <iostream>
#include <string>
#include <conio.h>
int main(){
    string name;
    cout << "Please enter your first name: ";
    cin >> name;
    cout << "Welcome " << name << "!" << endl;

    getch();//conio.h=loop display

}


TEORI STRING C++

Contoh: "c string tutorial" =>A string is terminated by null character /0.

Deklarasi String:
char s[5];
atau cara pointer. =>char *p

char c[]="abcd";//unlimited
     OR,
char c[5]="abcd";//init 5 char
     OR,
char c[]={'a','b','c','d','\0'};
     OR;
char c[5]={'a','b','c','d','\0'};

====================================
menggunakan char untuk string:
#include <iostream>
using namespace std;

int main (){
   char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
   cout << "Greeting message: ";
   cout << greeting << endl;

   return 0;
}

====================================
menggunakan printf:
#include <stdio.h>
int main(){
    char name[20];
    printf("Enter name: ");
    scanf("%s",name);
    printf("Your name is %s.",name);
    return 0;
}
====================================
menggunakan getchar:
#include <stdio.h>
int main(){
    char name[30],ch;
    int i=0;
    printf("Enter name: ");
    while(ch!='\n')    // terminates if user hit enter
    {
        ch=getchar();
        name[i]=ch;
        i++;
    }
    name[i]='\0';       // inserting null character at end
    printf("Name: %s",name);
    return 0;
}
====================================
menggunakan gets:
int main(){
    char name[30];
    printf("Enter name: ");
    gets(name);     //Function to read string from user.
    printf("Name: ");
    puts(name);    //Function to display string.
    return 0;
}
====================================
menggunakan gets
#include <stdio.h>
void Display(char ch[]);
int main(){
    char c[50];
    printf("Enter string: ");
    gets(c);            
    Display(c);     // Passing string c to function.   
    return 0;
}
void Display(char ch[]){
    printf("String Output: ");
    puts(ch);
}
====================================
menggunakan Manipulasi String function:
S.N.    Function & Purpose
1    strcpy(s1, s2);
Copies string s2 into string s1.
2    strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
3    strlen(s1);
Returns the length of string s1.
4    strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
5    strchr(s1, ch);
Returns a pointer to the first occurrence of character ch in string s1.
6    strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string s1.
Strupr():string to uppercase
strlwr():string to lowercase
strcat():joints /concatenates two strings

EXAMPLE
#include <iostream>
#include <cstring>

int main (){
   char str1[10] = "Hello";
   char str2[10] = "World";
   char str3[10];
   int  len ;

   // copy str1 into str3
   strcpy( str3, str1);
   cout << "strcpy( str3, str1) : " << str3 << endl;

   // concatenates str1 and str2
   strcat( str1, str2);
   cout << "strcat( str1, str2): " << str1 << endl;

   // total lenghth of str1 after concatenation
   len = strlen(str1);
   cout << "strlen(str1) : " << len << endl;

   return 0;
}
Output:
strcpy( str3, str1) : Hello
strcat( str1, str2): HelloWorld
strlen(str1) : 10

Source Code to Find the Frequency of Characters
#include <stdio.h>
int main(){
   char c[1000],ch;//ch=char 1 pointer,c=string
   int i,count=0;
   printf("Enter a string: ");
   gets(c);//get string
   printf("Enter a characeter to find frequency: ");
   scanf("%c",&ch);//get char
   for(i=0;c[i]!='\0';++i) {
       if(ch==c[i])
           ++count;
   }
   printf("Frequency of %c = %d", ch, count);
   return 0;
}
Source Code to Find Number of Vowels, Consonants, Digits and White Space Character

#include<stdio.h>
int main(){
    char line[150];
    int i,v,c,ch,d,s,o;
    o=v=c=ch=d=s=0;
    printf("Enter a line of string:\n");
    gets(line);
    for(i=0;line[i]!='\0';++i)
    {
        if(line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u' || line[i]=='A' || line[i]=='E' || line[i]=='I' || line[i]=='O' || line[i]=='U')
            ++v;
        else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
            ++c;
        else if(line[i]>='0'&&c<='9')
            ++d;
        else if (line[i]==' ')
            ++s;
    }
    printf("Vowels: %d",v);
    printf("\nConsonants: %d",c);
    printf("\nDigits: %d",d);
    printf("\nWhite spaces: %d",s);
    return 0;
}
Source Code to Reverse String
#include<stdio.h>
#include<string.h>
void Reverse(char str[]);
int main(){
    char str[100];
    printf("Enter a string to reverse: ");
    gets(str);
    Reverse(str);
    printf("Reversed string: ");
    puts(str);
 return 0;
}
void Reverse(char str[]){
    int i,j;
    char temp[100];
    for(i=strlen(str)-1,j=0; i+1!=0; --i,++j)
    {
        temp[j]=str[i];
    }
    temp[j]='\0';
    strcpy(str,temp);
}
Source Code to Calculated Length without Using strlen() Function
#include <stdio.h>
int main()
{
    char s[1000],i;
    printf("Enter a string: ");
    scanf("%s",s);
    for(i=0; s[i]!='\0'; ++i);
    printf("Length of string: %d",i);
    return 0;
}
Source Code to Concatenate Two Strings Manually
#include <stdio.h>
int main()
{
    char s1[100], s2[100], i, j;
    printf("Enter first string: ");
    scanf("%s",s1);
    printf("Enter second string: ");
    scanf("%s",s2);
    for(i=0; s1[i]!='\0'; ++i);  /* i contains length of string s1. */
    for(j=0; s2[j]!='\0'; ++j, ++i)
    {
        s1[i]=s2[j];
    }
    s1[i]='\0';
    printf("After concatenation: %s",s1);
    return 0;
}
Source Code to Copy String Manually
#include <stdio.h>
int main()
{
    char s1[100], s2[100], i;
    printf("Enter string s1: ");
    scanf("%s",s1);
    for(i=0; s1[i]!='\0'; ++i)
    {
        s2[i]=s1[i];
    }
    s2[i]='\0';
    printf("String s2: %s",s2);
    return 0;
}
Source Code to Remove Characters in String Except Alphabets
#include<stdio.h>
int main(){
    char line[150];
    int i,j;
    printf("Enter a string: ");
    gets(line);
    for(i=0; line[i]!='\0'; ++i)
    {
        while (!((line[i]>='a'&&line[i]<='z') || (line[i]>='A'&&line[i]<='Z' || line[i]=='\0')))
        {
            for(j=i;line[j]!='\0';++j)
            {
                line[j]=line[j+1];
            }
            line[j]='\0';
        }
    }
    printf("Output String: ");
    puts(line);
    return 0;
}
Output
Enter a string: p2'r"o@gram84iz./
Output String: programiz

Source Code to Sort Words in Dictionary Order
#include<stdio.h>
#include <string.h>
int main(){
    int i,j;
    char str[10][50],temp[50];
    printf("Enter 10 words:\n");
    for(i=0;i<10;++i)
        gets(str[i]);
    for(i=0;i<9;++i)
       for(j=i+1;j<10 ;++j){
          if(strcmp(str[i],str[j])>0)
          {
            strcpy(temp,str[i]);
            strcpy(str[i],str[j]);
            strcpy(str[j],temp);
          }
    }
    printf("In lexicographical order: \n");
    for(i=0;i<10;++i){
       puts(str[i]);
    }
return 0;
}

CLASS STRING
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string your_name;
    cout << "Hello, What is your name? ";
    cin >> your_name;
    cout << "Pleased to meet you, " << your_name;
}


#include <iostream>
#include <string>

int main(){
    cout << "Please enter your full name: ";
    string full_name;
    getline( cin, full_name );
    int middle = full_name.size() / 2;

    string greeting = "Hello " + full_name + ", ";
    cout << greeting << "the middle character of your name is "
         << full_name[middle] << " at position "
         << middle;
}


The comparison operators >, <, ==, >=, <=, != ,strcmp

if(str == "Hello World!"){
 std::cout << "Strings are equal!";
}
std::string haystack = "Hello World!";
std::string needle = "o";
std::cout << haystack.find(needle);
=4

string wikistr = "wikipedia is full of wikis (wiki-wiki means fast)";
for(string::size_type i = 0, tfind; (tfind = wikistr.find("wiki", i)) != string::npos; i = tfind + 1){
 cout << "Found occurrence of 'wiki' at position " << tfind << endl;
}
=0 21 28 33

string::size_type strSize =  str.size();
string::size_type strSize2 = str2.length();

string newstr = " Human";
str.insert (5,newstr);
=Hello Human World!
str.erase (6,11);
=Hello!
string str = "Hello World!";
string part = str.substr(6,5);
= World

Bahasa C++ Create New File -Write - Append .txt



MEMBUAT  FILE BARU DAN MENULIS PESAN:

#include <iostream>
#include <fstream>
using namespace std;

int writeFile();

int main()
{
    writeFile();
}

int writeFile ()
{
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}



 Append FILE

#include <fstream>



int main()
{
   ofstream ofile("example.txt", ios::app);
   if ( ofile )
   {
       ofile << "Dibuat di Jakarta by LP2MARAY";
       ofile.close();
   }
}

Bahasa C++ Penerapan GOTO Label dan Create and write FILE .txt



#include <iostream>
int main (){
  int n=10;
LP2MARAY:
  cout << n << ", ";
  n--;
  if (n>0) goto LP2MARAY;
  cout << "Mantab .....bahasa C or C++ is The Best!\n";
}
=========================
 
membuat dan Menulis di file .txt 

 
 
 
#include <iostream>
#include <fstream>
using namespace std;

int writeFile();

int main()
{
writeFile
();
}

int writeFile ()
{
ofstream myfile
;
myfile
.open ("example.txt");
myfile
<< "Writing this to a file.\n";
myfile
<< "Writing this to a file.\n";
myfile
<< "Writing this to a file.\n";
myfile
<< "Writing this to a file.\n";
myfile
.close();
return 0;
}
 
Maka pada PATH dimana Installer C++ berada akan terbentuk file baru dengan nama example.txt
 
 
 
 
 

Bahsa C++ fungsi String Join, Length, Replace, Copy




Menghitung jumlah karakter String:
#include <iostream>
#include <string>

int main ()
{
  std::string str ("Test string");
  std::cout << "The size of str is " << str.length() << " bytes.\n";
  return 0;
}


Merubah karakter String:
#include <iostream>
#include <string>
 
int main ()
{
  std::string base="this is a test string.";
  std::string str2="n example";
  std::string str3="sample phrase";
  std::string str4="useful.";
 
  // replace signatures used in the same order as described above:
 
  // Using positions:                 0123456789*123456789*12345
  std::string str=base;           // "this is a test string."
  str.replace(9,5,str2);          // "this is an example string." (1)
  str.replace(19,6,str3,7,6);     // "this is an example phrase." (2)
  str.replace(8,10,"just a");     // "this is just a phrase."     (3)
  str.replace(8,6,"a shorty",7);  // "this is a short phrase."    (4)
  str.replace(22,1,3,'!');        // "this is a short phrase!!!"  (5)
 
  // Using iterators:                                               0123456789*123456789*
  str.replace(str.begin(),str.end()-3,str3);                    // "sample phrase!!!"      (1)
  str.replace(str.begin(),str.begin()+6,"replace");             // "replace phrase!!!"     (3)
  str.replace(str.begin()+8,str.begin()+14,"is coolness",7);    // "replace is cool!!!"    (4)
  str.replace(str.begin()+12,str.end()-4,4,'o');                // "replace is cooool!!!"  (5)
  str.replace(str.begin()+11,str.end(),str4.begin(),str4.end());// "replace is useful."    (6)
  std::cout << str << '\n';
  return 0;
}


Mencetak Array to String

#include <iostream>
 
using namespace std;
 
int main ()
{
   char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
 
   cout << "Greeting message: ";
   cout << greeting << endl;
 
   return 0;
}



JOIN array dalam contoh (
strcpy atau strcat):
#include <iostream>
#include <cstring>
 
using namespace std;
 
int main ()
{
   char str1[10] = "Hello";
   char str2[10] = "World";
   char str3[10];
   int  len ;
 
   // copy str1 into str3
   strcpy( str3, str1);
   cout << "strcpy( str3, str1) : " << str3 << endl;
 
   // concatenates str1 and str2
   strcat( str1, str2);
   cout << "strcat( str1, str2): " << str1 << endl;
 
   // total lenghth of str1 after concatenation
   len = strlen(str1);
   cout << "strlen(str1) : " << len << endl;
 
   return 0;
}
 
//Greeting message: Hello
 
 
HASIL SAAT DI PLAY:
strcpy( str3, str1) : Hello
strcat( str1, str2): HelloWorld
strlen(str1) : 10


JOIN array dalam contoh (+):


#include <iostream>
#include <string>
 
using namespace std;
 
int main ()
{
   string str1 = "Hello";
   string str2 = "World";
   string str3;
   int  len ;
 
   // copy str1 into str3
   str3 = str1;
   cout << "str3 : " << str3 << endl;
 
   // concatenates str1 and str2
   str3 = str1 + str2;
   cout << "str1 + str2 : " << str3 << endl;
 
   // total lenghth of str3 after concatenation
   len = str3.size();
   cout << "str3.size() :  " << len << endl;
 
   return 0;
}

Hasil saat di Play:
str3 : Hello
str1 + str2 : HelloWorld
str3.size() :  10