C++ C++;确定给定值是否为数字的If语句
我正在编写一个简单的程序,生成一些数据到文件中的报告。到目前为止的最终文件如下所示:C++ C++;确定给定值是否为数字的If语句,c++,if-statement,operator-overloading,C++,If Statement,Operator Overloading,我正在编写一个简单的程序,生成一些数据到文件中的报告。到目前为止的最终文件如下所示: Date: Mon Oct 09 16:33:09 2017 LENOVO-KOMPUTER +------+----------+-------+------+ | ID | name | temp. | poj. | +------+--
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*
}
每一行都是一个单独的结构,如下所示:
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*
}
底部的空白处,我必须总结上面的整个列。我需要使用重载的+=运算符对列求和(一个必须对所有列都有效),但第二个需要保持干净,因为那里只有文本(在使用+=运算符之后需要保持干净)
我如何确定我能把这些值加起来(因为它们是数字)还是不能加起来(因为它是文本)?我认为在开始时需要某种“if语句”,但我找不到任何有效的方法。我希望它看起来像这样: 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*
}
您正在寻找通用解决方案,但它不是一个好的解决方案,因为您知道哪些列有数字,哪些列没有数字 但是作为一个比较高级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;
}
#包括
#包括
#包括
#包括
#包括
模板
标准::可选的仅求和数字(常数标准::向量和值){
if constexpr(std::is_算术_v){
const auto sum=std::accumulate(values.begin()、values.end()、T{0});
返回std::to_字符串(总和);
}
返回std::nullopt;
}
int main(){
std::cout下面是重载+=和+运算符以添加行的代码:
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
}
struct数据{
int-id;
字符名[10];
双温;
双poj;
}
struct DataSum{//this class/struct包含两行相加的结果(这意味着此处省略了name和id)
双温;
双poj;
DataSum():temp(0),poj(0){}//默认构造函数
DataSum(constdata&Data):temp(Data.temp)、poj(Data.poj){}//将数据对象转换为DataSum对象的构造函数
DataSum运算符+=(const DataSum&data){//重载+=运算符
temp+=data.temp;
poj+=data.poj;
归还*这个;
}
};
DataSum运算符+(DataSum左、const DataSum和右){//重载+运算符
返回左+=右;
}
内部主(空){
struct Data table[100];//100个表行的数组(需要初始化)
struct DatSum result_row;//需要计算的表的最后一行
对于(inti=0;i<100;i++){//循环遍历所有行
result_row+=表[i];//每次向结果中添加一行,请注意,表[i]在添加到结果_row之前,将使用转换构造函数隐式转换为DataSum对象
}
//打印表格和结果行
}
我没有运行这段代码,所以可能有一些输入错误/错误…如果你正在生成报告,你怎么会不知道哪些元素是数字?你的数据是什么样子的?然后加上整数和双精度,但不是字符串?你是在问如何重载+=
操作符来添加结构的对象吗?mystruct运算符+=(mystruct const&other){/*这里是什么?*/}
?如果看不到您的代码和数据结构,就很难看到您所要求的内容。您可以发布到目前为止的内容(仅相关位)?“是算术吗?”这是一个足够的答案。非常感谢,这就是我所需要的。@这将为每个数据类型生成一个单独的函数。您需要的可能只是一个函数,它接受两个类型为struct Data
的对象,并将它们加在一起,就像@Galik所写的那样。@SisterFister似乎没有人真正理解我需要什么以及它是什么ust这个叫做“是算术”的小东西我知道如何写其余的东西,只是不知道这个名字的名字。我很高兴你发现我的答案很有用,但是就像我说的,它是不平凡的C++,特别是ISHI算术和其他ISI,来自Type的特性是在类型上而不是在值上操作,这是一种高级C++。所以如果你或者其他人读到T。他的答案是初学者,他们可能希望使用更简单的逻辑来实现此功能。就像我说的,检查变量值和变量类型之间有区别。例如,你可以问自己字符串是否是数字类型(int、long、float double)?不是。这是一个关于类型的问题。另一个例子是询问字符串是否超过10个字符?这是一个关于值的问题。请确保不要混淆这两个问题。我的解决方案询问有关类型的问题,而isalpha询问有关值的问题。一点类型安全从来没有坏处:@MooingDuck谢谢,如果是,我会将它添加到答案中你不介意。毕竟我主要是写C代码,所以我必须查找重载语法:D@MooingDuck添加了你的代码,希望我一切正常。。。