4 / 4 / 3
Регистрация: 10.04.2013
Сообщений: 172
|
|
1
|
Не совсем корректный вывод при сортировке
24.10.2013, 21:36. Показов 511. Ответов 3
В скриншоте видно что у меня с файла выводит имя цветка, цвет, количество цветов(этот параметр выводит как текстовое значение, хотя в файле есть число), конструтор копирования тоже количество цветков выводит буквой, хотя так не должно быть. Подскажыте, пожалуйста! Спасибо
main
C++ | 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
| #include "flower.h"
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <cctype>
using namespace std;
void main()
{//konstryctoru
flower kvit_1, kvit_2("rosa","red",2), kvit_3(kvit_2), kvit_4;
flower *A = new flower[4];
cout << endl;
//.........................................................
cout <<kvit_1 << "\n" << kvit_2 << endl;
cin >> kvit_3 ;
cout << kvit_3 << endl;
kvit_4.read_file();
cout << kvit_4 << endl;
A[0]=kvit_1;
A[1] = kvit_2;
A[2] = kvit_3;
A[3] = kvit_4;
//.........................................................
//......сортування методом пошуку найменшого елемента......
int N = 4;
int max, i;
flower temp;
while(N >0)
{
max = 0;
for(int i = 0; i < N; i++)
{
if(A[i].sort(A[i], A[max]))
max = i;
temp = A[N-1];
A[N-1] = A[max];
A[max] = temp;
N--;
}
}
for(int i=0; i<4; i++)
A[i].show();
cout<<endl;
system("pause");
} |
|
flower.h
C++ | 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
| #ifndef flower_h_
#define flower_h_
#include<fstream>
#include<iostream>
#include <string>
using namespace std;
class flower
{
private:
int number;
static int count;
protected:
char *color;
public:
char *name;
string name_month;
int numb_month;
static int GetCount;
void set_color(char*);
char*get_color();
void set_number(int);
int get_number();
bool month(int );
bool month(string);
string month ();
void show ();
void vvid();
static int get_count()
{
return count;
};
//addition methods
friend istream& operator >>(istream& in, flower& C)
{
char *s = new char[10];
char *v = new char[10];
cout<<"\nPlease enter the number: ";
in>>C.number;
cout<<"\nPlease enter the name : "; in >> s; C.name = s;
cout<<"\nPlease enter the color: "; in >> v; C.color = v;
return in;
}
friend ostream& operator <<(ostream& out, flower& C)
{
return out << "\n\nnumber = " << C.number << "\nname is: " << C.name << "\ncolor is: " << C.color;
}
void read_file();
void write_in_file();
//.........................................................
bool operator >(flower &C)
{
if(number > C.number) return true;
return false;
}
//............................................//
bool operator <(flower &C)
{
if(number < C.number) return true;
return false;
}
//............................................//
bool operator ==(flower &C)
{
if(number == C.number) return true;
return false;
}
flower& operator = (flower& C)
{
if(&C == this) return *this;
if(C.name)
{
name = new char [strlen(C.name)+1];
strcpy (name, C.name);
}
else name = 0;
if(C.color)
{
color = new char [strlen(C.color)+1];
strcpy (color, C.color);
}
else color = 0;
number = C.number;
return *this;
}
int sort(flower,flower);
flower();
flower(char*,char*, int);
flower(flower &);
~flower();
};
#endif |
|
flower.cpp
C++ | 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
| #include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cctype>
#include "flower.h"
FILE*out;
FILE*Text;
using namespace std;
const int size = 12;
string a[size]={"january", "february", "mart", "april", "may", "june", "july", "ougest", "september", "october", "november", "december"};
int flower::count = 0;
bool flower::month(int n)
{ if((n<=0)||(n>=12)) return false;
name_month=a[n-1];
return true;
}
/*
int flower::GetCount()
{
return count;
}*/
bool flower::month(string s)
{
for(int i=0; i<12;i++)
{ if(s==a[i])
{numb_month=i+1;
return true;}
}
int j=0;
for(int k=0; k<12;j=0,k++ )
{for(int i=0; i<3;i++)
{if ((a[k])[i]==s[i])
{j++;
if(j==3) return true;}}}
return false;
}
string flower::month()
{
return name_month;
}
void flower::set_color(char*s)
{
color=s;
}
char* flower::get_color()
{
return color;
}
void flower::set_number(int s)
{
number=s;
}
int flower::get_number()
{
return number;
}
flower::flower()
{cout<<"Konstryktor\n";
cout<< "flower() {"<< hex << this << "}" <<endl;
number=13;
name="marguerite";
color="blue";
count ++;
}
void flower::read_file()
{
char *s = new char [10];
char *n = new char [10];
ifstream f("out.txt");
f >> s >> number >> n;
name = s; color = n;
f.close();
}
int flower::sort(flower a,flower b)
{
if(a.number > b.number) return 1;
}
void flower::write_in_file()
{
ofstream f("Text.txt", ios::out | ios::app);
f<<name<<' ';
f<<number<<' '<<color;
f.close();
}
void flower::vvid()
{
char*s=new char[12];
char*b=new char[12];
int a;
cout<<"Nazva kvitku "; cin>>s; cout<<"K-st pelustok "; cin>>a;
cout<<"Color "; cin>>b;
this->name=s;
this->set_number(a);
this->set_color(b);
};
void flower::show()
{
cout<<"Nazva kvitku "<<name<<endl; cout<<"K-st pelustok "<<number<<endl;
cout<<"Color "<<color<<endl;
};
flower::flower(flower &m)
{
cout<<"Konstructor kopiyvannia\n";
name=new char[strlen(m.name)+1];
color=new char[strlen(m.color)+1];
number=m.number;
strcpy(name, m.name );
strcpy(color,m.color);
count ++;
}
flower::flower(char*ptr,char*l, int d)
{cout<<"Konstryktor z parrametramu\n";
int len=strlen(ptr);
int dow=strlen(l);
name=(char*) malloc(len+1);
color=(char*) malloc(dow+1);
number=d;
strcpy(name,ptr);
strcpy(color,l);
count ++;
}
flower::~flower()
{cout<<"Robota destructora\n";
count --;
} |
|
0
|