Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include<math.h>
- enum Type{INT,CHAR};
- int isDigit(char c){return '0'<=c && c<='9';}
- int isAlpha(char c){return ('a'<=c && c<='z')||('A'<=c && c<= 'Z');}
- void ignoreTillNewline(File*in){while getc(in)!='\n' && ! feof(in));
- }
- int readArray(FILE *in,char *nums,int*nums_size,enumType*arr_type)
- {char c;
- int i=0;
- if((c=getc(in)=='[')
- c=getc(in);
- while(c!=']' && c!='\n' && ! feof(in))
- {
- if(c=='\")
- *arr_type=CHAR;
- else if (isDigit(c) || c=='-')
- *arr_type=INT;
- else
- {
- if(c !='\n')
- ignoreTillNewLine(in);
- return 0;
- }
- }
- if(*arr_type==CHAR)
- c=gets(in);
- if(isAlpha(c))
- {
- nums[i++] =c;
- c=gets(in);
- if(c!='\")
- {
- if(c !='\n')
- ignoreTillNewLine(in);
- }
- }
- else
- {
- if (c!='\n')
- ignoreTillNewLine(in);
- return 0;
- }
- }
- if (*arr_type==CHAR)
- c=getc(in)
- if(isAlpha(c)=
- {
- nums[i++] = c;
- c = getc(in);
- if (c !='\")
- {
- if(c !='\n')
- ignoreTillNewLine(in);
- }
- }
- else
- {
- if (c!='\n')
- ignoreTillNewLine(in);
- return 0;
- }
- }
- else
- {
- if (c == '-')
- {
- nums[i++] = -(getc(in-'0');
- }
- else if(isDigit(c))
- {
- nums[i++] = c-'0';
- }
- else
- {
- if (c!='\n')
- ignoreTillNewLine(in);
- return 0;
- }
- }
- c=getc(in);
- if (c==',') c=getc(in);
- if (c=='') c=getc(in);
- }
- *num_size = i;
- ignoreTillNewLine(in);
- return 1;
- }
- if (c !='\n')
- ignoreTillNewLine(in);
- return 0;
- }
- void printPiwerSet(FILE *out,char*nums,int nums_size,enum Type type)
- {
- fprint(out,"[[]");
- for (size_t i=1;i<pow(2,nums_size);i++)
- {
- fprint(out,",[");
- for(int j=0; j<nums_size;j++)
- {
- if(i & (1<<j))
- {
- fprintf(out,(type == INT)?"%d",:"%c",nums[i]);
- }
- }
- fseek(out,-2,SEEK_CUR);
- fprintf(out,"]");
- }
- fprintf(out,"]\n);
- }
- int main()
- {
- FILE *in = fopen("input.txt","r");
- FILE *out = fopen("output.txt","w");
- char nums[7];
- int nums_size = 0;
- enum Type arr_type;
- while(!fof(in))
- {
- if (readArray(in,nums,&nums_size,&arr_type))
- {
- printPowerSet(std out,nums_size,arr_type);
- printPowerSet(out,nums,nums_size,arr_type);
- }
- else
- {
- printf("Error,input is not in correct firmat.Use [] with elements " "seprated by,E.g.,[1,2,3]\n");
- fprint(out,"Error,input is not in correct firmat.Use [] with elements " "seprated by,E.g.,[1,2,3\n");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement