Join Us on Facebook Follow us on Twitter Add to Circles

Sunday, December 28, 2014

Ex5- សរសេកម្មវិធី សម្រាប់គិតលុយដូចខាងក្រោមៈ


a) សរសេកម្មវិធីដោយប្រើ
Switch… case statement
b)
សរសេកម្មវិធីដោយប្រើ
If… else statement


ដំណើរការៈក្នុងករណីជ្រើសរើសយក

item ណាមួយ នោះតំលៃរបស់នឹងត្រូវបាន 

បូកបញ្ចូលទៅក្នុង អថេរ Total  បន្ទាប់ពីចុច


សញ្ញា = វាបង្ហាញ Total
សរុបនៃតំលៃ itemទាំងអស់ ដែលបានជ្រើសរើស


a)ប្រើ Switch… case statement
#includde<stdio.h>
#include<conio.h>
void main()
{
   int done;
char c;
float total=0;
printf("==========Menu==========");
printf(“1- Coca 2$\n”);
printf(“2- Red Bull 2$ \n”);
printf(“3- ABC 4$\n”);
printf(“4- Anchor 4$\n”);
printf(“5- Pepsi 2$\n”);
printf(“= - Done \n”);
printf(“========================\n”);
done=0;
while(!done)
 {
    c=getchar();
    switch( c )
    {
     case ‘1’:
         printf(“Cost : 2$ \n”);
          total+=2;
          break;
     case ‘2’ :
         printf(“Cost 2$ \n”);
          total+=2;
          break;
     case ‘3’:
          printf(“Cost 4$ \n”);
           total+=4;
           break;
     case ‘4’:
          printf(“Cost 4$ \n”);
           total+=4;
           break;
     case ‘5’:
          printf(“Cost 2$ \n”);
           total+=2;
           break;
     case ‘=‘:
           printf(“ Total = %.2f\n”,total);
           done=1;
            break;
  }  }
getch();
}
b)ប្រើ If… else statement
#include<stdio.h>
#include<conio.h>
void main(){clrscr();
   int done;
   char c;
   float total;
   printf("========Menu========\n");
   printf("1-Coca=$2\n");
   printf("1-Red Bull=$2$\n");
   printf("2-ABC=$4$\n");
   printf("3-Anchor=$4\n");
   printf("4-Pepsi=$2\n");
   printf("=-Done\n")
   printf("====================\n");
   done=0;
   while (!done)
   {
            c=getchar();
            if (c=='1')
            {
            printf("cost=$2\n");
             total+=2;
            }
            else if (c=='2')
            {
            printf("cost=$2\n");
             total+=2;
            }
            else if (c=='3')
            {
            printf("cost=$4\n");
             total+=4;
            }
            else if (c=='4')
            {
            printf("cost=$4\n");
             total+=4;
            }
            else if (c=='5')
            {
            printf("cost=$2\n");
             total+=2;
            }
            else if (c=='=')
            {
            printf("Total=%.2f\n",total);
            done=1;          
            }         
   }
getch();
}


 
 




No comments:

Post a Comment