Pages

Wednesday, 27 July 2011

Program C++ menu case switch


mendekati UAS semester 2 di kasih tugas bikin program menu pake instruksi case switch, simple aja cuma gabungin program2 yang uda di buat di jadiin satu hhe...

/*program menu case switch hidayat chaniago ecD4 091354013*/
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main(void)
{
char karakter,t;
int liter,solar,premium,super,a,b,c,simpanan,tebakan,i,ch,n,x;
float y,X1,X2,D;
z:
clrscr();
gotoxy(34,5);cout<<"pilih menu";
gotoxy(28,7);cout<<"1. program deret prima";
gotoxy(28,8);cout<<"2. program persamaan kuadrat";
gotoxy(28,9);cout<<"3. program a+(b*c)";
gotoxy(28,10);cout<<"4. program bensin while do";
gotoxy(28,11);cout<<"5. program tebak tebakan";
unsigned short int number;
gotoxy(28,13);cout<<"enter number 1 sampai 5: ";
cin>>number;
clrscr();
switch(number)
{
case 0:   cout<<"too small pilihan hanya 1 s/d 5\n";
                 break;
case 5:   while(1)
                   {  cout<<"saya menyimpan satu nilai antara 0 s/d 99.\n";
                      cout<<"silahkan anda tebak nilai tersebut.\n";
                      cout<<"-----------------------------------\n";
                      simpanan=rand()%100;
                      for(i=1;i<7;i++)
                      {  printf("tebakan ke-%d: ",i);
                         scanf("%d",&tebakan);
                         if(tebakan<simpanan)
                         {  printf("tebakan terlalu kecil!\n"); continue;}
                         if(tebakan==simpanan)
                         {  printf("anda pinter!\n"); break;}
                         printf("tebakan terlalu besar!\n");
                      }
                       if(i==7)
                        {  printf("anda kurang beruntung..\n");
                           printf("nilai simpanan adalah %d\n",simpanan);}
                           printf("anda ingin mengulang permainan(Y/N)?");
                           ch=getche();
                           if(!(ch=='Y'||ch=='y'))
                           break;
                         printf("\n\n");
                }
              break;
case 4:   cout<<"liter   solar   premium   super\n";
               cout<<"-------------------------------\n";
               liter=1;
               while(liter<=15)
               {  solar=liter*250;
                    premium=liter*385;
                    super=liter*450;
                    printf("%5d   %5d   %5d    %5d\n",liter,solar,premium,super);liter++;}
                  cout<<"------------------------------\n";
                 break;
case 3:   printf("masukan nilai a\n");
               scanf("%d",&a);
               printf("masukan nilai b\n");
                 scanf("%d",&b);
                 printf("masukan nilai c\n");
               scanf("%d",&c);
                  y=a+b*c;
                printf("hasil a(b*c) adalah :%3.2f", y);
                 break;
case 2:   gotoxy(16,10);printf("Program Menghitung Akar Persamaan Kuadrat");
                 gotoxy(18,12);printf("silahkan masukan nilai a,b,dan c, :\n");
                 gotoxy(30,14);printf("nilai a:");scanf("%d",&a);
               gotoxy(30,15);printf("nilai b:");scanf("%d",&b);
                 gotoxy(30,16);printf("nilai c:");scanf("%d",&c);
                  {
                   D=b*b-4*a*c;
                    if(D<0)
                    {  gotoxy(27,18);printf("Hasil Imajiner \n");}
                    else
                    {
                      X1=((-b+D)/2*a);
                      X2=((-b-D)/2*a);
                 gotoxy(23,17);printf("Hasil Persamaan Kuadrat: \n");
                 gotoxy(30,18);printf("X1=%3.2f\n", X1);
                 gotoxy(30,19);printf("X2=%3.2f\n", X2);
                }}
                 break;
case 1:   p:
                 clrscr();
                 do
                 {printf("banyak bilangan prima = ");
                scanf("%d",&n);}
                 while(n<=0);
                  x=0;
                 for(i=1;i<=30000;i++)
                {
                   if(i==1) {}
                   else
                   {
                     if(i==2 || i==3 || i==5 || i==7) {printf("%d  ",i); x=x+1;} else {}
                     if(i%2==0 || i%3==0 || i%5==0 || i%7==0) {}
                  else {printf("%d  ",i); x=x+1; }
                   }
                    if(x==n) {i=30000;} else{}
                   }
                    printf("\n\npress t to try again");
                    t=getch();
                    if(t=='t') {goto p;} else {}
                    break;
default:  cout<<"too large pilihan hanya 1 s/d 5\n";
break;
}

  printf("\n\ntekan x untuk kembali ke menu");
  karakter=getch();
  if(karakter=='x') {goto z;} else {}
}

No comments:

Post a Comment