Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- //ex 1
- struct Course{
- char name[51];
- char date[11];
- int lectures;
- float price;
- };
- int main() {
- FILE *file;
- file = fopen("Course.bin", "rb");
- if (file == NULL) {
- printf("File could not be opened\n");
- return 1;
- }
- // Четем първо броя на курсовете който е посочен в първия ред на файла
- if(fseek(file, 0, SEEK_END) != 0){ //int n;
- printf("File could not be seeked\n"); //if (fread(&n, sizeof(int), 1, file) != 1) {
- fclose(file); //printf("Could not read number of courses\n");
- return 1; //fclose(file);
- } //return 1;
- //}
- long size = ftell(file);
- if(size < 0){ // За side safety: проверка дали броят е положителен
- printf("Error determining to end of file\n");
- fclose(file); //if (n <= 0) {
- return 1; //printf("Invalid number of courses in file\n");
- } //fclose(file);
- //return 1;
- rewind(file); //}
- int n = size / sizeof(struct Course);
- ////////////////////////////////////////////////
- struct Course* courses = (struct Course*) malloc(n * sizeof(struct Course));
- if (courses == NULL) {
- printf("Memory could not be allocated\n");
- fclose(file);
- return 1;
- }
- if(fread(courses, sizeof(struct Course), n, file) != n) {
- if (feof(file)) {
- perror(""); //printf("File could not be read\n");
- }
- else if (ferror(file)) {
- perror(""); //printf("Error reading file\n");
- }
- free(courses);
- fclose(file);
- return 1;
- }
- fclose(file);
- free(courses);
- return 0;
- }
- //ex 2
- int discount(struct Course* courses, int n, int index){
- if (index < 0 || index >= n) {
- printf("Invalid index\n");
- return 1;
- }
- courses[index].price *= 0.9;
- printf("%.2flv - %s - %s\n", courses[index].price, courses[index].name, courses[index].date);
- return 0;
- }
- //ex 3
- int write (struct Course* courses, int n, float max, float min){
- FILE *file;
- file = fopen("offer.txt", "w");
- if (file == NULL) {
- printf("File could not be opened\n");
- return 1;
- }
- int counter = 0;
- for (int i = 0; i < n; i++) {
- if (courses[i].price <= max && courses[i].price >= min) {
- counter++;
- fprintf("%s\n%s\n%d\n%.2flv", courses[i].name, courses[i].date, courses[i].lectures, courses[i].price);
- }
- }
- fclose(file);
- return counter;
- }
- //ex4
- struct Course* deleteCourse(struct Course* courses, int* n, const char name[51], const char date[11]) {
- int index = -1;
- for (int i = 0; i < *n; i++) {
- if (strcmp(courses[i].name, name) == 0 && strcmp(courses[i].date, date) == 0) {
- index = i;
- break;
- }
- }
- if (index == -1) {
- printf("Course not found.\n");
- return courses; // няма нужда от промяна
- }
- // изместване на елементите наляво
- for (int i = index; i < *n - 1; i++) {
- courses[i] = courses[i + 1];
- }
- // преоразмеряване на масива
- struct Course* resized = realloc(courses, (*n - 1) * sizeof(struct Course));
- if (resized == NULL && *n - 1 > 0) {
- perror("");
- return NULL;
- }
- (*n)--; // намаляваме броя на елементите
- return resized;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement