-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest1.cpp
118 lines (112 loc) · 2.22 KB
/
test1.cpp
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
#include<iostream>
#include<string>
#include<malloc.h>
using namespace std;
int m, n;
class Link
{
public:
Link(string s, char c[], int nu[] , double dl[])
{
num = (int *)malloc(sizeof(int)*(n+m+1));
ch = (char *)malloc(sizeof(char)*(n+m+1));
d = (double *)malloc(sizeof(double)*(n+m+1));
type = s;
num = nu;
ch = c;
d = dl;
};
~Link();
void show()
{
if (type == "int")
for (int i = 0; i < m + n; i++)
cout << num[i] << " ";
if (type == "char")
for (int i = 0; i < m + n; i++)
cout << ch[i] << " ";
if (type == "double")
for (int i = 0; i < m + n; i++)
cout << d[i] << " ";
}
Link operator +(Link &l);
private:
string type;
int *num;
char *ch;
double *d;
};
Link Link::operator +(Link &l)
{
for (int i = n,j=0; i < m + n; i++,j++)
{
if (this->type == "int")
this->num[i] = l.num[j];
if (this->type == "double")
this->d[i] = l.d[j];
if (this->type == "char")
this->ch[i] = l.ch[j];
}
return Link(this->type,this->ch,this->num,this->d);
}
Link::~Link()
{
}
int main()
{
string s;
int *num1 = {};
int *num2 = {};
char *ch1 = {};
char *ch2 = {};
char ch;
double *d1 = {};
double *d2 = {};
cin >> s;
cin >> m >> n;
if (s == "int")
{
num1 = (int *)malloc(sizeof(int)*n);
num2 = (int *)malloc(sizeof(int)*m);
for (int i = 0; i < n; i++)
cin >> num1[i];
for (int i = 0; i < m; i++)
cin >> num2[i];
Link l1(s,ch1,num1,d1);
Link l2(s,ch2,num2,d2);
Link l3 = l1 + l2;
l3.show();
}
if (s == "char")
{
ch1 = (char *)malloc(sizeof(char)*(n+1));
ch2 = (char *)malloc(sizeof(char)*(m+1));
for (int i = 0; i < n; i++)
{
cin >> ch1[i];
ch = getchar();
}
for (int i = 0; i < m; i++)
{
cin >> ch2[i];
ch = getchar();
}Link l1(s, ch1, num1, d1);
Link l2(s, ch2, num2, d2);
Link l3 = l1 + l2;
l3.show();
}
if (s == "double")
{
d1 = (double *)malloc(sizeof(double)*n);
d2 = (double *)malloc(sizeof(double)*m);
for (int i = 0; i < n; i++)
cin >> d1[i];
for (int i = 0; i < m; i++)
cin >> d2[i];
Link l1(s, ch1, num1, d1);
Link l2(s, ch2, num2, d2);
Link l3 = l1 + l2;
l3.show();
}
return 0;
}