Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- /*--- structure that defines a node ---*/
- struct Node{
- char data;
- struct Node *next;
- struct Node *previous;
- };
- typedef struct Node NODE;
- /*--- Function Prototypes ---*/
- void ReadFromFile(NODE**, char*);
- static void WriteInt(char*, int);
- static void WriteChar(char*, char);
- extern NODE* MakeNode(char);
- extern void InsertEnd(NODE**, char);
- static char Decode(NODE**, int);
- static int Code(NODE**, char);
- static int ListLength(NODE*);
- int main(int argc, char **argv){
- if(argc <= 4){
- printf("Invalid number of parameters");
- fprintf(stderr, "Error: %d: %s\n", errno, strerror(errno));
- }
- NODE *TopNode = NULL;
- fclose(fopen(argv[2], "w"));
- int i;
- printf("Parameter listing: \n");
- for(i = 1; i <argc; i++){
- printf(" %i: %s\n", i, argv[i]);
- }
- ReadFromFile(&TopNode, argv[1]);
- if(*argv[3] == 'D'){
- int i;
- char c;
- for(i = 4; i < argc; i++){
- c = Decode(&TopNode, atoi(argv[i]));
- WriteChar(argv[2], c);
- }
- }
- if(*argv[3] == 'C'){
- char* StrCode = argv[4];
- int i;
- int PositiveTurn;
- int NegativeTurn;
- for(i = 0; i < strlen(StrCode); i++){
- PositiveTurn = Code(&TopNode, StrCode[i]);
- NegativeTurn = PositiveTurn - ListLength(TopNode);
- if(PositiveTurn <= abs(NegativeTurn)){
- printf("%i\n", PositiveTurn);
- WriteInt(argv[2], PositiveTurn);
- }
- else{
- printf("%i\n", NegativeTurn);
- WriteInt(argv[2], NegativeTurn);
- }
- }
- }
- }
- /*--- Opens the file and reads the values
- Inputs: Double pointer of TopNode and text file reference
- Outputs: Void ---*/
- void ReadFromFile(NODE **TopNode, char *FileName){
- FILE *fp;
- char ch;
- fp = fopen(FileName, "r");
- if (fp == NULL){
- printf("Cannot open file for read access\n");
- fprintf(stderr, "Error: %d: %s\n", errno, strerror(errno));
- exit(1);
- }
- printf("\nCoding wheel values:\n ");
- while (!feof(fp)){
- ch = fgetc(fp);
- if (ch != 10 && ch != -1){
- printf("%c", ch);
- InsertEnd(TopNode, ch);
- }
- }
- printf("\n\n");
- fclose(fp);
- }
- /*---Writes Integer values into a text file
- Inputs: Pointer of a file name, integer value
- Outputs: Void ---*/
- static void WriteInt(char *FileName, int num){
- FILE *fp;
- char ch;
- fp = fopen(FileName, "a");
- if (fp == NULL){
- printf("Cannot open file for write access\n");
- fprintf(stderr, "Error: %d: %s\n", errno, strerror(errno));
- exit(1);
- }
- fprintf(fp, "%i\n", num);
- }
- static void WriteChar(char *FileName, char data){
- FILE *fp;
- char ch;
- fp = fopen(FileName, "a");
- if (fp == NULL){
- printf("Cannot open file for write access\n");
- fprintf(stderr, "Error: %d: %s\n", errno, strerror(errno));
- exit(1);
- }
- fprintf(fp, "%c", data);
- }
- /*---Writes character values into a text file
- Inputs: Pointer of a file name, character value
- Outputs: Void ---*/
- extern NODE* MakeNode(char data){
- NODE * NewNode = malloc(sizeof(*NewNode));
- if (NewNode != NULL){
- NewNode -> data = data;
- NewNode -> next = NULL;
- NewNode -> previous = NULL;
- }
- return NewNode;
- }
- /*---Creates the doubly linked list
- Inputs: Double pointer of TopNode, character value
- Outputs: Void ---*/
- extern void InsertEnd(NODE **TopNode, char data){
- if (*TopNode == NULL){
- NODE *NewNode = MakeNode(data);
- NewNode -> data = data;
- NewNode -> next = NewNode -> previous = NewNode;
- *TopNode = NewNode;
- return;
- }
- NODE *LastNode = (*TopNode) -> previous;
- NODE *NewNode = MakeNode(data);
- NewNode -> data = data;
- NewNode -> next = *TopNode;
- (*TopNode) -> previous = NewNode;
- NewNode -> previous = LastNode;
- LastNode -> next = NewNode;
- }
- /*---Decodes the given text
- Inputs: Double pointer of TopNode, integer value
- Outputs: a character value ---*/
- static char Decode(NODE **TopNode, int num){
- int n;
- NODE* NewNode = *TopNode;
- if (num >= 0){
- for(n = 0; n < num; n++){
- NewNode = NewNode -> next;
- }
- printf("%c\n", NewNode -> data);
- }
- else{
- for(n = 0; n > num; n--){
- NewNode = NewNode -> previous;
- }
- printf("%c\n", NewNode -> data);
- }
- *TopNode = NewNode;
- return NewNode -> data;
- }
- /*---Codes the given text
- Inputs: Double pointer of TopNode, character value
- Outputs: an Integer value ---*/
- static int Code(NODE **TopNode, char data){
- int n = 0;
- NODE* NewNode = *TopNode;
- while(NewNode -> next != *TopNode){
- if(NewNode -> data != data){
- NewNode = NewNode -> next;
- n++;
- }
- else{
- break;
- }
- }
- *TopNode = NewNode;
- return n;
- }
- /*---Finds the length of the list
- Inputs: Pointer of TopNode
- Outputs: an Integer value ---*/
- static int ListLength(NODE *TopNode){
- int n = 1;
- NODE* NewNode = TopNode;
- while(NewNode -> next != TopNode){
- NewNode = NewNode -> next;
- n++;
- }
- return n;
- }
Add Comment
Please, Sign In to add comment