I am writing a program in C please Help!!, The program is working the way I want
ID: 3708243 • Letter: I
Question
I am writing a program in C please Help!!,
The program is working the way I want it to, but I am supposed to have each function in its own separate file (one function per file) and each function should use an include statement for my header file. I also want to use one appropriate header file for all my structures and for the prototypes of the functions. Also I am not supposed to have any global variables in my header file.
Please help I am a bit confused about the meaning of these instructions and how to implement them.
Thank you
#include <stdio.h>
#include <stdlib.h>
struct list
{
char vertex, adjacent;
struct list * next;
};
typedef struct list List;
List * head = NULL;
void createlink(char ch1, char ch2)
{
List * th;
if (head == NULL)
{
head = (List * ) malloc(sizeof(List));
head-> vertex = ch1;
head -> adjacent = ch2;
head -> next = NULL;
return;
}
th = head;
while (th -> next != NULL)
th = th -> next;
th -> next = (List * ) malloc(sizeof(List));
th -> next -> vertex = ch1;
th -> next -> adjacent = ch2;
th -> next -> next = NULL;
return;
}
void displayadj(char ch)
{
List * th = head;
while (th != NULL)
{
if (th -> vertex == ch)
printf("%c ", th -> adjacent); //if a vertex match print its adjacent
th = th -> next;
}
}
int main(int argc, char** argv)
{
FILE * f1;
if(argc == 2){
f1 = fopen(argv[1], "r");
char ch, ch1;
int array[26] = {
0
};
if (f1 == NULL)
{
printf("error no such file ");
return 0;
}
while ((ch = fgetc(f1)) != EOF)
{
if (ch >= 'A' && ch <= 'Z')
if (array[ch - 'A'] == 0)
{
array[ch - 'A'] = 1;
}
}
f1 = fopen(argv[1], "r");
int num = 0;
char ch2;
//printf("i'm displaying the file now ");
while ((ch = fgetc(f1)) != EOF)
{
if (ch >= 'A' && ch <= 'Z')
{
num++;
if (num % 2 == 0) //if a char between A and Z comes again in a line it takes as ch1 adjacent
{
ch2 = ch;
createlink(ch1, ch2); //create link as vetex ch1,adjacent ch2
num = 0;
//printf("%c %c ",ch1,ch2);
} else
{
ch1 = ch;
}
}
}
int i = 0;
printf("vetex: adjacent vertices: ");
for (; i < 26; i++)
{
if (array[i] == 1)
{
ch = 'A' + i;
printf("%c ", ch);
displayadj(ch);
printf(" ");
}
}
}
else{
//printf("Please provide one command line argument");
}
}
Explanation / Answer
c code:
List.h
#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED
//structure list
struct list
{
char vertex, adjacent;
struct list * next;
};
typedef struct list List;
#endif // LIST_H_INCLUDED
link.c
#include <stdio.h>
#include <stdlib.h>
#include "List.h"
//create link function
void createlink(char ch1, char ch2, List *head)
{
List * th;
if (head == NULL)
{
head = (List * ) malloc(sizeof(List));
head-> vertex = ch1;
head -> adjacent = ch2;
head -> next = NULL;
return;
}
th = head;
while (th -> next != NULL)
th = th -> next;
th -> next = (List * ) malloc(sizeof(List));
th -> next -> vertex = ch1;
th -> next -> adjacent = ch2;
th -> next -> next = NULL;
return;
}
display.c
#include <stdio.h>
#include <stdlib.h>
#include "List.h"
//display function
void displayadj(char ch, List *head)
{
List * th = head;
while (th != NULL)
{
if (th -> vertex == ch)
printf("%c ", th -> adjacent); //if a vertex match print its adjacent
th = th -> next;
}
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "List.h"
//function declarations
void createlink(char ch1, char ch2, List *head);
void displayadj(char ch, List *head);
//main functions
int main(int argc, char** argv)
{
List * head = NULL;
FILE * f1;
if(argc == 2){
f1 = fopen(argv[1], "r");
char ch, ch1;
int array[26] = {
0
};
if (f1 == NULL)
{
printf("error no such file ");
return 0;
}
while ((ch = fgetc(f1)) != EOF)
{
if (ch >= 'A' && ch <= 'Z')
if (array[ch - 'A'] == 0)
{
array[ch - 'A'] = 1;
}
}
f1 = fopen(argv[1], "r");
int num = 0;
char ch2;
//printf("i'm displaying the file now ");
while ((ch = fgetc(f1)) != EOF)
{
if (ch >= 'A' && ch <= 'Z')
{
num++;
if (num % 2 == 0) //if a char between A and Z comes again in a line it takes as ch1 adjacent
{
ch2 = ch;
createlink(ch1, ch2 , head); //create link as vetex ch1,adjacent ch2
num = 0;
//printf("%c %c ",ch1,ch2);
} else
{
ch1 = ch;
}
}
}
int i = 0;
printf("vetex: adjacent vertices: ");
for (; i < 26; i++)
{
if (array[i] == 1)
{
ch = 'A' + i;
printf("%c ", ch);
displayadj(ch,head);
printf(" ");
}
}
}
else{
//printf("Please provide one command line argument");
}
}
//i have given the header file and .c files, since i dont know the file input, i cannot give the output file.. . for any clarification or modification, please do comments
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.