0% found this document useful (0 votes)
12 views18 pages

PF Assignment

The document contains three programming tasks related to file handling in C++. Task 1 calculates student grades based on scores read from a file and writes the results to another file. Task 2 analyzes word lengths from a file and identifies the longest words, while Task 3 implements an employee management system that allows adding records, printing them, finding the employee with the highest salary, and filtering records based on department or employee status.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views18 pages

PF Assignment

The document contains three programming tasks related to file handling in C++. Task 1 calculates student grades based on scores read from a file and writes the results to another file. Task 2 analyzes word lengths from a file and identifies the longest words, while Task 3 implements an employee management system that allows adding records, printing them, finding the employee with the highest salary, and filtering records based on department or employee status.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 18

Name: Abdullah Hassan

Reg no: L1F24BSCS0002


Assignment:
Programming Fundamentals
______________________________________________________________________________
____

Task 1:

#include<iostream>

#include<fstream>

using namespace std;

int main()

ifstream inputFile("students.txt");

ofstream outputFile("grades.txt");

if(!inputFile)

cout<<"Error"<<endl;

return 1;

char studentName[50],studentID[20];

float assignmentScore,quizScore,midtermScore,finalExamScore;

float totalPercentage;

while(inputFile.getline(studentName,50,','))
{

inputFile>>ws;

inputFile.getline(studentID,50,',');

inputFile>>assignmentScore;

inputFile.ignore();

inputFile>>quizScore;

inputFile.ignore();

inputFile>>midtermScore;

inputFile.ignore();

inputFile>>finalExamScore;

inputFile.ignore();

totalPercentage=((assignmentScore+quizScore+midtermScore+finalExamSc
ore)/115.0)*100.0;

outputFile<<studentName<<","<<studentID<<","<<totalPercentage<<",";

if(totalPercentage>=85&&totalPercentage<=100)

outputFile<<"A";

else if(totalPercentage>=80&&totalPercentage<=84)

outputFile<<"A-";

else if(totalPercentage>=75&&totalPercentage<=79)

outputFile<<"B+";

}
else if(totalPercentage>=70&&totalPercentage<=74)

outputFile<<"B";

else if(totalPercentage>=65&&totalPercentage<=69)

outputFile<<"B-";

else if(totalPercentage>=60&&totalPercentage<=64)

outputFile<<"C+";

else if(totalPercentage>=56&&totalPercentage<=59)

outputFile<<"C";

else if(totalPercentage>=50&&totalPercentage<=54)

outputFile<<"C-";

else if(totalPercentage<50)

outputFile<<"F";

outputFile<<endl;

inputFile.close();
outputFile.close();

cout<<"Grade calculation completed."<<endl;

return 0;

}
Task2:

#include<iostream>

#include<fstream>

using namespace std;

int main()

ifstream read1("word_length.txt");

ifstream read2("word_length.txt");

ofstream output("word_frequency.txt");

int length[1000], max_length = 0, i, j;

char max_word[100];

char word[100];

char line[100];
if (!read1)

cout << "error";

return 1;

while (read1.getline(line, 100))

i = 0;

while (line[i] != '\0')

j = 0;

while (line[i] != ' ' && line[i] != '\0')

word[j] = line[i];

j++;

i++;

word[j] = '\0';

int k = 0;

if (j > 0)

cout << word << ", " << j << endl;


output << word << ", " << j << endl;

max_length = j;

if (line[i] == ' ')

i++;

read1.close();

cout << "The longest words are: " << endl;

output << "The longest words are: " << endl;

while (read2.getline(line, 100))

i = 0;

while (line[i] != '\0')

j = 0;

while (line[i] != ' ' && line[i] != '\0')

word[j] = line[i];

j++;

i++;

}
word[j] = '\0';

int k = 0;

if (j == max_length)

cout << word << ", " << max_length << endl;

output << word << ", " << max_length << endl;

if (line[i] == ' ')

i++;

int s = 0;

if (word[s] == '\0')

cout << "There's nothing in the source to read";

}
read2.close();

output.close();

}
Task 3:

#include<iostream>

#include<fstream>

using namespace std;

int main()

char choice;

do {
cout << "****Employee system****" << endl;

cout << endl << "Press ‘S’: Add more 5 records in the file with existing file
format";

cout << endl << "Press ‘P’: Print all records on console";

cout << endl << "Press ‘E’: Find the employee which has max salary on the
console";

cout << endl << "Press ‘D’: Depart wise searching";

cout<< endl << "Press ‘R’: Write all the record into another file except the
one who is going to leave the depart";

Cout << endl << "Press ‘X’: exit" << endl;

cout << "Enter choice: ";

cin >> choice;

if (choice == 's' || choice == 'S')


{
ofstream write("source.txt", ios::app);
if (!write)
{
cout << "Error" << endl;
return 0;
}
cin.ignore();
for (int i = 1;i <= 5;i++)
{
char emp_name[50], depart[50];
int salary;
cout << "Enter name: ";
cin.getline(emp_name, 50);
cout << "Enter salary: ";
cin >> salary;
cin.ignore();
cout << "Enter depart: ";
cin.getline(depart, 50);
write << emp_name << "," << salary << "," << depart << endl;
}
write.close();
cout << "Records added successfully!" << endl;
}
else if (choice == 'p' || choice == 'P')
{
cout << endl << "Your results:" << endl;
ifstream read("source.txt");
if (!read)
{
cout << "Error" << endl;
return 0;
}
char word[100];
while (read.getline(word, 100))
{
cout << word << endl;
}
read.close();
cout << "End of records." << endl;
}
else if (choice == 'E' || choice == 'e')
{
char emp_name[100], max_employee[100], depart[100],
max_depart[100];
int salary, max_salary = 0;
ifstream read("source.txt");
if (!read)
{
cout << "Error" << endl;
return 0;
}
while (read.getline(emp_name, 100, ','))
{
read >> salary;
read.ignore();
read.getline(depart, 100);
if (salary > max_salary)
{
max_salary = salary;
int s = 0;
while (emp_name[s] != '\0')
{
max_employee[s] = emp_name[s];
s++;
}
max_employee[s] = '\0';
int k = 0;
while (depart[k] != '\0')
{
max_depart[k] = depart[k];
k++;
}
max_depart[k] = '\0';
}
}
read.close();
cout << "Employee with max salary: " << max_employee << ", " <<
max_salary << ", " << max_depart << endl;
}
if (choice == 'd' || choice == 'D')
{
cout << "Enter the depart to be found: ";
cin.ignore();
char search_depart[100];
cin.getline(search_depart, 100);
ifstream read("source.txt");
if (!read)
{
cout << "Error" << endl;
}
char emp_name[100], depart[100];
int salary;
bool found = false;
while (read.getline(emp_name, 100, ','))
{
read >> salary;
read.ignore();
read.getline(depart, 100);
int i = 0, j = 0;
while (depart[i] == ' ')
{
i++;
}
while (search_depart[j] == ' ')
{
j++;
}
bool match = true;
for (;depart[i] != '\0' && search_depart[j] != '\0';i++, j++)
{
if (search_depart[j] != depart[i])
{
match = false;
break;
}
}
if (match)
{
cout << emp_name << "," << salary << "," << depart << endl;
found = true;
}
}
read.close();
if (!found)
{
cout << "No match found" << endl;
}
}
else if (choice == 'r' || choice == 'R')
{
cout << "Enter the name of the employee not to be included: ";
cin.ignore();
char leaving_employee[100];
cin.getline(leaving_employee, 100);
ifstream read("source.txt");
ofstream write("result.txt");
if (!read)
{
cout << "Error" << endl;
return 1;
}
char emp_name[100], depart[100];
int salary;
bool found = false;
while (read.getline(emp_name, 100, ','))
{
read >> salary;
read.ignore();
read.getline(depart, 100);
int i = 0;
while (emp_name[i] == ' ')
{
i++;
}
int j = 0;
while (leaving_employee[j] == ' ')
{
j++;
}
bool match = true;
for (;emp_name[i] != '\0' && leaving_employee[j] != '\0';i++, j++)
{
if (leaving_employee[j] != emp_name[i])
{
match = false;
break;
}
}
if (!match)
{
write << emp_name << "," << salary << "," << depart << endl;
}
else
{
found = true;
}
}
read.close();
write.close();
if (!found)
{
cout << "Employee not found";
}
else
{
cout << "Written to file successfully";
}
}
cout << endl;
} while (choice != 'x' && choice != 'X');

cout << "Goodbye!";

You might also like