Skip to main content

C++ wala SQL (Project)(currently not complete)

//now posting as TECHY 3 ;-)

//New- Database Capability (groundwork ready for that)

// Further idea- Have an option to save the tables/databases in a database, or different databases
//               Declare more classes to get 1 column, 2 column.. tables also, probably maximum 5 columns
//               Have option to give/change Row names
// Create unlimited number of tables in a database using Linked List
// Maintain both, database with maximum 5 tables and database with more capabilities

//Features to be depricated
// Database with limitation of upto 5 tables

//Programmer - Aditya Gupta

#include<iostream>
#include<stdlib.h>
#include<string.h>
//#include<fstream> // will be required when save in database supported
using namespace std;

void inline title(void){
system("cls");
cout<<"\tNAMASTE!...\n\nProgram-C++ wala SQL\nPrograammer - MR. ADITYA GUPTA (Techy15)\n\n";
}
struct Node{
int dat1,dat2,dat3;
Node *next;
};

class Table{  //NOTE: I HAVE REMOVED ALL COMMENTS, REFER TO THE LINKED LIST TEMPLATE FOR DETAILED COMMENTS
Node *top,*rear;
char name[], A[], B[], C[]; //table name and column names
int asize, bsize, csize;
int state; //state has values 0 and 1, ie 0 if the table has never been used (being empty but even used once will get state=1)
char type; //type has values 's' or 'n' (currently)
   //s-special, n-normal
   //special table is the table i wanted (ie. havihng row names also)
public: int nrow;
void Push(int a1,int a2,int a3);
void Push(int a1,int a2, int a3, int loc); //THIS IS NOT IN SQL! :p
void tPush(int a1, int a2, int a3); //Places a new entry at top (THIS IS NOT IN SQL!)
void RWHERE(int a1, int a2=-1, int a3=-1); //random where (NOT IN SQL!)
   //IN CASE USER DOESNT REMEMBER WHICH COLUMN THE ELEMENT HE REMEMBERS BELONGS TO
void WHERE(int a1, int a2, int a3); //where() is an updated & advanced form of search()
void WHERE(int a1, int a2); //SUGGESTION- WHERE() KE ANDAR HI TABLE DISPLAY KARWALO AND CHOICE TO EDIT/DELETE PUCHH LO
void WHERE(int a1);
void WHERE(); //asks which WHERE function will the user want to use
int noofrow();
void Pop(); //SUGGESTED IMPROVEMENTS- 1. write pop(int,int,int) [for that user will have to remember all three of these]
//                        2. (i) get a function like WHERE a1=2, a2=3, a3=4 (same as suggestion 1)
//                           (AVAILABLE NOW!)(ii) get a function like Where a1=3, a2=3; or any other combination, TO REDUCE NUMBER OF COLUMNS DISPLAYED
void Pop(int loc);
void disp();
Table(){ top=rear=NULL; nrow=0;
state=0; //table name not to be initialised, since if unused state=0, and if used then it will have a name!
}
void ALTER(); //to ask for columnn names and table name
~Table(){   //Q. WILL DESTRUCTOR REQUIRE DELETE STATEMENTS? (LIKE DELETE TOP; ETC. ?)
}
void help(); //shows 'demo' of the table types available
};

/*   //A TRIAL DEFINITION OF A CLASS 'DATABASE HAVING UNLIMITED NUMBER OF TABLES'
struct TabNode{
Table Tab;
TabNode *nexttab;
};

class LDatabase{  //for managing databses with more than 5 tables
TabNode *top, *rear;
Database(){
top=rear=NULL;
}
char filename[32];
public: void Save();  //FUNCTIONS SAME AS GIVEN IN COMMENTS OF OTHER DEFINITION OF CLASS
void del();
};

*/

class Database{  //for managing databases with at max 5 tables
char filename[30]; //filename to which database to be saved
int stat; //stat has two values -1, 1 and 2
  //stat has 0 when database OK, ie less than 4 tables used (4 tables with state=1)
  //stat has 1 when database about to fill, ie 4 tables used; when stat=1 shows warning that whether the user will require more than 5 tables in the particular database
  //stat has 2 when database filled, ie all 5 tables used
Table Tab1, Tab2, Tab3, Tab4, Tab5; //maximum 5 tables
public: void Save(); //saves to binary file; name of file as given by user
void del(); //deletes the data of this databse to a file
//Q. CAN IT REALLY BE DONE ACCURATELY? (I MEAN DOESNT FOUT.READ(...) REQUIRES DATA IN DEFINED FORM, AND NOT IRREGULAR)
void print(); //prints database to a text file, ie. a printable format


void transfer(TDatabase TDat); //Q. CAN IT REALLY BE DONE!???
   //To transfer the 5 tables to the new linked list database


Database(){
filename={'D','a','t','a','b','a','s','e',' ','u','n','u','s','e','d'};
//Table constructors will automatically be called
}
~Database(){
//Destructors of tables automatically called
}
}
};

void Table::Push(int a1, int a2, int a3){ //adds row at end
Node *ptr=new Node;
ptr->dat1=a1; ptr->dat2=a2; ptr->dat3=a3;
rear->next=ptr;
ptr->next=rear;
rear=ptr; nrow=noofrow();
}
void Table::Push(int a1, int a2, int a3, int loc){
Node *temp=new Node,*ptr=new Node; temp->next=top; ptr->dat1=a1; ptr->dat2=a2; ptr->dat3=a3;
if(loc>(nrow+1)) cout<<endl<<loc<<" rows are not present in the table. So unable to add row to that location";
//added +1 since if there are n rows already, n+1 location will be a valid location to push row
else if(loc==nrow+1) Push(a1,a2,a3); //if push location provided is at end
else if(loc==1) tPush(a1,a2,a3);
else { for(int i=1;i<loc;i++) temp=temp->next; ptr->next=temp->next; temp->next=ptr; nrow=noofrow();  }
}
void Table::tPush(int a1, int a2, int a3){
Node *ptr=new Node, *temp; //Q. IS IT NECESSARY TO WRITE "NODE *PTR=NEW NODE"??
ptr->dat1=a1; ptr->dat2=a2; ptr->dat3=a3;
temp=top; top=ptr; ptr->next=temp; nrow=noofrow();
//Q. SHOULD/CAN I DELETE *TEMP WITHOUT AFFECTING ANY NODE??
}
void Table::Pop(){ Node *temp;
if(nrow==1) {
rear=top;
top=top;
delete temp;
}
else { Node *temp=new Node,*temp2=new Node; temp->next=top;
for(int i=1; i<nrow; i++) temp=temp->next; temp2=temp->next; //temp2 is the node to be deleted
temp->next=temp2->next; temp2->next=NULL; delete temp2;
} nrow=noofrow();
}
void Table::Pop(int loc){
Node *temp=new Node,*temp2=new Node; temp->next=top;
for(int i=1; i<loc; i++) temp=temp->next; temp2=temp->next; //temp2 is the node to be deleted
temp->next=temp2->next; temp2->next=NULL; delete temp2;  nrow=noofrow();
}
void Table::WHERE(int a1){
cout<<"   "; puts(A); cout<<"   |   "; puts(B); cout<<"   |   "; puts(C); cout<<endl;
Node*temp=top;
for(int i=0;i<nrow;i++)
{
if(temp->dat1==a1) { cout<<"   ";
if(asize%2==0) for(int j=0;j<(asize/2)-1;j++) cout<<" ";
else for(int j=0;j<(asize/2)-2;j++) cout<<" ";
cout<<temp->dat1; cout<<"  |";
if(bsize%2==0) for(int j=0;j<(bsize/2)-1;j++) cout<<" ";
else for(int j=0;j<(bsize/2)-2;j++) cout<<" "; cout<<temp->dat2; cout<<"  |";
if(csize%2==0) for(int j=0;j<(csize/2)-1;j++) cout<<" ";
else for(int j=0;j<(csize/2)-2;j++) cout<<" ";
cout<<temp->dat3; temp=temp->next; }
}
}
void Table::WHERE(){
int chc,a,b,c;
if(top==rear) { cout<<"Table is empty";
}
else {
rechc:
cout<<"\nHow many elements do you remember in columnwise order ? "
<<"\n1. All three\n2. First two\n3. Only first\n4. I remember one number but dont remember the coulumn\n";
switch(chc){
case 1: cout<<"Enter the numbers in order: "; cin>>a>>b>>c; WHERE(a,b,c); break;
case 2: cout<<"Enter the numbers in order: "; cin>>a>>b; WHERE(a,b); break;
case 3: cout<<"Enter the number: "; cin>>a; WHERE(a); break;
case 4: cout<<"Enter the number: "; cin>>a; RWHERE(a); break;
default: cout<<"Invalid Choice. Enter again..."; goto rechc; break;
} }
}
void Table::ALTER(){
cout<<"\nWhat name would you like to give this table? "; gets(name);
cout<<"\nEnter the names of column : \nColumn 1 -> "; gets(A);
cout<<"\nColumnn 2 -> "; gets(B);
cout<<"\nColumn 3 -> "; gets(C); cout<<endl;  asize=strlen(A); bsize=strlen(B); csize=strlen(C); //in case user changes name of columns, so will the lengths change
}
int Table::noofrow(){
Node *temp; int count=0;
temp=top; while(temp!=rear) { count++; temp=temp->next;
}
delete temp;  //Har jagah temp ko delete mat kar dena nahi to kabhi uski location wala node hi delete ho jayega, here we didnt need it to point to a data, so i deleted it
return count+1;  //adding 1 to compensate for last element since loop couldnt count the last element
}
void Table::disp(){
Node *temp=top;
if(top==rear) cout<<"Table is empty";
else{ cout<<"   "; puts(A); cout<<"   |   "; puts(B); cout<<"   |   "; puts(C); cout<<endl; }
for(int i=0;i<nrow;i++) //now printing table... (no need to write as SQL.noofrow, since we are inside the class for now)
{
cout<<"   ";
if(asize%2==0) for(int j=0;j<(asize/2)-1;j++) cout<<" "; //for printing sufficient number of spaces to look neat
else for(int j=0;j<(asize/2)-2;j++) cout<<" ";
cout<<temp->dat1; cout<<"  |"; // *THIS IS THE PLACE WHERE KNOWLEGDE OF GRAPHICS.H CAN REALLY HELP
if(bsize%2==0) for(int j=0;j<(bsize/2)-1;j++) cout<<" ";
else for(int j=0;j<(bsize/2)-2;j++) cout<<" ";
cout<<temp->dat2; cout<<"  |";
if(csize%2==0) for(int j=0;j<(csize/2)-1;j++) cout<<" ";
else for(int j=0;j<(csize/2)-2;j++) cout<<" ";
cout<<temp->dat3<<endl;
temp=temp->next;
}
}

int main()
{
title();
cout<<"This will create a table with three columns";
Table Tab1; char ch='Y'; int choice,a,b,c,l; //l is location
Tab1.ALTER();
cout<<"\nYou can change the column names whenever you want (ALTER)";
rechose:
do{
cout<<"\nChoose any one of the options : "
<<"1. ALTER\n2. INSERT\n3. SELECT * FROM TABLE\n4. WHERE\n5. DELETE\n";
cin>>choice;
switch(choice){
case 1: Tab1.ALTER(); break;
case 2: cout<<"Enter the values to insert (in respective order) :\n"; cin>>a; cout<<'\t'; cin>>b; cout<<"\t\t"; cin>>c; //intentional combination of \t
        Tab1.Push(a,b,c); break;
case 3: Tab1.disp(); break;
case 4: Tab1.WHERE(); break;
case 5: if(Tab1.nrow==0) cout<<"Table is already empty...";
else {
cout<<"If you want to delete the last element, enter 0\nElse you will be shown the table, then enter the row number to be deleted : "; cin>>l;
if(l==0) Tab1.Pop();
else { Tab1.disp(); cout<<"\nNow enter the row number to be deleted : "; cin>>l;
Tab1.Pop(l);
} break;
default: cout<<"\n\n"<<choice<<" is an invalid choice. Try again..."; goto rechose; break;
}
cout<<"\nDo you want to get to the menu again ? (y/n)"; cin>>ch; }
}while(ch=='Y'||ch=='y');


//cout<<"Do you want another table?" //This will be added when it supports saving to database, in case it is not being saved
                                    //then in that case the so called feature of multiple tables will just be worthless
return 0;
}

/* THINGS READY TO WORK->
. Display (ie. disp())
. ALTER
. INSERT
. DELETE
.
*/
/*
FEATURES AVAILABLE HERE BUT NOT IN SQL->
    . RANDOM WHERE ie. doesnt want you to remember which column the element entered by you is in, it will print all rows , in which at least one column contains that number
. INSERT AT TOP ie. inserting just entered row at top instead of bottom
. INTENTIONAL INSERT ie. inserting row whereever you want it to be
. ALTER TABLE NAMES ie. change the name of table
.
. MORE FEATURES WILL BE ADDED IN COURSE OF TIME(AND ON BASIS OF 'YOUR' SUGGESTIONS)
*/

Comments

Popular posts from this blog

Using 'ls' command as a replacement of 'find'

Advantage over find - Actually not probably in terms of speed, but... only point of this is that it's an observation, but... you can do like 'search only in' */*/*Programs* , /*/*Programs*/* , this may compensate the extra time took by using two commands 1. To search in current folder only (1 level... only this folder, no subfolder)     Type... ls | grep Search_Term 2. To search in current folder only (till level 2... this folder + subfolder)     Type... ls * | grep Search_Term #ls and ls * are NOT same! 3. To search in current folder only (till level 3... this folder + subfolder + subfolder)     Type... ls */* | grep Search_Term 4. To search in current folder only (till level 4... this folder + subfolder + subfolder + subfolder)     Type... ls */*/* | grep Search_Term ... and so on... FORMULA - To search till 'n' level (considering current folder as 1)... go till (n-1) stars If wanting to search whole disk... better type star...

LudO - The Game

/* Programmer - Aditya Gupta (Techy15) Language - C++ Program - LudO - The Game */ /* PLANS- Add functionality for name of player */ //IMP NOTE - See all notes, written in this form "//NOTE..." //NOTE- Mark 'R','G','B','Y' for gotis, ignore doubling for now //NOTE- Settings will have options: change default game play order(ie RBYG), change/give names to each colour //NOTE- Add code in case of attacks //NOTE- Add stops, and ways to show them distinct from others {may require graphics.h} #include<iostream> /* #include<fstream>    To be used in case save & resume to be used*/ #include<cstdio> #include<cstdlib> using namespace std; //Changes on 16th Oct //Show gotis as R1, R2... and ask user to enter string R1 or whatever // // Place within appropriate block ...     short diethrow(){     int fl=1; //holds 0 if decnum is 0     while(fl==0) { char *temp;     ...

More_Inferences_in_C++(1Aug'19-16thOct'19)

Programmer/Tester- Aditya Gupta (aka Techy3.5 :-) I used g++ to compile these on linux 1. Type casting of floating point numeral to integer (either automatic or by type casting)- Conversion of floating point to integer is by taking the GIF (Greatest Integer Funtion) and NOT ROUNDING OFF Greatest Integer Function(Maths,12th) it gives the integer part when the original number is written as integer+fractional part (where fractional part is less than 1 but positive) 2. NEsted MultiLine Comment [actually wont work as expected?!] if you do this in C++.... /*  blah /*blah blah blah*/ blah*/ the last blah will be visible to the compiler!!! (Inference- Multiline-Comment ends whenever */ is encountered, no matter where!!)NEsted MultiLine Comment 3. Size of various types (of C++) on 'Linux' Mint - (and same as in later versions of windows, and most PCs nowadays) char - 1 int - 4  (and: short - 2, unsigned long - 8) long long - 8 float - 4 double - 8 (and: l...