Add files via upload

complete all types of var
This commit is contained in:
Valk Richard Li 2019-07-28 00:01:28 +08:00 committed by GitHub
parent e57153286c
commit b6797013dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 377 additions and 3 deletions

23
nasal.h Normal file
View File

@ -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

View File

@ -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: "<<temp->Type<<".But type must be int."<<std::endl;
TempList.SetSize(1);
return TempList;
}
}
if(temp->Type!="int")
{
std::cout<<"[Error] Incorrect type inside: "<<temp->Type<<".But type must be int."<<std::endl;
TempList.SetSize(1);
return TempList;
}
TempList=*this;
ListUnit *FirstTempThis;
ListUnit *SecondTempThis;
ListUnit *NodeThis;
FirstTempThis=TempList.head->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: "<<temp->Type<<".But type must be string."<<std::endl;
TempList.SetSize(1);
return TempList;
}
}
if(temp->Type!="string")
{
std::cout<<"[Error] Incorrect type inside: "<<temp->Type<<".But type must be string."<<std::endl;
TempList.SetSize(1);
return TempList;
}
TempList=*this;
ListUnit *FirstTempThis;
ListUnit *SecondTempThis;
ListUnit *NodeThis;
FirstTempThis=TempList.head->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()

View File

@ -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

72
process_stack.h Normal file
View File

@ -0,0 +1,72 @@
#ifndef __PROCESS_STACK_H__
#define __PROCESS_STACK_H__
#include <iostream>
#include <cstring>
#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

153
var_lab.cpp Normal file
View File

@ -0,0 +1,153 @@
#include <iostream>
#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<<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.Append(&n,"array");
n.PrintList();
std::cout<<std::endl;
NasalList rm;
rm.Append(&a,"int");
rm.Append(&a,"int");
rm.Append(&a,"int");
rm.PrintList();
std::cout<<std::endl;
m.Append("fifth",&rm,"array");
m.Append("sixth",&m,"hash");
m.PrintHash();
std::cout<<std::endl;
NasalHash test1(m);
test1.PrintHash();
NasalList test2(n);
std::cout<<std::endl;
test2.PrintList();
std::cout<<std::endl;
n.Append(&m,"hash");
n.PrintList();
std::cout<<std::endl<<std::endl;
NasalList qt;
qt.Append(&a,"int");
a++;
qt.Append(&a,"int");
NasalHash qthash;
qthash.Append("testlist",&qt,"array");
qthash.PrintHash();
std::cout<<std::endl;
qthash.Append("int",&a,"int");
qt.Append(&qthash,"hash");
qt.PrintList();
std::cout<<std::endl;
NasalList _l;
_l.Append(&a,"int");
_l.SetSize(10);
_l.PrintList();
std::cout<<std::endl;
_l.SetSize(3);
_l.PrintList();
std::cout<<std::endl;
_l.SetSize(0);
_l.PrintList();
std::cout<<std::endl;
_l.Append(&a,"int");
_l.Append(&a,"int");
_l.Append(&a,"int");
_l.Append(&a,"int");
_l.Append(&a,"int");
_l.Append(&a,"int");
NasalList _k;
_l.PrintList();
std::cout<<std::endl;
_k=_l.SubVec(9,4);
_k.PrintList();
std::cout<<std::endl;
_k=_l.SubVec(1,10);
_k.PrintList();
std::cout<<std::endl;
NasalList _m;
var _temp=_k.Pop();
_m.Append(&_temp,"var");
_k.PrintList();
std::cout<<std::endl;
_m.PrintList();
std::cout<<std::endl;
qthash.Append("var",&_temp,"var");
qthash.PrintHash();
std::cout<<std::endl;
NasalList _test_list;
int _test_int=10;
_test_list.Append(&_test_int,"int");
_test_int--;
_test_list.Append(&_test_int,"int");
_test_int--;
_test_list.Append(&_test_int,"int");
_test_int--;
_test_list.Append(&_test_int,"float");
NasalList _test_temp_list;
_test_temp_list=_test_list.Sort(SORT_INT,1);
_test_temp_list.PrintList();
std::cout<<std::endl;
_test_list.Pop();
_test_temp_list=_test_list.Sort(SORT_INT,-1);
_test_temp_list.PrintList();
std::cout<<std::endl;
std::string _test_str;
_test_list.SetSize(0);
_test_str=".20.";
_test_list.Append(&_test_str,"string");
_test_str=".01.";
_test_list.Append(&_test_str,"string");
_test_str=".1";
_test_list.Append(&_test_str,"string");
_test_str=".3.";
_test_list.Append(&_test_str,"string");
_test_list.Append(&_test_int,"int");
_test_temp_list=_test_list.Sort(SORT_STRING,1);
_test_temp_list.PrintList();
std::cout<<std::endl;
_test_list.Pop();
_test_temp_list=_test_list.Sort(SORT_STRING,1);
_test_temp_list.PrintList();
std::cout<<std::endl;
return 0;
}