#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//
void createFile(char *filc)
{
FILE *fptr;
errno_t err;
int i=1, xn;
float y,x,a3,a2,a1,a0,xb,xe;
printf("\nInput a3, a2, a1, a0: ");
scanf_s("%f %f %f %f", &a3,&a2,&a1,&a0);
printf("\nInput xstart, xend, and number of points: ");
scanf_s("%f %f %d", &xb, &xe, &xn);
if (err = fopen_s(&fptr,filc,"w") != 0)
{
printf("File Error: %d", err);
system("PAUSE");
exit(1);
}
fprintf(fptr,"Evaluation of Function:");
fprintf(fptr,"\n");
fprintf(fptr,"\n%.4fX^3 + %.4fX^2 + %.4fX + %.4f\n",a3,a2,a1,a0);
fprintf(fptr,"\nx\ty\n");
for (i = 0; i <= xn; i++)
{
x = xb + (i/float(xn)) * (xe - xb);
y=(float)(a3*pow(x,3)+a2*pow(x,2)+a1*x+a0);
fprintf(fptr,"\n%.4f\t%.4f",x,y);
}
fclose(fptr);
return;
}
//
void readFile(char *filr)
{
int i = 0;
FILE *fptr;
errno_t err;
char line1[80];
if (err = fopen_s(&fptr,filr,"r") != 0)
{
printf("File Error: %d", err);
system("PAUSE");
exit(1);
}
printf("\n");
while (fgets(line1,80,fptr) != NULL)
{
printf("%s",line1);
}
printf("\n");
fclose(fptr);
return;
}
//
int main(void)
{
char sel;
char ch = 'a', filnam[20];
//menu
MENU:
printf("Select One of the Following Options:\n");
printf("\nc = Create a Polynomial File\n");
printf("\nr = Read the Polynomial File\n");
printf("\nt = Close Program\n");
printf("\nYour Selection?__");
scanf_s("%c",&sel,1);
//act upon selection
switch(sel){
case 'c':
printf("\nWhat is the File Name?__");
scanf_s("%s",filnam,20);
createFile(filnam);
printf("\nNew File Created!\n");
system("PAUSE");
while((ch = getchar()) != '\n' && ch != EOF);
break;
case 'r':
printf("\nWhat is the File Name?__");
scanf_s("%s",filnam,20);
readFile(filnam);
printf("\nFile Read!\n");
system("PAUSE");
while((ch = getchar()) != '\n' && ch != EOF);
break;
case 't':
printf("\nThis Application will now close.\nBye!\n");
goto FINISH;
default:
printf("\nNot a valid selection.\n");
system("PAUSE");
while((ch = getchar()) != '\n' && ch != EOF);
break;
}
system("CLS");
goto MENU;
FINISH:
system("PAUSE");
return 0;
}