السلام عليكم ورحمه الله وبركاته
اخواتى فالله
:)
اريد ان اقدم لكم برنامج صغير بلغه السى بلس بلس
هذا البرنامج كان مطلوب منى فى الكورس بتاع OOP اثناء الكليه
والحمدلله تعالى عملته فى ساعه بعد ما اخدته :lol: :lol:
ولكن الان وانا ادعبث فى ملفاتى وجته قولت اشاركم بيه
لعل وعسى ان يستفيد منه احد
فهو للاستفاده فقط
البرنامج يعتبر شرحه فى صورتين فقط مع القليل من الكلام عنه
والتفاصيل الاخرى فقد قومت بوضع التعليقات على الكود الان حتى اذ قرأه احد يفهمه ويستوعبه ان شاء الله
البرنامج يتحدث عن ان هناك بيانات طلاب مخزنه فى ملف واريد ان اصنع منها تقريرا بشكل منظم فى ملف اخر
هذا هو الملف المخزن به البيانات موضوع بالصوره التاليه
كما موضحه التفاصيل على البيانات الموجوده فى الملف
ونريد ان نخرجها بهذه الصوره
اتمنى ان يفيدكم هذا البرنامج الصغير واى سوال ان شاء الله اجاوب عليه
للتحميل
فاليكم الكود للاطلاع عليه فقط
هو عباره عن Student class & it's implementation & main file
Student.h
/*
**************************
* *
* Made by Mahmoud Kelany *
* *
**************************
*/
#include<fstream>
#include<string>
using namespace std;
class student{
private:
// private data members
unsigned id;
string name;
unsigned credite;
float tuition;
public:
// constractors
student();
student(unsigned sId , string sName);
// set functions
void setId(unsigned sId);
void setName(string sName);
void setCredite(unsigned sCredite);
// get functions
unsigned getId();
string getName();
unsigned getCredite();
float getTuition();
// other functions
void display();
// friend functions
friend ifstream & operator >> (ifstream & f , student & s);
friend ofstream & operator << (ofstream & f , student & s);
};Student.cpp
/*
**************************
* *
* Made by Mahmoud Kelany *
* *
**************************
*/
#include "student.h"
#include<iostream>
#include<fstream>
using namespace std;
const float TuitionRate = 50 ;
// constractor implementation
student::student(){
}
student::student(unsigned sId,string sName){
id = sId;
name = sName;
}
// setter implementation
void student::setId(unsigned sId){
id = sId;
}
void student::setName(string sName){
name = sName;
}
void student::setCredite(unsigned sCredite){
credite = sCredite;
}
// getter implementation
unsigned student::getId(){
return id;
}
string student::getName(){
return name;
}
unsigned student::getCredite(){
return credite;
}
float student::getTuition(){
return credite * TuitionRate;
}
//display function in the console window
void student::display(){
cout << "Student id: " << id << " Student name: " << name << endl
<< "Student credite: " << credite << endl;
}
//friend function implementation
ifstream & operator >> (ifstream & f , student & s){ // change the job of the operator >> to anothe job
f >> s.id >> s.name;
return f;
}
ofstream & operator << (ofstream & f , student & s){// change the job of the operator << to anothe job
f << s.id << " " << s.name << endl;
return f;
}main.cpp
/*
**************************
* *
* Made by Mahmoud Kelany *
* *
**************************
*/
#include<iostream>
#include<fstream>
#include<string>
#include"student.h"
using namespace std;
void main(){
ifstream infile; //input file stream object
ofstream outfile; // output file stream object
student * students; // pointer to object of student class
int numberOfStudents; // number of student in the input file
infile.open("info.txt"); // open the file name info.txt for input data
outfile.open("report.txt"); // open the file name report.txt for output data
infile >> numberOfStudents; // begin to read the first line in the file that contain the number of student in the file
students = new student[ numberOfStudents ] ; // make objects of the student as the number of students that readed
/**
* begain to move to every thing in the input file
* make for loop from 0 to number of students
* print in the output file this words " -------- Student Registation Report -------- "
* then read the student number i (it's id & name ) by the operator overloading >>
* and print it to the output file (it's id & name ) by the operator overloading <<
* then check if the course counter = 0
* print Tuition equales 0$ for 0 credites and break
* else
* make instade for loop form 0 to course counter
* then read the course name & it's number of hours
* and add the number of hours to the sum variable
* then print the course name & hours in the output file
* then pass the sum variable to the function " setCredite(sum) "
* to get the Tuition to this student and print it in the file
* then make sum = 0 to be ready for the next student
* after finishing the main for loop close the input & output file
**/
for(int i = 0 ; i < numberOfStudents ; i++) {
outfile << "-------- Student Registation Report --------" << endl;
infile >> students;
outfile << students;
int courseCounter = 0 , numberOfEachCourse = 0 ,sum = 0;
string courseName;
infile >> courseCounter;
if(courseCounter == 0){
outfile << "Tuition equales 0$ for 0 credites" << endl << endl;
}else{
for(int j = 0 ; j < courseCounter ; j++){
infile >> courseName;
infile >> numberOfEachCourse;
sum += numberOfEachCourse;
outfile << courseName << " " << numberOfEachCourse << endl;
}
students.setCredite(sum);
outfile << "Tuition equales " << students.getTuition()
<< "$ for " << students.getCredite() << " credites" << endl << endl;
sum = 0;
}
}
infile.close();
outfile.close();
cout << "Program done go to your path directory to see the output in 'report.txt' file." << endl << endl;
}