![]() |
| ||
| a simple C program to create/open/write/close files generaing basic arithmetic ops /* compiler used: */ /* simple application to generate basic arithmetic operations */ /* March/3rd/2006, John Bofarull */ #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<time.h> #define RAND__MAX 4294967295 #define OPS 40 /* how many operations per file */ /* poke about these 8 constants to harden or soften tests */ #define SDL 4 /* sums level */ #define RDL 4 /* differences level */ #define PDL_1 27 /* products level */ #define PDL_2 153 #define PDL 37768 #define DDL_1 398 /* division thresholds */ #define DDL_2 12 void insert_txt(char *, char *); void main() {FILE *fp; char file_name[12]; char s_txt[]=".txt"; int j=0; unsigned long int k,q,m,n; unsigned long int g; char c; /* which operation */ for(;;) {randomize(); printf("\nAdd/Sub/Prod/Div (A/S/P/D) (Q=quit program): "); c=getche(); if (c=='a') c='A'; if (c=='s') c='S'; if (c=='p') c='P'; if (c=='d') c='D'; if ((c=='q')||(c=='Q')) c=0x1b; j=0; switch(c) { {case'A': c='\0'; printf("\nWhich file to write sums in (8 chars max/.txt by default)?"); scanf("%s",file_name); insert_txt(file_name,s_txt); /* no need to type .txt at the end of file name */ if((fp=fopen(file_name,"w"))==NULL) printf("\nFile not found"); while(j<OPS) {fprintf(fp,"\n%3d .- ",j+1); k=rand();q=rand();m=rand();n=rand(); /* avoiding operations with 0 or 1 */ while((k==0)||(k==1)||(k==10)||(k==100)||(k==1000)) k=rand(); while((q==k)||(q==0)||(q==1)||(q==10)||(q==100)||(q==1000)) q=rand(); while((m==k)||(m==q)||(m==0)||(m==1)||(m==10)||(m==100)||(m==1000)) m=rand(); while((n==k)||(n==q)||(n==m)||(n==0)||(n==1)||(n==10)||(n==100)||(n==1000)) n=rand(); k=k/SDL;q=q/SDL;m=m/SDL;n=n/SDL; /* corrections */ g=k+q+m+n; /* operation */ fprintf(fp,"%5u + ",k); fprintf(fp,"%5u + ",q); fprintf(fp,"%5u + ",m); fprintf(fp,"%5u",n); fprintf(fp," = %15u ",g); /* 15 blank chars to the right */ j++; } fclose(fp); break;} /* case Addition over */ {case 'S': c='\0'; printf("\nWhich file to write substractions in (8 chars max/.txt by default)?"); scanf("%s",file_name); insert_txt(file_name,s_txt); /* no need to write .txt */ if((fp=fopen(file_name,"w"))==NULL) printf("\nFile not found"); while(j<OPS) {fprintf(fp,"\n%3d .- ",j+1); k=rand();q=rand(); while((k==0)||(k==1)||(k==10)||(k==100)||(k==1000)) k=rand(); while((q==k)||(q==0)||(q==1)||(q==10)||(q==100)||(q==1000)) q=rand(); k=k/SDL;q=q/RDL; if(k>q) {g=k-q; fprintf(fp,"%5u - ",k); fprintf(fp,"%5u",q); fprintf(fp," = %6u",g); } /* 15 blank chars to the right */ else {g=q-k; fprintf(fp,"%5u - ",q); fprintf(fp,"%5u",k); fprintf(fp," = %6u",g); } j++;} fclose(fp); break; } /* case Substraction over */ {case 'P': c='\0'; printf("\nWhich file to write divisions in (8 chars max/.txt by default)?"); scanf("%s",file_name); insert_txt(file_name,s_txt); /* no need to write .txt */ if((fp=fopen(file_name,"w"))==NULL) printf("\nFile not found"); while(j<OPS) {fprintf(fp,"\n%3d .- ",j+1); k=rand();q=rand(); k=k/PDL_1;q=q/PDL_2; while((k==0)||(k==1)||(k==10)||(k==100)||(k==1000)) k=rand(); while((q==k)||(q==0)||(q==1)||(q==10)||(q==100)||(q==1000)) q=rand(); g=k*q; fprintf(fp,"%5u * ",k); fprintf(fp,"%5u",q); fprintf(fp," = %15u",g); j++;} fclose(fp); break;} /* case Product over */ {case 'D': c='\0'; printf("\nWhich file to write divisions in (8 chars max/.txt by default)?"); scanf("%s",file_name); insert_txt(file_name,s_txt); /* no need to write .txt */ if((fp=fopen(file_name,"w"))==NULL) printf("\nFile not found"); while(j<OPS) {fprintf(fp,"\n%3d .- ",j+1); k=rand();q=rand(); k=k/PDL_1;q=q/PDL_2; while((k==0)||(k==1)||(k==10)||(k==100)||(k==1000)) k=rand(); while((q==k)||(q==0)||(q==1)||(q==10)||(q==100)||(q==1000)) q=rand(); while((k*q)>=65333) {k=rand();q=rand(); k=k/PDL_1;q=q/PDL_2; while((k==0)||(k==1)||(k==10)||(k==100)||(k==1000)) k=rand(); while((q==k)||(q==0)||(q==1)||(q==10)||(q==100)||(q==1000)) q=rand();} g=k*q; fprintf(fp,"%5u / ",g); fprintf(fp,"%5u",q); fprintf(fp," = %5u",k); j++;} fclose(fp); break;} /* case Division over */ case 0x1b: {puts("\nEnd of program"); exit(0);} default: puts("\nOption not Valid"); } /* switch terminated */ } /* for terminated */ } /* main terminated */ /****************** Functions Definitions *********************/ void insert_txt(char *s1, char *s2) /* adding .txt extension to result file names */ {while (*s1) s1++; while (*s1++=*s2++);} |
| ||
| Re: a simple C program to create/open/write/close files generaing basic arithmetic ops I'm impressed. That's a beautiful example of how not to program in C. |
| ||
| Re: a simple C program to create/open/write/close files generaing basic arithmetic ops :)) I like your comment. Honesty is a precious asset. Bofarull |
| ||
| Re: a simple C program to create/open/write/close files generaing basic arithmetic ops >I like your comment. Honesty is a precious asset. I'm glad you approve. You'll get nothing but honesty from me. ;) Anyway, did you have a question that you needed help with? |
| ||
| Re: a simple C program to create/open/write/close files generaing basic arithmetic ops It's certainly an excellent example of not following the rules http://www.daniweb.com/techtalkforum...cement8-3.html A lot of people just find other posts to read when presented with pages of unformatted code. |
| ||
| Re: a simple C program to create/open/write/close files generaing basic arithmetic ops Well said! |
| ||
| Re: a simple C program to create/open/write/close files generaing basic arithmetic ops Faced with the choice of code and no question vs. a question with no code, I prefer the former... ;) |
| ||
| Re: a simple C program to create/open/write/close files generaing basic arithmetic ops There was no intention to ask a single question about this specific code. I am aware of shorter ways to do the same, I am aware that I am not as skilled as most around in this forum, and the code is here just for any other beginner who would like to use it. I have plenty of questions but I ask them at the right time. I am also looking for C literature resources. I'm not the kind of guy hanging around others to learn. Only when jammed at a given point I will use artillery and ask you, hoggy doggy? Nevertherless I sincerely appreciate every single comment here appended. bofarull :mrgreen: |
| All times are GMT -4. The time now is 9:12 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC