;

Welcome, Guest. Please login or register.
Did you miss your activation email?

Username: Password:
Pages: [1] 2 3   Go Down

Author Topic: [Programming] Menghitung Kemunculan Karakter  (Read 6074 times)

arie26

  • Senior Sailor
  • ******
  • Keong: 0
  • Offline Offline
  • Posts: 13.241
  • *No More Me*
[Programming] Menghitung Kemunculan Karakter
« on: January 02, 2008, 12:28:58 pm »

hahahhha... ada ide lain untuk program apalagi yang menarik dibuat?

coba ini aja.... hmmm....
misal input : AAABCCCDDEFGGHHHH
outputnya jadi : 3AB3C2DEF2G4H

;D






« Last Edit: August 15, 2008, 12:41:02 am by mastery »
Logged
Guys, thanks for everything. Gw pamit... nice to know/meet you all

If you only knew.... those lyrics are 4 U....

fang_mei

  • Senior Sailor
  • ******
  • Keong: 0
  • Offline Offline
  • Posts: 13.472
  • Mana sapu kesayangan gue??? Oh, ada di gudang deng
Re: belajar pemrograman dengan c (lanjutan:D)
« Reply #1 on: January 03, 2008, 09:56:39 am »

Waaaah... kalo itu mah... gue bakal pake cara stack...
Kyknya butuh max 2 variabel char ama 1 variabel integer....

<udah ada bayangan>
Logged
Find me on FS : - J'ice - HaLF
    [im

mastery

  • Global Moderator
  • Senior Sailor
  • *****
  • Keong: 0
  • Offline Offline
  • Posts: 26.128
Re: belajar pemrograman dengan c (lanjutan:D)
« Reply #2 on: January 03, 2008, 12:20:38 pm »

gimana tuh kode-nya mei? hayo post di sini ;)


* kalo aku seh hanya butuh 1 variabel pointer ;D

arie26

  • Senior Sailor
  • ******
  • Keong: 0
  • Offline Offline
  • Posts: 13.241
  • *No More Me*
Re: belajar pemrograman dengan c (lanjutan:D)
« Reply #3 on: January 05, 2008, 03:57:32 pm »

heheheh.. ayo.. ayo.. diposting dong ;)
itu kalo inputnya 'kebetulan' berurut... kalo ndak, gimana coba? ;)
Logged
Guys, thanks for everything. Gw pamit... nice to know/meet you all

If you only knew.... those lyrics are 4 U....

fang_mei

  • Senior Sailor
  • ******
  • Keong: 0
  • Offline Offline
  • Posts: 13.472
  • Mana sapu kesayangan gue??? Oh, ada di gudang deng
Re: belajar pemrograman dengan c (lanjutan:D)
« Reply #4 on: January 05, 2008, 04:33:25 pm »

Gue coba ya.... kalo ada yg salah... maap deh.... apalagi kalo ada kecampur ama pascal wakakakakak  ;D.... BTW, kalo "AAABBCCCDDAAA" pake void hitungbiasa() bakal muncul "3A2B3C2D3A" tp kalo pake void hitungbycharacter() bakal muncul "6A2B3C2D".

Code: [Select]
/**** Fangmei on C-trial   ****/
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#include <string.h>

void hitungbiasa()
{
char teks[];
int count, a;
count=1;

clrscr();
cout<<"Masukkan kalimat:"<<endl;
cin>>teks;
cout<<"Output: ";
for (a=1;a<=strlen(teks);a++)
{
if (a<strlen(teks)) && (strcmp(teks[a],teks[a-1])==0)
{
count++;
}
else
{
cout<<count<<teks[a-1];
count=1;
}
}

}

void hitungbycharacter()
{
char teks[], hrf[];
int count, a, b, found;
count=0;

clrscr();
cout<<"Masukkan kalimat:"<<endl;
cin>>teks;
cout<<"Output: ";
hrf=teks[0];
for (a=1;a<strlen(teks);a++)
{
found=0;
for (b=0;b<strlen(hrf);b++)
{
if (strcmp(teks[a],teks[b])==0)
{
found=1;
}
}
if (found=0)
{
hrf=hrf+teks[a];
}
}
for (a=0;a<strlen(hrf);a++)
{
count=0;
for (b=0;b<strlen(teks);b++)
{
if (strcmp(hrf[a],teks[b])==0)
{
count++;
}
}
cout<<count<<hrf[a];
}
}

arie>> kalo mintanya keluar berurut kyk "RRRBBCCYYY" jadi "2B2C3R3Y" kyknya main if strcmp()<0 ama if strcmp()>0 ya?
« Last Edit: January 05, 2008, 04:54:21 pm by fang_mei »
Logged
Find me on FS : - J'ice - HaLF
    [im

arie26

  • Senior Sailor
  • ******
  • Keong: 0
  • Offline Offline
  • Posts: 13.241
  • *No More Me*
Re: belajar pemrograman dengan c (lanjutan:D)
« Reply #5 on: January 05, 2008, 05:35:24 pm »

hmmm... gw blom coba seh :) kalo inputnya ABACADAE bisa juga, khan? trus... bwat yang laen juga, gimana kalo AaBbCc dst, atao randomnya? :D
Logged
Guys, thanks for everything. Gw pamit... nice to know/meet you all

If you only knew.... those lyrics are 4 U....

mastery

  • Global Moderator
  • Senior Sailor
  • *****
  • Keong: 0
  • Offline Offline
  • Posts: 26.128
Re: belajar pemrograman dengan c (lanjutan:D)
« Reply #6 on: January 05, 2008, 05:50:55 pm »

coba ini aja.... hmmm....
misal input : AAABCCCDDEFGGHHHH
outputnya jadi : 3AB3C2DEF2G4H

;D

untuk kasus pertama, versi acrobat pointer;D

untuk kasus kedua, a.k.a. arbitrary input, harus pake teknik multipass,
pertama analisa data input ampe abis, baru kemudian hasilnya di print.

neh, akrobat pointernya :D


#include <stdio.h>
#include <stdlib.h>

int main(int i, char **p)
{
   char *n;

   for (p++; *p && **p; ){

      for (n=*p;*n==**p||*n==**p-'p'+'P'||*n-'n'+'N'== **p;(*p)++);
      
      *p - n > 1 ? printf("%d%c", *p - n, *n) : printf("%c", *n);
   }

   return 0;
}


mastery

  • Global Moderator
  • Senior Sailor
  • *****
  • Keong: 0
  • Offline Offline
  • Posts: 26.128
Re: belajar pemrograman dengan c (lanjutan:D)
« Reply #7 on: January 05, 2008, 06:53:24 pm »

ini versi 2 (multi-pass) untuk menjawab soal si arie ;)



#include <stdio.h>
#include <stdlib.h>

int main(int i, char **p)
{
   int *c = calloc(0xff, sizeof(int)); /* alokasi memory */
   char *n;

   /* pass 1: mirip dengan versi 1, hitung kemunculan character */
   for (p++; *p && **p; ) {

      for (n=*p;*n==**p||*n==**p-'p'+'P'||*n-'n'+'N'== **p;(*p)++); /* keren yak? ;D */

      c[*n] += *p - n;
   }

   /* pass 2: cetak karakter jika muncul minimal sekali */
   for (i = 0x41 /* A */; i <= 0x5A /* Z */; i++) {
      switch (c[i]+c[i+'n'-'N']) {
      case 0: /* karakter tidak pernah muncul */
         break;
      case 1: /* karakter muncul sekali saja */
         printf("%c", i);
         break;
      default: /* muncul lebih dari sekali */
         printf("%d%c", c[i]+c[i+'n'-'N'], i);
         break;
      }
   }

   free(c); /* hapus alokasi memory */

   return 0;
}


mastery

  • Global Moderator
  • Senior Sailor
  • *****
  • Keong: 0
  • Offline Offline
  • Posts: 26.128
Re: belajar pemrograman dengan c (lanjutan:D)
« Reply #8 on: January 05, 2008, 06:53:50 pm »

ini versi 2 revisi 1, agar tetap hanya menggunakan 1 variabel pointer ;)



#include <stdio.h>
#include <stdlib.h>

int main(int i, char **p)
{
   int *c = calloc(0xff, sizeof(int));

   /* pass 1 */
   for (p++; *p && **p; )
      c[*(*p)++]++; /* <-- keren juga yak? ;D */

   /* pass 2 */
   for (i = 0x41; i <= 0x5A; i++) {
      switch (c[i]+c[i+'n'-'N']) {
      case 0: break;
      case 1: printf("%c", i); break;
      default: printf("%d%c", c[i]+c[i+'n'-'N'], i); break;
      }
   }

   free(c);

   return 0;
}


arie26

  • Senior Sailor
  • ******
  • Keong: 0
  • Offline Offline
  • Posts: 13.241
  • *No More Me*
Re: belajar pemrograman dengan c (lanjutan:D)
« Reply #9 on: January 05, 2008, 07:32:15 pm »

seep... seep.... ;D ;D
Tapi ngomong2 yang mo blajar mana, ne? mumpung mbah2nya dah turun bagi2 ilmu  ??? ;D
Logged
Guys, thanks for everything. Gw pamit... nice to know/meet you all

If you only knew.... those lyrics are 4 U....
Pages: [1] 2 3   Go Up