From 7603ae21b61e99cada9c8f3434bf65ef7536e4ef Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Thu, 25 Jul 2019 15:24:42 +0800 Subject: [PATCH] Add files via upload Add more functions. --- lab.cpp | 43 +++++++++++ nasal_hash.h | 134 ++++++++++++++++++++++++++++++++- nasal_list.h | 205 +++++++++++++++++++++++++++++++++++++++++++++++++- nasal_print.h | 17 ++++- 4 files changed, 388 insertions(+), 11 deletions(-) create mode 100644 lab.cpp diff --git a/lab.cpp b/lab.cpp new file mode 100644 index 0000000..698c5cb --- /dev/null +++ b/lab.cpp @@ -0,0 +1,43 @@ +#include +#include "var.h" +using namespace nasal; + +int main() +{ + PrintString("This is a testing programme. \n"); + NasalHash m; + int a=1; + std::string k="hello world!"; + PrintString(k); + m.Append("first",&a,"int"); + m.Append("second",&a,"int"); + m.Append("third",&k,"string"); + m.PrintHash(); + std::cout< -#include +#include +#include +#include "nasal_list.h" + namespace nasal { @@ -31,6 +33,25 @@ class NasalHash while(temp->next) { head=temp->next; + if(temp->data) + { + if(temp->Type=="int") + delete (int *)temp->data; + if(temp->Type=="float") + delete (float *)temp->data; + if(temp->Type=="double") + delete (double *)temp->data; + if(temp->Type=="char") + delete (char *)temp->data; + if(temp->Type=="long long int") + delete (long long int *)temp->data; + if(temp->Type=="string") + delete (std::string *)temp->data; + if(temp->Type=="array") + delete (NasalList *)temp->data; + if(temp->Type=="hash") + delete (NasalHash *)temp->data; + } delete temp; temp=head; } @@ -56,6 +77,18 @@ class NasalHash std::cout<<"\""<<*((char *)temp->data)<<"\", "; if(temp->Type=="long long int") std::cout<<*((long long int *)temp->data)<<", "; + if(temp->Type=="string") + std::cout<<"\""<<*((std::string *)temp->data)<<"\", "; + if(temp->Type=="array") + { + ((NasalList *)temp->data)->PrintList(); + std::cout<<", "; + } + if(temp->Type=="hash") + { + ((NasalHash *)temp->data)->PrintHash(); + std::cout<<", "; + } } else { @@ -69,6 +102,12 @@ class NasalHash std::cout<<"\""<<*((char *)temp->data)<<"\""; if(temp->Type=="long long int") std::cout<<*((long long int *)temp->data); + if(temp->Type=="string") + std::cout<<"\""<<*((std::string *)temp->data)<<"\""; + if(temp->Type=="array") + ((NasalList *)temp->data)->PrintList(); + if(temp->Type=="hash") + ((NasalHash *)temp->data)->PrintHash(); } } std::cout<<"}"; @@ -82,11 +121,100 @@ class NasalHash } HashUnit *NewHashMember=new HashUnit; temp->next=NewHashMember; - NewHashMember->data=AppendData; NewHashMember->VarName=VariaName; NewHashMember->Type=TypeName; + if(TypeName=="int") + { + NewHashMember->data=new int; + *((int *)NewHashMember->data)=*((int *)AppendData); + } + if(TypeName=="float") + { + NewHashMember->data=new float; + *((float *)NewHashMember->data)=*((float *)AppendData); + } + if(TypeName=="double") + { + NewHashMember->data=new double; + *((double *)NewHashMember->data)=*((double *)AppendData); + } + if(TypeName=="char") + { + NewHashMember->data=new char; + *((char *)NewHashMember->data)=*((char *)AppendData); + } + if(TypeName=="long long int") + { + NewHashMember->data=new long long int; + *((long long int *)NewHashMember->data)=*((long long int *)AppendData); + } + if(TypeName=="string") + { + NewHashMember->data=new std::string; + *((std::string *)NewHashMember->data)=*((std::string *)AppendData); + } + if(temp->Type=="array") + ; + if(temp->Type=="hash") + ; NewHashMember->next=NULL; } + int Contains(const char *VariaName) + { + HashUnit *temp=head; + while(temp->next) + { + temp=temp->next; + if(temp->VarName==VariaName) + return 1; + } + return 0; + } + NasalList Keys() + { + NasalList FeedBackList; + HashUnit *temp=head; + while(temp->next) + { + temp=temp->next; + FeedBackList.Append(&(temp->VarName),"string"); + } + return FeedBackList; + } + void Delete(const char *VariaName) + { + HashUnit *temp=head; + HashUnit *LastNode; + while(temp->next) + { + LastNode=temp; + temp=temp->next; + if(temp->VarName==VariaName) + { + LastNode->next=temp->next; + if(temp->Type=="int") + delete (int *)temp->data; + if(temp->Type=="float") + delete (float *)temp->data; + if(temp->Type=="double") + delete (double *)temp->data; + if(temp->Type=="char") + delete (char *)temp->data; + if(temp->Type=="long long int") + delete (long long int *)temp->data; + if(temp->Type=="string") + delete (std::string *)temp->data; + if(temp->Type=="array") + delete (NasalList *)temp->data; + if(temp->Type=="hash") + delete (NasalHash *)temp->data; + delete temp; + return; + } + } + std::cout<<"[Error]: Could not find this element \""< -#include +#include +#include +#include "nasal_hash.h" + namespace nasal { - struct ListUnit { std::string Type; @@ -22,6 +23,7 @@ class NasalList NasalList() { head=new ListUnit; + head->data=NULL; head->next=NULL; } ~NasalList() @@ -30,9 +32,47 @@ class NasalList while(temp->next) { head=temp->next; + if(temp->data) + { + if(temp->Type=="int") + delete (int *)temp->data; + if(temp->Type=="float") + delete (float *)temp->data; + if(temp->Type=="double") + delete (double *)temp->data; + if(temp->Type=="char") + delete (char *)temp->data; + if(temp->Type=="long long int") + delete (long long int *)temp->data; + if(temp->Type=="string") + delete (std::string *)temp->data; + if(temp->Type=="array") + delete (NasalList *)temp->data; + if(temp->Type=="hash"); + //delete (NasalHash *)temp->data; + } delete temp; temp=head; } + if(temp->data) + { + if(temp->Type=="int") + delete (int *)temp->data; + if(temp->Type=="float") + delete (float *)temp->data; + if(temp->Type=="double") + delete (double *)temp->data; + if(temp->Type=="char") + delete (char *)temp->data; + if(temp->Type=="long long int") + delete (long long int *)temp->data; + if(temp->Type=="string") + delete (std::string *)temp->data; + if(temp->Type=="array") + delete (NasalList *)temp->data; + if(temp->Type=="hash") + ; + } delete temp; } void PrintList() @@ -54,6 +94,18 @@ class NasalList std::cout<<"\""<<*((char *)temp->data)<<"\", "; if(temp->Type=="long long int") std::cout<<*((long long int *)temp->data)<<", "; + if(temp->Type=="string") + std::cout<<"\""<<*((std::string *)temp->data)<<"\", "; + if(temp->Type=="array") + { + ((NasalList *)temp->data)->PrintList(); + std::cout<<", "; + } + if(temp->Type=="hash") + { + ;//((NasalHash *)temp->data)->PrintHash(); + std::cout<<", "; + } } else { @@ -67,12 +119,21 @@ class NasalList std::cout<<"\""<<*((char *)temp->data)<<"\""; if(temp->Type=="long long int") std::cout<<*((long long int *)temp->data); + if(temp->Type=="string") + std::cout<<"\""<<*((std::string *)temp->data)<<"\""; + if(temp->Type=="array") + ((NasalList *)temp->data)->PrintList(); + if(temp->Type=="hash") + ;//((NasalHash *)temp->data)->PrintHash(); } } std::cout<<"]"; } void Append(void *AppendData,const char *TypeName) { + NasalList TempList;//sometimes user may use n.append(n) + if(TypeName=="array") + TempList=*((NasalList *)AppendData); ListUnit *temp=head; while(temp->next) { @@ -80,10 +141,146 @@ class NasalList } ListUnit *NewListMember=new ListUnit; temp->next=NewListMember; - NewListMember->data=AppendData; NewListMember->Type=TypeName; + if(TypeName=="int") + { + NewListMember->data=new int; + *((int *)NewListMember->data)=*((int *)AppendData); + } + if(TypeName=="float") + { + NewListMember->data=new float; + *((float *)NewListMember->data)=*((float *)AppendData); + } + if(TypeName=="double") + { + NewListMember->data=new double; + *((double *)NewListMember->data)=*((double *)AppendData); + } + if(TypeName=="char") + { + NewListMember->data=new char; + *((char *)NewListMember->data)=*((char *)AppendData); + } + if(TypeName=="long long int") + { + NewListMember->data=new long long int; + *((long long int *)NewListMember->data)=*((long long int *)AppendData); + } + if(TypeName=="string") + { + NewListMember->data=new std::string; + *((std::string *)NewListMember->data)=*((std::string *)AppendData); + } + if(TypeName=="array") + { + NewListMember->data=new NasalList; + *((NasalList *)NewListMember->data)=TempList; + } + if(TypeName=="hash") + ; NewListMember->next=NULL; } + NasalList& operator=(const NasalList &Source) + { + ListUnit *temp=head; + ListUnit *SourceTemp=Source.head; + while(temp->next) + { + head=temp->next; + if(temp->data) + { + if(temp->Type=="int") + delete (int *)temp->data; + if(temp->Type=="float") + delete (float *)temp->data; + if(temp->Type=="double") + delete (double *)temp->data; + if(temp->Type=="char") + delete (char *)temp->data; + if(temp->Type=="long long int") + delete (long long int *)temp->data; + if(temp->Type=="string") + delete (std::string *)temp->data; + if(temp->Type=="array") + delete (NasalList *)temp->data; + if(temp->Type=="hash") + ; + } + delete temp; + temp=head; + } + if(temp->data) + { + if(temp->Type=="int") + delete (int *)temp->data; + if(temp->Type=="float") + delete (float *)temp->data; + if(temp->Type=="double") + delete (double *)temp->data; + if(temp->Type=="char") + delete (char *)temp->data; + if(temp->Type=="long long int") + delete (long long int *)temp->data; + if(temp->Type=="string") + delete (std::string *)temp->data; + if(temp->Type=="array") + delete (NasalList *)temp->data; + if(temp->Type=="hash") + ; + } + delete temp; + head=new ListUnit; + head->next=NULL; + + temp=head; + while(SourceTemp->next) + { + SourceTemp=SourceTemp->next; + temp->next=new ListUnit; + temp=temp->next; + temp->Type=SourceTemp->Type; + if(temp->Type=="int") + { + temp->data=new int; + *((int *)temp->data)=*((int *)SourceTemp->data); + } + if(temp->Type=="float") + { + temp->data=new float; + *((float *)temp->data)=*((float *)SourceTemp->data); + } + if(temp->Type=="double") + { + temp->data=new double; + *((double *)temp->data)=*((double *)SourceTemp->data); + } + if(temp->Type=="char") + { + temp->data=new char; + *((char *)temp->data)=*((char *)SourceTemp->data); + } + if(temp->Type=="long long int") + { + temp->data=new long long int; + *((long long int *)temp->data)=*((long long int *)SourceTemp->data); + } + if(temp->Type=="string") + { + temp->data=new std::string; + *((std::string *)temp->data)=*((std::string *)SourceTemp->data); + } + if(temp->Type=="array") + { + temp->data=new NasalList; + *((NasalList *)temp->data)=*((NasalList *)SourceTemp->data); + } + if(temp->Type=="hash") + ; + temp->next=NULL; + } + return *this; + } }; } diff --git a/nasal_print.h b/nasal_print.h index 4b4a8da..4f06401 100644 --- a/nasal_print.h +++ b/nasal_print.h @@ -5,6 +5,7 @@ #include #include "nasal_hash.h" #include "nasal_list.h" + namespace nasal { @@ -28,6 +29,14 @@ void PrintVar(long long int Var) { std::cout<=(int)PrintInfo.length()) { //error occurred - std::cout<=strlen(PrintInfo)) { //error occurred - std::cout<