Spisok cpp Defines the entry point for the console application include

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Spisok.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Num.h"
#include <string.h>
#include <stdlib.h>
#include <conio.h>
TSpisok N;
int main(int argc, char* argv[])
{ char str[10];
int k,i;
TElement *p;
puts("");
while(printf("Input numbers, letter or <end>:"),scanf("%s",str),strcmp(str,"end"))
{ k=atoi(str);
if (k||(strlen(str)==1 && str[0]=='0')) p=new TNum(k);
else p=new TChar(str[0]);
N.Add(p);
}
puts("All list:");
N.ForEach(Show);
p=N.First();
k=0;
while (p!=NULL)
{ if (TNum *q=dynamic_cast<TNum *>(p)) k+=q->num;
p=N.Next();
}
printf("\n");
printf("Summa= %d\n",k);
p=N.Last();
i=0;
while (p!=NULL)
{ if (TChar *q=dynamic_cast<TChar *>(p)) str[i++]=q->ch;
p=N.Previous();
}
str[i]='\0';
printf("String= %s\n",str);
puts("End.");
getch();
return 0;
}
#include "stdafx.h"
#include "Element.h"
TSpisok::~TSpisok()
{ puts("Delete TSpisok");
while ((cur=Del())!=NULL)
{ cur->Print();
delete(cur); }
}
void TSpisok::Add(TElement *e)
{ if (first==NULL)
{ first=last=e;
e->suc=e->pre=NULL; }
else
{ e->suc=first;
first->pre=e;
e->pre=NULL;
first=e; }
}
TElement *TSpisok::Del(void)
{ TElement *temp;
temp=last;
if (last!=NULL)
{ last=last->pre;
if (last!=NULL)last->suc=NULL;
}
if (last==NULL) first=NULL;
return temp;
}
void TSpisok::ForEach(void (*f)(TElement *e))
{ cur=last;
while (cur!=NULL)
{ (*f)(cur);
cur=cur->pre;}}
#include <stdio.h>
class TElement
{
public:
TElement *pre,*suc;
TElement() { pre=suc=NULL;}
virtual ~TElement() { puts("Delete TElement.");}
virtual void Print()=0;
};
class TSpisok
{
private:
TElement *first,*last,*cur;
public:
TSpisok() {first=last=cur=NULL;}
~TSpisok();
void Add(TElement *e);
TElement *Del();
void ForEach(void (*f)(TElement *e));
TElement *First(){return cur=first;}
TElement *Next(){return cur=cur->suc;}
TElement *Last(){return cur=last;}
TElement *Previous(){return cur=cur->pre;}
};
#include "Element.h"
class TNum:public TElement
{ public:
int num;
TNum(int n):TElement(),num(n) {}
virtual ~TNum() { puts("Delete TNum.");}
virtual void Print() { printf("%d ",num); }
};
class TChar:public TElement
{ public:
char ch;
TChar(char c):TElement(),ch(c) {}
virtual ~TChar() { puts("Delete TChar.");}
virtual void Print() { printf("%c ",ch);}
};
void Show(TElement *e)
{ e->Print();}