【问题标题】:Writing in headers and modulating my code写入标题并修改我的代码
【发布时间】:2017-01-24 13:54:27
【问题描述】:

我不知道如何通过创建 .h 文件和更多 .c 文件来调整我的代码。有人可以通过展示如何为此创建头文件来帮助我吗?

你的代码目前的作用是,如果你输入一个数字,它就会以 LSD 屏幕的格式显示。您还可以选择输出的大小(例如,大小 2 表示每个列段和行的长度为 2“-”)该代码还检查输入是否全为数字。如果不是,则显示“无效输入”

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

#define MAXNUM 50
#define MAXSIZE 100
#define ROWS 3 //From top to bottom
#define COLS 4 // (left to right, up to down)
#define OFF ' '
#define ROW_ON '-'
#define COL_ON '|'

//input number

char led_rows[MAXNUM][ROWS][MAXSIZE];
char led_cols[MAXNUM][COLS][MAXSIZE];
int input_num[MAXNUM];

//Check if integer
int is_digit(char ch)
{
    if (ch >= '0' && ch <= '9')
        return 1;
    return 0;
}

//Initalise all rows and columns to be OFF to prevent char array from displaying random signs 
void init_led(int input_count, int size)
{
    for (int n = 0; n < input_count; ++n)
    {
        for (int i = 0; i < ROWS; ++i)
        {
            for (int j = 0; j < size; ++j)
            {
                led_rows[n][i][j]= OFF;
            }
        }
        for (int i = 0; i < COLS; ++i)
        {
            for (int j = 0; j < size; ++j)
            {
                led_cols[n][i][j]= OFF;
            }
        }
    }

}

// Display row of "-" for the needed size amount 
void display_row(int num, int rowNum, int size)
{
    printf(" ");
    for (int i = 0; i < size; ++i)
    {
        //Decides whther row is ON or still OFF
        printf("%c",led_rows[num][rowNum][i]); 
    }
    printf(" ");
}

// Display column of "|" for the needed size amount (number postion,column number,size, number of current column position )
void display_columns(int num, int colNum, int size, int n)
{
    
    printf("%c",led_cols[num][colNum][n]);
    for(int j = 0; j < size; j++)
        printf(" ");
    printf("%c",led_cols[num][colNum+1][n]);
    
}

//Display entire LCD
void display_lcd(int input_count, int size)
{
    if (size == 0)
    {
        return;
    }
    
    //Goes through first row for input numbers 
    for (int i = 0; i < input_count-1; ++i)
    {
        display_row(i, 0, size);
        printf(" ");
    }
    display_row(input_count-1, 0, size);
    printf("\n");


    
    // print the first two columns for the inputed numbers
    for (int n = 0; n < size; ++n)
    {
        for (int i = 0; i < input_count-1; ++i)
        {
            display_columns(i, 0, size, n );
            printf(" ");

        }
        display_columns(input_count-1, 0, size, n );
        printf("\n");
    }
    
    //print second row for unputed numbers
    for (int i = 0; i < input_count-1; ++i)
    {
        display_row(i, 1, size);
        printf(" ");
    }
    display_row(input_count-1, 1, size);
    printf("\n");
    
    // print the third and fourth columns for input numbers
    for (int n = 0; n < size; ++n)
    {
        for (int i = 0; i < input_count-1; ++i)
        {
            display_columns(i, 2, size, n );
            printf(" ");

        }
        display_columns(input_count-1, 2, size, n );
        printf("\n");
    }

    // print last row for inputed numbers
    for (int i = 0; i < input_count-1; ++i)
    {
        display_row(i,2, size);
        printf(" ");
    }
    display_row(input_count-1,2, size);
    printf("\n");
    
}


//Takes in number position, which row, and the size, and it will display "-"
void set_row_on(int num, int row_id,int size)
{

    for (int i = 0; i < size; ++i)
    {

        led_rows[num][row_id][i] = ROW_ON;
    
    }
}

//Takes in number position, which column, and the size, and it will display "|"
void set_col_on(int num, int col_id, int size)
{
    for (int i = 0; i < size; ++i)
    {
        led_cols[num][col_id][i] = COL_ON;
        
    }
}

//The setting of the relevent Rows and Columns for the numbers 

void set_on_zero(int num, int size)
{
    set_row_on(num, 0,size);
    //set_row_on_off(OFF, num, 1,size);
    set_row_on(num, 2,size);
    set_col_on(num, 0,size);
    set_col_on(num, 1,size);
    set_col_on(num, 2,size);
    set_col_on(num, 3,size);
}


void set_on_one(int num, int size)
{
    //set_col_on(0,size, led_cols);
    set_col_on(num, 1,size);
    //set_col_on(2,size, led_cols);
    set_col_on(num, 3,size);
}

void set_on_two(int num, int size)
{
    set_row_on(num, 0,size);
    set_row_on(num, 1,size);
    set_row_on(num, 2,size);
    set_col_on(num, 1,size);
    set_col_on(num, 2,size);
    
}

void set_on_three(int num, int size)
{
    set_row_on(num, 0,size);
    set_row_on(num, 1,size);
    set_row_on(num, 2,size);
    set_col_on(num, 1,size);
    set_col_on(num, 3,size);

}

void set_on_four(int num, int size)
{
    
    set_row_on(num, 1,size);
    set_col_on(num, 0,size);
    set_col_on(num, 1,size);
    set_col_on(num, 3,size);
}
void set_on_five(int num, int size)
{
    set_row_on(num, 0,size);
    set_row_on(num, 1,size);
    set_row_on(num, 2,size);
    set_col_on(num, 0,size);
    set_col_on(num, 3,size);
}

void set_on_six(int num, int size)
{
    set_row_on(num, 0,size);
    set_row_on(num, 1,size);
    set_row_on(num, 2,size);
    set_col_on(num, 0,size);
    set_col_on(num, 2,size);
    set_col_on(num, 3,size);
}

    void set_on_seven (int num, int size)
{
    set_row_on(num, 0,size);
    set_col_on(num, 1,size);
    set_col_on(num, 3,size);
}



void set_on_eight(int num, int size)
{
    set_row_on(num, 0,size);
    set_row_on(num, 1,size);
    set_row_on(num, 2,size);
    set_col_on(num, 0,size);
    set_col_on(num, 1,size);
    set_col_on(num, 2,size);
    set_col_on(num, 3,size);
}

void set_on_nine(int num, int size)
{
    set_row_on(num, 0,size);
    set_row_on(num, 1,size);
    set_col_on(num, 0,size);
    set_col_on(num, 1,size);
    set_col_on(num, 3,size);
}


//Select which number is inputed and which set_on to activate 

void set_led(int input_count, int size)
{
    for (int i = 0; i < input_count; ++i)
    {
        if (input_num[i] == 0)
            set_on_zero(i, size);
        else if (input_num[i] == 1)
            set_on_one(i, size);
        else if (input_num[i] == 2)
            set_on_two(i, size);
        else if (input_num[i] == 3)
            set_on_three(i, size);
        else if (input_num[i] == 4)
            set_on_four(i, size);
        else if (input_num[i] == 5)
            set_on_five(i, size);
        else if (input_num[i] == 6)
            set_on_six(i, size);
        else if (input_num[i] == 7)
            set_on_seven(i, size);
        else if (input_num[i] == 8)
            set_on_eight(i, size);
        else if (input_num[i] == 9)
            set_on_nine(i, size);
    }
}

int main (){
    
    int input_count = 0;
    int size;


    printf("LCD calculator display.\n");
    printf("Enter input: ");
    char ch;
    while ((ch=getchar())!='\n')  
    {   
       if (ch==' ')
        continue;
       if(!is_digit(ch))
       {
            printf("Invalid input!\n");
            return 1;
       }
       input_num[input_count] = ch - '0';
       input_count++;
    }
    printf("Enter size: ");
    
    if(scanf("%d",&size) != 1)
    {
        printf("Invalid input!\n");
        return 1;
    }

//Set blank
    init_led(input_count,size);
//Set which number to dislplay
    set_led(input_count, size);
//Dsiplay all numbers 
    display_lcd(input_count,size);




    return 0;
}

【问题讨论】:

  • 只是一个友好的提示:你确实不想想要调制你的代码(查找它),但是modularise 它。 :-)。
  • 这不是咨询服务。请阅读一本好的 C 书。如果您有特定问题,请显示minimal reproducible example。在此之前:请参阅How to Ask

标签: c module header


【解决方案1】:

你的想法是你 a) 通过将代码放在一个单独的 .c 文件和一个随附的 .h 文件中来共享代码,这样许多其他 .c 文件可以使用你的模块的功能和/或 b) 你隐藏在 .c 文件中实现一些复杂代码,因此使用模块不必知道实现。

在您的情况下,您将制作一个包含所有 LED 功能的 .c 文件和一个包含所有 LED 功能原型的 .h 文件,然后在您的主 .c 文件中#include .h 文件。您还应该在 LED .c 文件中包含 LED .h 文件,以便编译器告诉您定义是否与实现不同(不应该)。

您还应该将全局 LED 变量和常量作为 extern 放在 .h 文件中,并在 LED .c 文件中分配变量。

不要忘记将 LED .c 文件添加到您的 makefile 中,以便编译。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-12
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多