Skip to main content

wordScore.cpp

0 likes • Apr 16, 2023 • 0 views
C++
Loading...

More C++ Posts

GCD using Stein's Algorithm

0 likes • Jun 30, 2023 • 2 views
C++
// Iterative C++ program to
// implement Stein's Algorithm
//#include <bits/stdc++.h>
#include <bitset>
using namespace std;
// Function to implement
// Stein's Algorithm
int gcd(int a, int b)
{
/* GCD(0, b) == b; GCD(a, 0) == a,
GCD(0, 0) == 0 */
if (a == 0)
return b;
if (b == 0)
return a;
/*Finding K, where K is the
greatest power of 2
that divides both a and b. */
int k;
for (k = 0; ((a | b) & 1) == 0; ++k)
{
a >>= 1;
b >>= 1;
}
/* Dividing a by 2 until a becomes odd */
while ((a & 1) == 0)
a >>= 1;
/* From here on, 'a' is always odd. */
do
{
/* If b is even, remove all factor of 2 in b */
while ((b & 1) == 0)
b >>= 1;
/* Now a and b are both odd.
Swap if necessary so a <= b,
then set b = b - a (which is even).*/
if (a > b)
swap(a, b); // Swap u and v.
b = (b - a);
} while (b != 0);
/* restore common factors of 2 */
return a << k;
}
// Driver code
int main()
{
int a = 12, b = 780;
printf("Gcd of given numbers is %d\n", gcd(a, b));
return 0;
}

Parking Lot Simulation

0 likes • Nov 18, 2022 • 1 view
C++
#include<iostream>
#include<fstream>
#include<vector>
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
using namespace std;
//data storage for parking lot info, admin, managers, users, and tickets
struct Parking_Lot_info{
int total_Space;
int regular;
int motorcycle;
int disability;
};
struct Admin{
string name;
string id;
string password;
};
struct Manager{
string name;
string id;
string password;
};
struct User{
string id;
string name;
string password;
string makeModel1;
string makeModel2;
string year;
string plate;
};
struct Ticket{
string ticket_Number;
string ticket_Amount;
string ticket_Issued;
string ticket_Status;
string reason;
string plate;
};
//prototypes
vector<Admin>admin_Vector;
vector<Admin>::iterator admin_it;
vector<Manager>manager_Vector;
vector<Manager>::iterator manager_it;
vector<User>user_Vector;
vector<User>::iterator user_it;
vector<Ticket>ticket_Vector;
vector<Ticket>::iterator ticket_it;
void Initial_Admin_Vector(char admin_People_File_Name[]);
void Initial_Manager_Vector(char management_People_File_Name[]);
void Initial_User_Vector(char user_People_File_Name[]);
void Initial_Ticket_Vector();
void Login_System();
void Checking_Id_Password();
void Admin_Menu();
void Management_Menu();
void User_Menu();
void Add_Admin();
void Remove_Admin();
void Display_All_Admin();
void Search_Admin();
void Change_Password_Admin();
void Add_Manager();
void Remove_Manager();
void Display_All_Manager();
void Search_Manager();
void Add_User();
void Display_All_User();
void Remove_User();
void Search_User();
void Change_Password_User();
void Change_Vehicle_Info();
void Display_All_Vehicle();
void Add_Ticket();
void Display_All_Ticket();
void Search_Ticket();
void Remove_Ticket();
void User_Display_Ticket();
void Pay_Ticket();
void Change_Password_Manager();
void Store_Admin_Data(char admin_People_File_Name[]);
void Store_Manager_Data(char manager_People_File_Name[]);
void Store_User_Data(char user_People_File_Name[]);
void Store_Ticket_Data();
void Initial_Admin_Vector(char admin_People_File_Name[]){
ifstream in;
Admin a;
in.open(admin_People_File_Name);
while(getline(in,a.name,',')){
getline(in,a.id,',');
getline(in,a.password);
admin_Vector.push_back(a);
}
in.close();
}
void Initial_Manager_Vector(char management_People_File_Name[]){
ifstream in;
Manager m;
in.open(management_People_File_Name);
while(getline(in,m.name,',')){
getline(in,m.id,',');
getline(in,m.password);
manager_Vector.push_back(m);
}
in.close();
}
void Initial_User_Vector(char user_People_File_Name[]){
ifstream in;
User u;
in.open(user_People_File_Name);
while(getline(in,u.name,',')){
getline(in,u.id,',');
getline(in,u.password,',');
getline(in,u.makeModel1,',');
getline(in,u.makeModel2,',');
getline(in,u.year,',');
getline(in,u.plate);
user_Vector.push_back(u);
}
in.close();
}
void Initial_Ticket_Vector(){
ifstream in;
Ticket t;
in.open("ticket.csv");
while(getline(in,t.ticket_Number,',')){
getline(in,t.ticket_Amount,',');
getline(in,t.ticket_Issued,',');
getline(in,t.ticket_Status,',');
getline(in,t.reason,',');
getline(in,t.plate);
ticket_Vector.push_back(t);
}
in.close();
}
void Login_System(){
int first_Option;
bool flag = true;
do{
cout << "Type in 1 to Log in to the system." << endl;
cout << "Type in 2 to register to the system." << endl;
cout << "Type in 3 to exit the system." << endl;
cout << "Option: ";
cin >> first_Option;
switch(first_Option){
case 1:
Checking_Id_Password();
flag = false;
break;
case 2:
Add_User();
break;
case 3:
flag = false;
break;
}
}while(flag);
}
void Checking_Id_Password(){
string Id1;
string Password1;
bool flag = true;
do{
cout << "Enter id: ";
cin >> Id1;
cout << "Enter password: ";
cin >> Password1;
for(admin_it = admin_Vector.begin(); admin_it != admin_Vector.end(); admin_it++){
if(Id1 == admin_it->id && Password1 == admin_it->password){
cout << endl;
Admin_Menu();
flag = false;
break;
}
}
for(manager_it = manager_Vector.begin(); manager_it != manager_Vector.end(); manager_it++){
if(Id1 == manager_it->id && Password1 == manager_it->password){
cout << endl;
Management_Menu();
flag = false;
break;
}
}
for(user_it = user_Vector.begin(); user_it != user_Vector.end(); user_it++){
if(Id1 == user_it->id && Password1 == user_it->password){
cout << endl;
User_Menu();
flag = false;
break;
}
}
if(flag){
cout << "ID or Password is wrong. Enter your credentials again."<<endl<<endl;
}
}while(flag);
}
void Admin_Menu(){
int Admin_Option;
bool flag = true;
do{
cout << "Select the following option for a particular task." << endl;
cout << "Select 1 to add new administrative employee." << endl;
cout << "Select 2 to remove an administrative employee." << endl;
cout << "Select 3 to view all administrative employee information." << endl;
cout << "Select 4 to search information of a specific administrative employee." << endl;
cout << "Select 5 to change password." << endl;
cout << "Select 6 to add new management employee." << endl;
cout << "Select 7 to remove a management employee." << endl;
cout << "Select 8 to view all management employee information." << endl;
cout << "Select 9 to search information of a specific management employee." << endl;
cout << "Select 10 to remove an user." << endl;
cout << "Select 11 to view all user information." << endl;
cout << "Select 12 to search information of a specific user." << endl;
cout << "Select 15 to Log out." << endl;
cout << "Option: ";
cin >> Admin_Option;
switch(Admin_Option){ //switch to manage the option selection
case 1:
Add_Admin();
break;
case 2:
Remove_Admin();
break;
case 3:
Display_All_Admin();
break;
case 4:
Search_Admin();
break;
case 5:
Change_Password_Admin();
break;
case 6:
Add_Manager();
break;
case 7:
Remove_Manager();
break;
case 8:
Display_All_Manager();
break;
case 9:
Search_Manager();
break;
case 10:
Remove_User();
break;
case 11:
Display_All_User();
break;
case 12:
Search_User();
break;
case 15:
flag = false;
cout << endl;
Login_System();
break;
}
}while(flag);
}
void Management_Menu(){
int Manager_Option;
bool flag = true;
do{
cout << "Select the following option for a particular task." << endl;
cout << "Select 1 to remove an user." << endl;
cout << "Select 2 to view all user information." << endl;
cout << "Select 3 to search information of a specific user." << endl;
cout << "Select 4 to issue a ticket." << endl;
cout << "Select 5 to view all the ticket information." << endl;
cout << "Select 6 to view a specific ticket information in details." << endl;
cout << "Select 7 to remove a specific ticket information." << endl;
cout << "Select 8 to change password." << endl;
cout << "Select 9 to Log out." << endl;
cout << "Option: ";
cin >> Manager_Option;
switch(Manager_Option){ //ditto
case 1:
Remove_User();
break;
case 2:
Display_All_User();
break;
case 3:
Search_User();
break;
case 4:
Add_Ticket();
break;
case 5:
Display_All_Ticket();
break;
case 6:
Search_Ticket();
break;
case 7:
Remove_Ticket();
break;
case 8:
Change_Password_Manager();
break;
case 9:
flag = false;
cout << endl;
Login_System();
break;
}
}while(flag);
}
void User_Menu(){
int User_Option;
bool flag = true;
do{
cout << "Select the following option for a particular task." << endl;
cout << "Select 1 to change password." << endl;
cout << "Select 2 to change vehicle information." << endl;
cout << "Select 3 to view vehicle information." << endl;
cout << "Select 4 to view all ticket information." << endl;
cout << "Select 5 to pay a ticket amount." << endl;
cout << "Select 8 to Log out." << endl;
cout << "Option: ";
cin >> User_Option;
switch(User_Option){
case 1:
Change_Password_User();
break;
case 2:
Change_Vehicle_Info();
break;
case 3:
Display_All_Vehicle();
break;
case 4:
User_Display_Ticket();
break;
case 5:
Pay_Ticket();
break;
case 8:
flag = false;
cout << endl;
Login_System();
break;
}
}while(flag);
}
void Add_Admin(){
string add_name;
string add_id;
string add_password;
Admin a;
srand((unsigned)time(NULL));
bool flag = true;
char temp1[100];
int temp2;
cout << "Enter administrative employee name: ";
cin.ignore();
getline(cin,add_name);
cout << "Auto generated id for this administrative employee account is: ";
do{
temp2 = rand()%400 + 101;
sprintf(temp1, "%d", temp2);
add_id = temp1;
for(admin_it = admin_Vector.begin(); admin_it != admin_Vector.end(); admin_it++){
if(add_id == admin_it->id){
break;
}
}
if(admin_it == admin_Vector.end()){
flag = false;
}
}while(flag);
cout << add_id << endl;
cout << "Set a password for this administrative employee account: ";
getline(cin,add_password);
cout << "administrative employee entry is complete." << endl << endl;
a.name = add_name;
a.id = add_id;
a.password = add_password;
admin_Vector.push_back(a);
}
void Remove_Admin(){
bool flag = true;
string remove_id;
cout << "Enter the administrative employee's Id: ";
cin >> remove_id;
for(admin_it = admin_Vector.begin(); admin_it != admin_Vector.end(); admin_it++){
if(admin_it->id == remove_id){
admin_Vector.erase(admin_it);
cout << "Administrative employee is removed." <<endl <<endl;
flag = false;
break;
}
}
if(flag){
cout << "Fail to remove the administrative employee's information!" << endl <<endl;
}
}
void Display_All_Admin(){
cout << "All Administrative Employee Information" << endl;
cout << "---------------------------------------" << endl;
cout << " Name ID" << endl;
cout << "-----------------------------------" << endl;
for(admin_it = admin_Vector.begin(); admin_it != admin_Vector.end(); admin_it++){
cout << admin_it->name << "\t" << "\t" << "\t";
cout << admin_it->id << endl;
}
cout <<endl << endl;
}
void Search_Admin(){
bool flag = true;
string search_id;
cout << "Enter the administrative employee's id: ";
cin >> search_id;
cout << " Name ID" << endl;
cout << "-----------------------------------" << endl;
for(admin_it = admin_Vector.begin(); admin_it != admin_Vector.end(); admin_it++){
if(admin_it->id == search_id){
cout << admin_it->name << "\t" << "\t" << "\t";
cout << admin_it->id << endl << endl;
flag = false;
break;
}
}
if(flag){
cout << "Can't find this administrative employee's information!" << endl << endl;
}
cout <<endl << endl;
}
void Change_Password_Admin(){
string new_password;
cout << "Enter a new password: ";
cin >> new_password;
admin_it->password = new_password;
cout << "You have changed your password successfully!" <<endl;
cout << endl;
}
void Add_Manager(){
string add_name;
string add_id;
string add_password;
Manager m;
srand((unsigned)time(NULL));
bool flag = true;
char temp1[100];
int temp2;
cout << "Enter management employee name: ";
cin.ignore();
getline(cin,add_name);
cout << "Auto generated id for this management employee account is: ";
do{
temp2 = rand()%500 + 501;
sprintf(temp1, "%d", temp2);
//itoa(temp2,temp1,10);
add_id = temp1;
for(manager_it = manager_Vector.begin(); manager_it != manager_Vector.end(); manager_it++){
if(add_id == manager_it->id){
break;
}
}
if(manager_it == manager_Vector.end()){
flag = false;
}
}while(flag);
cout << add_id << endl;
cout << "Set a password for this management employee account: ";
getline(cin,add_password);
cout << "Management employee entry is complete." << endl << endl;
m.name = add_name;
m.id = add_id;
m.password = add_password;
manager_Vector.push_back(m);
}
void Remove_Manager(){
bool flag = true;
string remove_id;
cout << "Enter the management employee's id: ";
cin >> remove_id;
for(manager_it = manager_Vector.begin(); manager_it != manager_Vector.end(); manager_it++){
if(manager_it->id == remove_id){
manager_Vector.erase(manager_it);
cout << "Management employee is removed." <<endl <<endl;
flag = false;
break;
}
}
if(flag){
cout << "Fail to remove the management employee's information!" << endl <<endl;
}
}
void Display_All_Manager(){
cout << "------All Employee Information-----" << endl;
cout << "-----------------------------------" << endl;
cout << " Name ID" << endl;
cout << "-----------------------------------" << endl;
for(manager_it = manager_Vector.begin(); manager_it != manager_Vector.end(); manager_it++){
cout << manager_it->name << "\t" << "\t" << "\t";
cout << manager_it->id << endl;
}
cout <<endl << endl;
}
void Search_Manager(){
bool flag = true;
string search_id;
cout << "Enter the management employee's id: ";
cin >> search_id;
cout << " Name ID" << endl;
cout << "-----------------------------------" << endl;
for(manager_it = manager_Vector.begin(); manager_it != manager_Vector.end(); manager_it++){
if(manager_it->id == search_id){
cout << manager_it->name << "\t" << "\t" << "\t";
cout << manager_it->id << endl << endl;
flag = false;
break;
}
}
if(flag){
cout << "Can't find this management employee's information!" << endl << endl;
}
cout << endl << endl;
}
void Add_User(){
string add_name;
string add_id;
string add_password;
string add_makeModel1;
string add_makeModel2;
string add_year;
string add_plate;
User u;
bool flag = true;
cout << "Enter name: ";
cin.ignore();
getline(cin,add_name);
do{
cout << "Enter id: ";
//cin.ignore();
getline(cin,add_id);
for(user_it = user_Vector.begin(); user_it != user_Vector.end(); user_it++){
if(add_id == user_it->id){
cout << "This id is already in use! Please Enter again!" << endl;
break;
}
}
if(user_it == user_Vector.end()){
flag = false;
}
}while(flag);
cout << "Enter password: ";
//cin.ignore();
getline(cin,add_password);
cout << "Now enter your vehicle information..." << endl;
cout << "Enter vehicle make: ";
//cin.ignore();
getline(cin,add_makeModel1);
cout << "Enter vehicle model: ";
//cin.ignore();
getline(cin,add_makeModel2);
cout << "Enter vehicle year: ";
//cin.ignore();
getline(cin,add_year);
cout << "Enter vehicle plate number: ";
//cin.ignore();
getline(cin,add_plate);
cout << "Registration is complete" << endl << endl;
u.name = add_name;
u.id = add_id;
u.password = add_password;
u.makeModel1 = add_makeModel1;
u.makeModel2 = add_makeModel2;
u.year = add_year;
u.plate = add_plate;
user_Vector.push_back(u);
}
void Remove_User(){
bool flag = true;
string remove_id;
cout << "Enter the user's id: ";
cin >> remove_id;
for(user_it = user_Vector.begin(); user_it != user_Vector.end(); user_it++){
if(user_it->id == remove_id){
user_Vector.erase(user_it);
cout << "User is removed." <<endl <<endl;
flag = false;
break;
}
}
if(flag){
cout << "Fail to remove the user's information!" << endl <<endl;
}
}
void Display_All_User(){
cout << "-----------------------------------------------------All User Information-------------------------------------------------------" << endl;
cout << "--------------------------------------------------------------------------------------------------------------------------------" << endl;
cout << " Name ID Make Model Year Plate Number" << endl;
cout << "--------------------------------------------------------------------------------------------------------------------------------" << endl;
for(user_it = user_Vector.begin(); user_it != user_Vector.end(); user_it++){
cout << user_it->name << "\t" << "\t" << "\t";
cout << user_it->id << "\t" << "\t";
cout << user_it->makeModel1 << "\t"<< "\t";
cout << user_it->makeModel2 << "\t" << "\t"<< "\t";
cout << user_it->year << "\t" << "\t";
cout << user_it->plate << endl;
}
cout <<endl << endl;
}
void Search_User(){
bool flag = true;
string search_id;
cout << "Enter the user's id: ";
cin >> search_id;
cout << " Name ID Make Model Year Plate Number" << endl;
cout << "--------------------------------------------------------------------------------------------------------------------------------" << endl;
for(user_it = user_Vector.begin(); user_it != user_Vector.end(); user_it++){
if(user_it->id == search_id){
cout << user_it->name << "\t" << "\t" << "\t";
cout << user_it->id << "\t" << "\t";
cout << user_it->makeModel1 << "\t"<< "\t";
cout << user_it->makeModel2 << "\t" << "\t"<< "\t";
cout << user_it->year << "\t" << "\t";
cout << user_it->plate << endl;
flag = false;
break;
}
}
if(flag){
cout << "Can't find this user's information!" << endl << endl;
}
cout << endl << endl;
}
void Change_Password_User(){
string new_password;
cout << "Enter a new password: ";
cin >> new_password;
user_it->password = new_password;
cout << "You have changed your password successfully!" <<endl;
cout << endl;
}
void Change_Vehicle_Info(){
string new_MakeModel1;
string new_MakeModel2;
string new_Year;
string new_Plate;
cout << "Enter the new vehicle make: ";
cin.ignore();
getline(cin,new_MakeModel1);
cout << "Enter the new vehicle model: ";
//cin.ignore();
getline(cin,new_MakeModel2);
cout << "Enter the new vehicle year: ";
//cin.ignore();
getline(cin,new_Year);
cout << "Enter the new vehicle plate number: ";
//cin.ignore();
getline(cin,new_Plate);
user_it->makeModel1 = new_MakeModel1;
user_it->makeModel2 = new_MakeModel2;
user_it->year = new_Year;
user_it->plate = new_Plate;
cout << "You have changed your vehicle information successfully!" <<endl << endl;
}
void Display_All_Vehicle(){
cout << endl;
cout << "Vehicle make: " << user_it->makeModel1 << endl;
cout << "Vehicle model: " << user_it->makeModel2 << endl;
cout << "Vehicle year: " << user_it->year << endl;
cout << "Vehicle plate number: " << user_it->plate << endl << endl;
}
void Add_Ticket(){
string add_ticket_number;
string add_ticket_amount;
string add_Reason;
string add_plate;
Ticket t;
srand((unsigned)time(NULL));
bool flag = true;
char temp1[100];
int temp2;
string ticketIssued;
string ticketStat;
cout << "Auto generated ticket number is: ";
do{
temp2 = rand()%49000 + 1001;
sprintf(temp1, "%d", temp2);
//itoa(temp2,temp1,10);
add_ticket_number = temp1;
for(ticket_it = ticket_Vector.begin(); ticket_it != ticket_Vector.end(); ticket_it++){
if(add_ticket_number == ticket_it->ticket_Number){
break;
}
}
if(ticket_it == ticket_Vector.end()){
flag = false;
}
}while(flag);
cout << add_ticket_number << endl;
cout << "Enter ticket amount: ";
cin.ignore();
getline(cin,add_ticket_amount);
cout << "Reason of ticket: ";
getline(cin,add_Reason);
cout << "Enter vehicle plate number: ";
getline(cin,add_plate);
cout << "Ticket has been recorded successfully."<<endl<<endl;
ticketIssued = manager_it->id;
ticketStat = "Unpaid";
t.ticket_Number = add_ticket_number;
t.ticket_Amount = add_ticket_amount;
t.ticket_Issued = ticketIssued;
t.ticket_Status = ticketStat;
t.reason = add_Reason;
t.plate = add_plate;
ticket_Vector.push_back(t);
}
void Display_All_Ticket(){
cout << "Ticket number Ticket amount Issued by Ticket Status Reason" << endl;
cout << "--------------------------------------------------------------------------" << endl;
for(ticket_it = ticket_Vector.begin(); ticket_it != ticket_Vector.end(); ticket_it++){
cout << "\t" << ticket_it->ticket_Number << "\t" << "\t";
cout << "$" << ticket_it->ticket_Amount << "\t" << "\t";
cout << ticket_it->ticket_Issued << "\t"<< "\t";
cout << ticket_it->ticket_Status << "\t"<< "\t";
cout << ticket_it->reason << endl;
}
cout <<endl << endl;
}
void Search_Ticket(){
bool flag = true;
string search_number;
cout << "Enter the ticket number: ";
cin.ignore();
getline(cin,search_number);
cout << "Ticket number Ticket amount Issued by Ticket Status Reason" << endl;
cout << "--------------------------------------------------------------------------" << endl;
for(ticket_it = ticket_Vector.begin(); ticket_it != ticket_Vector.end(); ticket_it++){
if(ticket_it->ticket_Number == search_number){
cout << "\t" << ticket_it->ticket_Number << "\t" << "\t";
cout << "$" << ticket_it->ticket_Amount << "\t" << "\t";
cout << ticket_it->ticket_Issued << "\t"<< "\t";
cout << ticket_it->ticket_Status << "\t"<< "\t";
cout << ticket_it->reason << endl;
}
}
cout <<endl << endl;
}
void Remove_Ticket(){
bool flag = true;
string remove_number;
cout << "Enter the ticket number: ";
cin.ignore();
getline(cin,remove_number);
for(ticket_it = ticket_Vector.begin(); ticket_it != ticket_Vector.end(); ticket_it++){
if(ticket_it->ticket_Number == remove_number){
ticket_Vector.erase(ticket_it);
cout << endl;
cout << "Ticket is removed." <<endl <<endl;
flag = false;
break;
}
}
if(flag){
cout << "Fail to remove the ticket!" << endl <<endl;
}
}
void Change_Password_Manager(){
string new_password;
cout << "Enter a new password: ";
cin >> new_password;
manager_it->password = new_password;
cout << "You have changed your password successfully!" <<endl;
cout << endl;
}
void User_Display_Ticket(){
cout << "Ticket number Ticket amount Issued by Ticket Status Reason" << endl;
cout << "--------------------------------------------------------------------------" << endl;
for(ticket_it = ticket_Vector.begin(); ticket_it != ticket_Vector.end(); ticket_it++){
if(user_it->plate == ticket_it->plate){
cout << "\t" << ticket_it->ticket_Number << "\t" << "\t";
cout << "$" << ticket_it->ticket_Amount << "\t" << "\t";
cout << ticket_it->ticket_Issued << "\t"<< "\t";
cout << ticket_it->ticket_Status << "\t"<< "\t";
cout << ticket_it->reason << endl;
break;
}
}
cout <<endl << endl;
}
void Pay_Ticket(){
string YorN;
cout << "Do you want to pay $" << ticket_it->ticket_Amount << " right now? (Type in Yes or No):";
cin >> YorN;
if(YorN == "Yes" || YorN == "YES" || YorN == "yes"){
for(ticket_it = ticket_Vector.begin(); ticket_it != ticket_Vector.end(); ticket_it++){
if(user_it->plate == ticket_it->plate){
ticket_it->ticket_Amount = "0";
ticket_it->ticket_Status = "Paid";
cout << "The ticket amount is paid off." << endl << endl;
break;
}
}
}
else if(YorN == "No" || YorN == "NO" || YorN == "no"){
cout << endl << endl;
}
}
void Store_Admin_Data(char admin_People_File_Name[]){ //Store all the changes to the admin's data in to the file.
ofstream out;
out.open(admin_People_File_Name);
for(admin_it = admin_Vector.begin(); admin_it != admin_Vector.end(); admin_it++){
out << admin_it->name << ",";
out << admin_it->id << ",";
out << admin_it->password << endl;
}
out.close();
}
void Store_Manager_Data(char management_People_File_Name[]){ //Store all the changes to the manager's data in to the file.
ofstream out;
out.open(management_People_File_Name);
for(manager_it = manager_Vector.begin(); manager_it != manager_Vector.end(); manager_it++){
out << manager_it->name << ",";
out << manager_it->id << ",";
out << manager_it->password << endl;
}
out.close();
}
void Store_User_Data(char user_People_File_Name[]){ //Store all the changes to the user's data in to the file.
ofstream out;
out.open(user_People_File_Name);
for(user_it = user_Vector.begin(); user_it != user_Vector.end(); user_it++){
out << user_it->name << ",";
out << user_it->id << ",";
out << user_it->password << ",";
out << user_it->makeModel1 << ",";
out << user_it->makeModel2 << ",";
out << user_it->year << ",";
out << user_it->plate << endl;
}
out.close();
}
void Store_Ticket_Data(){ //Store all the changes to the ticket's data in to the file.
ofstream out;
out.open("ticket.csv");
for(ticket_it = ticket_Vector.begin(); ticket_it != ticket_Vector.end(); ticket_it++){
out << ticket_it->ticket_Number << ",";
out << ticket_it->ticket_Amount << ",";
out << ticket_it->ticket_Issued << ",";
out << ticket_it->ticket_Status << ",";
out << ticket_it->reason << ",";
out << ticket_it->plate << endl;
}
out.close();
}
int main(){
//char array to store the user input for the file search
char input_File_Name[100];
char admin_People_File_Name[100];
char management_People_File_Name[100];
char user_People_File_Name[100];
ifstream in;
Parking_Lot_info *pli; //Structure pointer
int pln; //Parking lot number
int max_Space = 8; // Max number of parking space in a row
int t = 0; //Determine when it needs to switch to the next row
//relevant strings for admin, management, and users
string admin_Name, admin_Id, admin_Password;
string management_Name, management_Id, management_Password;
string user_Name, user_Id, user_Password;
string user_Make_Model1, user_Make_Model2, user_Year, user_Plate_Number;
//get filename
cout << "Enter file name: ";
cin.getline(input_File_Name,100);
in.open(input_File_Name);
if(!in){
cout << "Can't open the file!" << endl; //error check
return 0;
}
in >> pln;
pli = new Parking_Lot_info[pln];
//read data in for parking lot
for(int i=0; i<pln; i++){
in >> pli[i].regular;
in >> pli[i].motorcycle;
in >> pli[i].disability;
pli[i].total_Space = pli[i].regular + pli[i].motorcycle + pli[i].disability;
//display the initial read in data for parking lot
cout << "Parking lot number: " << i+1 << endl;
cout << "Number of parking space: " << pli[i].total_Space << endl;
cout << "Number of regular parking space: " << pli[i].regular << endl;
cout << "Number of motorcycle parking space: " << pli[i].motorcycle << endl;
cout << "Number of disability parking space: " << pli[i].disability << endl;
cout << "Parking space layout is shown below." << endl;
cout << "---------------------------------" << endl;
cout << "|";
t = 0; //Reset t to 0;
for(int j=0; j <pli[i].motorcycle; j++){
t++;
cout << " M " << "|";
if(t%max_Space == 0){
cout << endl;
cout << "---------------------------------" << endl;
}
}
for(int k=0; k <pli[i].disability; k++){
t++;
cout << " D " << "|";
if(t%max_Space == 0){
cout << endl;
cout << "---------------------------------" << endl;
}
}
for(int l=0; l <pli[i].regular; l++){
t++;
cout << " R " << "|";
if(t%max_Space == 0){
cout << endl;
cout << "---------------------------------" << endl;
}
}
cout << endl;
cout << "---------------------------------" << endl;
cout << endl;
}
//read in admin, management, and user data
in >> admin_People_File_Name;
in >> management_People_File_Name;
in >> user_People_File_Name;
in.close();
//display initial administrative employee info
in.open(admin_People_File_Name);
cout << "All Administrative Employee Information" << endl;
cout << "---------------------------------------" << endl;
cout << " Name ID" << endl;
cout << "-----------------------------------" << endl;
while(getline(in,admin_Name,',')){
cout << admin_Name << "\t" << "\t" << "\t";
getline(in,admin_Id,',');
cout << admin_Id << endl;
getline(in,admin_Password);
}
cout << endl;
in.close();
//display all employee information
in.open(management_People_File_Name);
cout << "------All Employee Information-----" << endl;
cout << "-----------------------------------" << endl;
cout << " Name ID" << endl;
cout << "-----------------------------------" << endl;
while(getline(in,management_Name,',')){
cout << management_Name << "\t" << "\t" << "\t";
getline(in,management_Id,',');
cout << management_Id << endl;
getline(in,management_Password);
}
cout << endl;
in.close();
//display all user information
in.open(user_People_File_Name);
cout << "-----------------------------------------------------All User Information-------------------------------------------------------" << endl;
cout << "--------------------------------------------------------------------------------------------------------------------------------" << endl;
cout << " Name ID Make Model Year Plate Number" << endl;
cout << "--------------------------------------------------------------------------------------------------------------------------------" << endl;
while(getline(in,user_Name,',')){
cout << user_Name << "\t" << "\t" << "\t";
getline(in,user_Id,',');
cout << user_Id << "\t" << "\t";
getline(in,user_Password,',');
getline(in,user_Make_Model1,',');
cout << user_Make_Model1 << "\t"<< "\t";
getline(in,user_Make_Model2,',');
cout << user_Make_Model2 << "\t" << "\t"<< "\t";
getline(in,user_Year,',');
cout << user_Year << "\t" << "\t";
getline(in,user_Plate_Number);
cout << user_Plate_Number << endl;
}
cout << endl;
in.close();
Initial_Admin_Vector(admin_People_File_Name);
Initial_Manager_Vector(management_People_File_Name);
Initial_User_Vector(user_People_File_Name);
Initial_Ticket_Vector();
Login_System();
//store NEW admin, manager, user, and ticket data
Store_Admin_Data(admin_People_File_Name);
Store_Manager_Data(management_People_File_Name);
Store_User_Data(user_People_File_Name);
Store_Ticket_Data();
return 0;
}

Big O(n^2) Ascending Sort

0 likes • Nov 18, 2022 • 6 views
C++
#include <iostream>
using namespace std;
int main() {
int arr[5];
for(int i = 0; i < 5; i++) {
arr[i] = i;
}
for(int i = 0; i < 5; i++) {
cout << "Outputting array info at position " << i + 1 << ": " << arr[i] << endl;
}
for(int i=0;i<5;i++)
{
for(int j=i+1;j<5;j++)
{
if(arr[i]>arr[j])
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout << endl;
for(int i = 0; i < 5; i++) {
cout << "Outputting sorted array info at position " << i + 1 << ": " << arr[i] << endl;
}
return 0;
}

Bit arithmetic + and -

0 likes • Sep 1, 2023 • 2 views
C++
#define NUM_BITS 8
#include <iostream>
struct Number{
int num : NUM_BITS;
Number(){}
Number(const int& bruh){
num = bruh;
}
operator int() const { return num; }
Number& operator=(const int& bruh){
num = bruh;
return (*this);
}
};
using namespace std;
bool isNegative(const int& num){
//This gets the bitwise and of num and 10000000000000000000000000000000
//This implicit casts to bool, which means (num & (1 << 31)) != 0
return (num & (1 << 31));
}
void printBinaryNumber(const int& num, const int numBits){
for(int i = numBits; i > 0; --i){
//8..1
int bitMask = 1 << (i-1);
if(num & bitMask){ //Test the bit
cout << '1';
}else{
cout << '0';
}
}
}
void printCarryBits(const int& a, const int& b, const int numBits){
int answer = 0;
bool carry = false;
for(int i = 0; i < numBits; ++i){
//8..1
int bitMask = 1 << i;
bool aBit = a & bitMask;
bool bBit = b & bitMask;
if(aBit && bBit || aBit && carry || bBit && carry){ //Carry bit is true next
if(carry)
answer |= bitMask;
carry = true;
}else{
if(carry)
answer |= bitMask;
carry = false;
}
}
printBinaryNumber(answer, 8);
}
void printBorrowBits(const int& a, const int& b, const int numBits){
int answer = 0;
bool carry = false;
for(int i = 0; i < numBits; ++i){
//8..1
int bitMask = 1 << i;
bool aBit = a & bitMask;
bool bBit = b & bitMask;
if((!(aBit ^ carry)) && bBit){ //Carry bit is true next
if(carry)
answer |= bitMask;
carry = true;
}else{
if(carry)
answer |= bitMask;
carry = false;
}
}
printBinaryNumber(answer, 8);
}
void doProblem(const int& a, const int& b, const char& sign, const int& result, const int& numBits){
if(sign == '+'){
cout << ' '; printCarryBits(a, b, numBits); cout << endl;
}else{
cout << ' '; printBorrowBits(a, b, numBits); cout << endl;
}
cout << ' '; printBinaryNumber(a, numBits); cout << endl;
cout << sign; printBinaryNumber(b, numBits); cout << endl;
cout << "----------" << endl;
cout << ""; printBinaryNumber(result, numBits + 1); cout << " = " << result;
cout << endl;
}
int main(){
Number a = 0b110;
Number b = 0b011;
cout<< a << endl << b << endl;
doProblem(a, b, '+', a + b, NUM_BITS);
doProblem(a, b, '-', a - b, NUM_BITS);
doProblem(-a, b, '+', -a + b, NUM_BITS);
doProblem(a, b, '-', -a - b, NUM_BITS);
return 0;
}

Hello, World!

0 likes • Nov 18, 2022 • 2 views
C++
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!" << endl;
return 0;
}

PlaylistNode.cpp (lab 9)

0 likes • Nov 18, 2022 • 0 views
C++
#include <string>
#include <iostream>
#include "PlaylistNode.h"
using namespace std;
PlaylistNode::PlaylistNode() {
uniqueID = "none";
songName = "none";
artistName = "none";
songLength = 0;
nextNodePtr = 0;
}
PlaylistNode::PlaylistNode(string uniqueID_, string songName_, string artistName_, int songLength_) {
uniqueID = uniqueID_;
songName = songName_;
artistName = artistName_;
songLength = songLength_;
nextNodePtr = 0;
}
void PlaylistNode::InsertAfter(PlaylistNode* ptr) {
this->SetNext(ptr->GetNext());
ptr->SetNext(this);
}
void PlaylistNode::SetNext(PlaylistNode* ptr) {
nextNodePtr = ptr;
}
string PlaylistNode::GetID() {
return uniqueID;
}
string PlaylistNode::GetSongName() {
return songName;
}
string PlaylistNode::GetArtistName() {
return artistName;
}
int PlaylistNode::GetSongLength() {
return songLength;
}
PlaylistNode* PlaylistNode::GetNext() {
return nextNodePtr;
}
void PlaylistNode::PrintPlaylistNode() {
cout << "Unique ID: " << uniqueID << endl;
cout << "Song Name: " << songName << endl;
cout << "Artist Name: " << artistName << endl;
cout << "Song Length (in seconds): " << songLength << endl;
}
Playlist::Playlist() {
head = tail = 0;
}
void Playlist::AddSong(string id, string songname, string artistname, int length) {
PlaylistNode* n = new PlaylistNode(id, songname, artistname, length);
if (head == 0) {
head = tail = n;
}
else {
n->InsertAfter(tail);
tail = n;
}
}
bool Playlist::RemoveSong(string id) {
if (head == NULL) {
cout << "Playlist is empty" << endl;
return false;
}
PlaylistNode* curr = head;
PlaylistNode* prev = NULL;
while (curr != NULL) {
if (curr->GetID() == id) {
break;
}
prev = curr;
curr = curr->GetNext();
}
if (curr == NULL) {
cout << "\"" << curr->GetSongName() << "\" is not found" << endl;
return false;
}
else {
if (prev != NULL) {
prev ->SetNext(curr->GetNext());
}
else {
head = curr->GetNext();
}
if (tail == curr) {
tail = prev;
}
cout << "\"" << curr->GetSongName() << "\" removed." << endl;
delete curr;
return true;
}
}
bool Playlist::ChangePosition(int oldPos, int newPos) {
if (head == NULL) {
cout << "Playlist is empty" << endl;
return false;
}
PlaylistNode* prev = NULL;
PlaylistNode* curr = head;
int pos;
if (head == NULL || head == tail) {
return false;
}
for (pos = 1; curr != NULL && pos < oldPos; pos++) {
prev = curr;
curr = curr->GetNext();
}
if (curr != NULL) {
string currentSong = curr->GetSongName();
if (prev == NULL) {
head = curr->GetNext();
}
else {
prev->SetNext(curr->GetNext());
}
if (curr == tail) {
tail = prev;
}
PlaylistNode* curr1 = curr;
prev = NULL;
curr = head;
for (pos = 1; curr != NULL && pos < newPos; pos++) {
prev = curr;
curr = curr->GetNext();
}
if (prev == NULL) {
curr1->SetNext(head);
head = curr1;
}
else {
curr1->InsertAfter(prev);
}
if (curr == NULL) {
tail = curr1;
}
cout << "\"" << currentSong << "\" moved to position " << newPos << endl;
return true;
}
else {
cout << "Song's current position is invalid" << endl;
return false;
}
}
void Playlist::SongsByArtist(string artist) {
if (head == NULL) {
cout << "Playlist is empty" << endl;
}
else {
PlaylistNode* curr = head;
int i = 1;
while (curr != NULL) {
if (curr->GetArtistName() == artist) {
cout << endl << i << "." << endl;
curr->PrintPlaylistNode();
}
curr = curr->GetNext();
i++;
}
}
}
int Playlist::TotalTime() {
int total = 0;
PlaylistNode* curr = head;
while (curr != NULL) {
total += curr->GetSongLength();
curr = curr->GetNext();
}
return total;
}
void Playlist::PrintList() {
if (head == NULL) {
cout << "Playlist is empty" << endl;
}
else {
PlaylistNode* curr = head;
int i = 1;
while (curr != NULL) {
cout << endl << i++ << "." << endl;
curr->PrintPlaylistNode();
curr = curr->GetNext();
}
}
}