Commit 3006f3fb by antsim

new

parent d9696441
Showing with 201 additions and 0 deletions
/**
File: HW2.c
Author: Anastassia Tšimbur
Created: 18.04.2017
Last edit: 26.04.2017
Description: IAG0582 Programming II Homework 2. Program with recursive function with using structure, dynamic allocation of memory.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/*
* deklaratsii po zadaniju i 4toby ubratj magic numbers.
* epsilon - float==float comparison to4nostj
*/
#define L 15
#define false 0
#define epsilon 0.0001
/*
* sozdal struct dlja massiva zna4enij, 4toby dobavljatj pamjat po nuzde
*/
struct massiiv{
float A;
};
/*
* sozdaju nuznije mne funkcii dlja raboty v menu kak prosili v zadanii
* printMenu - samo menu otkuda vybraju zadanija
* insertValues - menu dlja vbivanija X i E zna4enij
* recursion - rekursiv funktsija po zadaniju
* printList - pe4ataju v consolj i fail sdelannij massiv
*/
void printMenu(float** E, float** X, struct massiiv **andmed);
void insertValues(float** E, float** X);
void recursion(float** E, float** X, struct massiiv **andmed, int** n);
void printList(struct massiiv **andmed, int** kogus, FILE *file);
int areSame(float a, float b);
/*
* otsuda sozdaju zna4enija s kotorimi mne nado zanimatsa po zadaniju.
* rekutsioon sozdaju nulevoj massiv
* E i X daju zna4enije 0.
*/
int main(){
struct massiiv *rekursioon = NULL;
float *E = (float*)calloc(0, sizeof(float));
float *X = (float*)calloc(0, sizeof(float));
printMenu(&E, &X, &rekursioon);
free(E);
free(X);
free(rekursioon);
return 0;
}
/*
* otkrivaju fail tut poskolku mne nado povtorno snim zanimatsa i udaljatj dannije po mere dobavlenija novih. zakrivaju srazu posle zapisovanija faila.
* pri povtornom zapisovanii v fail on zanogo otkrivaet ego i udaljaet starije dannije.
* delaju pointer n, tak kak mne nuzno datj eto zna4enije dalwe v rekursiv funktsiju. 4toby vernutj veli4inu sozdanovo massiva, to privjazivaju ego k n pointeru.
* v konce udaljaju pamjatj na n.
*/
void printMenu(float** E, float** X, struct massiiv **andmed){
FILE *fileoutput;
int choice;
int *n = (int*)calloc(0, sizeof(int));
do{
printf("===========================\n");
printf("(1). Work with Epsilon and X values\n");
printf("(2). Generate array A with given values\n");
printf("(3). Save array A with calculations to txt\n");
printf("(0). Exit\n");
printf("===========================\n\n");
printf("Enter your choice Please\n");
scanf("%d", &choice);
switch(choice){
case 1:
insertValues(E, X);
break;
case 2:
*n=0;
if ((**E!=0)&&(**X!=0)&&(*E!=NULL)&&(*X!=NULL)){ //proverka na no 4to byli dany zna4enija E i X
recursion(E, X, andmed, &n);
printf("Array was generated with %d values\n", *n);
} else {
printf("Set your values first.\n");
}
break;
case 3:
if (*n==0){ //proverka, 4to byl sdelan massiv.
printf("Array is empty\n");
} else {
fileoutput = fopen("/Users/Anastassia/Desktop/F.txt", "w");
printList(andmed, &n, fileoutput);
fclose(fileoutput);
}
break;
case 0:
printf("Goodbye\n");
break;
default: printf("Wrong Choice. Enter again\n");
break;
}
} while(choice != false);
free(n);
}
/*
* po suti legkoe menu gde vbivaju zna4enija v ramkah po zadaniju.
*/
void insertValues(float** E, float** X){
int choice;
float temp,temp1;
do{
printf("===========================\n");
printf("(1). Insert/change Epsilon and X values\n");
printf("(2). Preview values\n");
printf("(0). Return to main menu\n");
printf("===========================\n\n");
printf("Enter your choice Please\n");
scanf("%d", &choice);
switch(choice){
case 1:
printf("Enter your epsilon value Please\n");
scanf("%f", &temp);
printf("Enter your X value Please\n");
scanf("%f", &temp1);
if ((temp>0)&&(temp<1)&&(temp1>-1)&&(temp<1)){ //proverka 4to byli dany vernije zna4enija E i X
memcpy(*E, &temp,sizeof(float));
temp1 = fabs(temp1);
memcpy(*X, &temp1,sizeof(float));
} else {
printf("You set wrong values...Try again\n");
}
break;
case 2:
if ((**E!=0)&&(**X!=0)&&(*E!=NULL)&&(*X!=NULL)){ //proverka na no 4to byli dany zna4enija E i X
printf("\nYour Epsilon:%.3f and X:%.3f\n\n", **E, **X);
} else {
printf("\nYour values are not set yet.\n\n");
}
break;
case 0:
break;
default: printf("Wrong Choice. Enter again\n\n");
break;
}
} while(choice != false);
}
/*
* rekursiv funktsija, gde dano E, X, massiv kuda zapisivaju, i veli4ina massiva
* sozdaju vremennij massiv s kotorim rabotaju i v itoge "*andmed = tempStruct" sohranjaju ih na osnovnoj massiv.
* skoree vsego v etoj funktsii memory leak, no ja ne znaju kak eto pofiksitj v rekursive.
*/
void recursion(float** E, float** X, struct massiiv **andmed, int** n){
struct massiiv *tempStruct = NULL;
tempStruct = (struct massiiv*) realloc(tempStruct, (**n+1)*sizeof(struct massiiv));
memcpy(tempStruct,*andmed,**n*sizeof(struct massiiv));
if ((**n>false)&&(**n<L)){ //proverka 4to n>0 i n<15(L)
(tempStruct+**n)->A = pow(-1, **n)*(pow(**X, pow(2, **n)))/(pow(2, **n)+1); //koro4e eto 4to oni hoteli
if (areSame(fabs(((tempStruct+**n)->A)-((tempStruct+(**n-1))->A)),**E)){ //proverka |AL – A(L – 1) | <= E
*andmed = tempStruct; //sohranaju dannije na osnovnoj massiv
**n = **n+1; //po suti n++, 4to teperj v massive na +1 zna4enije i daju ego dalwe v novuju funktsiju
recursion(E, X, andmed, n); //perevizivaju tu ze funktsiju aka rekursiv, kuda daju novije zna4enija
} else {
tempStruct = (struct massiiv*) realloc(tempStruct, (**n)*sizeof(struct massiiv));
*andmed = tempStruct;
}
} else if(**n==false){ //pervona4aljnoe vizivanije funktsii A1
(tempStruct+0)->A = **X;
*andmed = tempStruct; //sohranaju dannije na osnovnoj massiv
**n = **n+1; //dobavljaju 0+1=1, 4to teperj v massive 1 zna4enije i daju ego dalwe v novuju funktsiju
recursion(E, X, andmed, n); //perevizivaju tu ze funktsiju aka rekursiv, kuda daju novije zna4enija
}
}
/*
* legkaja raspe4atka dannih na konsolj i v fail.
*/
void printList(struct massiiv **andmed, int** kogus, FILE *file){
struct massiiv *tempStruct = *andmed;
printf("\nYour array:\n");
for(int i=0;i<**kogus;i++){
fprintf(file, "A%d = %f\n", (i+1), (tempStruct+i)->A);
printf("A%d = %f\n", (i+1), (tempStruct+i)->A);
}
}
/*
* poskolku nelzja delatj float == float, tak kak tam kakieto nerelano malenkije 4isla ranzije
* to ja delaju to4nostj epsilon s kotorim ja budu sravnivatj dva float 4isla k primeru 0.001 - 0.001 = 0 < epsilon(0.0001)
* delaju return 0 ili 1 i ispoljzuju ego v if() v rekursiv funktsii
*/
int areSame(float a, float b){
return fabs(a-b) < epsilon;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment