作者ric2k1 (Ric)
看板EE_DSnP
标题[公告] Reference code 更正
时间Fri Nov 17 14:08:26 2006
1. If we define "ModNum::_num" as unsigned, there may be problems in
constructing ModNum from negative integer. For example, you will see ---
mcalc> mset 7
mcalc> madd x 0 -2
x(2) = 0 + 2
which is NOT correct!!
we should see ---
mcalc> madd x 0 -2
x(5) = 0 + 5
The reference program "modCalc" in ccws* had this problem too (it's now
fixed).
An easy way to solve this is to change "ModNum::_num" to "int" type.
Please change the "unsigned" in the following code to "int", or download
hw3.tgz again...
calc/calcModNum.h:19: ModNum(unsigned i = 0);
calc/calcModNum.h:27: static void setModulus(unsigned m) { _modulus = m; }
calc/calcModNum.h:28: static unsigned getModulus() { return _modulus; }
calc/calcModNum.h:56: unsigned _num;
calc/calcModNum.h:58: static unsigned _modulus;
calc/calcCmd.cpp:27: if (ModNum::getModulus() != unsigned(m)) {
Of course, if you have other way to fix this, that's fine too. Just make
sure when the arguments are negative integers, you can handle the modular
number operations right!
2. In "myStrNCmp(const string& s1, const string& s2, unsigned n)", the
comparison will be wrong if the string s2 is a superset of s1. For example,
s1 = "HELp", s2 = "HELpkk", n = 3 ---
we will see the comparison result = 0 (i.e. equivalent), which is NOT
correct. We should return a negative integer (i.e. s1 < s2).
Please update the "myStrNCmp" as follows, or download the new hw3.tgz ---
=============
int
myStrNCmp(const string& s1, const string& s2, unsigned n)
{
assert(n > 0);
if (s2.size() == 0) return -1;
unsigned l = s1.size();
assert(l >= n);
for (unsigned i = 0; i < l; ++i) {
if (!s2[i])
return (i < n)? 1 : 0;
char ch1 = (isupper(s1[i]))? tolower(s1[i]) : s1[i];
char ch2 = (isupper(s2[i]))? tolower(s2[i]) : s2[i];
if (ch1 != ch2)
return (ch1 - ch2);
}
return (l - s2.size());
}
======
The red lines are modified.
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.4.242
1F:→ ric2k1:对了 madd 没有加参数我现在 run 起来没有问题呀... 11/17 19:07
2F:→ ric2k1:忘记是谁问的了... Anyway, 应该是修好了. 见 145 篇. 11/17 19:09