这个程序对非常大的数字进行除法(我需要它最多1000个数字)。因为没有数据类型可以处理非常大的数字我们使用数组。 我试图将这个Java程序翻译成C.我已经完成了一些但是在将字符串转换为C兼容数据类型时遇到了麻烦。记住我们需要将数字作为字符串然后转换为int。
最大的挑战似乎是String,StringBuilder和追加。我不知道如何翻译它们。
最麻烦的是:
if (len1 < len2) return new String[]{"0", n1};
StringBuilder digits = new StringBuilder();
String n3 = n1.substring(0, len2);
Java代码:
import java.io.*;
import java.util.*;
class BigDiv
{
public static void main(String[] args)
{
String n1 = "30";
String n2 = "2";
String[] results = Divide(n1, n2);
System.out.println("Quotient is : " + results[0]);
System.out.println("Remainder is : " + results[1]);
}
static String[] Divide(String n1, String n2)
{
Boolean negative = false;
if (n1.charAt(0) == '-' ^ n2.charAt(0) == '-') negative = true;
if (n1.charAt(0) == '-') n1 = n1.substring(1);
if (n2.charAt(0) == '-') n2 = n2.substring(1);
if (n1.equals("0") && n2.equals("0"))
{
return new String[] {"Not a number", "0"};
}
if (n2.equals("0"))
{
if (!negative) return new String[] {"Infinity", "0"};
return new String[] {"-Infinity", "0"};
}
int len1 = n1.length();
int len2 = n2.length();
if (len1 < len2) return new String[]{"0", n1};
StringBuilder digits = new StringBuilder();
String n3 = n1.substring(0, len2);
int len3 = len2;
String n4;
int quotient;
int index = len2 - 1;
while(true)
{
quotient = 0;
while(true)
{
n4 = Subtract(n3, n2);
if (n4 == "-1")
{
break;
}
quotient++;
//System.out.println(quotient);
if (n4 == "0")
{
n3 = "0";
break;
}
n3 = n4;
}
if (digits.toString().equals("0"))
{
digits.setCharAt(0, (char)(quotient + 48));
}
else
{
digits.append((char)(quotient + 48));
}
if (index < len1 - 1)
{
index++;
if (n3.equals("0")) n3 = "";
n3 += n1.charAt(index);
len3 = n3.length();
}
else
{
String result = new String(digits);
if (negative)
{
if (!result.equals("0")) result = "-" + result;
if (!n3.equals("0")) n3 = "-" + n3;
}
return new String[]{result, n3};
}
}
}
static String Subtract(String n1, String n2)
{
int len1 = n1.length();
int len2 = n2.length();
if (len1 < len2) return "-1";
int max = Math.max(len1, len2);
int[] ia1 = new int[max];
int[] ia2 = new int[max];
int[] ia3 = new int[max];
for(int i = max - len1; i < max; i++) ia1[i] = n1.charAt(i + len1 - max) - 48;
for(int i = max - len2; i < max; i++) ia2[i] = n2.charAt(i + len2 - max) - 48;
int diff = 0;
int carry = 0;
for(int i = max - 1; i >= 0; i--)
{
diff = ia1[i] - ia2[i] - carry;
carry = 0;
if (diff < 0)
{
diff += 10;
carry = 1;
}
ia3[i] = diff;
}
if (carry == 1) return "-1";
// find first non-zero element of array ia3
int first = -1;
for (int i = 0; i < max; i++)
{
if (ia3[i] != 0)
{
first = i;
break;
}
}
if (first == -1) first = max - 1;
char[] c3 = new char[max - first];
for(int i = first; i < max; i++) c3[i - first] = (char)(ia3[i] + 48);
//System.out.println("c IS : " + c3[0]);
return new String(c3);
}
到目前为止我的C代码:(在除法函数中检查NaN和负数,我并不真正需要。我也不应该使用VLA。)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int Divide(char n1[], char n2[]);
int Subtract(char n1[], char n2[]);
int main()
{
char n1[] = "30";
char n2[] = "2";
char results[] = Divide(n1, n2);
printf("Quotient is : %d", results[0]);
printf("Remainder is : %d", results[1]);
}
int Divide(char n1[], char n2[])
{
/*Boolean negative = false;
if (n1[0] == '-' ^ n2[0] == '-') negative = true;
if (n1[0] == '-') n1 = n1.substring(1);
if (n2[0] == '-') n2 = n2.substring(1);
if (n1.equals("0") && n2.equals("0"))
{
return new String[] {"Not a number", "0"};
}
if (n2.equals("0"))
{
if (!negative) return new String[] {"Infinity", "0"};
return new String[] {"-Infinity", "0"};
}*/
int len1 = strlen(n1);
int len2 = strlen(n2);
if (len1 < len2) return new String[]{"0", n1};
StringBuilder digits = new StringBuilder();
String n3 = n1.substring(0, len2);
int len3 = len2;
String n4;
int quotient;
int index = len2 - 1;
while(true)
{
quotient = 0;
while(true)
{
n4 = Subtract(n3, n2);
if (n4 == "-1")
{
break;
}
quotient++;
if (n4 == "0")
{
n3 = "0";
break;
}
n3 = n4;
}
if (digits.toString().equals("0"))
{
digits.setCharAt(0, (char)(quotient + 48));
}
else
{
digits.append((char)(quotient + 48));
}
if (index < len1 - 1)
{
index++;
if (n3.equals("0")) n3 = "";
n3 += n1[index];
len3 = n3.length();
}
else
{
String result = new String(digits);
if (negative)
{
if (!result.equals("0")) result = "-" + result;
if (!n3.equals("0")) n3 = "-" + n3;
}
return new String[]{result, n3};
}
}
}
int Subtract(char n1[], char n2[])
{
int len1 = n1.length();
int len2 = n2.length();
if (len1 < len2) return "-1";
int max;
if(len1>len2) max = len1;
else if(len2>len1) max = len2;
else max = len1;
int ia1[max];
int ia2[max];
int ia3[max];
for(int i = max - len1; i < max; i++) ia1[i] = n1[i + len1 - max] - 48;
for(int i = max - len2; i < max; i++) ia2[i] = n2[i + len2 - max] - 48;
int diff = 0;
int carry = 0;
for(int i = max - 1; i >= 0; i--)
{
diff = ia1[i] - ia2[i] - carry;
carry = 0;
if (diff < 0)
{
diff += 10;
carry = 1;
}
ia3[i] = diff;
}
if (carry == 1) return "-1";
// find first non-zero element of array ia3
int first = -1;
for (int i = 0; i < max; i++)
{
if (ia3[i] != 0)
{
first = i;
break;
}
}
if (first == -1) first = max - 1;
char c3[max - first];
for(int i = first; i < max; i++) c3[i - first] = (char)(ia3[i] + 48);
return new String(c3);
}
答案 0 :(得分:0)
C中的字符串只是由0值字节终止的字符序列。没有专用的字符串类型;它们存储为char
的数组,并且没有为数组定义=
,+
和==
等运算符。
要分配字符串,请使用strcpy
库函数。要相互追加字符串,请使用strcat
库函数。要比较字符串,请使用strcmp
。
你必须做自己的记忆管理; C不会在扩展字符串时自动分配更多内存,也不会在没有对该内存的更多引用时执行任何自动垃圾收集,这意味着您必须显式释放任何动态分配的内存。使用malloc
或calloc
分配内存realloc
以分配或扩展已使用malloc
,calloc
或{{分配的缓冲区1}}和free
取消分配使用realloc
,malloc
或calloc
分配的内存。
realloc
不初始化动态分配的内存; malloc
会将其初始化为all-bits-0。
这里有一些示例C代码,用于分配,分配,追加,比较和解除分配字符串:
calloc
您必须确保您的目标缓冲区足够大,以容纳您正在写入的内容。 C不会对数组访问进行边界检查,如果你试图在数组范围之外读取或写入,它不会抛出异常(除非你试图访问受保护的内存,但是& #39;系统异常,而不是语言异常)。