#include <stdio.h>
#include <stdlib.h>
typedef struct {
int codigo;
int estoque;
} Cadastro;
void imprimir( Cadastro * cadastro, int n) {
printf ( "\n \n ---------RESULTADO DOS PEDIDOS DOS CLIENTES-------\n \n " ) ; for ( int i = 0 ; i < n; i++ ) {
printf ( "Código da mercadoria %d: %d \n " , i
+ 1 , cadastro
[ i
] .
codigo ) ; printf ( "Quantidade da mercadoria no estoque: %d\n \n " , cadastro
[ i
] .
estoque ) ; }
}
int main( ) {
int n;
printf ( "Entre com o número de mercadorias: " ) ; Cadastro
* cadastro
= malloc ( n
* sizeof ( Cadastro
) ) ; if ( cadastro == NULL) {
printf ( "Proberlmas de alocação de memoria!\n " ) ; return 1 ;
} else {
for ( int i = 0 ; i < n; i++ ) {
printf ( "Entre com o código da mercadoria %d: " , i
+ 1 ) ; scanf ( "%d%*c" , & cadastro
[ i
] .
codigo ) ; printf ( "Entre com o estoque da mercadoria %d: " , i
+ 1 ) ; scanf ( "%d%*c\n " , & cadastro
[ i
] .
estoque ) ; }
}
printf ( "----------PEDIDOS DOS CLIENTES----------\n \n " ) ; while ( 1 ) {
printf ( "Entre com o código da mercadoria: " ) ; int codcli;
scanf ( "%d%*c" , & codcli
) ; int achou = 0 ;
for ( int i = 0 ; i < n; i++ ) {
if ( cadastro[ i] .codigo == codcli) {
printf ( "Entre com o estoque da mercadoria: " ) ; int estoquecli;
scanf ( "%d" , & estoquecli
) ; if ( cadastro[ i] .estoque >= estoquecli) {
cadastro[ i] .estoque -= estoquecli;
printf ( "ESTOQUE ATUALIZADO\n " ) ; } else printf ( "ESTOQUE INSUFICIENTE!\n " ) ; achou = 1 ;
}
}
if ( achou
== 0 ) printf ( "MERCADORIA NÃO ENCONTRADA!\n " ) ; printf ( "Deseja enviar outra solicitacao? (Sim: 1/Não: 0): " ) ; int escolha;
if ( escolha == 0 ) break ;
}
imprimir( cadastro, n) ;
}
//https://pt.stackoverflow.com/q/333638/101
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNs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AgICAgICBwcmludGYgKCJFbnRyZSBjb20gbyBlc3RvcXVlIGRhIG1lcmNhZG9yaWEgJWQ6ICIsIGkrMSk7CiAgICAgICAgICAgIHNjYW5mICgiJWQlKmNcbiIsICZjYWRhc3Ryb1tpXS5lc3RvcXVlKTsKICAgICAgICB9CiAgICB9CiAgICBwcmludGYgKCItLS0tLS0tLS0tUEVESURPUyBET1MgQ0xJRU5URVMtLS0tLS0tLS0tXG5cbiIpOwogICAgd2hpbGUgKDEpIHsKICAgICAgICBwcmludGYgKCJFbnRyZSBjb20gbyBjw7NkaWdvIGRhIG1lcmNhZG9yaWE6ICIpOwogICAgICAgIGludCBjb2RjbGk7CiAgICAgICAgc2NhbmYgKCIlZCUqYyIsICZjb2RjbGkpOwogICAgICAgIGludCBhY2hvdSA9IDA7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgaWYgKGNhZGFzdHJvW2ldLmNvZGlnbyA9PSBjb2RjbGkpIHsKICAgICAgICAgICAgICAgIHByaW50ZiAoIkVudHJlIGNvbSBvIGVzdG9xdWUgZGEgbWVyY2Fkb3JpYTogIik7CiAgICAgICAgICAgICAgICBpbnQgZXN0b3F1ZWNsaTsKICAgICAgICAgICAgICAgIHNjYW5mICgiJWQiLCAmZXN0b3F1ZWNsaSk7CiAgICAgICAgICAgICAgICBpZiAoY2FkYXN0cm9baV0uZXN0b3F1ZSA+PSBlc3RvcXVlY2xpKXsKICAgICAgICAgICAgICAgICAgICBjYWRhc3Ryb1tpXS5lc3RvcXVlIC09IGVzdG9xdWVjbGk7CiAgICAgICAgICAgICAgICAgICAgcHJpbnRmICgiRVNUT1FVRSBBVFVBTElaQURPXG4iKTsKICAgICAgICAgICAgICAgIH0gZWxzZSBwcmludGYgKCJFU1RPUVVFIElOU1VGSUNJRU5URSFcbiIpOwogICAgICAgICAgICAgICAgYWNob3UgPSAxOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmIChhY2hvdSA9PSAwKSBwcmludGYgKCJNRVJDQURPUklBIE7Dg08gRU5DT05UUkFEQSFcbiIpOwogICAgICAgIHByaW50ZiAoIkRlc2VqYSBlbnZpYXIgb3V0cmEgc29saWNpdGFjYW8/IChTaW06IDEvTsOjbzogMCk6ICIpOwogICAgICAgIGludCBlc2NvbGhhOwogICAgICAgIHNjYW5mICgiJWQiLCAmZXNjb2xoYSk7CiAgICAgICAgaWYgKGVzY29saGEgPT0gMCkgYnJlYWs7CiAgICB9ICAKICAgIGltcHJpbWlyKGNhZGFzdHJvLCBuKTsKICAgIGZyZWUoY2FkYXN0cm8pOwp9CgovL2h0dHBzOi8vcHQuc3RhY2tvdmVyZmxvdy5jb20vcS8zMzM2MzgvMTAx