Tag: cs50

Vigenere Cipher只能处理C中的空格(“”) – 为什么?

#include #include #include #include #include int main(int argc, string argv[]) { string k = argv[1]; string s = GetString(); int l = strlen(k); for(int i = 0, n = strlen(s); i = 65 && s[i] = 97 && s[i] <= 122) { int i2 = ((s[i]-97) + (k[i%l]-97)) % 26; printf("%c", i2+97); } else { […]

C中的凯撒密码适用于较低的上部

密码适用于较低部分但不适用于上部分。 例如,如果我给一个3键,然后输入I like pie!! 要加密,我得到O olnh slh!! 我也尝试了HELLO并获得了NKRRU 。 isupper部分也返回标点而不仅仅是字母。 我还没弄清楚为什么原始消息被改变以匹配密码消息。 #include #include #include #include #include int main (int argc, string argv[]) { /* Get key from user at command line Get plaintext from user Use key to encipher text: c[i] = (p[i] + k)%26 Print ciphered message */ string message, cipher; int key; // […]

区分单词中的单词

我的trie中有一个单词“all”,单词“alter”但是“alt”不是trie中的单词。 但是当我检查“alt”时它仍然返回true,因为is_word为真,因为“all”是一个单词。 应该如何处理这个错误。 //Here’s the code typedef struct node{ bool is_word; struct node *children[27]; } node; unsigned int wsize=0; node * root; bool check(const char* word) { // TODO node *chrawler=root; for(int i=0;i=65&&word[i]children[t]==NULL) return false; else chrawler=chrawler->children[t]; } if(chrawler->is_word) return true; return false; } // Load function bool load(const char* dictionary) { // TODO FILE […]

哈佛CS50库,需要帮助在Mac OS X上安装

我正在从cs50.tv学习这门课程,这是哈佛扩展学校,在课程中他们使用的是他们制作的名为cs50的图书馆,在这个链接上可以下载 https://manual.cs50.net/CS50_Library#Mac_OS 我下载了zip文件并将其解压缩,然后打开终端并以cd的方式进入库目录,但每次我都按照手册中的步骤操作。 我执行此命令后立即gcc -c -ggdb -std = c99 cs50.c -o cs50.o 我在终端中收到此错误 cs50.c:15:16: error: gc.h: No such file or directory cs50.c: In function ‘GetString’: cs50.c:207: warning: implicit declaration of function ‘GC_FREE’ cs50.c:212: warning: implicit declaration of function ‘GC_REALLOC’ cs50.c:212: warning: initialization makes pointer from integer without a cast cs50.c:230: warning: implicit declaration of function […]

未定义的引用`__ubsan_handle_nonnull_arg’

我一直在研究问题集拼写器的最后几天,到目前为止这就是我所拥有的。 不幸的是,它没有编译,我有点迷失。 如果有人可以帮助我告诉我,我做错了什么,我将非常感激。 // Implements a dictionary’s functionality #include #include #include #include #include #include “dictionary.h” #define HASHTABLE_SIZE 65536 // A struct for a node typedef struct node { // Length is up to 45 + 1 for the Null char word[LENGTH + 1]; // A pointer to the next node struct node *next; } node; […]

整数溢出贪婪的硬币计数

#include #include #include int main (void) { printf (“Enter amount: “); float amount = GetFloat(); int coins = 0; while (amount != 0) { if (fmod(amount, 0.25) == 0) { amount = amount – 0.25; coins += 1; } else if (fmod(amount, 0.10) == 0) { amount = amount – 0.10; coins += 1; } […]

开始C程序

我正在使用哈佛大学的在线CS50课程开始学习一些问题。 我得到了正常工作的问题,但我想知道是否有更清洁或更好的方法让程序工作。 该程序的目标是打印由哈希标记和空格字符组成的右对齐金字塔。 任何关于风格或技巧的指导都将非常受欢迎。 /* Creating the mario program, whose goal is to create a * pyramid by accepting input from the user to get the * height then aligning the pyrimid to the right. * */ #include #include int main(void) { // get user input and set to variable printf(“Height: “); int height = […]

为什么我的程序适用于某些测试而不适用于其他测试?

这是我测试的输出: 🙂 greedy exists 🙂 greedy compiles 🙁 input of 0.41 yields output of 4 expected “4\n”, not “3\n” 🙁 input of 0.01 yields output of 1 expected “1\n”, not “0\n” 🙂 input of 0.15 yields output of 2 🙂 input of 1.6 yields output of 7 🙂 input of 23 yields output of 92 […]

当strlen产生分段错误时,来自GetString()的C字符串

我在C中运行程序。当我运行程序时,我得到了分段错误错误。 在我回溯时它在gdb中告诉我 程序接收信号SIGSEGV,分段故障。 __strlen_sse2_bsf()at ../sysdeps/i386/i686/multiarch/strlen-sse2-bsf.S:51 51 movdqu(%edi),%xmm1 我相信它与strlen有关。 我使用strlen的唯一一次是: string s = GetString(); int stringlength = strlen(s); 当我更改strlen for sizeof错误停止时。 我的代码出了什么问题? GetString的文档 /* * Reads a line of text from standard input and returns it as a * string (char *), sans trailing newline character. (Ergo, if * user inputs only “\n”, returns “” not […]

学习C,需要一些“Greedy”CS50解决方案的帮助

我是C的新手。我来自python背景。 我想知道我的代码出错了。 我正在做cs50贪心的问题。 我的代码出了什么问题? 它适用于某些数字,但其他数字不起作用。 我试图从用户那里得到一个输入,询问要回馈多少更改,然后使用$ .25,$ .10,$ .05,$ .01计算我可以回馈的最小硬币数量。 #include #include int main(void) { float n; do { n = get_float(“How much change is owed?\n”); } while(n == EOF); int minimumamountofcoins = 0; if (n/.25 >=1){ do { n -= .25; minimumamountofcoins++; } while (n/.25 >= 1); } if (n/.1 >=1){ do { n […]