Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<ctype.h>
- #include<string.h>
- typedef struct{
- char name[64];
- int quantity;
- int res; } info;
- void insertion_s(info receipt[100], int n){
- int i, j;
- info tmp;
- for(i = 1; i < n; ++i){
- j = i;
- tmp = receipt[i];
- while(j > 0 && strcmp(tmp.name, receipt[j - 1].name) < 0){
- receipt[j] = receipt[j - 1];
- --j;
- }
- receipt[j] = tmp;
- }
- }
- void rm_blanks(char x[256]){
- int l = strlen(x);
- while(!isalpha(x[l])){
- --l;
- if(isalpha(x[l])){
- x[l + 1] = '\0';
- }
- }
- }
- int main(){
- info input[100] = {};
- info receipt[100] = {};
- int i, j, n = -1, m = -1;
- int trig = 0, exist = 0;
- char key[256];
- char line[256];
- FILE *fptr = fopen("input.dat", "r");
- fgets(key, 64, stdin);
- rm_blanks(key);
- do{
- ++n;
- scanf("%[^,],", input[n].name);
- if(input[n].name[0] != '-'){
- scanf("%d ", &input[n].quantity);
- }
- }while(input[n].name[0] != '-');
- while(fgets(line, 128, fptr) != NULL){
- rm_blanks(line);
- if(!strcmp(line, key)) trig = 1;
- while(trig){
- ++m;
- fscanf(fptr, "%[^,], ", receipt[m].name);
- if(receipt[m].name[0] != '-'){
- fscanf(fptr, "%d ", &receipt[m].quantity);
- }
- else trig = 0;
- }
- }
- for(i = 0; i < m; ++i){
- exist = 0;
- for(j = 0; j < n; ++j){
- if(!strcmp(receipt[i].name, input[j].name)){
- exist = 1;
- receipt[i].res = input[j].quantity - receipt[i].quantity;
- if(receipt[i].res < 0) trig = 1;
- }
- }
- if(!exist) receipt[i].res = -receipt[i].quantity;
- }
- insertion_s(receipt, m);
- if(!trig) puts("DA");
- else{
- puts("NE");
- for(i = 0; i < m; ++i){
- if(receipt[i].res < 0){
- printf("%s, %d\n", receipt[i].name, abs(receipt[i].res));
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement