程序设计综合实践1.9

更新时间:2023-06-24 16:38:09 阅读: 评论:0

程序设计综合实践1.9
有符号⼤数加、减运算。请在样例程序Ex1.4基础上实现⽆符号⼤数⽐较运算(⼩于、⼩于等于、等于、⼤于、⼤于等于),并进⼀步实现有符号⼤数的加、减运算。题⽬要求输⼊两个有符号⼤数,输出它们的和、差。
输⼊格式:
两个有符号⼤数。
输出格式:
两个有符号⼤数,每个⼀⾏,正号不输出。
输⼊样例:-1234567890987654321333888999666 147655765659657669789687967867
输出样例:-1086912125327996651544201031799 -1382223656647311991123576967533
作者李卫明
单位杭州电⼦科技⼤学代码长度限制16 KB
时间限制400 ms
内存限制64 MB
Java版本:
import java.util.ArrayList;
import java.util.Scanner;
public class BigNumber2 {
public static int singed_Compare(String a,String b){
//1表⽰ a>b 0表⽰ a<b
int count=0;
if( a.contains("-") && !b.contains("-")) return 0;
el if( !a.contains("-") && b.contains("-") ) return 1;
el if(!a.contains("-") && !b.contains("-")){
if(a.length()>b.length())return 1;
el if(a.length()<b.length()) return 0;
el if(a.length()==b.length()){
int[] int_arr1 = split_Number(a);
int[] int_arr2 = split_Number(b);
for (int i = int_arr1.length-1; i >=0 ;i--) {
if(int_arr1[i]>int_arr2[i])return 1;
el if (int_arr1[i]<int_arr2[i])return 0;
}
}
}el ains("-") && b.contains("-")){
if(a.length()>b.length())return 0;
el if(a.length()<b.length()) return 1;
el if( a.length()==b.length() ){
int[] int_arr1 = split_Number(a);
int[] int_arr2 = split_Number(b);
for (int i = int_arr1.length-2; i >=0 ;i--) {
if(int_arr1[i]>int_arr2[i])return 0;
el if (int_arr1[i]<int_arr2[i])return 1;
}
}
}
return count;
}
public static void signed_Add(String a,String b){
if(!a.contains("-") && !b.contains("-")){
白毫银针是什么茶int[] int_arr1 = split_Number(a);
int[] int_arr2 = split_Number(b);
bigNumber_add(int_arr1,int_arr2);
}el if( (!a.contains("-")&&ains("-")) || (a.contains("-")&&!b.contains("-")) ){            //判断a,b的⼤⼩
int flag = singed_Compare(a,b);
ains("-"))place("-","");
el place("-","");
int[] int_arr1 = split_Number(a);
int[] int_arr2 = split_Number(b);
if(singed_Compare(a,b)==1) {
if(flag==1)
bigNumber_sub(int_arr1,int_arr2);
el{
System.out.print("-");
bigNumber_sub(int_arr1,int_arr2);
}
}el{
if(flag==1){
阳朔渔火节System.out.print("-");
bigNumber_sub(int_arr2,int_arr1);}el{
台湾本土剧bigNumber_sub(int_arr2,int_arr1);
}
}
}el {
place("-","");
place("-","");
int[] int_arr1 = split_Number(a);
int[] int_arr2 = split_Number(b);
System.out.println("-");
bigNumber_add(int_arr1,int_arr2);
}
}
public static void signed_Sub(String a,String b){
if(!a.contains("-") && !b.contains("-")){
if(singed_Compare(a,b)==1){
int[] int_arr1 = split_Number(a);
int[] int_arr2 = split_Number(b);
bigNumber_sub(int_arr1,int_arr2);
}el {
int[] int_arr1 = split_Number(a);
int[] int_arr2 = split_Number(b);
System.out.print("-");
bigNumber_sub(int_arr2,int_arr1);
}
}el if( (!a.contains("-")&&ains("-")) || (a.contains("-")&&!b.contains("-")) ){            int flag = singed_Compare(a,b);
ains("-"))place("-","");
el place("-","");
int[] int_arr1 = split_Number(a);
int[] int_arr2 = split_Number(b);
if(flag==1) {
bigNumber_add(int_arr1,int_arr2);
}el{
System.out.print("-");
bigNumber_add(int_arr1,int_arr2);
}
}el {
place("-","");
place("-","");
int flag = singed_Compare(a,b);
int[] int_arr1 = split_Number(a);
int[] int_arr2 = split_Number(b);
if(flag==1){
System.out.println("-");
bigNumber_sub(int_arr1,int_arr2);
}el{
bigNumber_sub(int_arr2,int_arr1);
}
}
}
public static int[] split_Number(String str){
str = new StringBuffer(str).rever().toString();
char[] str_arr = CharArray();
int count=0;
int gment = str.length()/8;//java会⾃动向下取整
int[] number_Array =new int[gment+1];
String input_str="";
for (int i = 0; i < gment; i++) {
男主是反派for (int j = 0; j < 8; j++) {
int index = 8*i+j;
input_str = at(str_arr[index]+"");
}
input_str = new StringBuffer(input_str).rever().toString();
number_Array[i]=Integer.parInt(input_str);
input_str="";
if(i==(gment-1)){
input_str="";
for (int j = 0; j < str.length()-8*(i+1); j++) {
int index = 8*(i+1)+j;
input_str = at(str_arr[index]+"");
}
input_str = new StringBuffer(input_str).rever().toString();
中国是礼仪之邦number_Array[i+1]=Integer.parInt(input_str);
}
}
return number_Array;
}
public static ArrayList input_number(){
//8位⼀段
Scanner sc=new Scanner(System.in);
String str="";
关于选择的名人素材ArrayList arr = new ArrayList();
while (sc.hasNext()){
str = sc.nextLine();
arr.add(str);
}
return arr;
}
public static void bigNumber_add(int[] int_arr1,int[] int_arr2){
int minSegment = (int_arr1.length>int_arr2.length) ?int_arr2.length:int_arr1.length;        int maxSegment = (int_arr1.length>int_arr2.length) ?int_arr1.length:int_arr2.length;        int gmentLength=8;
int[] resSet = new int[maxSegment];
int count=0;//count=0表⽰没有进位
for (int i = 0; i < maxSegment; i++) {
if(i<=minSegment){
if(count==0){
int number_int = (int_arr1[i]+int_arr2[i]); //* ( (int)Math.pow(10,i*8) );
//有进位
个人日志count = (new String(number_int+"")).length() > 8?1:0;
if(count==1) resSet[i]=number_int-100000000;
el{ resSet[i]=number_int;}
}el{
int number_int =  int_arr1[i]+int_arr2[i]+1;
count = (new String(number_int+"")).length() > 8?1:0;
if(count==1) resSet[i]=number_int-100000000;
el{
resSet[i]=number_int;
//                        count=0;
}
}
}el{
if(count==1){
int number_int = (int_arr1.length>int_arr2.length) ?int_arr1[i]+1 : int_arr2[i]+1;                    count = (new String(number_int+"")).length() > 8?1:0;
if(count==1) resSet[i]=number_int-100000000;
el {
resSet[i]=number_int;
}
}
}
}
干煸鱼
for (int i = resSet.length-1; i >=0; i--) {
System.out.print(resSet[i]);
}
System.out.println();
}
public static String sub_Normalization(int number){
String str="";
if(number<100000000) str=""+number;
if(number<10000000) str="0"+number;
if(number<1000000) str="00"+number;
if(number<100000) str="000"+number;
if(number<10000) str="0000"+number;
if(number<1000) str="00000"+number;
if(number<100) str="000000"+number;
if(number<10) str="0000000"+number;
if(number==0) str="00000000";
return str;
}
public static void bigNumber_sub(int[] int_arr1,int[] int_arr2){
int minSegment = (int_arr1.length>int_arr2.length) ?int_arr2.length:int_arr1.length;        int maxSegment = (int_arr1.length>int_arr2.length) ?int_arr1.length:int_arr2.length;        int gmentLength=8;
int[] resSet = new int[maxSegment];
int count=0;//count=0表⽰没有借位
for (int i = 0; i < maxSegment; i++) {
if(i<=minSegment){
if(count==0){
//第1次计算不需要借位count==0
if( (int_arr1[i]-int_arr2[i]) <0 ){
//需要补位
resSet[i] = int_arr1[i]-int_arr2[i]+100000000;
count=1;
}
el {
//不需要补位
int number_int = (int_arr1[i]-int_arr2[i]); //* ( (int)Math.pow(10,i*8) );
resSet[i] = number_int;
}
}el{
if( (int_arr1[i]-int_arr2[i]-1) <0 ){
resSet[i] = int_arr1[i]-int_arr2[i]-1+100000000;
count=1;
} el {
int number_int = (int_arr1[i]-int_arr2[i]-1); //* ( (int)Math.pow(10,i*8) );                        resSet[i] = number_int;
count=0;
}
}
}el{
int number_int = (int_arr1.length>int_arr2.length) ?int_arr1[i] : int_arr2[i];                resSet[i]=number_int;
}
}
for (int i = resSet.length-1; i >=0; i--) {
if(i==(resSet.length-1)) System.out.print(resSet[i]);
el {
System.out.print(sub_Normalization(resSet[i]));
}
}
System.out.println();
}
public static void main(String[] args) {
ArrayList arr = input_number();
System.out.println();
signed_(0).toString(),(1).toString());
signed_(0).toString(),(1).toString());
}
}

本文发布于:2023-06-24 16:38:09,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1029996.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:限制   输出   需要   科技   样例   基础
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图