hqt

peHoa

hqt
Jul 20th, 2012
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.01 KB | None | 0 0
  1. #include <conio.h>
  2. #include <math.h>
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #define MAX 100
  8. #define MAX_C 50
  9.  
  10. long convertNumber(char c[MAX_C]){
  11.     int i = 0;
  12.     long res = c[0] - 48;
  13.     for(i=1; i<strlen(c); i++)
  14.         res = res*10 + c[i]-48;
  15.     return res;
  16. }
  17.  
  18. void validateISBN(char c[MAX_C]) {
  19.    
  20.     // keep input in array to keep 0 before number
  21.     // canbe use ` typedef char[MAX_C] String ` for conventience :)
  22.     int flag = 1;
  23.     do {
  24.         flag = 1;
  25.         scanf("%s",c);
  26.         int i = 0;
  27.         for(i=0; i<strlen(c); i++){
  28.             if (c[i]<'0' || c[i]>'9'){
  29.                 flag = 0;
  30.                 if (c[0] != '-')
  31.                     printf("Trailing character meet\nTry again : ");
  32.                 else
  33.                     printf("Negative is unacceptable\nTry again : ");                
  34.                 break;
  35.             }
  36.         }
  37.     } while (flag == 0);
  38.  
  39. }
  40.  
  41. double validatePrice(){
  42.      int flag=1;
  43.      char c[20];
  44.      memset(c,0,sizeof(c));
  45.      
  46.      do{
  47.      flag =1;
  48.      scanf("%s",c);
  49.      
  50.      /*
  51.       * loop to validate and  count number of dot
  52.       * if more than one dot --> error
  53.       */
  54.      int j = 0, dot = 0;
  55.      for(j=0;j<strlen(c);j++){
  56.          if(c[j]<'0' || c[j]>'9' ){
  57.              if (dot == 0){
  58.                  dot++;
  59.                  continue;
  60.              }
  61.              flag = 0;
  62.              if (c[j] == '.')
  63.                  printf("Wrong format only 1 dot (.) allow\nTry again :");
  64.              else
  65.                  printf("Just contain number.\nTry again:");
  66.          }           
  67.      }
  68.  
  69.      /* prevent someone fucking */
  70.      if(strlen(c)==0)
  71.          flag=0;
  72.      
  73.      }while(flag == 0);
  74.  
  75.      /*
  76.       * output : divide two phrase:
  77.       *        phrase 1 : integer part
  78.       *        phrase 2: fraction part
  79.       */
  80.  
  81.      double res = 0.0;
  82.      /* phrase one*/
  83.      
  84.      /* first step : find location of dot */
  85.      int i = 0;
  86.      for (i=0;i<strlen(c);i++){
  87.          if(c[i] =='.')
  88.                  break;
  89.      }
  90.      /* second step : sum of integer part */
  91.      int k;
  92.      res = 1.0*c[0] - 48;
  93.      for (k=1;k<i;k++)
  94.         res = res* 10 + c[k] - 48;
  95.  
  96.      /* phrase two */
  97.      int m =k;
  98.      k++;
  99.      for(k;k<strlen(c);k++)
  100.         res += (c[k]-48) * pow((float)10,-(k-m));
  101.  
  102.      printf("%res:%f",res);
  103.      return res;     
  104. }
  105.  
  106. int validateQuantity() {
  107.     int flag=1;
  108.     int i = 0;
  109.     char c[MAX_C];
  110.     memset(c,0,sizeof(c));
  111.     do {
  112.         flag = 1;
  113.         // input
  114.         fflush(stdin);
  115.         scanf("%s",c);
  116.        
  117.         // prevent someone fucking
  118.         if (strlen(c)<=0 || strlen(c)>7){
  119.             printf("Too big or too small !!!\nTry again : ");
  120.             flag = 0;
  121.         }
  122.  
  123.         // check if number ?
  124.         for(i=0; i<strlen(c); i++){
  125.             if (!isdigit(c[i])){
  126.                 flag = 0;
  127.                 if (c[0] != '-')
  128.                     printf("Trailing character meet\nTry again : ");
  129.                 else
  130.                     printf("Negative is unacceptable\nTry again : ");
  131.                 break;
  132.             }
  133.         }
  134.     } while (flag == 0);
  135.    
  136.    return convertNumber(c);
  137. }
  138.  
  139. int input(char barcode[MAX][MAX_C], long barcodeV[MAX], double price[MAX], int quantity[MAX], int n) {
  140.     long bcode;
  141.     double p;
  142.     int quan, i;
  143.     char c[30];
  144.    
  145.     memset(c,0,sizeof(c));
  146.    
  147.     printf("\nISBN : ");
  148.     validateISBN(c);
  149.     bcode = convertNumber(c);
  150.     for (i = 0; i < n; i++) {
  151.         if (barcodeV[i] == bcode) {
  152.             printf("Item exits\n");
  153.             return n;
  154.         }
  155.     }
  156.    
  157.     printf("Price: ");
  158.     p = validatePrice();
  159.     printf("Quantity: ");
  160.     quan = validateQuantity();
  161.    
  162.     barcodeV[n] = bcode;
  163.     strcpy(barcode[n],c);
  164.     price[n] = p;
  165.     quantity[n] = quan;
  166.     n++;
  167.     return n;
  168. }
  169.  
  170. int sort(char barcode[MAX][MAX_C], long barcodeV[MAX], double price[MAX], int quantity[MAX], int n) {
  171.    
  172.     /* temporary variable for sorting */
  173.     long _bcodeV;
  174.     double _price;
  175.     char _bcode[MAX_C];
  176.     int _quantity;
  177.  
  178.     // implement bubble sort here
  179.     int i=0, j=0;
  180.     for (i = 0; i < n - 1; i++) {
  181.         for (j = i + 1; j < n; j++) {
  182.             if (barcodeV[i] > barcodeV[j]) {
  183.                
  184.                 _bcodeV = barcodeV[i];
  185.                 _price = price[i];
  186.                 _quantity = quantity[i];
  187.                 strcpy(_bcode, barcode[i]);
  188.  
  189.                 barcodeV[i] = barcodeV[j];
  190.                 strcpy(barcode[i], barcode[j]);
  191.                 price[i] = price[j];
  192.                 quantity[i] = quantity[j];
  193.  
  194.                 barcodeV[j] = _bcodeV;
  195.                 strcpy(barcode[j], _bcode);
  196.                 price[j] = _price;
  197.                 quantity[j] = _quantity;
  198.             }
  199.         }
  200.     }
  201.  
  202.     double sum = 0;
  203.     printf("\n------------------------------------------------\n");
  204.     printf("ISBN----Price-------Quantity--------Total---\n");
  205.     for (i = 0; i < n; i++) {
  206.         sum += price[i] * quantity[i];
  207.         printf("%s\t       %.3lf      %d           %.3lf\n", barcode[i], price[i], quantity[i], price[i] * quantity[i]);
  208.     }
  209.     printf("Total is : %lf\n", sum);
  210.     return 1;
  211. }
  212.  
  213. void main() {
  214.     char barcode[MAX][MAX_C];    // keep barcode (still remain 0 before number).  MAX_C : maxinum number of barcode
  215.     long barcodeV[MAX];          // barcode under integer type (keep this array for convenience when sorting)
  216.     double price[MAX];                     
  217.     int quantity[MAX];
  218.     int n = 0;
  219.     char c;
  220.    
  221.     // make all array to zero by memset() in <string.h>
  222.     memset(barcodeV,0,sizeof(barcodeV));
  223.     memset(barcode,0,sizeof(barcode));
  224.     memset(price,0,sizeof(price));
  225.     memset(quantity,0,sizeof(quantity));
  226.  
  227.     // just normal menu
  228.     do {
  229.         printf("=================\n");
  230.         printf("1.Enter new\n");
  231.         printf("2.Sort\n");
  232.         printf("3.Exit");
  233.         printf("Choice : ");
  234.         do {
  235.             c = _getch();
  236.             printf("%c\n",c);
  237.             if (c != '1' && c != '2' && c != '3')
  238.                 printf("\nInvalid choice : ");
  239.         } while (c != '1' && c != '2' && c != '3');
  240.  
  241.         if (c == '1')
  242.             n = input(barcode, barcodeV, price, quantity, n);
  243.         else if (c == '2')
  244.             n = sort(barcode, barcodeV, price, quantity, n);
  245.     } while (c != 3);
  246. }
Advertisement
Add Comment
Please, Sign In to add comment