Advertisement
mayankjoin3

end

Jul 11th, 2021
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include<math.h>
  3.  
  4. enum Type{INT,CHAR};
  5. int isDigit(char c){return '0'<=c && c<='9';}
  6. int isAlpha(char c){return ('a'<=c && c<='z')||('A'<=c && c<= 'Z');}
  7.  
  8. void ignoreTillNewline(File*in){while getc(in)!='\n' && ! feof(in));
  9. }
  10. int readArray(FILE *in,char *nums,int*nums_size,enumType*arr_type)
  11. {char c;
  12. int i=0;
  13.  
  14. if((c=getc(in)=='[')
  15. c=getc(in);
  16. while(c!=']' && c!='\n' && ! feof(in))
  17. {
  18. if(c=='\")
  19. *arr_type=CHAR;
  20. else if (isDigit(c) || c=='-')
  21. *arr_type=INT;
  22. else
  23. {
  24. if(c !='\n')
  25. ignoreTillNewLine(in);
  26. return 0;
  27. }
  28. }
  29. if(*arr_type==CHAR)
  30. c=gets(in);
  31. if(isAlpha(c))
  32. {
  33. nums[i++] =c;
  34. c=gets(in);
  35. if(c!='\")
  36. {
  37. if(c !='\n')
  38. ignoreTillNewLine(in);
  39. }
  40. }
  41. else
  42. {
  43. if (c!='\n')
  44. ignoreTillNewLine(in);
  45. return 0;
  46. }
  47. }
  48. if (*arr_type==CHAR)
  49. c=getc(in)
  50. if(isAlpha(c)=
  51. {
  52. nums[i++] = c;
  53. c = getc(in);
  54. if (c !='\")
  55. {
  56. if(c !='\n')
  57. ignoreTillNewLine(in);
  58. }
  59. }
  60. else
  61. {
  62. if (c!='\n')
  63. ignoreTillNewLine(in);
  64. return 0;
  65. }
  66. }
  67. else
  68. {
  69. if (c == '-')
  70. {
  71. nums[i++] = -(getc(in-'0');
  72. }
  73. else if(isDigit(c))
  74. {
  75. nums[i++] = c-'0';
  76. }
  77. else
  78. {
  79. if (c!='\n')
  80. ignoreTillNewLine(in);
  81. return 0;
  82. }
  83. }
  84. c=getc(in);
  85. if (c==',') c=getc(in);
  86. if (c=='') c=getc(in);
  87. }
  88. *num_size = i;
  89. ignoreTillNewLine(in);
  90. return 1;
  91. }
  92. if (c !='\n')
  93. ignoreTillNewLine(in);
  94. return 0;
  95. }
  96. void printPiwerSet(FILE *out,char*nums,int nums_size,enum Type type)
  97. {
  98. fprint(out,"[[]");
  99. for (size_t i=1;i<pow(2,nums_size);i++)
  100. {
  101. fprint(out,",[");
  102. for(int j=0; j<nums_size;j++)
  103. {
  104. if(i & (1<<j))
  105. {
  106. fprintf(out,(type == INT)?"%d",:"%c",nums[i]);
  107. }
  108. }
  109. fseek(out,-2,SEEK_CUR);
  110. fprintf(out,"]");
  111. }
  112. fprintf(out,"]\n);
  113. }
  114. int main()
  115. {
  116. FILE *in = fopen("input.txt","r");
  117. FILE *out = fopen("output.txt","w");
  118. char nums[7];
  119. int nums_size = 0;
  120. enum Type arr_type;
  121. while(!fof(in))
  122. {
  123. if (readArray(in,nums,&nums_size,&arr_type))
  124. {
  125. printPowerSet(std out,nums_size,arr_type);
  126. printPowerSet(out,nums,nums_size,arr_type);
  127. }
  128. else
  129. {
  130. printf("Error,input is not in correct firmat.Use [] with elements " "seprated by,E.g.,[1,2,3]\n");
  131. fprint(out,"Error,input is not in correct firmat.Use [] with elements " "seprated by,E.g.,[1,2,3\n");
  132. }
  133. }
  134. return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement