Add files via upload

Add more functions.
This commit is contained in:
Valk Richard Li 2019-07-25 15:24:42 +08:00 committed by GitHub
parent d4e3e1868b
commit 7603ae21b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 388 additions and 11 deletions

43
lab.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <iostream>
#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<<std::endl;
PrintVar(m);
std::cout<<std::endl;
NasalList n;
n=m.Keys();
n.PrintList();
std::cout<<std::endl;
PrintVar(n);
std::cout<<std::endl;
m.Delete("fifth");
m.Delete("second");
m.PrintHash();
n=m.Keys();
std::cout<<std::endl;
n.PrintList();
std::cout<<std::endl;
//n.Append(&n,"array","n");//cause infinite loop
n.Append(&n,"array");
n.Append(&n,"array");
n.PrintList();
std::cout<<std::endl;
return 0;
}

View File

@ -1,8 +1,10 @@
#ifndef __NASAL_HASH_H__ #ifndef __NASAL_HASH_H__
#define __NASAL_HASH_H__ #define __NASAL_HASH_H__
#include<iostream> #include <iostream>
#include<cstring> #include <cstring>
#include "nasal_list.h"
namespace nasal namespace nasal
{ {
@ -31,6 +33,25 @@ class NasalHash
while(temp->next) while(temp->next)
{ {
head=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; delete temp;
temp=head; temp=head;
} }
@ -56,6 +77,18 @@ class NasalHash
std::cout<<"\""<<*((char *)temp->data)<<"\", "; std::cout<<"\""<<*((char *)temp->data)<<"\", ";
if(temp->Type=="long long int") if(temp->Type=="long long int")
std::cout<<*((long long int *)temp->data)<<", "; 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 else
{ {
@ -69,6 +102,12 @@ class NasalHash
std::cout<<"\""<<*((char *)temp->data)<<"\""; std::cout<<"\""<<*((char *)temp->data)<<"\"";
if(temp->Type=="long long int") if(temp->Type=="long long int")
std::cout<<*((long long int *)temp->data); 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<<"}"; std::cout<<"}";
@ -82,11 +121,100 @@ class NasalHash
} }
HashUnit *NewHashMember=new HashUnit; HashUnit *NewHashMember=new HashUnit;
temp->next=NewHashMember; temp->next=NewHashMember;
NewHashMember->data=AppendData;
NewHashMember->VarName=VariaName; NewHashMember->VarName=VariaName;
NewHashMember->Type=TypeName; 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; 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 \""<<VariaName<<"\"."<<std::endl;
return;
}
}; };
} }

View File

@ -1,12 +1,13 @@
#ifndef __NASAL_LIST_H__ #ifndef __NASAL_LIST_H__
#define __NASAL_LIST_H__ #define __NASAL_LIST_H__
#include<iostream> #include <iostream>
#include<cstring> #include <cstring>
#include "nasal_hash.h"
namespace nasal namespace nasal
{ {
struct ListUnit struct ListUnit
{ {
std::string Type; std::string Type;
@ -22,6 +23,7 @@ class NasalList
NasalList() NasalList()
{ {
head=new ListUnit; head=new ListUnit;
head->data=NULL;
head->next=NULL; head->next=NULL;
} }
~NasalList() ~NasalList()
@ -30,9 +32,47 @@ class NasalList
while(temp->next) while(temp->next)
{ {
head=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; delete temp;
temp=head; 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; delete temp;
} }
void PrintList() void PrintList()
@ -54,6 +94,18 @@ class NasalList
std::cout<<"\""<<*((char *)temp->data)<<"\", "; std::cout<<"\""<<*((char *)temp->data)<<"\", ";
if(temp->Type=="long long int") if(temp->Type=="long long int")
std::cout<<*((long long int *)temp->data)<<", "; 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 else
{ {
@ -67,12 +119,21 @@ class NasalList
std::cout<<"\""<<*((char *)temp->data)<<"\""; std::cout<<"\""<<*((char *)temp->data)<<"\"";
if(temp->Type=="long long int") if(temp->Type=="long long int")
std::cout<<*((long long int *)temp->data); 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<<"]"; std::cout<<"]";
} }
void Append(void *AppendData,const char *TypeName) 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; ListUnit *temp=head;
while(temp->next) while(temp->next)
{ {
@ -80,10 +141,146 @@ class NasalList
} }
ListUnit *NewListMember=new ListUnit; ListUnit *NewListMember=new ListUnit;
temp->next=NewListMember; temp->next=NewListMember;
NewListMember->data=AppendData;
NewListMember->Type=TypeName; 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; 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;
}
}; };
} }

View File

@ -5,6 +5,7 @@
#include<cstring> #include<cstring>
#include "nasal_hash.h" #include "nasal_hash.h"
#include "nasal_list.h" #include "nasal_list.h"
namespace nasal namespace nasal
{ {
@ -28,6 +29,14 @@ void PrintVar(long long int Var)
{ {
std::cout<<Var; std::cout<<Var;
} }
void PrintVar(NasalHash &Var)
{
Var.PrintHash();
}
void PrintVar(NasalList &Var)
{
Var.PrintList();
}
void PrintString(std::string &PrintInfo) void PrintString(std::string &PrintInfo)
@ -64,7 +73,7 @@ void PrintString(std::string &PrintInfo)
break; break;
default: default:
//error occurred //error occurred
std::cout<<std::endl<<"[Error]: Incorrect escape character \'"<<PrintInfo[i]<<PrintInfo[i+1]<<"\' ."; std::cout<<"[Error]: Incorrect escape character \'"<<PrintInfo[i]<<PrintInfo[i+1]<<"\' .";
++i; ++i;
break; break;
} }
@ -72,7 +81,7 @@ void PrintString(std::string &PrintInfo)
else if(PrintInfo[i]=='\\' && i+1>=(int)PrintInfo.length()) else if(PrintInfo[i]=='\\' && i+1>=(int)PrintInfo.length())
{ {
//error occurred //error occurred
std::cout<<std::endl<<"[Error]: Missing character after \'\\\'"; std::cout<<"[Error]: Missing character after \'\\\'";
} }
else else
std::cout<<PrintInfo[i]; std::cout<<PrintInfo[i];
@ -114,7 +123,7 @@ void PrintString(const char *PrintInfo)
break; break;
default: default:
//error occurred //error occurred
std::cout<<std::endl<<"[Error]: Incorrect escape character \'"<<PrintInfo[i]<<PrintInfo[i+1]<<"\' ."; std::cout<<"[Error]: Incorrect escape character \'"<<PrintInfo[i]<<PrintInfo[i+1]<<"\' ."<<std::endl;
++i; ++i;
break; break;
} }
@ -122,7 +131,7 @@ void PrintString(const char *PrintInfo)
else if(PrintInfo[i]=='\\' && i+1>=strlen(PrintInfo)) else if(PrintInfo[i]=='\\' && i+1>=strlen(PrintInfo))
{ {
//error occurred //error occurred
std::cout<<std::endl<<"[Error]: Missing character after \'\\\'"; std::cout<<"[Error]: Missing character after \'\\\'"<<std::endl;
} }
else else
std::cout<<PrintInfo[i]; std::cout<<PrintInfo[i];