-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
147 lines (108 loc) · 3.39 KB
/
main.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
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
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include "include/DataStructs.h"
#include "include/Add.h"
#include "include/Sub.h"
#include "include/Mul.h"
#include <random>
#include <string>
void String_toInt(std::string str, int *arr, int length);
void CreateLinkList(LinkList &L, int arr[], int n);
void output(LinkList &L);
int substring(std::string str, int length);
int main() {
LinkList first_LinkList;
first_LinkList = (LinkList)malloc(sizeof(LNode));
first_LinkList->next = nullptr;
LinkList second_LinkList;
second_LinkList = (LinkList)malloc(sizeof(LNode));
second_LinkList->next = nullptr;
// std::string first = "74342226";
// std::string second = "35757";
// std::string first = "74343";
// std::string second = "35757";
std::string first = "985757456189";
std::string second = "1274314252687532121";
// std::string first = "1274314252687532121";
// std::string second = "985757456189";
// std::string first = "7434222687532121";
// std::string second = "35757456189";
// std::string first = "985757456189";
// std::string second = "127431";
first = std::string(first.rbegin(),first.rend());
second = std::string(second.rbegin(),second.rend());
int first_length = 0, second_length = 0 ;
first_length = substring(first, first_length);
second_length = substring(second, second_length);
// std::cout<<"first_length: "<<first_length<<std::endl;
// std::cout<<"first: "<<first.length()<<std::endl;
// std::cout<<"second_length: "<<second_length<<std::endl;
// std::cout<<"second: "<<second.length()<<std::endl;
// 定义一个字符串数组 存储 数据的每一位
int firstarr[first_length];
int secondarr[second_length];
String_toInt(first, firstarr, first_length);
String_toInt(second, secondarr, second_length);
CreateLinkList(first_LinkList,firstarr,first_length);
CreateLinkList(second_LinkList,secondarr,second_length);
Add firstAddsecond;
if(first.length()>=second.length())
{
LinkList result = firstAddsecond.add(first_LinkList,second_LinkList,first_length,second_length);
}
else
{
LinkList result = firstAddsecond.add(second_LinkList,first_LinkList,second_length,first_length);
}
output(first_LinkList);
output(second_LinkList);
}
void String_toInt(std::string str, int *arr, int length)
{
int k = 0;
for (int i = 0; i < length; ++i) {
while(k < str.length())
{
std::cout<<"K: "<<k<<std::endl;
std::string temp_char = str.substr(k, 4);
std::cout<<"temp_char.c_str(): "<<temp_char.c_str()<<std::endl;
arr [i] = atoi(temp_char.c_str());
k=k+4;
break;
}
}
}
int substring(std::string str, int length)
{
if(str.length()%4==0)
{
length = str.length()/4;
}
else
{
length = str.length() / 4 + 1;
}
return length;
}
void CreateLinkList(LinkList &L, int arr[], int n)
{
LNode *p;
for(int i = n; i>0; i--)
{
p = (LinkList) malloc(sizeof(LNode));
p->data = arr[i-1];
p->next = L->next;
L->next=p;
}
}
void output(LinkList &L)
{
LNode *p;
p=L->next;
while(p!=NULL)
{
printf("%d\n",atoi(p->data.c_str()));
p=p->next;
}
}