943,614 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 31285
  • C RSS
Mar 8th, 2006
0

a simple C program to create/open/write/close files generaing basic arithmetic ops

Expand Post »
/* 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++);}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bofarull is offline Offline
9 posts
since Mar 2006
Mar 8th, 2006
0

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 9th, 2006
0

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bofarull is offline Offline
9 posts
since Mar 2006
Mar 9th, 2006
0

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?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 10th, 2006
0

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Mar 10th, 2006
0

Re: a simple C program to create/open/write/close files generaing basic arithmetic ops

Well said!
Reputation Points: 13
Solved Threads: 1
Junior Poster in Training
AhmedHan is offline Offline
71 posts
since Apr 2005
Mar 10th, 2006
0

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...
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Mar 13th, 2006
0

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:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bofarull is offline Offline
9 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: C double data type emulation library
Next Thread in C Forum Timeline: @@ ListFuns.c @@





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC