Advertisement
lpuarmy

Project Akhir 1 - Tanggal

Jan 14th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string.h>
  4. #define MAKS 10
  5.  
  6. int kab (int);
  7. int error (int, int);
  8. void harikah (char *, char *, int);
  9.  
  10. struct date {
  11.     int tgl, bln, thn;
  12.     char hari[MAKS];
  13. };
  14.  
  15. int bl, bln[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
  16.  
  17. main() {
  18.     struct date today;
  19.     struct date tom;
  20.     int n, cektgl, cekbln;
  21.    
  22.     puts("=============== PROYEK AKHIR ===============\n");
  23.    
  24.     input:
  25.     printf("Masukkan hari          : "); gets(today.hari);
  26.     printf("Masukkan tanggal       : "); scanf("%d", &today.tgl);
  27.     printf("Masukkan bulan         : "); scanf("%d", &today.bln);
  28.     printf("Masukkan tahun         : "); scanf("%d", &today.thn);
  29.     printf("Masukkan jangka hari   : "); scanf("%d", &n);
  30.    
  31.     fflush(stdin);
  32.  
  33.     // proses penyalinan value
  34.     bl = today.bln;
  35.     tom.tgl = today.tgl+n;
  36.     tom.bln = today.bln;
  37.     tom.thn = today.thn;
  38.     strcpy(tom.hari,today.hari);
  39.  
  40.     // pengecekan kabisat
  41.     if(kab(today.thn))
  42.         bln[2] = 29;
  43.    
  44.     // error checking
  45.     harikah(today.hari,tom.hari,n);
  46.     cektgl = error(today.tgl,1);
  47.     cekbln = error(today.bln,2);
  48.  
  49.     if(cektgl || cekbln || tom.hari[0] == '0') {
  50.         puts("--------------------------------------------");
  51.         printf("Data yang Anda masukkan salah, ulangi lagi\n");
  52.         //if(
  53.         puts("--------------------------------------------");
  54.         goto input;
  55.     }
  56.  
  57.     // proses penentuan tanggal
  58.     while(tom.tgl > bln[tom.bln]) {
  59.         tom.tgl -= bln[tom.bln];
  60.         tom.bln += 1;
  61.         if(tom.bln > 12) {
  62.             tom.bln = 1;
  63.             tom.thn += 1;
  64.         }
  65.     }
  66.  
  67.     // output
  68.     puts("--------------------------------------------");
  69.     printf("Hari ini : \nHari    : %s\n", today.hari);
  70.     printf("Tanggal : %d\n", today.tgl);
  71.     printf("Bulan   : %d\n", today.bln);
  72.     printf("Tahun   : %d\n", today.thn);
  73.     puts("--------------------------------------------");
  74.     printf("%d Hari lagi : \nHari    : %s\n", n,tom.hari);
  75.     printf("Tanggal : %d\n", tom.tgl);
  76.     printf("Bulan   : %d\n", tom.bln);
  77.     printf("Tahun   : %d\n\n", tom.thn);
  78.  
  79.     return 0;
  80. }
  81.  
  82. int kab (int x) {
  83.     int a;
  84.     if(x % 400 == 0)
  85.         a = 1;
  86.     else if(x % 100 == 0)
  87.         a = 0;
  88.     else if(x % 4 == 0)
  89.         a = 1;
  90.     else
  91.         a = 0;
  92.  
  93.     return (a);
  94. }
  95. int error (int y,int z) {
  96.     int re;
  97.  
  98.     switch(z) {
  99.         case 1 : if((y < 1) || (y > bln[bl])) {
  100.                     re = 1;
  101.                  }
  102.         case 2 : if((y < 1) || (y > 12)) {
  103.                     re = 1;
  104.                  }
  105.         default : re = 0;
  106.     }
  107.  
  108.     return (re);
  109. }
  110. void harikah (char *h, char *k, int n) {
  111.     int a, i, cek;
  112.     char *hari[] = {"0","Senin","Selasa","Rabu","Kamis","Jumat","Sabtu","Ahad"};
  113.    
  114.     for(i=0;i<8;i++) {
  115.         cek = strcmpi(h,hari[i]);
  116.         if(cek == 0) {
  117.             a = i;
  118.             break;
  119.         }
  120.     }
  121.     if(cek != 0)
  122.         strcpy(k,hari[0]);
  123.     else {
  124.         a += n;
  125.         while(a > 7) {
  126.             a -= 7;
  127.         }
  128.  
  129.         strcpy(k,hari[a]);
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement