current position:Home>Want to know how to complete the following C + + program?

Want to know how to complete the following C + + program?

2022-02-03 02:12:04 CSDN Q & A

Car management simulation program
【 requirement 】 Create two classes according to the following description and requirements moto and All :
class moto{ // Automobile
friend class ALL;
private:
int ; // Car code
char ; // Car type ('A'- truck ,'B'- Passenger car )
int ; // Free / Use (0/1)
float ; // load / Number of passengers
public:
moto(){ id=0; type=NULL; flag=0; weight=0;}
void set(int a, char b) // Enter a car data
{ id=a; type=b;}
void disp() // Output a car data
{ cout<<id<<','<<type<<','<<weight<<endl;}
};
class ALL{ // Automobile management
private:
moto car[12]; // Manage at most 12 car
int num; // Number of cars actually stored
public:
ALL(){num=0;} // Constructors
void AddCar(); // Add a new car , Code and type are required
void list(); // Print passenger train list and freight train list respectively
void load(char t,float w); // Dispatch vehicle , Enter the vehicle type and loading weight ( Or number of passengers ),
// Select an empty vehicle to modify its data , Output the dispatch vehicle number
};
Please complete the above All The member function of the function body is not defined in the class , And debug and run .All class AddCar() and list() You can call moto The corresponding member functions of the class realize its functions .
The main function used for the test is referred to as follows :
void main(){
ALL mt;
for(int i=0;i<4;i++) mt.AddCar();
mt.list();
mt.load('A',50);
mt.load('B',40);
mt.load('A',60);
mt.list();
}

Learning achievement management simulation program
【 requirement 】 Create two classes according to the following description and requirements student and manage :
class student{ // Students
friend class manage;
private:
int id; // Student number
char name; // Initials
float math; // Math exam results
float physic; // Physics exam results
void input(){cin>>id>>name>>math>>physic;} // Input record
void display(){
cout<<id<<','<<name<<','<<math<<','<<physic<<endl;
}// Output record
public:
student(); // Constructors
};
class manage{ // Management category
private:
student s[10]; // Storing student information , Up to 10 Bar record
int num; // The number of elements in the array that have written data ( Students record the number )
float mav; // Math average
float pav; // Physics GPA
public:
manage(){num=0;} // Constructors
void addst(); // In the array s Add a student record to , Enter the required data from the keyboard .
void list(); // The screen prints a list of student information and the number of students , Have a data title
int cavrage(); // Calculate the average score of all students in mathematics and Physics ,
// Assign the calculation result to the corresponding data member , And output to the screen .
// If the number of student records is 0 Then the function returns 0, Otherwise, if the average value is calculated, it returns 1.
};

Void manage::addst()
{

Among them addst() and list() Callable student class . Please complete the member function without function body defined above , And debug and run .
The main function used for the test is referred to as follows :
void main(){
manage ms;
ms.addst();
ms.addst();
ms.addst();
ms.list();
if(ms.cavrage()) cout<<" Calculation completed !"<<endl;
else cout<<" Student table is empty !"<<endl;
}

It is an early program problem of non electricity in Southeast University , I hope teachers can teach me




Refer to the answer 1:

first :

#include <iostream>using namespace std;class moto{ // Automobile     friend class ALL;private:    int id;    // Car code     char type; // Car type ('A'- truck ,'B'- Passenger car )    int flag;  // Free / Use (0/1)    float weight; // load / Number of passengers public:    moto(){ id=0; type=NULL; flag=0; weight=0;}    void set(int a, char b) // Enter a car data  { id=a; type=b;}    void disp() // Output a car data  { cout<<id<<','<<type<<','<<weight<<endl;}};class ALL{ // Automobile management private:    moto car[12]; // Manage at most 12 car     int num; // Number of cars actually stored public:    ALL(){num=0;} // Constructors     void AddCar(); // Add a new car , Code and type are required     void list(); // Print passenger train list and freight train list respectively     void load(char t,float w); // Dispatch vehicle , Enter the vehicle type and loading weight ( Or number of passengers ),    // Select an empty vehicle to modify its data , Output the dispatch vehicle number };void ALL::AddCar(){    int id;    char type;    cout << " Please enter code and type :";    cin >> id >> type;    car[num].set(id,type);    num++;}void ALL::list(){    for (int i = 0;i<num;i++)    {        car[i].disp();    }}void ALL::load(char t,float w){    for (int i=0;i<num;i++)    {        if(car[i].flag == 0 && car[i].type == t)        {            car[i].flag = 1;            car[i].weight =w;            cout <<" Send the car number :" <<car[i].id<<endl;            break;        }    }}void main(){        ALL mt;        for(int i=0;i<4;i++) mt.AddCar();        mt.list();        mt.load('A',50);        mt.load('B',40);        mt.load('A',60);        mt.list();}

The second question is :

#include <iostream>using namespace std;class student{ // Students     friend class manage;private:    int id; // Student number     char name; // Initials     float math; // Math exam results     float physic; // Physics exam results     void input(){cin>>id>>name>>math>>physic;} // Input record     void display(){        cout<<id<<','<<name<<','<<math<<','<<physic<<endl;    }// Output record public:    student(); // Constructors };student::student(){    id = 0;    name =0;    math = 0;    physic = 0;}class manage{ // Management category private:    student s[10]; // Storing student information , Up to 10 Bar record     int num; // The number of elements in the array that have written data ( Students record the number )    float mav; // Math average     float pav; // Physics GPA public:    manage(){num=0;} // Constructors     void addst(); // In the array s Add a student record to , Enter the required data from the keyboard .    void list(); // The screen prints a list of student information and the number of students , Have a data title     int cavrage(); // Calculate the average score of all students in mathematics and Physics ,    // Assign the calculation result to the corresponding data member , And output to the screen .    // If the number of student records is 0 Then the function returns 0, Otherwise, if the average value is calculated, it returns 1.};void manage::addst(){    cout << " Please enter the student id number 、 Initials 、 Math and physics :"<<endl;    s[num].input();    num++;}void manage::list(){    cout << " Student data list :"<<endl;    for (int i=0;i<num;i++)    {        s[i].display();    }}int manage::cavrage(){    mav = 0;    pav = 0;    if (num == 0)    {        return 0;    }else    {        for (int i = 0;i<num;i++)        {            mav += s[i].math;            pav += s[i].physic;        }        mav /= num;        pav /= num;        return 1;    }}    // Among them addst() and list() Callable student class . Please complete the member function without function body defined above , And debug and run .        // The main function used for the test is referred to as follows :void main(){    manage ms;            ms.addst();            ms.addst();            ms.addst();            ms.list();            if(ms.cavrage()) cout<<" Calculation completed !"<<endl;            else cout<<" Student table is empty !"<<endl;}



Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202030212016068.html

Random recommended