fork download
  1. #include<stdio.h>
  2. //1.void with no parameter แสดงข้อมูลร้านค้า
  3. void store_Info(){
  4. char storename [30];
  5. printf("Enter your store name "); //ใส่ชื่อร้าน
  6. scanf("%s",storename); // %S ชื่อร้านมีหลายตัวอักษร ไม่ต้องใส่& เพราะใช้%S นอกนั้นใส่&
  7. printf("Store name is %s\n",storename);
  8. }
  9.  
  10. //2.return function with no parameter คืนค่า ปีโปรโมชั่น
  11. int promotionyear(){
  12. int year ;
  13. printf("Enter promotion year ");
  14. scanf("%d",&year );
  15. return year ;
  16. }
  17.  
  18. // ข้อ 3 คือ return double
  19. float discount1 (float totalprice){
  20. int choice ;
  21. float discount ;
  22. printf("Enter your choice >>>1 is member discount 5%% >>> 2 is festival discount 10%% >>> 3 is speaial discount 15%% : ");
  23. scanf("%d",&choice);
  24. switch(choice){ // ข้อ 4 swith case
  25. case 1:
  26. printf("you select member discount 5%% \n");
  27. discount= totalprice*0.95 ;
  28. break;
  29. case 2 :
  30. printf("you select festival discount 10%% \n");
  31. discount= totalprice*0.90 ;
  32. break;
  33. case 3 :
  34. printf("you select special discount 15%% \n");
  35. discount= totalprice*0.85 ;
  36. break;
  37. default:
  38. printf("please select again\n");
  39. }
  40. return discount;
  41. }
  42.  
  43. const int secret_number= 34;
  44. int guess_number(){ //ข้อ 5 return function with out parameter
  45. int guess ;
  46. while(1){
  47. printf("guess number (1-99): ");
  48. scanf("%d",&guess);
  49.  
  50. if (guess > secret_number){
  51. printf("Too many\n");
  52. }else if(guess < secret_number){
  53. printf("Too low\n");
  54. }else{
  55. return 1; // คืนค่า 1 ถ้าทายถูก
  56. }
  57. }
  58. }
  59. void processPayment(int total_price , char paymentMethod[] , float *totalwithfee){
  60. printf ("\npaymentMethod: %s\n",paymentMethod);
  61. if ((paymentMethod[0] == 'C' || paymentMethod[0] == 'c') && paymentMethod[1] == '\0'){ // \0 คือ จุดจบ
  62. //ถ้าผู้ใช้เลือกใช้เงินสด
  63. printf("Total price %d bath\n",&total_price);
  64. *totalwithfee = total_price;
  65. }else if((paymentMethod[0] == 'B' || paymentMethod[0] == 'b') && paymentMethod[1] == '\0'){
  66. //ถ้าใช้บัตรเครดิต
  67. float fee = total_price*0.05; //ค่าธรรมเนียม
  68. *totalwithfee = total_price + fee;
  69. printf("Credit card fee 5%% : %.2f baht\n",fee);
  70. printf("Total with fee : %2.f",*totalwithfee);
  71. }else{
  72. printf("Wrong payment method ! Select agian\n");
  73. *totalwithfee = total_price; //ตั้วค่าเริ่มต้นกันพลาด
  74. }
  75.  
  76. }
  77. char calculateChange(int totalwithfee,double paid_amout , double *result1){
  78.  
  79. if(paid_amout > totalwithfee){
  80. *result1 = (paid_amout - totalwithfee);
  81. return 'C'; //มีเงินทอน
  82. }else if(paid_amout < totalwithfee){
  83. *result1 = (totalwithfee - paid_amout);
  84. return 'S';//เงินไม่พอ
  85. }else{
  86. *result1 = 0;//ไม่มีเงินทอนหรือเงินขาด
  87. return 'E'; //เงินพอดี
  88. }
  89. }
  90.  
  91. int main (){
  92. store_Info (); // call function 1
  93.  
  94. int year = promotionyear ();
  95. printf("Enter promotion year is %d\n",year);
  96.  
  97. char exit ;
  98. float price1[100];
  99. int a =0 ;
  100. float total =0 ;
  101.  
  102. do{
  103. printf("Enter price ");
  104. scanf("%f",&price1[a]);
  105. total=total+price1[a];
  106.  
  107. printf("\n");
  108. printf ("Exit for y/Y,Want to exit(y/n) ");
  109. scanf(" %c",&exit);
  110.  
  111. }while ((exit !='y')&&(exit != 'Y'));
  112. printf("total is %.2f\n",total);
  113.  
  114. float price_after_discount=discount1(total);
  115. printf("price after discount is %.2f\n",price_after_discount);
  116.  
  117. float total_price;
  118. if (price_after_discount <2000){
  119. printf("No discount\n");
  120. total_price = price_after_discount*1.0;
  121. }else if((price_after_discount >=2000) && (price_after_discount <=5000)){
  122. printf("have more discount 5%%\n");
  123. total_price = price_after_discount*0.95;
  124. }else if((price_after_discount >=5001) && (price_after_discount <=10000)){
  125. printf("have more discount 10%%\n");
  126. total_price = price_after_discount*0.90;
  127. }else{
  128. printf("have more discount 20%%\n");
  129. total_price = price_after_discount*0.80;
  130. }
  131. printf("total price is %.2f\n",total_price);
  132.  
  133. printf("===Lucky Number===\n");
  134. // เรียกใช้ฟังก์ชันทายเลข
  135. if(guess_number()){
  136. printf("congratulations\n");
  137. }
  138.  
  139. char paymentMethod[10]; //ใช้เหมือน char name[10]
  140. // รับข้อมูล
  141. printf("Please Select payment (C for Cash, B for Credit card): ");
  142. scanf(" %s",paymentMethod);
  143. //เรียกฟังก์ชัน processPayment()
  144. float totalwithfee;
  145. processPayment(total_price,paymentMethod, &totalwithfee);
  146.  
  147.  
  148. double paid_amout, result1;
  149. char status;
  150. //รับข้อมูล
  151. printf("\nEnter paid money: ");
  152. scanf("%lf",&paid_amout);
  153. //เรียกฟังก์ชันคำนวณเงินทอน
  154. status = calculateChange(totalwithfee , paid_amout, &result1);
  155. //แสดงผลตามที่คืนค่า
  156. if(status == 'C'){
  157. printf("Give change: %.2f\n",result1);
  158. }else if(status == 'S'){
  159. printf("Money Not enough: %.2f\n",result1);
  160. }else{
  161. printf("Exactly final total\n");
  162. }
  163.  
  164. return 0 ;
  165.  
  166.  
  167. }
Success #stdin #stdout 0.03s 25712KB
stdin
Standard input is empty
stdout
#include<stdio.h>
//1.void with no parameter  แสดงข้อมูลร้านค้า
void store_Info(){
    char storename [30]; 
    printf("Enter your store name "); //ใส่ชื่อร้าน
    scanf("%s",storename); //  %S ชื่อร้านมีหลายตัวอักษร ไม่ต้องใส่& เพราะใช้%S นอกนั้นใส่&
    printf("Store name is %s\n",storename); 
}

//2.return function with no parameter  คืนค่า ปีโปรโมชั่น
int promotionyear(){ 
int year ; 
printf("Enter promotion year "); 
scanf("%d",&year ); 
return year ; 
}

// ข้อ 3 คือ return double 
float discount1 (float totalprice){
    int choice ;
    float discount ;   
    printf("Enter your choice >>>1 is member discount 5%% >>> 2 is festival discount 10%% >>> 3 is speaial discount 15%% : "); 
    scanf("%d",&choice); 
    switch(choice){ // ข้อ 4 swith case
        case 1: 
            printf("you select member discount 5%% \n");
            discount= totalprice*0.95 ;
            break;        
        case 2 :
            printf("you select festival discount 10%% \n");
            discount= totalprice*0.90 ; 
            break; 
        case 3 :
            printf("you select special discount 15%% \n");
            discount= totalprice*0.85 ; 
            break; 
        default: 
            printf("please select again\n"); 
    }
    return discount;
} 

    const int secret_number= 34;
int guess_number(){  //ข้อ 5 return function with out parameter
    int guess ;
    while(1){
    printf("guess number (1-99): "); 
    scanf("%d",&guess);
     
    if (guess > secret_number){
        printf("Too many\n"); 
    }else if(guess < secret_number){
        printf("Too low\n");
    }else{
        return 1; // คืนค่า 1 ถ้าทายถูก
    }
    }
} 
void processPayment(int total_price , char paymentMethod[] , float *totalwithfee){
    printf ("\npaymentMethod: %s\n",paymentMethod); 
    if ((paymentMethod[0] == 'C' || paymentMethod[0] == 'c') && paymentMethod[1] == '\0'){ // \0 คือ จุดจบ
        //ถ้าผู้ใช้เลือกใช้เงินสด
        printf("Total price %d bath\n",&total_price);
        *totalwithfee = total_price;
    }else if((paymentMethod[0] == 'B' || paymentMethod[0] == 'b') && paymentMethod[1] == '\0'){
        //ถ้าใช้บัตรเครดิต
        float fee = total_price*0.05; //ค่าธรรมเนียม
        *totalwithfee = total_price + fee;
        printf("Credit card fee 5%% : %.2f baht\n",fee);
        printf("Total with fee : %2.f",*totalwithfee);
    }else{
        printf("Wrong payment method ! Select agian\n");
        *totalwithfee = total_price; //ตั้วค่าเริ่มต้นกันพลาด
    }

}
char calculateChange(int totalwithfee,double paid_amout , double *result1){

    if(paid_amout > totalwithfee){
        *result1 = (paid_amout - totalwithfee);
        return 'C'; //มีเงินทอน
    }else if(paid_amout < totalwithfee){
        *result1 = (totalwithfee - paid_amout);
        return 'S';//เงินไม่พอ    
    }else{
        *result1 = 0;//ไม่มีเงินทอนหรือเงินขาด
        return 'E'; //เงินพอดี
    }
}

int main (){
    store_Info (); // call function 1
    
    int year = promotionyear (); 
    printf("Enter promotion year is %d\n",year); 
 
    char exit ;            
    float price1[100];
    int a =0 ;
    float total =0 ;

    do{ 
    printf("Enter price ");
    scanf("%f",&price1[a]); 
    total=total+price1[a];  
    
    printf("\n"); 
    printf ("Exit for y/Y,Want to exit(y/n)  "); 
    scanf(" %c",&exit);  

    }while ((exit !='y')&&(exit != 'Y'));
    printf("total is %.2f\n",total); 
    
    float price_after_discount=discount1(total);
    printf("price after discount is %.2f\n",price_after_discount); 
    
    float total_price; 
    if (price_after_discount <2000){
        printf("No discount\n");
        total_price = price_after_discount*1.0;
    }else if((price_after_discount >=2000) && (price_after_discount <=5000)){
        printf("have more discount 5%%\n");
        total_price = price_after_discount*0.95;
    }else if((price_after_discount >=5001) && (price_after_discount <=10000)){
        printf("have more discount 10%%\n");
        total_price = price_after_discount*0.90;
    }else{
        printf("have more discount 20%%\n");
        total_price = price_after_discount*0.80;
    }
     printf("total price is %.2f\n",total_price);

    printf("===Lucky Number===\n");
    // เรียกใช้ฟังก์ชันทายเลข
    if(guess_number()){
        printf("congratulations\n");
    }

    char paymentMethod[10]; //ใช้เหมือน char name[10]
    // รับข้อมูล
    printf("Please Select payment (C for Cash, B for Credit card): ");
    scanf(" %s",paymentMethod);
    //เรียกฟังก์ชัน processPayment()
    float totalwithfee;
    processPayment(total_price,paymentMethod, &totalwithfee);


    double paid_amout, result1;
    char status;
    //รับข้อมูล
    printf("\nEnter paid money: ");
    scanf("%lf",&paid_amout);
    //เรียกฟังก์ชันคำนวณเงินทอน
    status = calculateChange(totalwithfee , paid_amout, &result1);
    //แสดงผลตามที่คืนค่า
    if(status == 'C'){
        printf("Give change: %.2f\n",result1);
    }else if(status == 'S'){
        printf("Money Not enough: %.2f\n",result1);
    }else{
        printf("Exactly final total\n");
    }

    return 0 ;
   

}