/*write the c program implement the structure name student with the following file
sudent id (int)
name(string)
age(int)
GPA(float)
create function for the following opration
1.add a new student record
2.display all student record by id
3.search the student id
4.update the student record name,age,GPA
5.delet the record of student
6.calculate the avarage GPA of all record
7.sort student record base as assending or decending order
8.exit the file by getting the command
rule
manual define interface
error handeling
dont exit until user command
save in txt file*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student {
int id;
char name[50];
int age;
float gpa;
};
struct student students[100];
int num_students = 0;
void save_to_file() {
FILE *fp = fopen("nischal.txt", "w");
if (fp == NULL) {
printf("error opening file for writing.\n");
return;
}
for (int i = 0; i < num_students; i++) {
fprintf(fp, "%d %s %d %.2f\n", students[i].id, students[i].name, students[i].age, students[i].gpa);
}
fclose(fp);
}
void load_from_file() {
FILE *fp = fopen("nischal.txt", "r");
if (fp == NULL) {
printf("no existing records found.\n");
return;
}
while (fscanf(fp, "%d %s %d %f", &students[num_students].id, students[num_students].name,
&students[num_students].age, &students[num_students].gpa) == 4) {
num_students++;
}
fclose(fp);
}
int is_valid_integer(const char *str) {
while (*str) {
if (*str < '0' || *str > '9')
return 0;
str++;
}
return 1;
}
int is_valid_float(const char *str) {
int has_dot = 0;
while (*str) {
if (*str == '.') {
if (has_dot)
return 0;
has_dot = 1;
} else if (*str < '0' || *str > '9')
return 0;
str++;
}
return has_dot;
}
void add_new_student_record() {
if (num_students >=100) {
printf("maximum number of students reached.\n");
return;
}
struct student new_student;
char temp[50];
printf("enter student id: ");
scanf("%s", temp);
if (!is_valid_integer(temp)) {
printf("invalid input for id. id should be an integer.\n");
return;
}
scanf(temp, "%d", &new_student.id);
printf("enter student name: ");
scanf("%s", new_student.name);
printf("enter student age: ");
scanf("%s", temp);
if (!is_valid_integer(temp)) {
printf("invalid input for age. age should be an integer.\n");
return;
}
scanf(temp, "%d", &new_student.age);
printf("enter student gpa: ");
scanf("%s", temp);
if (!is_valid_float(temp)) {
printf("invalid input for gpa. gpa should be a float.\n");
return;
}
sscanf(temp, "%f", &new_student.gpa);
students[num_students++] = new_student;
save_to_file();
printf("student record added successfully.\n");
}
void display_all_student_record_by_id() {
printf("student records sorted by id:\n");
for (int i = 0; i < num_students; i++) {
printf("id: %d, name: %s, age: %d, gpa: %.2f\n", students[i].id, students[i].name,
students[i].age, students[i].gpa);
}
}
void search_the_student_id(int id) {
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
printf("student found:\n");
printf("id: %d, name: %s, age: %d, gpa: %.2f\n", students[i].id, students[i].name,
students[i].age, students[i].gpa);
return;
}
}
printf("student with id %d not found.\n", id);
}
void update_the_student_record_name_age_gpa(int id) {
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
printf("enter new name: ");
scanf("%s", students[i].name);
const char* temp;
printf("enter new age: ");
scanf("%s",&temp);
if (!is_valid_integer(temp)) {
printf("invalid input for age. age should be an integer.\n");
return;
}
scanf(temp, "%d", &students[i].age);
printf("enter new gpa: ");
scanf("%s", temp);
if (!is_valid_float(temp)) {
printf("invalid input for gpa. gpa should be a float.\n");
return;
}
sscanf(temp, "%f", &students[i].gpa);
save_to_file();
printf("student record updated.\n");
return;
}
}
printf("student with id %d not found.\n", id);
}
void delete_the_record_of_student(int id) {
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
for (int j = i; j < num_students - 1; j++) {
students[j] = students[j + 1];
}
num_students--;
save_to_file();
printf("student record deleted.\n");
return;
}
}
printf("student with id %d not found.\n", id);
}
float calculate_the_avarage_gpa_of_all_record() {
if (num_students == 0) {
printf("no student records found.\n");
return 0;
}
float total = 0;
for (int i = 0; i < num_students; i++) {
total += students[i].gpa;
}
return total / num_students;
}
void sort_student_record_base_as_assending_or_decending_order(int ascending) {
for (int i = 0; i < num_students - 1; i++) {
for (int j = 0; j < num_students - i - 1; j++) {
if (ascending ? students[j].id > students[j + 1].id : students[j].id < students[j + 1].id) {
struct student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
}
int main() {
load_from_file();
int choice, id;
char temp[50];
while (1) {
printf("\nmenu:\n");
printf("1. add a new student record\n");
printf("2. display all student record by id\n");
printf("3. search the student id\n");
printf("4. update the student record name age gpa\n");
printf("5. delete the record of student\n");
printf("6. calculate the average gpa of all record\n");
printf("7. sort student record base as ascending or descending order\n");
printf("8. exit\n");
printf("enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_new_student_record();
break;
case 2:
display_all_student_record_by_id();
break;
case 3:
printf("enter student id to search: ");
scanf("%d", &id);
search_the_student_id(id);
break;
case 4:
printf("enter student id to update: ");
scanf("%d", &id);
update_the_student_record_name_age_gpa(id);
break;
case 5:
printf("enter student id to delete: ");
scanf("%d", &id);
delete_the_record_of_student(id);
break;
case 6:
printf("average GPA of all records: %.2f\n", calculate_the_avarage_gpa_of_all_record());
break;
case 7:
printf("sort in ascending (1) or descending (0) order: ");
scanf("%d", &id);
sort_student_record_base_as_assending_or_decending_order(id);
printf("records sorted.\n");
break;
case 8:
save_to_file();
printf("exiting...\n");
return 0;
default:
printf("invalid choice, please try again.\n");
}
}
return 0;
}
0 Comments