Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>
int main()
{
char *as[100], *bs[100], *cs[100], *Xos[100], *Yos[100], *Zos[100], *filename[20];//the s at the end of the name tell me it is stored as a string.
FILE *recentdata, *EulerData;//pointer to store recent data in text file.
while (1)
{
menu();
//if the user typed in data, it will bring them back to here.
printf("Please enter the data");
printf("A:");
scanf("%99s", as);
printf("B:");
scanf("%99s", bs);
printf("C:");
scanf("%99s", cs);
printf ("%s %s %s\n\n", as, bs, cs);
printf("Please input Xo, Yo and Zo\n");
printf("Xo:");
scanf("%99s", Xos);
printf("Yo:");
scanf("%99s", Yos);
printf("Zo:");
scanf("%99s", Zos);
printf ("%s %s %s\n\n", Xos, Yos, Zos);
recentdata = fopen("recentdata.txt", "w");
fprintf(recentdata, "%s %s %s %s %s %s\n", as, bs, cs, Xos, Yos, Zos);
fclose;
printf ("\n a = %.2s\n b = %.2s\n c = %.2s\nXo = %.2s\nYo = %.2s\nZo = %.2s\n", as, bs, cs, Xos, Yos, Zos);
Sleep(1000);
printf("\nPlease enter the name of the file to store the data in\n");
printf("If it doesnt exist, it will be created.\n\n");
scanf("%s", filename);
printf("\nStoring all Data in file '%s.txt'\n", filename);
Sleep(1000);
printf("Beginning execution of improved Euler...\n\n");
Sleep(1000);
//send Euler function the relevent variables.
Euler(as, bs, cs, Xos, Yos, Zos);
}
return 0;
}
int menu()
{
printf("\nType 'recent' to use the last entered data or:\nInput a, b and c, hitting Enter between each one,\n");
printf("Typing 'q' at any time will bring you back here,\n");
printf("To exit the program, from here type 'exit'.\n\n");
user_command();
return 0;
}
int Euler(t, u, v, x0, y0, z0)
char t, u, v, x0, y0, z0;
{
float x[100], y[100], z[100], dxdt_i, dydt_i, dzdt_i,dx, dy, dz, dxdt_new, dydt_new, dzdt_new, dt = 1;
float a, b, c, Xo, Yo, Zo;
int i;
a=atof(t); //converting strings to floats so that the compiler understands it is a number that can be used mathematically.
b=atof(u);
c=atof(v);
Xo=atof(x0);
Yo=atof(y0);
Zo=atof(z0);
x[0] = Xo;
y[0] = Yo;
z[0] = Zo;
system("cls");
printf ("\nLorenz formulas:\n\n");
printf("dx dy dz \n");
printf("-- = a(y - x) : -- = x(b - z) - y : -- = xy - cz\n");
printf("dt dt dt\n\n\n");
for (i = 0; i<101; i++)
{
dxdt_i = t*(y[i] - x[i]);
dydt_i = x[i]*(u - z[i]) - y[i];
dzdt_i = (x[i]*y[i]) - (v*z[i]);
dx = (t*(y[i] - x[i]))*dt;
dy = (x[i]*(u - z[i]) - y[i])*dt;
dz = ((x[i]*y[i]) - (v*z[i]))*dt;
dxdt_new = t*(y[i] - (x[i] + dx));
dydt_new = x[i]*(u - z[i]) - (y[i] + dy);
dzdt_new = (x[i]*y[i]) - (v*(z[i] + dz));
x[i+1] = x[i] +(dt/2)*(dxdt_i + dxdt_new);
y[i+1] = y[i] +(dt/2)*(dydt_i + dydt_new);
z[i+1] = z[i] +(dt/2)*(dzdt_i + dzdt_new);
printf("dx dy dz\n");
printf("-- = %5.2f : -- = %5.2f : -- = %5.2f\n", dxdt_i, dydt_i, dzdt_i);
printf("dt dt dt\n");
printf("\nnew xgrad = %.3f new ygrad = %.3f new zgrad = %.3f\n\n", dxdt_new, dydt_new, dzdt_new);
printf("\nnew x = %.3f new y = %.3f new z = %.3f\n\n", x[i+1], y[i+1], z[i+1]);
i = i++;
}
}
int user_command()
{
char *options[100];
FILE *recent;
char *af[100], *bf[100], *cf[100], *Xof[100], *Yof[100], *Zof[100];
//function decides what to do based on user input
scanf("%99s", options);
//What to do if user types exit
if(strcmpi(options,"exit") == 0)//compare options to "recent" NOT case sensitive.
{
exit(0);
}
//What to do if user types q
else if(strcmpi(options,"q") == 0)//compare options to "recent" NOT case sensitive.
{
system("cls");
menu();
}
//What to do if user types recent
else if(strcmpi(options,"recent") == 0)//compare options to "recent" NOT case sensitive.
{
recent = fopen("recentdata.txt", "r");
fscanf (recent, "%s %s %s %s %s %s", af, bf, cf, Xof, Yof, Zof);
fclose;
Euler(af, bf, cf, Xof, Yof, Zof);
}
else if (strcmpi(options,"data") == 0)
{
return(0);
}
}