Code:
//---------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#define PI 3.14159265
#pragma hdrstop
//---------------------------------------------------------------------------
//Functions Prototypes.
double mFunction(double b, double gamma)
{
return b * cos(gamma * PI / 180);
}
double nFunction(double c, double beta)
{
return c * cos(beta * PI / 180);
}
double gFunction(double b, double gamma)
{
return b * sin(gamma * PI / 180);
}
double SquareRootT(double c, double n, double h)
{
double R = pow(c,2) - pow(n,2) - pow(h,2);
if(R < 0)
R = -R;
return sqrt(R);
}
double hFunction(double c, double alfa, double beta, double gamma)
{
return (c/ sin(gamma * PI / 180)) * (cos(alfa * PI /180)) - (cos(beta * PI / 180) * cos(gamma * PI / 180));
}
void CreateNewFile(char *oldFile, char *nFile,double a, double b, double c, double alfa, double beta, double gamma)
{
if(!oldFile || !nFile)
{
printf("Error, NULL parametres.\n");
return;
}
else
{
FILE *of = fopen(oldFile, "r");
FILE *nf = fopen(nFile, "w");
//Variables.
double Col1 = 0.0;
double Col2 = 0.0;
double Col3 = 0.0;
//Array to hold the numbers.
double arr[3] = {0.0,0.0,0.0};
if(!of)
{
fprintf(stderr, "Error can not open '%s' file.\n", oldFile);
return;
}
if(!nf)
{
fprintf(stderr, "Error can not open '%s' file.\n", nFile);
return;
}
while(fscanf(of,"%lf\t%lf\t%lf\n", &arr[0], &arr[1], &arr[2]) != EOF)
{
//Weird numbers.
Col1 = a * arr[0] + arr[1] * mFunction(b, gamma) + arr[2] * nFunction(c, beta);
Col2 = arr[1] * gFunction(b, gamma) + arr[2] * hFunction(c, alfa, beta, gamma);
Col3 = arr[2] * SquareRootT(c, nFunction(c, beta), hFunction(c, alfa, beta, gamma));
//Got the numbers write them down to file.
fprintf(nf,"%lf\t%lf\t%lf\n", Col1,Col2,Col3);
//Zero the values for next iteration.
arr[0] = 0.0;
arr[1] = 0.0;
arr[2] = 0.0;
}
fclose(nf);
fclose(of);
}
}
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
CreateNewFile("numbers.txt", "result.txt", 1.234, 3.456, 3.4534, 2.56, 4.78, 8.98);
printf("Hit any key to continue......");
getch();
return 0;
}
//---------------------------------------------------------------------------
If you run the program it will create a new file in the debug folder that will hold the results. Have a look at them and tell me,