An eccentric wine lover buys individual labels of wine in ei

An eccentric wine lover buys individual labels of wine in either 1, 3, 6, or 12 bottle lots and consumes

either 1 or 2 bottles of the same type at a time.

Unfortunately, his wine cellar can only hold 50 bottles and he is further constrained by the fact that he

wishes ti keep wines of a particular label in contiguous areas of his cellar so that he can easily tell how

many bottles of a particular type he has left. Once positioned in the cellar, a bottle is never moved to

another position.

Write and test a C program that shows how his wine stock varies as he buys and consumes wine.

Input: lines of the form

N,C N is 1, 3, 6, or 12 representing purchases or 1 or 2 representing consumption

C is an identifying character for the wine type

N = 0 terminates the program

Output

After each input line your program outputs either an error message or a depiction of the cellar

Example program run

Enter number,type :7,a

invalid buy quantity: 7

Enter number,type :3,a

aaa

Enter number,type :6,b

aaabbbbbb

Enter number,type :-3,b

invalid consume quantity: -3

Enter number,type :-2,b

aaa bbbb

Enter number,type :6,c

aaa bbbbcccccc

Enter number,type :-1,c

aaa bbbb ccccc

Enter number,type :-2,d

consume failed

Enter number,type :6,b

buy failed

Enter number,type :3,b

aabbbbbbbbccccc

Enter number,type :12,d

aabbbbbbbbcccccdddddddddddd

Enter number,type :12,e

aabbbbbbbbcccccddddddddddddeeeeeeeeeeee

Enter number,type :12,f

buy failed

Enter number,type :-2,d

aabbbbbbbbccccc ddddddddddeeeeeeeeeeee

Enter number,type :-2,e

aabbbbbbbbccccc dddddddddd eeeeeeeeee

Enter number,type :3,f

aabbbbbbbbccccc dddddddddd eeeeeeeeeefff

Enter number,type :3,d

aabbbbbbbbccccdddddddddddddddeeeeeeeeeefff

Enter number,type :1,e

buy failed

Enter number,type :1,f

aabbbbbbbbccccdddddddddddddddeeeeeeeeeeffff

Enter number,type :0,x

Bye

Solution

#include <stdio.h>

int main(void) {
// your code goes here

//initialize the characters array which is useful as flag setting if a particular character is there in the array already
int characters[256];

//Initialize the stocks array which is useful to store the stocks of beer.
char stock[10000];

int i,j,k,n;

//Initialize the stocks to empty labels
for(i = 0; i< 10000; i++){
     stock[i] = \' \';
}
//Initialize all the characters count to zero
for(i = 0;i < 256 ; i++){
     characters[i] = 0;
}

//Initialize length of the stocks to zero
    int stockLength = 0;
char c;
//Scan the number and character to do the operation
scanf(\"%d\ %c\ \",&n,&c);

//While we want to do more operations
while(n != 0){
     //n >0 means buying operations
     if(n >0){
     //buy only the buy quantity is 1 or 3 or 6 or 12
     if(n == 1 || n == 3 || n == 6 || n == 12){
        
         int buyCount = n;
         // If the last character of the stock is same as the buy label or this was never bought before
         if(characters[c] == 0 || stock[stockLength]== c){
            
             //Add the bought labels to the end of the stock
             while(buyCount--){
                 stock[++stockLength] = c;
             }
             characters[c] = 1;
             //Print the stock
             printf(\"%s\ \",stock+1);
         }
         //buy failed because we can\'t put this at the end of stock.
         else{
                 printf(\"buy falied\ \");
         }
        
     }
     //Invalid buy quantity
     else{
         printf(\"invalid buy quantity : %d\ \",n);
        
     }
     }
     //n <0 means consume operations
     else if(n <0){
         //consume only if consumeQuantity is -1 or -2
         if((n == -1) || (n == -2)){
             int consumeCount = n;
             if(consumeCount == -1){
                 //We can only consume if the last labels in the stock is the same as we wanted to consume label.
                 if(stock[stockLength] == c){
                     stock[stockLength--] = \' \';
                    
                     if(stock[stockLength]!= c)
                     characters[c] = 0;
                     //Decrease the stock length and print the stock afte removing consumed quantities.
                     printf(\"%s\ \",stock+1);
                 }else{
                     printf(\"consume falied\ \");
                 }
             }
             else if(consumeCount == -2){
                
                 //We have to check whether we have 2 number stock of same label to consume.
                 if(stock[stockLength] == c && stock[stockLength - 1] == c){
                     stock[stockLength--] = \' \';
                     stock[stockLength--] = \' \';
                    
                     if(stock[stockLength]!= c)
                     characters[c] = 0;
                     //decrease the stock count and print the stock after consuming 2 ones.
                     printf(\"%s\ \",stock+1);
                
                 //else consume failed because we don\'t have consumable quantity of stock of same label at the back.
                 }else{
                     printf(\"consume falied\ \");
                 }
             }
             //Invalid Consume quantity. Must be -1 or -2.
         }else{
             printf(\"invalid consume quantity : %d\ \",n);
         }
     }
     //Scan repeatedly in the loop until n= 0 which is a terminate operation.
     scanf(\"%d\ %c\ \",&n,&c);
    
}
return 0;
}

Use the above code for your question.

You mentioned the case which stores the bottles can only store 50 . so please handle that case. Remaining everything my code wil take care.

An eccentric wine lover buys individual labels of wine in either 1, 3, 6, or 12 bottle lots and consumes either 1 or 2 bottles of the same type at a time. Unfor
An eccentric wine lover buys individual labels of wine in either 1, 3, 6, or 12 bottle lots and consumes either 1 or 2 bottles of the same type at a time. Unfor
An eccentric wine lover buys individual labels of wine in either 1, 3, 6, or 12 bottle lots and consumes either 1 or 2 bottles of the same type at a time. Unfor
An eccentric wine lover buys individual labels of wine in either 1, 3, 6, or 12 bottle lots and consumes either 1 or 2 bottles of the same type at a time. Unfor

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site