我无法通过创建.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 collumns 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;
}
答案 0 :(得分:0)
这个想法是你a)通过将代码放在一个带有.h文件的单独的.c文件中共享代码,这么多其他.c文件可以使用你的模块的功能和/或b)你隐藏了在.c文件中实现一些复杂的代码,因此使用模块不必知道实现。
在您的情况下,您将制作一个包含所有LED功能的.c文件和一个包含所有LED功能原型的.h文件,然后#include主.c文件中的.h文件。您还应该在LED .c文件中包含LED .h文件,以便编译器告诉您定义是否与实现不同(不应该)。
您还应将全局LED变量和常量放在.h文件中extern
,并在LED .c文件中分配变量。
不要忘记将LED .c文件添加到您的makefile中,以便进行编译。