Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Question 2
2.
Modify Code 5.2.1 so that the object is added at the end of the list
#include<iostream>
#include"5.1.1.cpp"
using namespace std;
class StudentList{
private:
struct ListNode{
//the data of the list is an instance of class Student
Student astudent;
ListNode*next;
};
ListNode*head;
ListNode*tail;
public:
StudentList();
~StudentList();
void StudentList::Add(Student
int IsEmpty(); newstudent)
{
if (newPtr==NULL)
void DisplayList();
cout<<"Cannot allocate memory";
else
};//class StudentList
{
newPtr->astudent=newstudent;
StudentList::StudentList(){
newPtr->next=NULL;
head=NULL;};
if(IsEmpty()){
StudentList::~StudentList(){
head=newPtr;
cout<<"\nDestructing the objects..\n";
}
while(IsEmpty()!=0)
else{
Remove();
tail->next=newPtr;
if(IsEmpty()==0)
tail=newPtr;
};//method destructor
}int StudentList::IsEmpty()
}{
if(head==NULL)
return 0;
else
return 1;
};
void StudentList::Remove(){
if(IsEmpty()==0)
cout<<"List empty on remove";
else
{
ListNode*temp=head;
head=head->next;
temp->next=NULL;
delete temp;
}
};
void StudentList::DisplayList()
{
ListNode*cur=head;
if(IsEmpty()==0)
cout<<"List empty \n";
else
{
cout<<"current List :\n";
while(cur!=NULL){
cur->astudent.GetData();
cur=cur->next;
}
cout<<"\n";
}//end if
};
int main(){
//create a new instance of student
Student student1;
//invoke methods
student1.SetData();
student1.GetData();
return 0;
};
#include <iostream>
#include <fstream>
using namespace std;
class number{
public :
struct num{
int n=0;
int sum=0,total=0;
}stdata;
public:
void display();
};
void number::display()
{
fstream file("numbers.txt");
while(file >> stdata.n) // or while(cin >> n) to read from stdin, commandline
{
stdata.sum += stdata.n;
stdata.total++;
}
int average = (float) stdata.sum/stdata.total;
Question 3
Write a program using OOP techniques to read numbers from a file and print the
cout<<"sum:
" <<of
stdata.sum
<< endl;
sum and average
these numbers
to the screen.
cout << "average: " << average << endl;
}
int main() {
number num;
num.display();
return 0;
}
Output :
#include <fstream>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include "transact.cpp"
class System{
private:
struct Data{
char pinnum[7];
char name[20];
char accnum[8];
int balance;
}dat;
public:
void Add (Transaction Users);
void ReadFile();
Questionvoid
4 Login();
Code 5.6.3 void
usesActivity();
an array to store and search the information details for the
Account case study Modify code 5.6.3 so that the storing and searching uses a
};
linked list.
struct ListNode{
Transaction aTrans;
ListNode *next;
};
ListNode *head;
ListNode *currUser;
Transaction Users;
void System::Add (Transaction Users) // Function nak add data dalam linked list
{
ListNode *newPtr = new ListNode;
if (newPtr==NULL){
cout << "\nUnable to locate memory";
}
else{
newPtr->aTrans = Users;
newPtr->next = head;
head = newPtr;
}
}
void System::ReadFile ()
{
ifstream input;
input.open("account.dat");
if (input.fail()){
cout << "Unable to open a file \n";
exit (1);
}
while (input >> dat.pinnum >> dat.name >> dat.accnum >> dat.balance){
Users.SetPin(dat.pinnum);
Users.SetName(dat.name);
Users.SetAcc(dat.accnum);
Users.SetBalance(dat.balance);
}
input.close();
}
void System::Login()
{
cout << "\n-----------------------------\n";
cout << "
Welcome To
\n";
currUser = cur;
}
else{
cur->next = cur;
exit (0);
}
}
if (cur->next = NULL){
//pin does not match
cout << "Incorrect Pin Number, program terminates";
exit (0);
}
}
void System::Activity()
{
char option = 'Y';
int choice;
int withdrawAmount;
case 2:
currUser->aTrans.CheckBalance();
//
option = currUser->aTrans.GetAnotherTrans(); //
break;
break;
case 3:
currUser->aTrans.DisplayDetails();
option = currUser->aTrans.GetAnotherTrans(); //
break;
case 4:
option = 'N';
break;
default:
option = 'N';
break;
}
}
cout << "\nThank You. End of Program." << endl;
}
//
#include<iostream>
using namespace std;
class kedai{
public:
struct keadaimakan{
//string cus_name;
//string cus_no;
string meal1;
float meal1_price;
float gst ;
float totalpay;
Question 5
int numorder;
Write a program to monitor
business process of any business. The process
include receiving
orders from buyers ,checking availability of stock, delivering
}data;
goods to customers and billing invoices.
public:
void menu();
void setorder();
void calctotal();
void delivering();
void checkstock();
};
void kedai::setorder()
{
cout<<"\t\tset 1 : 2pc chicken | ice cream | soda\n\t\tset 2 : 3pc chicken | fried rice
| coffee\n";
cout<<"\t\tset 3 : mashed potato | bread | hot tea\n\t\tset 4 : 1pc chicken | salad |
carbonate drink\n";
cout<<" \n\nEnter your order : ";
cin>>data.meal1;
switch(choice)
{
case 1:
cout<<" \nThe service very bad! Staff not smile to customer
"<<endl;
break;
case 2:
cout<<" \nThe servise quite good , but order get late
"<<endl;
break;
case 3:
cout<<" \nThe servise good , order get on time "<<endl;
break;
case 4:
cout<<" \nThe servise very good , staff always smile to
customer and order also on time "<<endl;
break;
default:
cout<<"\nINVALID"<<endl;
}
}
void kedai::checkstock()
{
cout<<"\n\n";
cout<<"Enter the amount of stock : ";
cin>>data.stock;
cout<<"Enter the amount of number order : ";
cin>>data.numorder;
data.stock_update = data.stock - data.numorder;
cout<<"Available stock is "<<data.stock_update<<endl;
void kedai::menu()
{
cout<<"\n\n";
cout<<"\t\t-------------------------------------"<<endl;
cout<<"\t\t WELCOME TO KEDAI TEPI JALAN "<<endl;
cout<<"\t\t PLEASE SELECT THE MENU ON THE SCREEN "<<endl;
cout<<"\t\t ENJOY YOUR MEAL "<<endl;
cout<<"\t\t-------------------------------------";
cout<<"\n\n";
int main()
{
kedai ked;
ked.menu();
ked.setorder();
ked.calctotal();
ked.delivering();
ked.checkstock();
return 0;
}
Output :