我从SEE开始编程抽象(CS106B)。我很难开始任务。这是热身练习的代码。我已经咨询了各种不同的解决方案,但没有一个能够工作,因此,我们不得不在这里发布。
#include <iostream>
#include <cstring>
#include "console.h"
using namespace std;
/* Constants */
const int HASH_SEED = 5381; /* Starting point for first cycle */
const int HASH_MULTIPLIER = 33; /* Multiplier for each cycle */
const int HASH_MASK = unsigned(-1) >> 1; /* All 1 bits except the sign */
/* Function prototypes */
int hashCode(string name);
/* Main program to test the hash function */
int main() {
string name = getLine("Please enter your name: ");
int code = hashCode(name);
cout << "The hash code for your name is " << code << "." << endl;
return 0;
}
/*
* Function: hash
* Usage: int code = hashCode(key);
* --------------------------------
* This function takes the key and uses it to derive a hash code,
* which is nonnegative integer related to the key by a deterministic
* function that distributes keys well across the space of integers.
* The general method is called linear congruence, which is also used
* in random-number generators. The specific algorithm used here is
* called djb2 after the initials of its inventor, Daniel J. Bernstein,
* Professor of Mathematics at the University of Illinois at Chicago.
*/
int hashCode(string str) {
unsigned hash = HASH_SEED;
int nchars = str.length();
for (int i = 0; i < nchars; i++) {
hash = HASH_MULTIPLIER * hash + str[i];
}
return (hash & HASH_MASK);
}
编译日志:
> 1>------ Build started: Project: Warmup, Configuration: Debug Win32 ------
1>Compiling...
1>Warmup.cpp
1>c:\users\users\google drive\courses\programming abstractions (stanford cs106b) 2012\assignments\assignment1-vs2008\assignment1-vs2008\0-warmup\src\warmup.cpp(30) : error C3861: 'getLine': identifier not found
1>Build log was saved at "file://c:\Users\Users\Google Drive\Courses\Programming Abstractions (Stanford CS106B) 2012\Assignments\Assignment1-vs2008\Assignment1-vs2008\0-Warmup\Warmup\Debug\BuildLog.htm"
1>Warmup - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
答案 0 :(得分:5)
快速浏览Stanford's C++ library的文档,通过CS106B的主页找到(我知道这听起来不可信,但确实如此),显示你应该
#include "simpio.h"
我建议您为文档添加书签;它可以帮助你避免许多深夜的家庭作业恐慌。
答案 1 :(得分:3)
首先,您必须包含标题<string>
而不是<cstring>
#include <string>
其次使用getline
代替getLine
。并且参数的类型和数量指定不正确。也许你想使用其他一些用户定义的函数getLine。