这一切都在标题中; 我估计超级简单,但在任何地方搜索语法都很难.
这是我从CS50.net复制的两个库文件,我想知道他们为什么有两个不同的扩展名.
我在Mac OSX Lion上编译一个非常简单的name.c文件时遇到了一些问题.
现在,我开始在cs50.net上关注哈佛CS50课程.我不是全新的编程,但我很好奇这门课程的教学方式.
这是name.c的来源:
#include <stdio.h>
#include <cs50.h>
int
main(void)
{
printf("State your name:\n");
string name = GetString();
printf("O hai, %s!\n", name);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,它需要这个库:https://manual.cs50.net/CS50_Library.
现在,当我编译它时,会发生这种情况:
Undefined symbols for architecture x86_64:
"_GetString", referenced from:
_main in name-vAxcar.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [name] Error 1
Run Code Online (Sandbox Code Playgroud)
如果我在源文件中使用相同的GetString()cs50.c函数,它可以完美地工作:
#include <stdio.h>
#include <string.h>
#include <float.h>
#include <limits.h>
#include <stdbool.h> …
Run Code Online (Sandbox Code Playgroud) import os
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
engine=create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))
def main():
flights = db.execute("SELECT origin, destination, duration FROM flights").fetchall()
for flight in flights:
print(f"{flight.origin} to {flight.destination}, {flight.duration} minutes.")
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
回溯(最近一次调用最后一次):文件“list.py”,第 6 行,在 engine=create_engine(os.getenv("DATABASE_URL")) 文件“C:\Users\Aakash\AppData\Local\Programs\Python\Python38 -32\lib\site-packages\sqlalchemy\engine__init__.py",第 479 行,在 create_engine 返回策略中。create(*args, **kwargs) 文件“C:\Users\Aakash\AppData\Local\Programs\Python\ Python38-32\lib\site-packages\sqlalchemy\engine\strategies.py”,第 56 行,在 create plugins = u._instantiate_plugins(kwargs) AttributeError: 'NoneType' 对象没有属性 '_instantiate_plugins'
如果将我的代码更改为:
我目前正在学习 C,通过 EDX/Harvard 参加 CS50 在线课程。他们让我们在基于云的 CS50 IDE/Sandbox 中使用 Clang。他们还没有解释如何在此之外使用 Clang。因此我想知道;如何在 Windows 10 中设置 clang ?以及与 VisualStudio Code 一起使用?
我正在使用哈佛的CS50设备并尝试将字符设为小写.我正在尝试使用该tolower()
功能,但当我尝试使用它时,我收到了消息implicit declaration of function 'tolower' is invalid in C99
.任何人都在关注为什么我会收到这条消息.我已经包括stdio.h
,以及string.h
.
作为哈佛大学CS50课程的一项任务,学生的任务是创建一个拼写检查程序.任务的主要目标是速度 - 纯粹的速度 - 我已经达到了我打败员工实施的程度,但我觉得我可以做得更好,并且正在寻找正确方向的推动力.
这是我的伪代码:
// read the dictionary word list
Read entire dictionary in one fread into memory
rawmemchr through and pick out the words
send each word through the hash function
create chain links for any index where collisions occur
// accept the incoming test words
Run the test word through the hash function
compare to the existing table / linked list
return the result of the comparison
Run Code Online (Sandbox Code Playgroud)
使用150K字的字典,输入文本高达6MB,我能够准确地拼写检查大约半秒钟.
但是,当我查看来自输入文本的单词时,很明显这些单词的大部分是常见的(如"the","and","for"),以及大多数拼写错误的单词也会多次检查.
我的直觉说我应该能够"缓存""好点击"和"糟糕点击",这样我就不会一遍又一遍地为表格查找扫描相同的单词.即使当前结果非常接近O(1),我觉得我应该能够通过重新评估我的方法来减少几微秒的时间.
例如,在我加载字典后,文本输入可能只有8MB,但是:"误导".因此,我不想反复哈希/检查相同的单词(以计算费用),我想了解是否有一种方法可以编程方式丢弃已经被散列和拒绝的单词,但是以一种比哈希/检查本身.(我正在使用MurmurHash3,fwiw).
我意识到理论性能改进将局限于输入文本很长的情况,并且存在大量重复拼写错误.基于我评估的一些输入文本,以下是一些结果:
Unique Misspellings: 6960
Total …
Run Code Online (Sandbox Code Playgroud) 我希望有一个人可以帮助我。我正在研究 CS50x 并正在研究 Pset1 - 贪婪。每当我编译代码时,我都会收到以下错误:
/tmp/greedy-46be96.o: In function `main':
greedy.c:(.text+0x95): undefined reference to `round'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激。如果问题含糊不清,我深表歉意,我已尽力深入探讨。我在终端中使用了man round ,并到处搜索,尝试不同的解决方案,但没有任何效果。
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
float owed;
float change;
float quarter = 0.25;
float dime = 0.10;
float nickel = 0.05;
float penny = 0.01;
do {
printf("How much change is owed?: ");
owed = GetFloat();
} while(owed <= 0);
change = round(owed …
Run Code Online (Sandbox Code Playgroud) 为什么会出现运行时错误?我做了range2
很长很长。
代码:
/*VARIABLES FOR WHILE LOOP*/
long long range1 = 9;
int length = 1;
/*FIND NUM'S LENGTH*/
while (NUM > range1)
{
long long range2 = range1 * 10 + 9;
length += 1;
}
Run Code Online (Sandbox Code Playgroud)
错误:
Credit.c:25:25:运行时错误:有符号整数溢出:999999999 * 10 无法以“int”类型表示
这是一些提供的(CS50)代码的片段,这些代码在嵌套循环中一遍又一遍地声明相同的STRUCT“三元组”。为什么这样可以呢?在我看来,将STRUCT“三元组”声明为嵌套循环的范围上方并在每次迭代中更改值会更有效。
typedef struct
{
BYTE rgbtBlue;
BYTE rgbtGreen;
BYTE rgbtRed;
} __attribute__((__packed__))
RGBTRIPLE;
Run Code Online (Sandbox Code Playgroud)
。
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
// iterate over pixels in scanline
for (int j = 0; j < bi.biWidth; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// write RGB triple to outfile
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
Run Code Online (Sandbox Code Playgroud) 我有一个相当奇怪的问题,实际上根本不是很实用。错误(以r
模式读取二进制文件)显而易见,但我对其他事情感到困惑。
这是代码-
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdint.h>
#define BUFFER_LEN 512
typedef uint8_t BYTE;
int main()
{
FILE* memcard = fopen("card.raw", "r");
BYTE buffer[BUFFER_LEN];
int count = 0;
while (fread(buffer, sizeof(*buffer), BUFFER_LEN, memcard) != 0)
{
printf("count: %d\n", count++);
}
fclose(memcard);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在,card.raw
是一个二进制文件,所以这个读取会出错,因为是以r
mode 而不是rb
. 但我很好奇的是,该循环正好执行了 3 次,在最终执行中,它甚至没有读取 512 个字节。
现在,如果我将该循环更改为
while (fread(buffer, sizeof(*buffer), BUFFER_LEN, memcard) != 0)
{
printf("ftell: %ld\n", ftell(memcard));
}
Run Code Online (Sandbox Code Playgroud)
它不再在执行 3 次时停止。事实上,它一直持续到(大概)文件结束。该fread
计数仍搞砸。许多读取不会在读取元素时返回 512。但这很可能是由于文件以r
模式打开以及它伴随的所有编码错误。 …