954,123 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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
#include
#include
#include

#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(jq)
{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=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++);}

bofarull
Newbie Poster
9 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

I'm impressed. That's a beautiful example of how not to program in C.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

:))
I like your comment. Honesty is a precious asset.
Bofarull

bofarull
Newbie Poster
9 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

>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?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

It's certainly an excellent example of not following the rules
http://www.daniweb.com/techtalkforums/announcement8-3.html

A lot of people just find other posts to read when presented with pages of unformatted code.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Well said!

AhmedHan
Junior Poster in Training
71 posts since Apr 2005
Reputation Points: 13
Solved Threads: 1
 

Faced with the choice of code and no question vs. a question with no code, I prefer the former... ;)

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

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:

bofarull
Newbie Poster
9 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You