标签: cs50

C 从 int 中获取所有其他数字

我正在学习哈佛大学的 CS50 课程,我必须用 C 创建一个程序来查找信用卡号是否有效。例如,我想从这个数字 378282246310005 得到像这个 7224300 这样的每隔一个数字,所以我是这样开始的:

printf("Number\n");
long long cardNum = GetLongLong();
Run Code Online (Sandbox Code Playgroud)

我不知道我需要使用哪些方法来获取其他数字来进行计算。

c cs50

-2
推荐指数
1
解决办法
6587
查看次数

交换两个相邻元素的功能不起作用

当我尝试移动合法的图块(即与'空白'图块0相邻的图块)时,没有任何反应.如果磁贴是非法的,程序将按预期运行.这是移动功能:

bool move(int tile)
{
    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < d; j++)
        {
            if (board[i][j] == tile)
            {
                // stops program from going out of bounds
                if (j < d)
                {
                    if (board[i][j + 1] == 0)
                        {
                            swap(board[i][j], board[i][j + 1]);
                            return true;
                        }
                }

                if (j > 0)
                {
                    if (board[i][j - 1] == 0)
                        {
                            swap(board[i][j], board[i][j - 1]);
                            return true;
                        }
                }

                if (i …
Run Code Online (Sandbox Code Playgroud)

c arrays multidimensional-array cs50

-2
推荐指数
1
解决办法
61
查看次数

如何使这段代码形成的金字塔(CS50马里奥程序)右对齐?

请帮助我使用右对齐的哈希值和空格正确打印高度“n”的金字塔。我已在下面发布了代码本身。该程序正确地要求用户输入,但不会构建右对齐的金字塔。如果有人可以解决这个问题,请帮忙。

#include <stdio.h>
#include <cs50.h>

int main(void)
{
    int i=0;
    int n=0;

    do
    {
        printf("Height:");
        n=GetInt();
    } while (n<0 || n>23);

    for (i=0; i<n; i++)
    {
        printf(" ");
        for (int x=0; x<i+2; x++)
        {
            printf("#");
        }
        printf("\n");
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c stdio cs50

-3
推荐指数
1
解决办法
9万
查看次数

在C中循环.程序中的第一个循环不起作用并且是无限的

我不明白为什么第一个循环不起作用.即使填充的欠款浮动实际上大于0,它也是一个无限循环.为什么循环不起作用?

#import <cs50.h> 
#import <stdio.h>

int main(void)
{
    float owed = -1 ;

    while (owed < 0) 
    {
        printf("O hai! How much change is owed?\n") ;
        float owed = GetFloat() ;
        owed = owed * 100 ;
    }

    int coins = 0 ;

    while (owed >= 25)
    {
        owed = owed - 25 ;
        coins = coins + 1 ;
    }

    while (owed >= 10)
    {
        owed = owed - 10 ;
        coins = coins + 1 ;
    }

    while …
Run Code Online (Sandbox Code Playgroud)

c loops cs50

-3
推荐指数
1
解决办法
63
查看次数

错误:else if语句中的预期表达式

这是我得到的错误:

test.c:110:21: error: expected expression
                    else if(isupper(p_i))
                    ^
1 error generated.
Run Code Online (Sandbox Code Playgroud)

else if代码结尾的声明中else if(isupper(p_i))- " " - 生成错误.

我在上面评论了这个'else if'声明.请让我知道什么是错的.谢谢.

#include <stdlib.h>         // The library which contains the 'atoi()' function
#include <stdio.h>          //        
#include <cs50.h>           // typedef char *string; and GetString()
#include <string.h>         // 

// 'argv[]' is an array of strings.  (Fun fact: A string is an array of characters.) 
// 'argc' is the integer variable which stores the number of strings that are in 'argv[]'. …
Run Code Online (Sandbox Code Playgroud)

c caesar-cipher cs50

-4
推荐指数
1
解决办法
1万
查看次数

为什么我的程序打印0而不是计算的正弦值?

#include <cs50.h>
#include <stdio.h>

float sine(float a, float b);

int main()
{
    printf ("Choose an option:\n1- Sin\n2-Cos\n3-Tan\n");
    int option = get_int();
    if (option==1)
    {
        printf("What is the hypotnuse of the triangle?\n");
        float hypotnuse = get_float();
        printf("What is the opposite side of the triangle?\n");
        float opposite = get_float();
        sine(opposite, hypotnuse);
        printf ("The answer is %f", sine);

    }   
    // else … cosine, tangent
}

float sine(float a, float b)
{
  return a/b;
}
Run Code Online (Sandbox Code Playgroud)

无论价值我提供了hypotnuseopposite,输出我得到的是0.000000.请解释代码有什么问题?

c cs50

-4
推荐指数
1
解决办法
79
查看次数

在C编程中寻找模数或%的直接相反

构建了一个使用Caesar Cipher加密事物的C程序.它将它转换为字母数字(0 = a/1 = b/2 = c),然后在移动了使用模运算符指定的字符数后转换它.我目前正在尝试构建一个解密它的程序,我需要与模运算符完全相反的方法来反转序列.

c caesar-cipher cs50

-4
推荐指数
1
解决办法
212
查看次数

什么是有符号整数溢出?

我正在从 CS50 学习 C。当我运行我的代码时,它说“有符号整数溢出”。

#include <stdio.h>
#include <cs50.h>

int main(void)
{
    int x = 41;
    int c = 0;
    while(x>=25)
    {
      c = c+1;
    }

    printf("%i\n", c);
}
Run Code Online (Sandbox Code Playgroud)

有人能解释一下这是什么意思吗?

c cs50

-4
推荐指数
1
解决办法
2万
查看次数

继续得到这个编译错误

当我编译时,我不断收到此错误.

mario.c:4:1: error: expected identifier or '('
Run Code Online (Sandbox Code Playgroud)

我已经尝试过更换东西然后修改它们并改变其他东西然后修复它们但似乎没有任何帮助.我是新来的.有人可以帮忙吗?

#include <stdio.h>
#include <cs50.h>
int main(void);
{
int n;

    do 
   { 
    n = GetInt();
   }
    while (n=<0);
}
Run Code Online (Sandbox Code Playgroud)

c cs50

-5
推荐指数
1
解决办法
69
查看次数

使用未声明的标识符'o'

#include <stdio.h>
#include <cs50.h>

int main(void) 
{
    int height;
    {
        printf("Please select a height value between 1-23.");
        height = GetInt();

        while (height < 1 || height > 23)
        {
            printf("Height mustbe between 1-23, please choose new value.\n");
            height = GetInt();
        }
        {
            for (int n = 0; n < height; n++)

            for (int o = 0; o + n + 1 < height; o++)
            {
                printf(" ");
            }            
            for (int p = 0; p <= o; p++)
            {
                printf("#");
            } …
Run Code Online (Sandbox Code Playgroud)

c cs50

-6
推荐指数
1
解决办法
762
查看次数

'undeclared identifier'错误,即使变量是在代码中先前声明的

inth当用户输入值时,在第10行声明变量.

但是,当代码编译时,它表示它是未声明的.

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    printf("How tall would you like your pyramid?\n");
    bool inc = true;
    while (inc)
    {
        int h = GetInt();
        if (h <= 23 && h >= 1)
        {
            inc = false;
        }
        else
        {
            printf("your value needs to be between 1 and 26\n");
        }
    }

    for (int i=0; i<=h; i++)
    {
        printf("#\n");
    }
}
Run Code Online (Sandbox Code Playgroud)

c cs50

-6
推荐指数
1
解决办法
418
查看次数

如何使用for循环设置数组元素的值?

我有一个数组,它包含 5 个没有值的元素,但我想通过使用 for 循环为每个元素给出一个值。

我想给第一个元素值 1 并在第二个循环中将该值加倍。 在此输入图像描述

int size[5];

for (int i = 0; i < 5; i++){

size[i] = 1;

// how to double this value in second loop
}

Run Code Online (Sandbox Code Playgroud)

c arrays cs50

-6
推荐指数
1
解决办法
233
查看次数

标签 统计

c ×12

cs50 ×12

arrays ×2

caesar-cipher ×2

loops ×1

multidimensional-array ×1

stdio ×1