【问题标题】:How to display whole numbers without decimals如何显示不带小数的整数
【发布时间】:2020-07-14 03:47:14
【问题描述】:

我创建了一个程序来显示用户决定输入的一组数字的平均值。该程序询问用户他/她将输入的数字数量,然后他们输入所有正数。平均值的输出始终是小数,我怎样才能只显示整数而没有任何小数点。前任。 12.34 = 12 / 8.98 = 8

#include <iostream>
#include <iomanip>
using namespace std;

void sortingTheScores(double *, int); 
void showsTheScoresNumber(double *, int);  
double averageForAllScores(double, int);  

int main()
{
    double *scores;                        
    double total = 0.0;                         
    double average;                            
    int numberOfTestScores;                      
                                       
    cout << "How many test scores do you have? ";
    cin >> numberOfTestScores;

    
    scores = new double[numberOfTestScores];
    if (scores == NULL)
        return 0;

    
    
    for (int count = 0; count < numberOfTestScores; )  
    {
        cout << "Test Score #" << (count + 1) << ": ";
        cin >> scores[count];
        while (scores[count] <= 0)
        {
            cout << "Value must be one or greater: " ;  
               
            cin >> scores[count];
        }
        
        count = count +1; 
    }

    


    for (int count = 0; count < numberOfTestScores; count++)  
    {
        total += scores[count]; 
        
        
    }
    
    
    

    
    sortingTheScores(scores, numberOfTestScores);

    cout << "The numbers in set are: \n";
    showsTheScoresNumber(scores, numberOfTestScores);
    
    
    averageForAllScores(total, numberOfTestScores);
    
    cout << fixed << showpoint << setprecision(2);
    cout << "Average Score: " << averageForAllScores(total,numberOfTestScores);


    return 0;

}



void sortingTheScores (double *array, int size)
{
    int sorting;
    int theIndex;
    double  theNumbers;
    
    
    for (sorting = 0; sorting < (size - 1); sorting++)  
    {
        theIndex = sorting;
        theNumbers = array[sorting];
        for (int index = sorting + 1; index < size; index++) 
        {
            if (array[index]  < theNumbers)
            {
                theNumbers = array[index];
                theIndex = index;
            }
        }
        array[theIndex] = array[sorting];
        array[sorting] = theNumbers;
    }
}



void showsTheScoresNumber (double *array, int size)
{
    for (int count = 0; count < size; count++) 
        cout << array[count] << " ";
    cout << endl;
    
    
}






double averageForAllScores(double total, int numberOfTestScores)
{   double average;
    
    average = total / numberOfTestScores;
    return average;

    
    
}  

【问题讨论】:

  • 你不能将其转换为 int 或将另一个变量设为 int 并为其分配双精度值
  • @henhen,我实际上会很长时间。 :)
  • @d4rk4ng31 我认为 op 使用的是有符号整数,但是是的,应该使用适当的数据类型

标签: c++ arrays visual-c++ c++17


【解决方案1】:

您可以在这里使用I/O manipulators

#include <iostream>
#include <iomanip>

int main()
{
    std::cout << std::setprecision(0) << 1.231321 << '\n';
}

输出:

1

【讨论】:

    【解决方案2】:

    你可以不使用iomanip库来做到这一点:

    std::cout.precision(0);
    std::cout << 1.231321 << std::endl;
    

    然后你会得到:

    1
    

    你只需要使用std::cout.precision(),它相当于iomanip库中的std::setprecision()

    编辑:

    上述解决方案适用于较小的浮点值,但如果您尝试类似1334.231321 的方法,std::cout 将导致显示一些科学记数法,例如:

    1e+03
    

    这实际上是奇怪的阅读和理解。要解决它,你需要std::fixed 标志,你可以这样写:

    std::cout.precision(0), std::cout << std::fixed;
    std::cout << 1334.231321 << std::endl;
    

    然后它会显示:

    1334
    

    【讨论】:

    • 嘿。很好:) 我不知道有人能做到这一点!紫外线即将出现:) 但是,如果他不想明确地进一步降低精度,他是否不必将精度改回原来的?如何做到这一点?
    【解决方案3】:

    对于 +/-2^31 范围内的数字,您可以这样做:

    cout << int(12.34) << " " << int(8.98) << endl;
    

    产生输出

    12 8
    

    您可能还需要考虑舍入到最接近的整数。这样做 添加一行

    #include <cmath>
    

    然后做

    cout << int(rint(12.34)) << " " << int(rint(8.98)) << endl;
    

    这给了

    12 9
    

    【讨论】:

    • 他使用的整数类型是(有符号的)int,最多只能达到+/- 2^31
    猜你喜欢
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-06
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    相关资源
    最近更新 更多