Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.5 kB
4
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>

#define MAXLEN 20

typedef struct
{
	char subject[MAXLEN];
	int gradecount;
	int grades[MAXLEN];
	float avg;
	
} ained;

int TextSisse(ained *list1);
void ListValja(ained *list1, int max);
void LogiValja(char tekst[], int rida);
void Valik(ained *list1, int kumb, char mis[], float keskmine, int max);


int main(void)
{
	int max, edasi;
	float keskmine;
	char mis[MAXLEN];
	ained list1[MAXLEN];

	LogiValja("programmi kaivitamine", 0);
	max = TextSisse(list1);	
	printf("lugesin %d ainet\n", max);

	ListValja(list1, max);
	
	LogiValja("programmi lopetamine", 0);

	while (edasi != 0)
	{
		printf("vali aine:\n");
		scanf("%s", mis);
		printf("valisid %s\n", mis);
		Valik(list1, 1, mis, 0, max);
		printf("\n\n");
		
		printf("vali keskmine hinne:\n");
		scanf("%f", &keskmine);
		printf("valisid %.2f\n", keskmine);
		Valik(list1, 2, 0, keskmine, max);
		printf("\n\n");
		
		printf("kas soovid edasi otsida?\n");
		printf("kirjuta 0 kui ei\n");
		scanf("%d", &edasi);
		printf("\n\n");
	}


	return EXIT_SUCCESS;
}

int TextSisse(ained *list1)
{
	
	int i, j, palju;
	j = 0; 
	
	FILE*fData = fopen("data61.txt", "r");
	
	if (fData == NULL)
	{
		perror ("JAMA!! ei avane");
		LogiValja("ei suutnud avada faili", 0);
	}
	
	LogiValja("faili avamine ja sisse lugemine", 0);
	
	while (fscanf (fData, "%s %d ", (list1 + j)-> subject, &(list1 + j) -> gradecount) == 2)
	{		
		palju = (list1 + j) -> gradecount;
		//printf("\naines %s on %d hinnet\n", (list1 + j)-> subject, (list1 + j) -> gradecount);
		
		for (i = 0; i < palju; i++)
		{
			
			if (fscanf(fData, "%d ", &(list1 + j) -> grades[i]) != 1)
			{
				return 0;	
			}
		}
		
		LogiValja("   rea hinnete lugemine labi", i + 1);
		putchar('\n');
		j++;
		
		if (j > MAXLEN)
		{
			break;
			LogiValja("ERROR liiga palju elemente array jaoks", 0);
		}
		
	}
	
	fclose(fData);
	LogiValja("faili rea ja aine kontroll", j + 1);
	LogiValja("faili sulgemine", 0);
	
	return j;
}


void ListValja(ained *list1, int max)
{
	int i, j, hindeKogus;
	float hindeSumma;
	
	for (j = 0; j < max; j++)
	{
		printf("\naines %s on %d hinnet\n", (list1 + j)-> subject, (list1 + j) -> gradecount);
		
		
		hindeKogus = (list1 + j) -> gradecount;
		hindeSumma = 0;
		
		if (hindeKogus == 0)
		{
			printf("hindeid ei olnud, keskmist ei ole");
		}
		else
		{
			for (i = 0; i < hindeKogus; i++)
			{
				printf("%d ", (list1 + j) -> grades[i]);
				hindeSumma += (list1 + j) -> grades[i];
			}
			
			//printf("hinnete summa on %.f ", hindeSumma);
			((list1 + i) -> avg) = hindeSumma / hindeKogus;
			printf("\nkeskmine hinne on %.3f", (list1 + i) -> avg);
		}
		putchar('\n');
	}
	LogiValja("aine ja hinnete arvu valjastamise lopetamine", j + 1);
}


void LogiValja(char tekst[], int rida)
{
	FILE*fData2 = fopen("data62.txt", "a+");

	if (fData2 == NULL)
	{
		perror ("JAMA!! ei avane");
	}

	time_t currentTime;
	time(&currentTime);
	
	fprintf(fData2, "%s", tekst);
	if (rida != 0)
	{
		fprintf(fData2, " real %d", rida);
	}
	
	fprintf(fData2, "\naeg on %s\n", ctime(&currentTime));

	fclose(fData2);
}

void Valik(ained *list1, int kumb, char aine[], float keskmine, int max)
{
	int i, j, keskValik;
	float kesk;
	
	if (kumb == 1)
	{
		for (i = 0; i < max; i++)
		{
			if ((strcmp(aine, (list1 + i) -> subject)) == 0)
			{
				printf("%s keskmine hinne on %f\n", (list1 + i) -> subject, (list1 + i) -> avg);
				printf("teised hinded on:  ");
				for (j = 0; j < (list1 + i) -> gradecount; j++)
				{
					printf("%d  ", (list1 + i) -> grades[j]);
				}
				putchar('\n');
			}
		}
	}
	else if (kumb == 2)
	{
		printf("kas tahad naha parema(1) voi halvema (2) keskmisega aineid\n");
		scanf("%d", &keskValik);
		if (keskValik == 1)
		{
			for (i = 0; i < max; i++)
			{
				kesk = (list1 + i) -> avg; 
				
				if (kesk > keskmine)
				{
					printf("%s\n", (list1 + i) -> subject);
				}
			}
			putchar('\n');
		}
		else if (keskValik == 2)
		{	
			printf("siin3");
			for (i = 0; i < max; i++)
			{
				kesk = (list1 + i) -> avg; 
				if (kesk > keskmine)
				{
					printf("%s\n", (list1 + i) -> subject);
				}
			}
			putchar('\n');
		}
		else
		{
			printf("miskit on m2da\n");
		}
	}
	else
	{
		printf("midagi on m2da\n");
	}
}