我将代码写到一个文件中,然后编译并运行,但我应该把它分成称为模块化代码的片段,你将每个函数放在单独的 .c 文件中,.h 表示包含和原型,但我很难是时候这样做了,因为我的 .h 文件中不应该有任何变量。我将如何在每个函数文件中正确传递我的指针
这是我的代码:
---------------------------------------------------------main.c
#include "my.h"
int *pointerNUM; //<----------------------------------------------here is the pointer
// main function
int main(int argc, char *argv[])
{
// To store the numbers of number in file
int numberOfNUM;
// Calls the function to read numbers and stores the length
numberOfNUM = readFile(argc, argv);
// Calls the function to displays the numbers before sorting
printf("\n Before sort : ");
show(numberOfNUM);
// Calls the function for sorting
insertSORT(numberOfNUM);
// Calls the function to displays the numbers after sorting
printf("\n After sort: ");
show(numberOfNUM);
}// End of main function
--------------------------------------------------------------insertSORT.c
#include "my.h"
// Function for insertion sort
void insertSORT(int numberOfNUM)
{
int x, key, y;
// Loops numberOfNUM times
for (x = 1; x < numberOfNUM; x++)
{
// Stores i index position data in key
key = pointerNUM[x];
// Stores x minus one as y value
y = x - 1;
/*
Move elements of pointerNUM[0..x - 1], that are greater than key,
to one position ahead of their current position
*/
while (y >= 0 && pointerNUM[y] > key)
{
// Stores pointerNUM y index position value at pointerNUM y next index position
pointerNUM[y + 1] = pointerNUM[y];
// Decrease the y value by one
y = y - 1;
}// End of while
// Stores the key value at pointerNUM y plus one index position
pointerNUM[y + 1] = key;
}// End of for loop
}// End of function
-------------------------------------------------------------readFile.c
#include "my.h"
// Read in the parts file and returns the length
int readFile(int argc, char *argv[])
{
// File pointer
FILE *fptr;
// numberOfNUM for number of numbers
// cntVAR for counter variable
int numberOfNUM, cntVAR;
// Open the file for reading
fptr = fopen(argv[1], "r"); // "r" for read
// Check that it opened properly
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}// End of if condition
// Reads number of numbers in the file
fscanf(fptr, "%d", &numberOfNUM);
// Dynamically allocates memory to pointer pointerNUM
pointerNUM = (int *) calloc(numberOfNUM, sizeof(int));
// Loops numberOfNUM times
for(cntVAR = 0; cntVAR < numberOfNUM; cntVAR++)
// Reads each number and stores it in array
fscanf(fptr, "%d", &pointerNUM[cntVAR]);
// Returns the length of the numbers
return numberOfNUM;
fclose(fptr);
}// End of function
----------------------------------------------------------------------show.c
#include "my.h"
// Function to show numbers
void show(int numberOfNUM)
{
int cntVAR;
// Loops numberOfNUM times
for(cntVAR = 0; cntVAR < numberOfNUM; cntVAR++)
// Displays each number
printf("%4d, ", pointerNUM[cntVAR]);
}// End of function
-----------------------------------------------------------------Now my my.h
#include
#include
//prototypes
void insetSORT(int numberOfNUM);
int readFile(int argc, char *argv[]);
void show(int numberOfNUM);
-----------------------------------------------------------------