请问在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里面类似的类型以及里面方法该如何实现?
多谢!