| | |
a simple C program to create/open/write/close files generaing basic arithmetic ops
![]() |
•
•
Join Date: Mar 2006
Posts: 9
Reputation:
Solved Threads: 0
a simple C program to create/open/write/close files generaing basic arithmetic ops
0
#1 Mar 8th, 2006
/* 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++);}
/* 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
0
#2 Mar 8th, 2006
•
•
Join Date: Mar 2006
Posts: 9
Reputation:
Solved Threads: 0
Re: a simple C program to create/open/write/close files generaing basic arithmetic ops
0
#3 Mar 9th, 2006
Re: a simple C program to create/open/write/close files generaing basic arithmetic ops
0
#4 Mar 9th, 2006
Re: a simple C program to create/open/write/close files generaing basic arithmetic ops
0
#5 Mar 10th, 2006
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.
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
0
#6 Mar 10th, 2006
Re: a simple C program to create/open/write/close files generaing basic arithmetic ops
0
#7 Mar 10th, 2006
•
•
Join Date: Mar 2006
Posts: 9
Reputation:
Solved Threads: 0
Re: a simple C program to create/open/write/close files generaing basic arithmetic ops
0
#8 Mar 13th, 2006
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:
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:
![]() |
Similar Threads
- How to open multiple textfile (Visual Basic 4 / 5 / 6)
Other Threads in the C Forum
- Previous Thread: C double data type emulation library
- Next Thread: @@ ListFuns.c @@
| Thread Tools | Search this Thread |
* ansi api array arrays binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory dynamic fflush file floatingpointvalidation forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux highest histogram homework i/o inches include infiniteloop input intmain() iso keyboard km linked linkedlist linux linuxsegmentationfault list logical_drives looping loopinsideloop. lowest match matrix microsoft mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pdf posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test threads unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi







)