From b6797013dd321386bd87541d76c656f1b801627b Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Sun, 28 Jul 2019 00:01:28 +0800 Subject: [PATCH] Add files via upload complete all types of var --- nasal.h | 23 ++++++++ nasal_list.cpp | 130 +++++++++++++++++++++++++++++++++++++++- nasal_list.h | 2 +- process_stack.h | 72 +++++++++++++++++++++++ var_lab.cpp | 153 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 377 insertions(+), 3 deletions(-) create mode 100644 nasal.h create mode 100644 process_stack.h create mode 100644 var_lab.cpp diff --git a/nasal.h b/nasal.h new file mode 100644 index 0000000..3d16a59 --- /dev/null +++ b/nasal.h @@ -0,0 +1,23 @@ +#ifndef __NASAL_H__ +#define __NASAL_H__ + +#include "nasal_hash.cpp" +#include "nasal_list.cpp" +#include "nasal_print.h" + +// int +// char +// long long int +// double +// float +// string +// const char * +// NasalHash +// NasalList + +/* +NasalList->delete NasalList & NasalHash + +NasalHash->delete NasalList & NasalHash +*/ +#endif diff --git a/nasal_list.cpp b/nasal_list.cpp index 30e4918..180d8aa 100644 --- a/nasal_list.cpp +++ b/nasal_list.cpp @@ -688,9 +688,135 @@ var NasalList::Pop() return TempVar; } -void NasalList::Sort(bool _cmp_1,bool _cmp_2) +#ifndef __SORT_TYPE__ +#define SORT_INT 1 +#define SORT_STRING 2 +#endif + +NasalList NasalList::Sort(const int SortType,const int _cmp) { - return; + NasalList TempList; + if(SortType==SORT_INT) + { + ListUnit *temp=head; + while(temp->next) + { + temp=temp->next; + if(temp->Type!="int") + { + std::cout<<"[Error] Incorrect type inside: "<Type<<".But type must be int."<Type!="int") + { + std::cout<<"[Error] Incorrect type inside: "<Type<<".But type must be int."<next; + while(FirstTempThis->next) + { + NodeThis=FirstTempThis; + SecondTempThis=FirstTempThis->next; + while(SecondTempThis->next) + { + if(_cmp>0 && *((int *)NodeThis->data)>*((int *)SecondTempThis->data))//from small to large + { + NodeThis=SecondTempThis; + } + else if(_cmp<=0 && *((int *)NodeThis->data)<*((int *)SecondTempThis->data))//from large to small + { + NodeThis=SecondTempThis; + } + SecondTempThis=SecondTempThis->next; + } + if(_cmp>0 && *((int *)NodeThis->data)>*((int *)SecondTempThis->data))//from small to large func(a,b) a-b + { + NodeThis=SecondTempThis; + } + else if(_cmp<=0 && *((int *)NodeThis->data)<*((int *)SecondTempThis->data))//from large to small func(a,b) b-a + { + NodeThis=SecondTempThis; + } + if(NodeThis!=FirstTempThis) + { + int t; + t=*((int *)FirstTempThis->data); + *((int *)FirstTempThis->data)=*((int *)NodeThis->data); + *((int *)NodeThis->data)=t; + } + FirstTempThis=FirstTempThis->next; + } + } + else if(SortType==SORT_STRING) + { + ListUnit *temp=head; + while(temp->next) + { + temp=temp->next; + if(temp->Type!="string") + { + std::cout<<"[Error] Incorrect type inside: "<Type<<".But type must be string."<Type!="string") + { + std::cout<<"[Error] Incorrect type inside: "<Type<<".But type must be string."<next; + while(FirstTempThis->next) + { + NodeThis=FirstTempThis; + SecondTempThis=FirstTempThis->next; + while(SecondTempThis->next) + { + if(_cmp>0 && *((std::string *)NodeThis->data)>*((std::string *)SecondTempThis->data))//from small to large + { + NodeThis=SecondTempThis; + } + else if(_cmp<=0 && *((std::string *)NodeThis->data)<*((std::string *)SecondTempThis->data))//from large to small + { + NodeThis=SecondTempThis; + } + SecondTempThis=SecondTempThis->next; + } + if(_cmp>0 && *((std::string *)NodeThis->data)>*((std::string *)SecondTempThis->data))//from small to large func(a,b) cmp(a,b) + { + NodeThis=SecondTempThis; + } + else if(_cmp<=0 && *((std::string *)NodeThis->data)<*((std::string *)SecondTempThis->data))//from large to small func(a,b) -cmp(a,b) or cmp(b,a) + { + NodeThis=SecondTempThis; + } + if(NodeThis!=FirstTempThis) + { + std::string t; + t=*((std::string *)FirstTempThis->data); + *((std::string *)FirstTempThis->data)=*((std::string *)NodeThis->data); + *((std::string *)NodeThis->data)=t; + } + FirstTempThis=FirstTempThis->next; + } + } + return TempList; } var::~var() diff --git a/nasal_list.h b/nasal_list.h index b6de14b..06df4db 100644 --- a/nasal_list.h +++ b/nasal_list.h @@ -35,7 +35,7 @@ class NasalList void SetSize(const int); NasalList SubVec(const int,const int); var Pop(); - void Sort(bool,bool); + NasalList Sort(const int,const int); }; class var diff --git a/process_stack.h b/process_stack.h new file mode 100644 index 0000000..0a43628 --- /dev/null +++ b/process_stack.h @@ -0,0 +1,72 @@ +#ifndef __PROCESS_STACK_H__ +#define __PROCESS_STACK_H__ + +#include +#include +#include "nasal.h" +namespace nasal +{ + + +struct process_stack_unit +{ + int line; //place the unit first appear + std::string content;//content of the unit or name of the var/class/function + std::string type; //var class function + process_stack_unit *next; + process_stack_unit *last; +}; + +class process_stack +{ + private: + process_stack_unit *head; + process_stack_unit *ptr; + public: + process_stack() + { + head->line=0; + head->content="# Nasal language for FlightGear."; + head->last=NULL; + head->next=NULL; + ptr=NULL; + } + ~process_stack() + { + process_stack_unit *temp=head; + while(temp->next) + { + temp=temp->next; + delete head; + head=temp; + } + delete head; + } + void stack_append(process_stack_unit &p) + { + process_stack_unit *temp=head; + process_stack_unit *last_node; + while(temp->next) + { + if(!temp->next->next) + last_node=temp; + temp=temp->next; + } + if(temp==head) + last_node=head; + temp=new process_stack_unit; + temp->last=last_node; + temp->next=NULL; + temp->content=p.content; + temp->line=p.last; + temp->type=p.type; + return; + } +}; + + + + +} + +#endif diff --git a/var_lab.cpp b/var_lab.cpp new file mode 100644 index 0000000..2e33096 --- /dev/null +++ b/var_lab.cpp @@ -0,0 +1,153 @@ +#include +#include "nasal.h" +using namespace nasal; +using namespace std; + +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<