相关疑难解决方法(0)

GCC划分截断(舍入问题)

在Ubuntu Linux 10.04上使用GCC,我在分割后进行了不必要的舍入.

我试过了:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void FormatReading(int temp)
{
    double reading = temp / 100;
    printf("%f\n",reading);  /* displays 226.000000, was expecting 226.60 */
}

int main(void)
{
    FormatReading(22660);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

有人建议我试试:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void FormatReading(int temp)
{
    long reading = temp ;
    reading = reading / 100;
    printf("%3.2ld\n",reading);  /* displays 226 */
}

int main(void)
{
    FormatReading(22660);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void FormatReading(int temp) …
Run Code Online (Sandbox Code Playgroud)

c floating-point gcc

0
推荐指数
1
解决办法
884
查看次数

为什么7%-5给出2但-7%5给-2?这两种情况都不应该是-2吗?

请解释以下原因,数学上正确的答案是-2两种情况:

int a=7%-5; //Assigns 2 to a
int a=-7%5;  //Assigns -2 to a
Run Code Online (Sandbox Code Playgroud)

代码在C中.

c modulo

0
推荐指数
1
解决办法
543
查看次数

C中简单赋值语句中的错误

在下面的简单代码中,当我指定dearness=(40*basic)/100 or rent=(20*basic)/100它时工作正常,但是按照以下方式进行,亲属和租金的赋值语句没有任何效果

请暗示可能的原因.

#include "stdio.h"
#include "conio.h"

int main()
{
    int salary=0,basic=0,dearness=0,rent=0;
    clrscr();
    printf("enter basic salary");
    scanf("%d",&basic);
    dearness=(40/100)*basic; //no effect
    rent=(20/100)*basic;  //no effect
    salary=basic+dearness+rent;
    printf("%d %d",dearness,rent);
    printf("\n%d",salary);
    getch();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

谢谢.

c

0
推荐指数
1
解决办法
163
查看次数

在C中,是否保证1/2 == 0?

它在C中得到保证1/2 == 0吗?我需要它来实现二进制搜索:

/*
 * len is the array length
 * ary is an array of ptrs
 * f is a compare function
 * found is a ptr to the found element in the array
 * both i and offset are unsigned integers, used as indexes
 */

for(i = len/2; !found && i < len; i += offset) {
    res = f(c->ary[i]);

    if (res == 0) {
        found = c->ary[i];
    }
    else {
        offset = (res < 0 …
Run Code Online (Sandbox Code Playgroud)

c arrays math integer binary-search

0
推荐指数
1
解决办法
517
查看次数

c编程 - 无法获得所需的输出

char choice, y, n;
float percentage;
int marks;
printf("Do you want to know your percentage?(y/n):\n\n");
scanf(" %c", &choice);
if (choice == 'y') {
    printf("Enter Your 2nd Sem Marks:\n\n");
    scanf(" %d", &marks);
    percentage = (marks / 775) * 100;
    printf("Your 2nd Sem Percentage is %.2f", percentage);
} else
    printf("Thankyou!!!!\n\n\n");
Run Code Online (Sandbox Code Playgroud)

我无法使用上面的代码计算百分比 - 我输出0.00 ...

例如,当我输入为536而不是显示结果为69.16时,它显示0.00请帮助我

c

0
推荐指数
1
解决办法
92
查看次数

C++ 计算两个数之比

大家好,我对编程还很陌生,正在学习 Stroustrup 的“使用 C++ 进行编程、原理和实践”,在第 3 章末尾我完全陷入停滞状态,并进行了一项要求您编写一段代码的练习它会进行一些涉及 2 个数字的计算,其中包括计算数字的比率。不幸的是,这本书根本没有涵盖这一点,我正在费尽心思地试图自己弄清楚,只能找到代码示例来为我的小脑袋进阶。

我现在的代码是:

 double ratio;
    if (val2 > val1)
        ratio = (val2 / val1);
    if (val2 < val1)
        ratio = (val1 / val2);
    cout << "The ratio of " << val1 << " and " << val2 << " is 1:" << ratio << '\n';
Run Code Online (Sandbox Code Playgroud)

这对于等于整数比率(例如 100 和 25)的数字效果很好,但是尽管我将变量“比率”设置为双精度,但在非整数比率的情况下,它会从答案中删除任何小数。谁能告诉我哪里错了?

c++

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

预期结果在1和0之间的除法总是给出0

可能重复:
C中整数除法的行为是什么?

当我使用应该在1和0之间的答案进行除法计算时,它总是返回0.

试试这个:

float a = 1;
float b = 2;
float c = a / b; //variable divide by variable gives 0.5 as it should
float d = 1 / 2; //number divide by number gives 0
float e = 4 / 2;
NSLog(@"%f %f %f %f %f", a,b,c, d, e);
Run Code Online (Sandbox Code Playgroud)

我从控制台得到这个:

2013-01-17 14:24:17.512 MyProject [1798:c07] 1.000000 2.000000 0.500000 0.000000 0.500000

我不知道发生了什么事.

math integer objective-c division

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

C++舍入错误

可能重复:
C中整数除法的行为是什么?

为什么这行代码没有返回任何内容?

float i = 10/3;
if (i>3) cout << i;
Run Code Online (Sandbox Code Playgroud)

如果有帮助,请使用CodeBlocks编译器.

c++ math rounding

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

什么导致我的c ++代码中的错误?

我尝试编写一个代码,它应该计算4 - 4/3 + 4/5 - 4/7 + 4/9 + ...但它仍然打印"3"作为答案.

#include <iostream>
#include <math.h>
#include <conio.h>

using namespace std;

int main()
{
    int s=0,a,n;
    cin>>n;
    for(int i=0 ; i<=n ; i++)
    {
        a=(4/((2*i)+1))*pow(-1,i);
        s=s+a;
    }
    cout<<s;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++

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

用于计算两个整数的平均值的函数

我写了这个:

    public static decimal Average(int a, int b)
    {
        return (a + b) / 2;
    }

    public static void Main(string[] args)
    {
        Console.WriteLine(Average(2, 1));
        Console.ReadKey();
    }
Run Code Online (Sandbox Code Playgroud)

但它回来了1.但它应该返回1.5 如何修复它返回1.5?

c#

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

标签 统计

c ×5

c++ ×3

math ×3

integer ×2

arrays ×1

binary-search ×1

c# ×1

division ×1

floating-point ×1

gcc ×1

modulo ×1

objective-c ×1

rounding ×1