Define an array and the type of a function in C

2

Hello,

I'm developing a C language initialization project, namely creating a Multimedia Management System that will have to be able to manage a database of record items like CDs, DVDs and Vinyl Records. >

At the moment, after declaring the structure (I do not know if it is correct), I have this question:

  • How to define an array that can contain articles so that you can later define the type of function Query () and in this case, how to define this function? Basically, I wanted to "tell" the function what the article reference is for the function to look for in that book. In the end, the function would have to return the result of the article.

Here is my source code for now:

#include <stdio.h>
#include <stdlib.h>

// prototipagem de funções:
int menuPrincipal(void);
void consultarArtigo(void);
void removerArtigo(void);
void alterarArtigo(void);
void inserirArtigo(void);

// estruturas:
typedef struct {
    int numero_unico_de_registo;
    char titulo_do_artigo[30];
    char area_discografica; // dizer se é do tipo CD, DVD ou Vinil?
    char nome_do_artista[30];
    int ano_de_lancamento;
    int estante_de_localizacao; // as estantes seriam identificadas por numeros?    
} ARTIGO;

// Função MAIN:
int main()
{
    menuPrincipal();
    return;
}

// Funções AUX:
int menuPrincipal(void){

    int opcao;

    do {

    printf("Bem-Vindo ao MMS!\n");
    printf("1 - Consultar Artigo Discografico\n");
    printf("2 - Remover Artigo Discografico\n");
    printf("3 - Alterar Artigo Discografico!\n");
    printf("4 - Inserir Artigo!\n");
    printf("0 - Fechar!\n");
    printf("Escolha a sua opcao!\n");
    scanf("%d", &opcao);

    switch(opcao) {
        case 1: consultarArtigo();
        break;
        case 2: removerArtigo();
        break;
        case 3: alterarArtigo();
        break;
        case 4: inserirArtigo();
        break;
        case 0: printf("Sair do Menu!");
        default: printf("Opcao invalida, tente de novo.\n");
    }

    } while (opcao != 0);

    return(opcao);
}

void consultarArtigo(void) {
    printf("Entrei na funcao Consultar Artigo!\n");
}

void removerArtigo(void) {
    printf("Entrei na funcao Remover Artigo!\n");
}

void alterarArtigo(void){
    printf("Entrei na funcao Alterar Artigo!\n");
}

void inserirArtigo(void){
    printf("Entrei na funcao Inserir Artigo!\n");
}

Code originally placed in ideone

Any tips, would be welcome.

    
asked by anonymous 02.03.2016 / 14:32

1 answer

2

1) What is the data?

The first part is to define a structure that will store the articles and all the information about it that you need, what you have already done.

// estruturas:
typedef struct {
    int numero_unico_de_registo;
    char titulo_do_artigo[30];
    char area_discografica; // dizer se é do tipo CD, DVD ou Vinil?
    char nome_do_artista[30];
    int ano_de_lancamento;
    int estante_de_localizacao; // as estantes seriam identificadas por numeros?    
} ARTIGO;

2) What is the structure?

The second part is to define which data structure you are going to use. If the number of active articles has a limit (perhaps the number you think the most for your library), you do not have much trouble using an array, but you may have to allocate memory indefinitely to place the articles, in which case I I recommend that you try to use a tree or a list.

You have to decide whether to keep the items sorted by the number of the registry or not. From what I understand, you're going to need to search any field, so it does not seem to help you much to sort.

To declare the array, simply, just below the definition of the ARTICLE, place:

#define NUMERO_MAXIMO_DE_ARTIGOS 50 //número máximo permitido
struct listaDeArtigos ARTIGO[NUMERO_MAXIMO_DE_ARTIGOS]; //declaração do array
int numeroDeArtigos = 0; //para guardar o numero atual de artigos

3) Implement the functions

Here are some tips:

To query, you simply ask the user which field he wants to fetch and the value of the field. Then you go through all the positions up to the number of Articles and return the article found or return not found.

When removing an article, move all the others to the end, always keeping the beginning of your array filled.

Always try to insert an article at the end if the order does not make a difference.

The article change function is basically a progress of the query function, so do it first.

    
02.03.2016 / 14:47