请问在c 中怎样才能实现类似java里面的数据类型的问题?急急急!

宙斯之神 2006-09-13 10:10:24
有下面这样的java代码:
package systeme.defaulttypes;

import java.math.BigInteger;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;

public class IntNUM extends systeme.typclasses.NUM {

/** stores the int value */
private BigInteger biValue;

/** constructor for the class */
public IntNUM(int iVal) {
biValue = new BigInteger("" + iVal);
}

/** constructor for the class */
public IntNUM(String sVal) {
biValue=new BigInteger(sVal);
}

/** method to get the BigIntegrValue */
public BigInteger bigIntegerValue() {
return biValue;
}

// implementation of the class DATA
/////////////////////////////////////

/**
* method for getting the name of the type, returns "Int"
*/
public String getName() {
return "Int";
}

/**
* parses the element, uses BigInteger(String)
*/
public static IntNUM valueOf(String sVal) {
return new IntNUM(new BigInteger(sVal));
}

/**
* method for parsing the elements, implemenets DATAs sTyp is just for
* implementing the interface, is ignored
*/
public systeme.typclasses.DATA fromString(String sVal, String sTyp) {
return IntNUM.valueOf(sVal);
}

/**
* help function for Parsing reads a IntNUM from the Tokenizer
*/
public static IntNUM fromTokenizer(StringTokenizer ST) {
try {
if (!(ST.hasMoreTokens()))
throw new NumberFormatException("error in parsing Int: Int expected instead of <>");
String T1 = ST.nextToken().trim();
IntNUM iRet = valueOf(T1);
return iRet;
} catch (NoSuchElementException nse) {
throw new NumberFormatException("generation error: no tokens any more");
// should be unreachable
}
}

// implementation of the class EQ
/////////////////////////////////////

/** method for comparing two elements for equality */
public systeme.defaulttypes.BoolEQ eq(systeme.typclasses.EQ eqOther) {
if (eqOther instanceof systeme.defaulttypes.IntNUM)
return new systeme.defaulttypes.BoolEQ(bigIntegerValue().equals(((IntNUM) eqOther).bigIntegerValue()));
return systeme.defaulttypes.BoolEQ.FALSE;
}

/** method for comparing two elements for inequality */
public systeme.defaulttypes.BoolEQ neq(systeme.typclasses.EQ beqOther) {
return eq(beqOther).not();
}

// implementation of the class ORD
///////////////////////////////////

/** method for comparing two elements for < */
public systeme.defaulttypes.BoolEQ lt(systeme.typclasses.ORD eqOther) {
if (eqOther instanceof systeme.defaulttypes.IntNUM)
if (bigIntegerValue().compareTo(((IntNUM) eqOther).bigIntegerValue()) == -1)
return systeme.defaulttypes.BoolEQ.TRUE;
return systeme.defaulttypes.BoolEQ.FALSE;
}

// "implementation" of the class NUM
// work directly on the BigIntegers (NUM does string conversion)
/////////////////////////////////////////////////////////////////

/** add an element */
public systeme.typclasses.NUM add(systeme.typclasses.NUM nmOther) {
if (nmOther instanceof systeme.defaulttypes.IntNUM) {
return new IntNUM(bigIntegerValue().add(((systeme.defaulttypes.IntNUM) nmOther).bigIntegerValue()));
}
return super.add(nmOther);
}

/** subtract an element */
public systeme.typclasses.NUM sub(systeme.typclasses.NUM nmOther) {
if (nmOther instanceof systeme.defaulttypes.IntNUM) {
return new systeme.defaulttypes.IntNUM(bigIntegerValue().subtract(((systeme.defaulttypes.IntNUM) nmOther).bigIntegerValue()));
}
return super.sub(nmOther);
}

/** multiply an element */
public systeme.typclasses.NUM mult(systeme.typclasses.NUM nmOther) {
if (nmOther instanceof IntNUM) {
return new IntNUM(bigIntegerValue().multiply(((IntNUM) nmOther).bigIntegerValue()));
}
return super.mult(nmOther);
}

/** divide an element */
public systeme.typclasses.NUM divide(systeme.typclasses.NUM nmOther) {
if (nmOther instanceof systeme.defaulttypes.IntNUM) {
return new systeme.defaulttypes.IntNUM(bigIntegerValue().divide(((systeme.defaulttypes.IntNUM) nmOther).bigIntegerValue()));
}
return super.divide(nmOther);
}

}

请问在c里面类似的类型以及里面方法该如何实现?
多谢!
...全文
130 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
宙斯之神 2006-09-17
  • 打赏
  • 举报
回复
谢谢 各位
JFDream_15 2006-09-13
  • 打赏
  • 举报
回复
JAVA不懂,但是类型转换上的话应该不会有问题,如果是字符串的,可以用CHAR,如果是整型的可以转为INT
BigInteger是什么类型?如果是位运算的话,C语言可以用相应的移位操作或者相应的位操作运算符来对应.

LZ可以把具体的问题放上来讨论一下.
hailongchang 2006-09-13
  • 打赏
  • 举报
回复
我的意思是说因为BigInteger系统无法表示,所以你只能自己来表示,比如说你用一个数组来表示BigInteger,那么做用在这个抽象类型BigInteger的操作比如计算都要你自己用数组的方式在底层实现,这涉及到进位等等复杂的问题,而且用的时候你也只能以函数调用的方式来进行计算,表面上看起来很不直观,因为C没有C++的运算符重载

比如定义BigInteger

struct BigInteger
{
int a[20];
};

BigInteger *add(BigInteger *s1,BigInteger *s2);

//以上我只是随便写了一下,你可以有更精巧的设计,个人建议如果可能,改用C++更好一些.

宙斯之神 2006-09-13
  • 打赏
  • 举报
回复
hailongchang(novice)
能否再说的详细点,给点思路,因为系统要求的用c 实现,没有办法啊!尤其在类型如何定义上还很迷惑! 多谢!
hailongchang 2006-09-13
  • 打赏
  • 举报
回复
你说的这些用C++都可以做到,
问题是如果用C实现,就不要拘泥于什么CLASS,用基本类型都可以做到,但可能实现起来比较复杂,用的时候也不是很方便.
宙斯之神 2006-09-13
  • 打赏
  • 举报
回复
用struct实现class的话,那么类似于 BigInteger ,BigDecimal这样的类型在c里面该如何模仿呢?是不是要用到c里面的最基本的类型来实现呢?并且像BigInteger这样的类型的加减运算也要全部转换为c 里面基本类型的运算吗?
睡在床板下_ 2006-09-13
  • 打赏
  • 举报
回复
还有继承啊...那你还要把那个class NUM也写出来啊
hailongchang 2006-09-13
  • 打赏
  • 举报
回复
class 可以用struct实现

69,370

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧