-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdivision_using_string.cpp
More file actions
196 lines (174 loc) · 4.85 KB
/
division_using_string.cpp
File metadata and controls
196 lines (174 loc) · 4.85 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
Author : Abhishek Yadav
Roll NO: CS12B032
this programs calculates the quotient of a division upto nth places of decimal
So as a part of input you have to enter first numerator which can be anything(fraction, some invalid string
, which, after validation check will be rejected), and second input as denominator and third is the number of places
after decimal you want to print the result.
*/
#include<iostream>
#include<cmath>
using namespace std;
#include"double_linked_list.cpp"
void divide(string &str1,string &str2,unsigned long long n)
{
int flag1=0,flag2=0,i=0,j=0;
if(str1[0]=='-'){
flag1++; i++;
}
if(str2[0]=='-'){
flag2++;j++;
}
//counts how many digits are there in numerator after decimal
int count_after_decimal_num=0;
//counts digits after decimal in denominator
int count_after_decimal_denom=0;
int decimal_flag=0,k=0;
int l1=str1.length();
int temp=l1;
l1--;
unsigned long long num=0;
unsigned long long denom=0;
while(l1>=i){
if(isdigit(str1[l1]))
num+=pow(10,k++)*(str1[l1]-'0');
else decimal_flag++;
if(decimal_flag) count_after_decimal_num++;
l1--;
}
if(count_after_decimal_num){
if(str1[0]!='-')
count_after_decimal_num=temp-count_after_decimal_num;
else count_after_decimal_num=temp-count_after_decimal_num-1;
}
// cout <<num << " "<<count_after_decimal_num<<endl;
l1=str2.length();
temp=l1;
l1--;k=0;
decimal_flag=0;
while(l1>=j){
if(isdigit(str2[l1]))
denom+=pow(10,k++)*(str2[l1]-'0');
else decimal_flag++;
if(decimal_flag) count_after_decimal_denom++;
l1--;
}
if(count_after_decimal_denom){
if(str2[0]!='-')
count_after_decimal_denom=temp-count_after_decimal_denom;
else count_after_decimal_denom=temp-count_after_decimal_denom-1;
//cout <<denom << " "<<count_after_decimal_denom<<endl;
}
if(denom==0){
cout <<"infinity\n";
return ;}
else if(num==0){
cout <<num <<endl;return;}
if(num==denom){
cout << num/denom <<" " <<endl;
return;
}
temp=num;
int temp_=denom;
unsigned long long counter=0;
int count_upto_decimal=0;
int count_num=0;
list* div_list=NULL;
if(temp<temp_){
while(temp<temp_)
{
temp*=10;
count_num++;
}
}
if(temp>temp_){
int local=temp/temp_;
while(local>0){
div_list=add_list_in_reverse(div_list,local%10);
count_upto_decimal++;
local/=10;
}
temp%=temp_;
}
while(temp<temp_){
if(counter==n|| temp==0)
break;
int flag=0;
while(temp<temp_){
temp*=10;
if(flag>=1)
div_list=add_node_in_list(div_list,0);
flag++;
}
div_list=add_node_in_list(div_list,temp/temp_);
temp%=temp_;
counter++;
}
if(flag1^flag2)
cout <<"-";
int temp3= count_after_decimal_denom - count_after_decimal_num - count_num + count_upto_decimal;
// cout <<temp3 <<" " <<count_after_decimal_denom <<" "<<count_after_decimal_num
//<<" " << count_num <<" " <<count_upto_decimal <<endl;
if(temp3<0){
cout <<".";
while(temp3<0){
cout <<"0" ;
temp3+=1;
}
list *flag_list=div_list;
while(flag_list!=NULL){
cout <<flag_list->data;
flag_list=flag_list->rear;
}
}else {
list *flag_list=div_list;
while(flag_list!=NULL){
if(temp3==0)
cout <<".";
cout <<flag_list->data;
flag_list=flag_list->rear;
--temp3;
}
}
cout <<endl;
return ;
}
bool verify_num(string &str)
{
int l=str.length();
int flag=0;
int i=0;
if(str[i]=='-')
i++;
while(i<l){
if(isdigit(str[i])){i++;continue;}
else if(str[i]=='.' && flag==0)
flag++;
else break;
i++;
}
if(i<l) return false ;
else return true;
}
int main()
{
string str1,str2;
cout <<"enter Numerator : ";
getline(cin,str1);
if(!verify_num(str1)){
cout <<"Input not consistent:\n";
return 0;
}
cout <<"Enter denominator: ";
getline(cin,str2);
if(!verify_num(str2)){
cout <<"Input not consistent:\n";
return 0;
}
cout <<"After Decimal, upto how many digits do you want the result:? \n";
unsigned long long n;
cin >>n;
cout <<"result of division is: \n";
divide(str1,str2,n);
return 0;
}