Jumat, 13 November 2015

Program C++ saat uas


#include <iostream.h>//mendefinisikan cin,cout,endl,strcmp,strupr
#include <stdio.h> //mendefinisikan gets,fopen,fwrite,fclose,fread,printf
#include <conio.h>//mendefinisikan textcolor,getch,cprint,clrscr,getch,gotoxy

void Garis (int posx, int posy, int pjg);
void Transaksi ();
void Lihat ();

char kode[30], nm[100][30];
int i, hrg[100], jml[100], stot[100], total, bayar, kembali;


Klik link ini untuk melihat Daftar Arsip Saya.


 main () //menu utama
{
   char pil;
  
   do
   {
    clrscr();

    Garis(20, 7, 40); Garis(20, 9, 40);
    gotoxy(31, 8);textcolor(14);cprintf ("M E N U    U T A M A");
    gotoxy(26, 10);textcolor(14);cprintf("1. Transaksi Baru");
    gotoxy(26, 11);textcolor(14);cprintf ("2. Lihat Data Transaksi");
      gotoxy(26, 12);textcolor(14);cprintf("3. Menghapus file");
    gotoxy(26, 13);textcolor(14);cprintf("4. Keluar");
    Garis(20, 15, 40); Garis(20, 17, 40);
    gotoxy(26, 16);textcolor(14);cprintf("Input pilihan : "); cin >> pil;
    switch (pil)
    {
    case '1':

            // Reset ulang variabel yang digunakan dalam fungsi Transaksi()
            i=0; total=0; bayar=0;
          Transaksi();
            break;
      case '2':
       
            Lihat();
            break;
         case'3':
         remove("Transaksi.txt");
         clrscr();
gotoxy(20,18);textcolor(13);cprintf ("File sudah dihapus, tekan Enter !");getch();
break;
      case '4':
      default:
      break;
    }
   } while (pil != '4');
   clrscr();
   gotoxy(28, 15); ;textcolor(11);cprintf ( "Terima Kasih Atas Kunjungannya"); getch();
}



void Garis (int posx, int posy, int pjg)
{
int i;
for (i=1; i<=pjg; i++) {gotoxy(posx+i, posy); cout << char(196); }
}

void Transaksi ()
{
   int x, n=0;
   char a='Y';
   // Kalo file belum ada, otomatis dibuatkan
   // Kalo sudah ada, data yang berada di file tsb ditambahkan
   FILE *fp = fopen("Transaksi.txt", "a");

   if (fp==NULL)
   {
    gotoxy(16, 10);textcolor(12);cprintf("ERROR IN READING FILE!!!");getch();
      goto Exit;
   }

   do {
      clrscr();
Garis(10, 5, 60); Garis(10, 7, 60);
    gotoxy(25, 6);textcolor(14);cprintf ("T R A N S A K S I  B A R A N G");
    gotoxy(14, 9); textcolor(14);cprintf  ("BARANG ke-") ;cout <<(i+1);
      Garis (10, 10, 30);
    gotoxy(14, 11); textcolor(14);cprintf("Nama barang   :");
    gotoxy(14, 12); textcolor(14);cprintf (" [input # = kembali ke menu utama");
    gotoxy(14, 13); textcolor(14);cprintf("Harga barang  :");
    gotoxy(14, 14); textcolor(14);cprintf( "Jumlah barang :");
      gotoxy(30, 11); gets(nm[i]);
      if (strcmp(nm[i], "#")==0) goto Exit; // Kalo user menginputkan tanda #, maka akan langsung ditujukan ke Exit:
      strupr(nm[i]); // strupr -> mengganti ke huruf kapital
      gotoxy(30, 13); cout << "Rp "; cin >> hrg[i];
      gotoxy(30, 14); cin >> jml[i];

      fwrite(&kode, sizeof(kode), 1, fp);
      fwrite(&nm[i], sizeof(nm[i]), 1, fp);
      fwrite(&hrg[i], sizeof(hrg[i]), 1, fp);
      fwrite(&jml[i], sizeof(jml[i]), 1, fp);

      i++;
      Garis(10, 16, 60); Garis(10, 18, 60);
    gotoxy(14, 17); textcolor(14);cprintf("Tambah transaksi? (Y/T) "); cin >> a;
   } while (a!='T' && a!='t');


   if (i>0)
   {
      clrscr();
    gotoxy(27, 2); textcolor(7);cprintf("N O T A    P E M B E L I A N");
    Garis(5, 4, 70); Garis(5, 6, 70);
    gotoxy(7, 5); textcolor(7);cprintf("NO.  NAMA BARANG                          HARGA     JML   SUBTOTAL");
    while (n!=i)   //n=0 and i=0 jadi tdk sama dengan 0
    {
    gotoxy(7, 7+n); cout << (n+1) << '.';
    gotoxy(12, 7+n); cout << nm[n];
    gotoxy(49, 7+n); cout << "Rp " << hrg[n];
    gotoxy(59, 7+n); cout << jml[n];
      stot[n] = hrg[n] * jml[n];
    gotoxy(65, 7+n); cout << "Rp " << stot[n];
         n++;
    }
      Garis(5, 7+n, 70);
      gotoxy(55, 8+n); cout << "  TOTAL";
      for (x=0; x<=i-1; x++) total+=stot[x];
      gotoxy(65, 8+n); cout << "Rp " << total;

      while (bayar < total)
      {
      gotoxy(55, 9+n); cout << "DIBAYAR";
      gotoxy(65, 9+n); cout << "Rp       ";
         gotoxy(68, 9+n); cin >> bayar;
      }

      kembali = bayar - total;
      gotoxy(55, 10+n); cout << "KEMBALI";
      textcolor(14);
      gotoxy(65, 10+n); cprintf("Rp %d", kembali);
      Garis(63, 11+n, 12);
      gotoxy(64, 12+n); cout << "TERIMA KASIH";
    getch();
   }

   Exit:
   fclose(fp);
}

void Lihat ()
{
   FILE *fp = fopen("Transaksi.txt", "r");

   clrscr();
if (fp==NULL)
   {
    gotoxy(16, 10);;textcolor(12);cprintf ( "ERROR IN READING FILE!!!");
      goto Exit;
   }

   i=1;
   gotoxy(27, 2);textcolor(14);cprintf("D A T A   T R A N S A K S I");
   Garis(5, 4, 70); Garis(5, 6, 70);
   gotoxy(7, 5); textcolor(14);cprintf("NO.                  NAMA BARANG                    HARGA      JML  ");

   while (fread(&kode, sizeof(kode), 1, fp)==1)
   {
      gotoxy(7, 6+i); printf("%2d.", i);
    if (fread(&nm[i], sizeof(nm[i]), 1, fp)==1) {gotoxy(28, 6+i); cout << nm[i];}
if (fread(&hrg[i], sizeof(hrg[i]), 1, fp)==1) {gotoxy(59, 6+i); cout << "Rp " << hrg[i];}
if (fread(&jml[i], sizeof(jml[i]), 1, fp)==1) {gotoxy(70, 6+i); cout << jml[i];}
    i++;
   }

   Exit:
   getch();
   fclose(fp);
}

0 komentar:

@Copyright 2014 ANJAR KURNIAJAYA-All Rights Recerved