我正在尝试将C库用于哈佛大学的开放课程.可以在此处找到教师有关设置外部库的说明.
我正在按照ubuntu特有的说明进行操作,因为我试图在我的ubuntu盒子上使用这个库.我按照页面上的说明进行设置,但是当我helloWorld.c使用cs50库函数运行一个简单的程序时,gcc不想播放.
例:
helloWorld.c
#include <stdio.h>
#include <cs50.h>
int
main(void){
printf("What do you want to say to the world?\n");
string message = GetString();
printf("%s!\n\n", message);
}
Run Code Online (Sandbox Code Playgroud)
$ gcc helloWorld.c
/tmp/ccYilBgA.o: In function `main':
helloWorld.c:(.text+0x16): undefined reference to `GetString'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
我按照指示中的说明按照说明进行了操作,但它们对我不起作用.我正在运行ubuntu 12.04.如果我能进一步澄清我的问题,请告诉我.
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int n;
printf("Please give me an integer greater than zero!\n");
n=GetInt();
if(n<0)
{
printf("You are giving me a bad value!\n");
return 1;
}
for(int i=n-1;i<n;n--)
printf("%d\n",n);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想知道为什么如果用户输入一个数字,循环不会无限n.让我们说用户输入40 n; 并不i总是n-1,所以39岁和n40岁,然后i变成38岁时n变成39岁等等 - 所以这不会成为一个无限循环吗?
我的问题与CS50,pset5的任务有关.对于那些不了解的人,我会试着解释一下.没什么特别的.我只需要创建将输入字典文件的函数(之前写过,该文件中的所有单词都是大写的),其中包含超过20K的单词,并以某种方式对它们进行排序.我已经制作了简单而天真的算法,构建了哈希表,根据他们的第一个字母对单词进行排序.我已经通过CS50的所有检查,所以我的程序运行良好.但与课程相比 - 它太慢了.执行人员的时间是0.1秒,但对于我的 - 5.0s - 7.0s.我可以在此代码中改进哪些内容以加快速度?或者我应该彻底改变一切吗?我没有优化经验,因为刚开始学习.从你们任何人那里学习会很棒=)在此先感谢!
// Some constant values, which are declared before the function
#define LENGTH 46
#define ALPHALENGTH 26
/* Definition of node struct. Nothing special, in fact =) */
typedef struct node {
char word[LENGTH +1];
struct node *next;
} node;
node *hashTable[ALPHALENGTH];
bool load(const char *dictionary) {
FILE *f = fopen(dictionary, "r");
if (f == NULL) {
return false;
}
char word[LENGTH + 1];
int hash = 0;
for (int i = 0; i …Run Code Online (Sandbox Code Playgroud) 我正在浏览哈佛CS50在线课程,其中一个问题是使用空格和哈希创建一个"马里奥风格的金字塔".我已经解决了空间,但是哈希给了我麻烦.这是代码:
#include <stdio.h>
#include <cs50.h>
int main(void)
{
//get height between 1 and 23
int height;
do
{
printf("Please enter a height: ");
height = GetInt();
}
while (height < 1 || height > 23);
//build pyramid
for (int i = 0; i < height ; i++)
{
//add spaces
for (int space = height - 1 - i; space >= 0; space--)
printf(" ");
//add hashtags
for (int hash = 2 + i; hash <= height; hash++)
printf("#");
printf("\n"); …Run Code Online (Sandbox Code Playgroud) #include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
//ask user for input
string s = get_string();
//make sure get_string() returned a string
if(s != NULL)
{
//iterate over the characters one at a time
for(int i = 0, int n = strlen(s); i < n; i++)
{
//print i'th character in s
printf("%c\n", s[i]);
}
}
else
{
//tell the user that their input is not a string
printf("Sorry, no good\n");
}
}
Run Code Online (Sandbox Code Playgroud)
编译器抱怨这一行:
for(int i = 0, int …Run Code Online (Sandbox Code Playgroud) 我正在为二分搜索算法编写代码。
代码:
#include "cs50.h"
int main(void) {
int n = GetInt();
int value = GetInt();
int values[n];
for (int i = 0; i < n; i++) {
printf("Put in number %i ", i + 1);
values[i] = GetInt();
}
int mid = (n - 1) / 2;
int en = 0;
int ex = n - 1;
for (int i = 0, xt = i + 1; i < xt; i++) {
if (value > values[mid]) {
en = mid; …Run Code Online (Sandbox Code Playgroud) 我正在尝试查询一个名为 results 的变量,在该变量中我查询数据库以查找标题类似于从 post 方法接收到的搜索栏输入的书籍。我正在运行的查询如下:
results = db.execute("SELECT * FROM books WHERE title LIKE (%:search%)", {"search": search}).fetchall();
Run Code Online (Sandbox Code Playgroud)
通过上述查询,我收到以下错误:
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) syntax error at or near "%".
Run Code Online (Sandbox Code Playgroud)
如果我删除 %,或者如果我手动提供LIKEa 参数(例如:),这将按预期工作LIKE ('%the%'),但这并没有真正返回任何结果,除非搜索与数据库中的书名之一完全相同,并且它失败了通过对参数进行硬编码来使用变量替换的目的。我还想知道在使用 SQLAlchemy 查询时是否可以使用 ILIKE 来区分大小写。
我知道我可以使用对象关系映射,并使用不同的函数,例如过滤器函数等等,但是对于这个分配,我们打算不使用 ORM 并使用简单的查询。有什么建议?
在学习哈佛大学CS50课程的过程中,有一个问题让我很困惑。下面是困扰我很久的问题。
对于下面的代码,它想将名为“EMMA”的字符串与名为“names”的数组进行比较,其中包含 4 个名称。
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
// An array of names
string names[] = {"EMMA", "RODRIGO", "BRIAN", "DAVID"};
// Search for EMMA
for (int i = 0; i < 4; i++)
{
if (strcmp(names[i], "EMMA") == 0)
{
printf("Found\n");
return 0;
}
}
printf("Not found\n");
return 1;
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,它用于if (strcmp(names[i], "EMMA") == 0)检查名称“EMMA”。
然而,它也想如果我像如果我更换另一种方式写代码的运行if (strcmp(names[i], "EMMA") == 0)用if (!strcmp(names[i], "EMMA")),和原来相同的答案“找到”。
如果我没记错的话!,C中的感叹号的意思是“NOT”。在第一种方法中,它使用两个等号来表示与 0 相同的值。但在第二种方法中,它在函数 前面使用感叹号strcmp …
我正在网上做 Cs50 哈佛大学,已经是第三周了,但在观看视频时,我注意到迭代和循环看起来是一样的,因为它们一遍又一遍地重复事情。但一定有区别,否则他们不会为同一事物有两个名字。无论我重新观看视频多少次,我都找不到差异。请帮助我理解。
我对整个 WSL 的情况感到非常困惑。有时我觉得我明白了,但实际上却没有。我主要的困惑是Ubuntu(我使用Ubuntu 20.04)在哪里保存文件?它们如何与我使用Windows 命令行安装的文件交织在一起?我通过 Ubuntu 完成的所有安装都是浪费时间,我找不到为任何编程语言安装的软件包或库。如果有人知道可以帮助我理解的课程或视频,如果您也分享它,我将不胜感激。
这个问题的具体情况是因为我在Ubuntu上安装了一个C语言包(cs50),但我无法让VS Code识别它。我尝试添加 /usr/local 路径,但c_cpp_properties.json没有找到该路径。
我在资源管理器中转到此文件夹,但没有找到任何内容,正如预期的那样。
提前致谢。
c visual-studio-code windows-subsystem-for-linux cs50 ubuntu-20.04
cs50 ×10
c ×9
loops ×2
algorithm ×1
declaration ×1
for-loop ×1
hash ×1
hashtable ×1
iteration ×1
postgresql ×1
python ×1
sqlalchemy ×1
strcmp ×1
ubuntu-20.04 ×1