Imran2554

bnr script

Feb 19th, 2020
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5.  
  6.  
  7. /*--- structure that defines a node ---*/
  8.  
  9. struct Node{
  10. char data;
  11. struct Node *next;
  12. struct Node *previous;
  13. };
  14. typedef struct Node NODE;
  15.  
  16. /*--- Function Prototypes ---*/
  17.  
  18. void ReadFromFile(NODE**, char*);
  19. static void WriteInt(char*, int);
  20. static void WriteChar(char*, char);
  21. extern NODE* MakeNode(char);
  22. extern void InsertEnd(NODE**, char);
  23. static char Decode(NODE**, int);
  24. static int Code(NODE**, char);
  25. static int ListLength(NODE*);
  26.  
  27. int main(int argc, char **argv){
  28. if(argc <= 4){
  29. printf("Invalid number of parameters");
  30. fprintf(stderr, "Error: %d: %s\n", errno, strerror(errno));
  31. }
  32. NODE *TopNode = NULL;
  33.  
  34. fclose(fopen(argv[2], "w"));
  35. int i;
  36. printf("Parameter listing: \n");
  37. for(i = 1; i <argc; i++){
  38. printf(" %i: %s\n", i, argv[i]);
  39. }
  40. ReadFromFile(&TopNode, argv[1]);
  41. if(*argv[3] == 'D'){
  42. int i;
  43. char c;
  44. for(i = 4; i < argc; i++){
  45. c = Decode(&TopNode, atoi(argv[i]));
  46. WriteChar(argv[2], c);
  47. }
  48. }
  49. if(*argv[3] == 'C'){
  50. char* StrCode = argv[4];
  51. int i;
  52. int PositiveTurn;
  53. int NegativeTurn;
  54. for(i = 0; i < strlen(StrCode); i++){
  55. PositiveTurn = Code(&TopNode, StrCode[i]);
  56. NegativeTurn = PositiveTurn - ListLength(TopNode);
  57. if(PositiveTurn <= abs(NegativeTurn)){
  58. printf("%i\n", PositiveTurn);
  59. WriteInt(argv[2], PositiveTurn);
  60. }
  61. else{
  62. printf("%i\n", NegativeTurn);
  63. WriteInt(argv[2], NegativeTurn);
  64. }
  65. }
  66. }
  67. }
  68.  
  69. /*--- Opens the file and reads the values
  70. Inputs: Double pointer of TopNode and text file reference
  71. Outputs: Void ---*/
  72.  
  73. void ReadFromFile(NODE **TopNode, char *FileName){
  74. FILE *fp;
  75. char ch;
  76. fp = fopen(FileName, "r");
  77.  
  78. if (fp == NULL){
  79. printf("Cannot open file for read access\n");
  80. fprintf(stderr, "Error: %d: %s\n", errno, strerror(errno));
  81. exit(1);
  82. }
  83.  
  84. printf("\nCoding wheel values:\n ");
  85. while (!feof(fp)){
  86. ch = fgetc(fp);
  87. if (ch != 10 && ch != -1){
  88. printf("%c", ch);
  89. InsertEnd(TopNode, ch);
  90. }
  91. }
  92. printf("\n\n");
  93. fclose(fp);
  94. }
  95.  
  96.  
  97. /*---Writes Integer values into a text file
  98. Inputs: Pointer of a file name, integer value
  99. Outputs: Void ---*/
  100.  
  101. static void WriteInt(char *FileName, int num){
  102. FILE *fp;
  103. char ch;
  104. fp = fopen(FileName, "a");
  105.  
  106. if (fp == NULL){
  107. printf("Cannot open file for write access\n");
  108. fprintf(stderr, "Error: %d: %s\n", errno, strerror(errno));
  109. exit(1);
  110. }
  111. fprintf(fp, "%i\n", num);
  112.  
  113. }
  114.  
  115. static void WriteChar(char *FileName, char data){
  116. FILE *fp;
  117. char ch;
  118. fp = fopen(FileName, "a");
  119.  
  120. if (fp == NULL){
  121. printf("Cannot open file for write access\n");
  122. fprintf(stderr, "Error: %d: %s\n", errno, strerror(errno));
  123. exit(1);
  124. }
  125. fprintf(fp, "%c", data);
  126.  
  127. }
  128.  
  129. /*---Writes character values into a text file
  130. Inputs: Pointer of a file name, character value
  131. Outputs: Void ---*/
  132.  
  133. extern NODE* MakeNode(char data){
  134. NODE * NewNode = malloc(sizeof(*NewNode));
  135. if (NewNode != NULL){
  136. NewNode -> data = data;
  137. NewNode -> next = NULL;
  138. NewNode -> previous = NULL;
  139. }
  140. return NewNode;
  141. }
  142.  
  143. /*---Creates the doubly linked list
  144. Inputs: Double pointer of TopNode, character value
  145. Outputs: Void ---*/
  146.  
  147. extern void InsertEnd(NODE **TopNode, char data){
  148. if (*TopNode == NULL){
  149. NODE *NewNode = MakeNode(data);
  150. NewNode -> data = data;
  151. NewNode -> next = NewNode -> previous = NewNode;
  152. *TopNode = NewNode;
  153. return;
  154. }
  155.  
  156. NODE *LastNode = (*TopNode) -> previous;
  157. NODE *NewNode = MakeNode(data);
  158. NewNode -> data = data;
  159. NewNode -> next = *TopNode;
  160. (*TopNode) -> previous = NewNode;
  161. NewNode -> previous = LastNode;
  162. LastNode -> next = NewNode;
  163. }
  164.  
  165. /*---Decodes the given text
  166. Inputs: Double pointer of TopNode, integer value
  167. Outputs: a character value ---*/
  168.  
  169. static char Decode(NODE **TopNode, int num){
  170. int n;
  171. NODE* NewNode = *TopNode;
  172. if (num >= 0){
  173. for(n = 0; n < num; n++){
  174. NewNode = NewNode -> next;
  175. }
  176. printf("%c\n", NewNode -> data);
  177. }
  178. else{
  179. for(n = 0; n > num; n--){
  180. NewNode = NewNode -> previous;
  181. }
  182. printf("%c\n", NewNode -> data);
  183. }
  184. *TopNode = NewNode;
  185. return NewNode -> data;
  186. }
  187.  
  188. /*---Codes the given text
  189. Inputs: Double pointer of TopNode, character value
  190. Outputs: an Integer value ---*/
  191.  
  192. static int Code(NODE **TopNode, char data){
  193. int n = 0;
  194. NODE* NewNode = *TopNode;
  195. while(NewNode -> next != *TopNode){
  196. if(NewNode -> data != data){
  197. NewNode = NewNode -> next;
  198. n++;
  199. }
  200. else{
  201. break;
  202. }
  203. }
  204. *TopNode = NewNode;
  205. return n;
  206. }
  207.  
  208. /*---Finds the length of the list
  209. Inputs: Pointer of TopNode
  210. Outputs: an Integer value ---*/
  211.  
  212. static int ListLength(NODE *TopNode){
  213. int n = 1;
  214. NODE* NewNode = TopNode;
  215. while(NewNode -> next != TopNode){
  216. NewNode = NewNode -> next;
  217. n++;
  218. }
  219. return n;
  220. }
Add Comment
Please, Sign In to add comment