main Lesson4 Created by Admin on 12 02 15 Copyright 2015 Andrei Poliak

  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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//
// main.m
// Lesson4
//
// Created by Admin on 12.02.15.
// Copyright (c) 2015 Andrei Poliakov. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Figure.h"
#import "Circle.h"
#import "Rectangle.h"
#import "Ellipse.h"
// типы фигур
typedef enum {
_circle,
_rectangle,
_ellipse
} typeOfFigure;
//Figure* createList(float _width, float _height){
// Figure* temp;
// return temp;
//}
// Создаем циклический линейный односвязный список с головой
Figure* createListWithRandomCountOfElements(int count){
Figure *head = [[Figure alloc] init], *currentNode, *nextNode;
head->nextNode = head;
currentNode = head;
for (int i = 0; i < count; i++) {
typeOfFigure type = rand() % 3 + 0;
switch (type) {
case _circle:
nextNode = [[Circle alloc] initWithRadius: (rand() % 6 + 5)];
break;
case _rectangle:
nextNode = [[Rectangle alloc] initWithWidth:(rand() % 6 + 5)];
break;
case _ellipse:
nextNode = [[Ellipse alloc] initWithAxis:(rand() % 6 + 5) minorSemiaxis:(rand() % 6 + 5)];
break;
default:
nextNode = [[Figure alloc] init];
break;
}
currentNode->nextNode = nextNode;
nextNode->nextNode = head;
currentNode = nextNode;
// currentNode = [[Figure alloc] init];
// NSLog(@"figure %i", i);
}
return head;
}
BOOL deleteNode(Figure* head, int indexOfNode){
Figure* currentNode, *nextNode;
if (head != nil) {
currentNode = head->nextNode;
nextNode=currentNode->nextNode;
} else{
NSLog(@"error1 ");
return false;
}
int i = 1;
while (currentNode != head){
if (i == indexOfNode - 1) {
currentNode->nextNode = nextNode->nextNode;
return true;
} else{
currentNode = currentNode->nextNode;
}
nextNode=currentNode->nextNode;
i++;
}
// if (i >= indexOfNode) {
// return true;
// }
NSLog(@"error2 ");
return false;
}
void addNode(Figure* head, Figure* node){
Figure* currentNode = head->nextNode;
while (currentNode->nextNode != head) {
currentNode=currentNode->nextNode;
}
currentNode->nextNode = node;
(currentNode->nextNode)->nextNode = head;
}
void BubbleSort(Figure* head){
Figure *currentNode, *nextNode, *temp;
while(true){
bool flag = true;
for (currentNode = head, nextNode = currentNode->nextNode; nextNode->nextNode != head; currentNode = currentNode->nextNode, nextNode = currentNode->nextNode) {
if ([nextNode getSquare] > [(nextNode->nextNode) getSquare]) { // если следующее меньше предыдущего
currentNode->nextNode = nextNode->nextNode;
nextNode->nextNode = (nextNode->nextNode)->nextNode;
(nextNode->nextNode)->nextNode = nextNode;
// currentNode->nextNode = nextNode->nextNode;
}
else{
continue;
}
flag = false; // на данном шаге сортировку выполнили, переходим на следующий
}
if(flag) break;
}
// Figure *currentNode, *nextNode, *temp;
// for (currentNode = head->nextNode, nextNode = currentNode->nextNode; (currentNode->nextNode)->nextNode != head; currentNode = currentNode->nextNode, nextNode = currentNode) {
// for (Figure *currentNode1 = head->nextNode, *nextNode1 = currentNode1->nextNode; (currentNode1->nextNode)->nextNode != head; currentNode1 = currentNode1->nextNode, nextNode1 = currentNode1) {
// if ([nextNode getSquare] > [nextNode1 getSquare]) {
// temp = nextNode1;
// currentNode->nextNode = nextNode1;
// nextNode1->nextNode = nextNode->nextNode;
// currentNode1->nextNode = nextNode;
// nextNode->nextNode = temp->nextNode;
// break;
// }
// }
// }
}
int main(int argc, const char * argv[]) {
Circle* myCircle = [[Circle alloc] initWithRadius:10.0];
[myCircle description];
Rectangle* myRectangle = [[Rectangle alloc] initWithWidth:2.0];
[myRectangle description];
Ellipse* myEllipse = [[Ellipse alloc] initWithAxis:3.0 minorSemiaxis:2.0];
[myEllipse description];
Figure* list = createListWithRandomCountOfElements(10);
NSLog(@"///////////////");
for(Figure *currentNode = list->nextNode; currentNode != list; currentNode = currentNode->nextNode){
if ([currentNode isKindOfClass:[Circle class]]) {
NSLog(@"Circle: radius is %.2f", [((Circle*)currentNode) getRadius]);
} else if ([currentNode isKindOfClass:[Rectangle class]]) {
NSLog(@"Rectangle: width is %.2f", [((Rectangle*)currentNode) getWidth]);
} else if ([currentNode isKindOfClass:[Ellipse class]]) {
NSLog(@"Ellipse: minor is %.2f, magor is %.2f", [((Ellipse*)currentNode) getMinorSemiaxis], [((Ellipse*)currentNode) getMajorSemiaxis]);
}
}
NSLog(@"///////////////");
if(deleteNode(list, 3)){
for(Figure *currentNode = list->nextNode; currentNode != list; currentNode = currentNode->nextNode){
if ([currentNode isKindOfClass:[Circle class]]) {
NSLog(@"Circle: radius is %.2f", [((Circle*)currentNode) getRadius]);
} else if ([currentNode isKindOfClass:[Rectangle class]]) {
NSLog(@"Rectangle: width is %.2f", [((Rectangle*)currentNode) getWidth]);
} else if ([currentNode isKindOfClass:[Ellipse class]]) {
NSLog(@"Ellipse: minor is %.2f, magor is %.2f", [((Ellipse*)currentNode) getMinorSemiaxis], [((Ellipse*)currentNode) getMajorSemiaxis]);
}
}
} else{
NSLog(@"error");
}
NSLog(@"///////////////");
Figure* testFigure = [[Circle alloc] initWithRadius: (rand() % 6 + 5)];
addNode(list, testFigure);
for(Figure *currentNode = list->nextNode; currentNode != list; currentNode = currentNode->nextNode){
if ([currentNode isKindOfClass:[Circle class]]) {
NSLog(@"Circle: radius is %.2f", [((Circle*)currentNode) getRadius]);
} else if ([currentNode isKindOfClass:[Rectangle class]]) {
NSLog(@"Rectangle: width is %.2f", [((Rectangle*)currentNode) getWidth]);
} else if ([currentNode isKindOfClass:[Ellipse class]]) {
NSLog(@"Ellipse: minor is %.2f, magor is %.2f", [((Ellipse*)currentNode) getMinorSemiaxis], [((Ellipse*)currentNode) getMajorSemiaxis]);
}
}
NSLog(@"///////////////");
BubbleSort(list);
for(Figure *currentNode = list->nextNode; currentNode != list; currentNode = currentNode->nextNode){
if ([currentNode isKindOfClass:[Circle class]]) {
NSLog(@"Circle %.2f: radius is %.2f",[((Circle*)currentNode) getSquare], [((Circle*)currentNode) getRadius]);
} else if ([currentNode isKindOfClass:[Rectangle class]]) {
NSLog(@"Rectangle %.2f: width is %.2f",[((Rectangle*)currentNode) getSquare], [((Rectangle*)currentNode) getWidth]);
} else if ([currentNode isKindOfClass:[Ellipse class]]) {
NSLog(@"Ellipse %.2f: minor is %.2f, magor is %.2f",[((Ellipse*)currentNode) getSquare], [((Ellipse*)currentNode) getMinorSemiaxis], [((Ellipse*)currentNode) getMajorSemiaxis]);
}
} return 0;
}