My favorites
▼
|
Sign in
kpi-java-training-6
KPI Java Training fall 2013
Project Home
Wiki
Issues
Source
Export to GitHub
READ-ONLY: This project has been
archived
. For more information see
this post
.
Search
Search within:
All issues
Open issues
New issues
Issues to verify
for
Advanced search
Search tips
Subscriptions
Issue
5
attachment: Division.java
(2.4 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
package Result;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Division {
/*
* Íàïèñàòü ïðîãðàììó äåëåíèÿ öåëûõ ÷èñåë â ñòîëáèê. Ïðîãðàììà äîëæíà
ïðèíèìàòü äåëèìîå è äåëèòåëü èç êîíñîëè è âûâîäèòü íà ýêðàí ñòîëáèê
äåëåíèÿ, ÷àñòíîå è îñòàòîê.
**/
public static void main(String[] args)
throws java.io.IOException {
int a,B;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter dividend & divisor");
String scan = reader.readLine();
a=Integer.parseInt(scan);
String scan2 = reader.readLine();
B=Integer.parseInt(scan2);
System.out.println(" "+a+" |___"+B);
if (((a<0)&(B>0)) | ((a<0)&(B<0)))
{
B=-B;
a=-a;
}
int l1 = String.valueOf(a).length();
int am[] = new int[l1];
int A=a;
for (int i=0;i<l1;i++){
am[l1-i-1] = a%10;
a= (int) a/10;
}
int z = 0;
int x1=0;
if (am[0]>=B) z=am[0];
else
for ( int x=1,y=am[0]; (x<l1) & (z<B); x++)
{
y = y*10+am[x];
z=y;
x1=x;
}
int res[]=new int[l1-x1];
res[0] = (int) z/B;
System.out.println(-res[0]*B+" | "+res[0]);
for (int i1=1; i1<(l1-x1);i1++)
{
int z1 = (z-res[i1-1]*B)*10+am[x1+i1];
res[i1] = (int) z1/B;
System.out.println(" "+(z-res[i1-1]*B)+am[i1+x1]+" | "+res[i1]);
System.out.println(" "+(-res[i1]*B)+" | ");
z=z1;
}
int sum=0;
for (int i=0; i<(l1-x1); i++)
{
sum = (int) (sum+res[i]*Math.pow(10, l1-i-1-x1));
}
System.out.println(" "+A%B);
System.out.println();
System.out.println(A+" /"+B+" = quotient "+sum);
System.out.println("modulo = "+A%B);
}
}
Powered by
Google Project Hosting