سلام ...
أرجو المساعدة من الجميع في حل هالاسايمنت
هذا السؤال
Records (struct)
Part (A)
Define a struct named courseType, which has four components: courseName and courseCode of type string, courseCredit of type int, and numSections of type int (numSections is between 1 and 12) to represent number of sections allowed for this course.
Part (B)
Using the struct defined in (A). Write a program that reads courses codes followed by their names , credits and number sections from a file. The program should then print the courses in ascending order based on number of sections. Suppose that the department has 20 courses. Use an array of 20 components of type courseType.
Your program should perform the required operations using the following functions:
a. A function to read the courses data into the array. The function prototype is:
void getData(courseType List[], int listSize);
b. A function to sort the array data based on number of sections. The function prototype is: void sortList(courseType List[], int listSize);
c. A function to print the data in the array into a file. The function prototype is:
void printResult(const courseType sList[], int listSize);
------------
وهذا الحل ...
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
struct courseType{
string courseName,courseCode ;
int courseCredit , numSections ;
};
const int size=20;
void getData(courseType list[],int size)
{
ifstream infile;
infile.open("sec.txt");
if(!infile)
cout<<"No file found";
int s;
for(int i=0;i<size;i++)
{
infile>>list.courseName>>list.courseCode
>>list.courseCredit>>s;
if(s<0||s>12)
cout<<"invalid numSections"<<endl;
else
list.numSections=s;
infile.close();
}
}
void sortList(courseType list[], int listSize)
{
courseType temp;
for (int i=1;i<size;i++)
{
for (int j=0;j<size-i;j++)
if(list[j].numSections>list[j+1].numSections)
{
temp=list[j];
list[j]=list[j+1];
list[j+1]=temp;
}
}
}
void printResult(const courseType sList[], int listSize)
{
ofstream outfile;
outfile.open("sec.txt");
for(int i=0;i<listSize;i++)
{
outfile<<sList.courseName<<sList.courseCode
<<sList.courseCredit<<sList.numSections;
}
}
int main()
{
courseType list [20];
getData(list,20);
sortList(list,20);
printResult(list,20);
system("pause");
return 0;
}