Solve the task using C++, Qt, and QXlsx:
Given an Excel table and a cell number. Check that the cell value can be correctly converted to one of the following numbers: 0, 1, 2, 3, 10, 11, 14, 42, 100, 14.3 (and only those). Details:
The numbers can be written in hex, dec, oct or binary format. Numbers can be written with or without prefixes. For instance:
number "8" can be written as "8", "010" , "10" etc.
number "10" can be written as "10", "012", "12" , "1010", "0b1010" etc.
Numbers can be written in float format (for instance, "1" can be written as "1.0"). If number is float, it's always written in decimal format.
Use "switch".
Assistant
Here's a sample solution using C++, Qt, and QXlsx:
cpp
#include <QXlsx>
#include <iostream>
#include <string>
#include <stdexcept>
// Function to convert string to double, considering different number formats
double convertToDouble(const QString& str) {
// Remove any leading or trailing whitespace
QString trimmedStr = str.trimmed();
// Check for prefixes and convert accordingly
if (trimmedStr.startsWith("0b") || trimmedStr.startsWith("0B")) {
// Binary
return QString::number(trimmedStr.mid(2).toInt(0, 2)).toDouble();
} else if (trimmedStr.startsWith("0x") || trimmedStr.startsWith("0X")) {
// Hexadecimal
return QString::number(trimmedStr.mid(2).toInt(0, 16)).toDouble();
} else if (trimmedStr.startsWith("0")) {
// Octal
return QString::number(trimmedStr.toInt(0, 8)).toDouble();
} else {
// Decimal
return trimmedStr.toDouble();
}
}
// Function to check if a cell value is valid
bool isValidCellValue(const QString& str) {
double value;
try {
value = convertToDouble(str);
} catch (const std::exception& e) {
// If conversion fails, return false
return false;
}
// Use switch to check for valid values
int intValue = static_cast<int>(value);
switch (intValue) {
case 0:
case 1:
case 2:
case 3:
case 10:
case 11:
case 14:
case 42:
case 100:
// If value is integer, check for exact match
return value == intValue;
default:
// If value is not integer, check for float value 14.3
return qFuzzyCompare(value, 14.3);
}
}
int main() {
// Load Excel file using QXlsx
QXlsx::Document xlsx("example.xlsx");
// Get cell value
QString cellValue = xlsx.read("A1");
// Check if cell value is valid
if (isValidCellValue(cellValue)) {
std::cout << "Cell value is valid." << std::endl;
} else {
std::cout << "Cell value is not valid." << std::endl;
}
return 0;
}
This solution defines two functions: convertToDouble and isValidCellValue. The convertToDouble function converts a string to a double, considering different number formats (binary, hexadecimal, octal, and decimal). The isValidCellValue function checks if a cell value is valid by converting it to a double and then using a switch statement to check for exact matches with the allowed integer values. If the value is not an integer, it checks for a fuzzy match with the float value 14.3. The main function demonstrates how to use these functions to check the value of a cell in an Excel file loaded using QXlsx.