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

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2006
Posts: 9
Reputation: bofarull is an unknown quantity at this point 
Solved Threads: 0
bofarull bofarull is offline Offline
Newbie Poster

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++);}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,734
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 738
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #2
Mar 8th, 2006
I'm impressed. That's a beautiful example of how not to program in C.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 9
Reputation: bofarull is an unknown quantity at this point 
Solved Threads: 0
bofarull bofarull is offline Offline
Newbie Poster

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

 
0
  #3
Mar 9th, 2006
)
I like your comment. Honesty is a precious asset.
Bofarull
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,734
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 738
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #4
Mar 9th, 2006
>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?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 71
Reputation: AhmedHan is an unknown quantity at this point 
Solved Threads: 1
AhmedHan's Avatar
AhmedHan AhmedHan is offline Offline
Junior Poster in Training

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

 
0
  #6
Mar 10th, 2006
Well said!
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

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

 
0
  #7
Mar 10th, 2006
Faced with the choice of code and no question vs. a question with no code, I prefer the former...
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 9
Reputation: bofarull is an unknown quantity at this point 
Solved Threads: 0
bofarull bofarull is offline Offline
Newbie Poster

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:
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC