C ++ If语句判断给定值是否为数字

时间:2017-10-09 15:16:32

标签: c++ if-statement operator-overloading

我正在编写一个简单的程序,它可以生成一些数据报告到文件中。到目前为止,最终文件如下所示:

    Date: Mon Oct 09 16:33:09 2017

LENOVO-KOMPUTER                                                                           
+------+----------+-------+------+
|  ID  |   name   | temp. | poj. |
+------+----------+-------+------+
|000000|hejka     |    1.5|     0|
|000001|naklejka  |   31.8|     1|
|000002|dupa      |    0.0|     2|
|000003|proba     |    0.0|     3|
|000004|cos       |    0.0|     4|
|000005|nic       |    0.0|     5|
|000006|polewamy  |    0.0|     6|
|000007|sie       |    0.0|     7|
|000008|szampanem |    0.0|     8|
|000009|ojojoj    |    0.0|     9|
+------+----------+-------+------+
|      |          |       |      |
+------+----------+-------+------+

每一行都是一个单独的结构,如下所示:

struct Data{
    int id;
    char name[10];
    double temp;
    double poj;

在底部的空白处,我必须总结上面的整个列。我需要使用重载的+ =运算符来对一列进行求和(一个必须适用于所有这些),但第二个需要保持干净,因为那里只有文本(它需要在使用该+ =运算符后保持清晰事情就好了。)

如何确定我是否可以总结这些值(因为它们是数字)或者我不能(导致它的文本)?我认为它需要某种“如果声明“在开始但我找不到任何有用的东西。我希望它看起来像这样:

if(the given value is a number){
*add up all the numbers and put the result in a right spot*
}
else{
*it's not a number so i can't do anything, leave the spot (where the result should be) blank*
}

2 个答案:

答案 0 :(得分:1)

您正在寻找一般解决方案,因为您知道哪些列有数字,哪些没有数字。

但是作为相对高级C ++的练习,可以这样做:

    #include <type_traits>
    #include <numeric>
    #include <vector>
    #include <optional>
    #include <iostream>

    template <typename T>
    std::optional<std::string> sum_only_numbers(const std::vector<T>& values) {
        if constexpr(std::is_arithmetic_v<T>){
            const auto sum = std::accumulate(values.begin(), values.end(), T{0});
            return std::to_string(sum);  
        }
        return std::nullopt;
    }
    int main() {
        std::cout << *sum_only_numbers(std::vector<float>{1.1,2.2}) << std::endl;
        std::cout << *sum_only_numbers(std::vector<int>{11,22}) << std::endl;
        std::cout << std::boolalpha << sum_only_numbers(std::vector<std::string>{"will", "be", "ignored"}).has_value() 
                  << std::endl;    
    }

答案 1 :(得分:1)

这里是重载+ =和+运算符以添加行的代码:

struct Data {
    int id;
    char name[10];
    double temp;
    double poj;
}

struct DataSum { // this class/struct contains the result of an addition of two rows (this means that name and id are omitted here)
    double temp;
    double poj;

    DataSum() : temp(0), poj(0) {} // default constructor

    DataSum (const Data& data) : temp(data.temp), poj(data.poj) {} // constructor which converts a Data object to a DataSum object

    DataSum operator+= (const DataSum& data) { // overloaded += operator
        temp += data.temp;
        poj += data.poj;
        return *this;
    }
};

DataSum operator+ (DataSum left, const DataSum& right) { // overloaded + operator
    return left += right;
}

int main (void) {
    struct Data table[100]; // array of 100 table rows (needs to be initialized)
    struct DatSum result_row; // final row of the table which needs to be calculated

    for (int i = 0; i < 100; i++) { // loop through all the rows
        result_row += table[i]; // add one row at a time to the result, note that table[i] will be implicitly converted to a DataSum object with the converting constructor before it will be added to result_row
    }

    // print the table and the result_row
}

我没有运行此代码,因此可能存在一些拼写错误/错误...