作者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
→ ric2k1:對了 madd 沒有加參數我現在 run 起來沒有問題呀... 11/17 19:07
→ ric2k1:忘記是誰問的了... Anyway, 應該是修好了. 見 145 篇. 11/17 19:09