这个程序做除法非常大的数字(我需要它只是高达1000位)。因为没有数据类型可以处理非常大的数字,所以我们使用数组。我想把这个java程序翻译成c。我已经做了一些,但是在将字符串转换成c兼容的数据类型时遇到了问题。请记住,我们需要将数字作为字符串,然后转换为int。
最大的挑战似乎是string、stringbuilder和append。我不知道怎么翻译这些。
在以下方面遇到最大问题:
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);
}
1条答案
按热度按时间fcg9iug31#
c中的字符串只是以0值字节结尾的字符序列。没有专用的字符串类型;它们以数组的形式存储
char
,以及运算符,例如=
,+
,和==
未为数组定义。要指定字符串,请使用
strcpy
库函数。要将字符串相互附加,可以使用strcat
库函数。要比较字符串,请使用strcmp
.你必须做你自己的内存管理;当您扩展字符串时,c不会自动分配更多内存,当不再有对该内存的引用时,c也不会自动进行垃圾回收,这意味着您必须显式地释放任何动态分配的内存。使用
malloc
或者calloc
为了分配内存,realloc
分配或扩展已分配的缓冲区malloc
,calloc
,或realloc
,和free
释放分配给的内存malloc
,calloc
,或realloc
.malloc
不初始化动态分配的内存;calloc
将其初始化为所有位0。下面是一些将分配、分配、追加、比较和取消分配字符串的示例c代码:
您必须确保您的目标缓冲区足够大,以容纳您正在写入的内容。c不会对数组访问进行边界检查,如果您尝试在数组边界之外读或写,它也不会引发异常(除非您尝试访问受保护的内存,但这是系统异常,而不是语言异常)。