Advertisement
Guest User

ubicuse

a guest
Apr 19th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.15 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<ctype.h>
  4. #include<string.h>
  5.  
  6. typedef struct{
  7.     char name[64];
  8.     int quantity;
  9.     int res; } info;
  10.  
  11. void insertion_s(info receipt[100], int n){
  12.     int i, j;
  13.     info tmp;
  14.  
  15.     for(i = 1; i < n; ++i){
  16.         j = i;
  17.         tmp = receipt[i];
  18.         while(j > 0 && strcmp(tmp.name, receipt[j - 1].name) < 0){
  19.             receipt[j] = receipt[j - 1];
  20.             --j;
  21.         }
  22.         receipt[j] = tmp;
  23.     }
  24. }
  25.  
  26. void rm_blanks(char x[256]){
  27.     int l = strlen(x);
  28.  
  29.     while(!isalpha(x[l])){
  30.         --l;
  31.         if(isalpha(x[l])){
  32.             x[l + 1] = '\0';
  33.         }
  34.     }
  35. }
  36.  
  37. int main(){
  38.     info input[100] = {};
  39.     info receipt[100] = {};
  40.  
  41.     int i, j, n = -1, m = -1;
  42.     int trig = 0, exist = 0;
  43.     char key[256];
  44.     char line[256];
  45.  
  46.     FILE *fptr = fopen("input.dat", "r");
  47.  
  48.     fgets(key, 64, stdin);
  49.     rm_blanks(key);
  50.  
  51.     do{
  52.         ++n;
  53.  
  54.         scanf("%[^,],", input[n].name);
  55.  
  56.         if(input[n].name[0] != '-'){
  57.             scanf("%d ", &input[n].quantity);
  58.         }
  59.     }while(input[n].name[0] != '-');
  60.  
  61.  
  62.     while(fgets(line, 128, fptr) != NULL){
  63.        rm_blanks(line);
  64.  
  65.        if(!strcmp(line, key)) trig = 1;
  66.  
  67.        while(trig){
  68.             ++m;
  69.  
  70.             fscanf(fptr, "%[^,], ", receipt[m].name);
  71.  
  72.             if(receipt[m].name[0] != '-'){
  73.                 fscanf(fptr, "%d ", &receipt[m].quantity);
  74.             }
  75.             else trig = 0;
  76.         }
  77.     }
  78.    
  79.     for(i = 0; i < m; ++i){
  80.         exist = 0;
  81.         for(j = 0; j < n; ++j){
  82.             if(!strcmp(receipt[i].name, input[j].name)){
  83.                 exist = 1;
  84.                 receipt[i].res = input[j].quantity - receipt[i].quantity;
  85.                 if(receipt[i].res < 0) trig = 1;
  86.             }
  87.         }
  88.         if(!exist) receipt[i].res = -receipt[i].quantity;
  89.     }
  90.  
  91.     insertion_s(receipt, m);
  92.  
  93.  
  94.     if(!trig) puts("DA");
  95.     else{
  96.         puts("NE");
  97.         for(i = 0; i < m; ++i){
  98.             if(receipt[i].res < 0){
  99.                 printf("%s, %d\n", receipt[i].name, abs(receipt[i].res));
  100.             }
  101.         }
  102.     }
  103.  
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement