My favorites | Sign in
Project Home Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions

Issue 7 attachment: division.java (5.2 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
//package division;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.Math;
public class division {
private static BufferedReader BufferedReader;
public static int read(String str,int i, int j,int remain)//чтение из строки str с i по j с учетом остатка remain
{
int z,a,pos;
pos = j-i-1;
a = remain*((int) Math.pow(10,pos+1));
for(z=i;z<j;z++)
{
a = a + (str.charAt(z)-48)*((int) Math.pow(10, pos));
pos--;
}
return a;
}
public static int need_read(int remain,String str,int pos,int b)//возвращает необходимое кол-во чисел для следующего этапа деления
{
int kol,curr,j;
kol = 0;
curr = remain;
while(curr<b)
{
kol++;
if ((kol+pos>str.length())) return -1;
curr = read(str,pos,pos+kol,remain);
}
return kol;
}
public static String control(String st1,String st2) //проверка на ввод
{
String msg="";
int i;
for(i=0;i<st1.length();i++)
{
if (((int)st1.charAt(i) == 45) && (i>0))
msg = "illegal minus";
if (((int) st1.charAt(i)!=45) && (((int)st1.charAt(i)>58) | ((int)st1.charAt(i)<47)))
msg = "illegal characters";
}
for(i=0;i<st2.length();i++)
{
if (((int)st2.charAt(i) == 45) && (i>0))
msg = "illegal minus";
if (((int)st2.charAt(i)!=45) && (((int)st2.charAt(i)>58) | ((int)st2.charAt(i)<47)))
msg = "illegal characters";
}
if (read(st2,0,st2.length(),0)==0) msg = "division by zero";
return msg;
}
public static String inp(String s,String in) throws IOException
{
char c;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print(in);
s = br.readLine();
return s;
}
public static void main(String[] args) throws IOException {
String str1="",str2="",s1="",s2="",msg="",sign1="",sign2="";
char ch = 0;
int a,b=0,d,c,curr,kol,arr[],curr_remain,pos;


do
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
str1 = inp(str1,"Enter devident ");
str2 = inp(str2,"Enter divisor ");
msg = control(str1,str2);
if (msg=="")
{
if ((str1.charAt(0)==45)&&(str2.charAt(0)!=45))
{
sign1 = "-";
sign2 = "-";
s1 = str1.substring(1,str1.length());
s2 = str2;
}
if ((str1.charAt(0)==45)&&(str2.charAt(0)==45))
{
sign2 = "-";
s1 = str1.substring(1,str1.length());
s2 = str2.substring(1,str2.length());
}
if ((str1.charAt(0)!=45)&&(str2.charAt(0)==45))
{
sign1 = "-";
s2 = str2.substring(1,str2.length());
s1 = str1;
}
if ((str1.charAt(0)!=45)&&(str2.charAt(0)!=45))
{
s1 = str1;
s2 = str2;
}
b = read(s2,0,s2.length(),0);
a = 0; c = 0; d = 0; curr_remain = 0; kol = 0;curr = 0;
while (kol!=-1)
{
kol = need_read(curr_remain,s1,curr,b);
if (kol==-1)
{
if (curr<s1.length()) curr_remain = read(s1,curr,s1.length(),curr_remain);
}
else
{
a = read(s1,curr,curr+kol,curr_remain);
curr = curr + kol;
curr_remain = a%b;
System.out.print("current remain ");
System.out.print(sign2);System.out.println(curr_remain);
}
}
System.out.print("quotient ");
System.out.print(sign1);
System.out.println((read(s1,0,s1.length(),0)-curr_remain)/b);
}
else System.out.println(msg);
System.out.println("Would you like continue? y/n");
ch = (char) br.read();
} while (ch=='y');
}
}
Powered by Google Project Hosting