I am creating two tables with the following queries. Based on the region, the number of columns might be different and some columns might not be used for some regions.
String currentTableQuery = "CREATE TABLE \"" + currentTable + "\" \n" +
"AS \n" +
"SELECT " + columns + "\n" +
"FROM \"" + region + "\" \n" +
"WHERE partition_0=\'" + date + "\' AND partition_1=\'" + table + "\';";
Columns is an array:
String[] columns = new String[]{"col0", "col1", "col2", "col3", "col4", ...
Previous table:
String previousTableQuery = "CREATE TABLE \"" + previousTable + "\" \n" +
"AS \n" +
"SELECT " + columns + "\n" +
"FROM \"" + region + "\" \n" +
"WHERE partition_0=\'" + previousDate + "\' AND partition_1=\'" + table + "\';";
And after this, I am creating a delta table like below:
String deltaProcessingQuery =
"CREATE TABLE " + deltaTable + "\n" +
"WITH (\n" +
"format = 'TEXTFILE', \n" +
"field_delimiter = '\\t', \n" +
"write_compression = 'NONE', \n" +
"external_location = \'" + uploadLocation + "\' \n" +
")" + "\n" +
"AS" + "\n" +
"SELECT * FROM \"" + currentTable + "\" \n" +
"EXCEPT" + "\n" +
"SELECT * FROM \"" + previousTable + "\" ;";
But in the final delta table, columns which do not have data (or are extra columns) are backfilled with garbage data: \N. I am not sure where this data (\N) is coming from. How can I change that to be empty data?
CLICK HERE TO SEE THE IMAGE PROBLEM
C++ - I'm having a big problem with my code and I don't understand where I did the mistake because I'm not getting the result that I want for example the 1st output that problem is asking me :(
My FULL CODE
Outputs that I'm getting wrong
#include <iostream>
#include <string>
using namespace std;
int main() {
string firstName, middleName, lastName, theName;
getline(cin, theName);
int findN = theName.find(" ");
firstName = theName.substr(0, findN);
int findN2 = theName.find(" ", findN + 1);
if (findN2 != string::npos){
middleName = theName.substr(findN2 + 1, findN2 - findN - 1);
lastName = theName.substr(findN2 + 1, theName.length() - findN2 - 1);
cout << lastName << ", " << firstName[0] << "." << middleName[0] << "." << endl;
}
else {
lastName = theName.substr(findN + 1, theName.length() - findN - 1);
cout << lastName << ", " << firstName[0] << " . " << endl;
}
return 0;
}
I suggest learning to use debugger.
#include <iostream>
#include <string>
using namespace std;
int main() {
string firstName, middleName, lastName, theName;
getline(cin, theName);
int findN = theName.find(" ");
firstName = theName.substr(0, findN);
int findN2 = theName.find(" ", findN + 1);
if (findN2 != string::npos) {
//changed from findN2 + 1 to findN + 1
middleName = theName.substr(findN + 1, findN2 - findN - 1);
lastName = theName.substr(findN2 + 1, theName.length() - findN2 - 1);
cout << lastName << ", " << firstName[0] << "." << middleName[0] << "." << endl;
}
else {
lastName = theName.substr(findN + 1, theName.length() - findN - 1);
//fixed the white space " . " -> ". "
cout << lastName << ", " << firstName[0] << ". " << endl;
}
return 0;
}
This is easily solved with debugger.
If there is a second space, both of your middleName and middleName are assigned substring starting from findN2 + 1.
If there is no second space, you have extra spaces around . here: " . "
I have to build a qml line serie chart. I must build the series to display in C++ because I've got many data to insert. I called my serie QLineSeries * TaProbe and I need to pass it to QML. How can I do it?
Below my QML code, where I want to load the TaProbe line serie
ChartView {
id: chart
anchors.fill: parent
ValueAxis{
id: xAxis
min: 1.0
max: 10.0
},
ValueAxis{
id: yAxis
min: 0.0
max: 10.0
}
}
Where I can load my C++ lineserie in the code above?
Can someone help me?
Many thanks in advance.
So what you want is basically this:
ChartView {
title: "Line"
anchors.fill: parent
antialiasing: true
LineSeries {
name: "LineSeries"
XYPoint { x: 0; y: 0 }
XYPoint { x: 1.1; y: 2.1 }
XYPoint { x: 1.9; y: 3.3 }
XYPoint { x: 2.1; y: 2.1 }
XYPoint { x: 2.9; y: 4.9 }
XYPoint { x: 3.4; y: 3.0 }
XYPoint { x: 4.1; y: 3.3 }
}
}
but the XYPoint are from your Cpp so you need to create this from your C++?
I did a similar thing with the Shape class of QML.
In the C++:
ShapeEditor::ShapeEditor() {
_shapeInit = ""
"import QtQuick 2.5; \n"
"import QtQuick.Shapes 1.11; \n"
"Shape { \n"
" property string name: \"\" \n"
" id: prlShape; \n"
" opacity: 0.8; \n"
" containsMode: Shape.FillContains; \n"
" function changeColor(newColor) { \n"
" shapePrlPath.fillColor = newColor; \n"
" shapePrlPath.strokeColor = newColor; \n"
" } \n"
" function changeOpacity(opac) { \n"
" prlShape.opacity = opac; \n"
" } \n"
" function destroyArea() { \n"
" destroy(); \n"
" } \n"
" ShapePath { \n"
" joinStyle: ShapePath.RoundJoin; \n"
" strokeColor: \"yellow\"; \n"
" fillColor: \"yellow\"; \n"
" id: shapePrlPath; \n"
" ";
_shapeEnd = " }}";
}
void ShapeEditor::createShapeItemFromPoint()
{
QVector<QPoint> reorderedList = _pointList;
_shapeForm = " startX:" + QVariant(reorderedList[0].x()).toString() + " ; startY: " + QVariant(reorderedList[0].y()).toString() + " \n";
for (int i = 1 ; i < reorderedList.size() ; i++) {
_shapeForm += " PathLine { id: line" + QVariant(i).toString() + "; x: " + QVariant(reorderedList[i].x()).toString() + " ; y: " + QVariant(reorderedList[i].y()).toString() + "} \n";
}
_shapeForm += " PathLine { id: line" + QVariant(reorderedList.size() + 1).toString() + "; x: " + QVariant(reorderedList[0].x()).toString() + " ; y: " + QVariant(reorderedList[0].y()).toString() + "} \n";
QObject* obj = _frontRoot->findChild<QObject*>("shapeEditor");
QMetaObject::invokeMethod(obj, "createShape", Q_ARG(QVariant, QVariant::fromValue(_shapeInit + _shapeForm + _shapeEnd)))
}
qml:
Item {
id: shapeEditor
objectName: "shapeEditor"
function createShape(str) {
var item = Qt.createQmlObject(str, prlEditor, "shape");
}
}
What you could do is to create your QML dynamic object dynamically. So create a QString in the C++ then send it to a javascript function via the invokeMethod.
You can follow this tutorial: Dynamic QML Object Creation from JavaScript.
I got a task from my lecture to make association program from my data mining class, and i'm using c++ in microsoft visual studio 2017 since that is the only language i understand.
I'm trying to get support result but all i got is 0. i use an algorithm i got from some sites, but i can't implement it to my code because the value is 0.
I think the problem is in the input data reading, the one with for(int i=0;i<n;i++).
this is my code :
#include<iostream>
#include<string>
using namespace std;
int main()
{
float n = 5, support1 = 0, support2 = 0, support3 = 0;
string item1, item2;
//dataset fixed
string tra1[5] = { "milk", "beer" , "coffee" , "sugar" , "detergen" };
string tra2[5] = { "egg", "flour" , "milk" , "sugar" };
string tra3[5] = { "coffee", "butter" , "cigarette" , "sugar" };
string tra4[5] = { "doritos", "tea" , "coconut oil" , "soap" };
string tra5[5] = { "detergen", "milk" , "sugar" , "coca cola" };
cout << "item 1 : "; cin >> item1;//for example coffee
cout << "item 2 : "; cin >> item2;//for example sugar
cout << endl << "------------------------------" << endl;
//i think this is where the problem is
for (int i = 0;i < n;i++)
{
//tra1
if (item1 == tra1[5]) { support1 + 1; }
if (item2 == tra1[5]) { support2 + 1; }
if (item1 == tra1[5] && item2 == tra1[5]) { support3 + 1; }
//tra2
if (item1 == tra2[5]) { support1 + 1; }
if (item2 == tra2[5]) { support2 + 1; }
if (item1 == tra2[5] && item2 == tra2[5]) { support3 + 1; }
//tra3
if (item1 == tra3[5]) { support1 + 1; }
if (item2 == tra3[5]) { support2 + 1; }
if (item1 == tra3[5] && item2 == tra3[5]) { support3 + 1; }
//tra4
if (item1 == tra4[5]) { support1 + 1; }
if (item2 == tra4[5]) { support2 + 1; }
if (item1 == tra4[5] && item2 == tra4[5]) { support3 + 1; }
//tra5
if (item1 == tra5[5]) { support1 + 1; }
if (item2 == tra5[5]) { support2 + 1; }
else if (item1 == tra1[5] && item2 == tra5[5]) { support3 + 1; }
}
//print how many times are coffee and sugar purchased
cout << "Transaction done " << item1 << " : " << support1 << endl;
cout << "Transaction done " << item2 << " : " << support2 << endl;
cout << "Transaction done " << item2 << " dan " << item2 << " : " << support3 << endl;
cout << endl << "------------------------------" << endl;
float result1,result2,result3;
result1 = (support1 / n) * 100;
result2 = (support2 / n) * 100;
result3 = (support3 / n) * 100;
cout << "Item 1 : " << item1 << "\t" << "Item 2 : " << item2 << endl;
cout << "support " << item1 << " : " << result1 << endl;
cout << "support " << item2 << " : " << result2 << endl;
cout << "support " << item1 << " dan " << item2 << " : " << result3 << endl;
return 0;
}
in your code, inside the loop you keep referencing tra2[5] . I think you mean to use tra2[i] instead. The way you have it now you're only looking at one past the last item in your arrays (arrays are 0-based. Valid indices are [0-4])
2 things, you’re for loop is trying to access the 6th index of the array when it’s only initialized to have 5 indicis.
2nd the support1,2,3 variables are initialized to 0 but you never actually increment those variables, which are used as part of your final calculation.
Your for loop needs to change to have “support1 + 1” be “support1+=1”.
Working on a project where I am supposed to load a file based on user input, convert the data in that file into coordinates in the window and then use ASCII characters to draw a picture.
Files are in .art and start with the width and height of the window needed and each subsequent line is the ROW, COLUMN, CHARACTER, and COUNT that needs to be drawn. So basically the position the particular character should be started at and then how many times it should be drawn.
What I am struggling with is how to import that data into something usable so as I can draw the requested image. My initial thought is to import the data as a 4-D array but then I draw a blank as to where I should go from there.
Example lines for a .art file:
50 x 25
2, 15, *, 9
2, 48, *, 9
3, 6, *, 15
UPDATE: Given up on trying to load the files since I just can't wrap my head around that so I've changed to just drawing the art by hand and calling a string but I'm even having issues with that. With my code below, if I select option 1 the output is 001FFA80 rather than outputting the ASCII art that is in the string.
#include <stdio.h>
#include <cstdlib>
#include <string>
#include <iostream>
using namespace std;
int main ()
{
string shapeCupid[]=
{" ",
" .. ",
" $. ,o$$$o. ",
" $. $$$$$$$o. .. ",
" .$. $' $$$$$$ ,o'' ",
" .$' $ '$$$$$,o'.,' .oo ' ",
" .$' $. $$$$' ,, .o'. ",
" .$' '$o. 'O$ .. ooo''',oo ' ",
" .$' .o$' '$$'' ,,o' ",
" .%$,,,,,ooO' ' ,,o'' ",
" .$o. ,o' $o ..oo' ",
" ''O'''''''''',' $'$. .o' "};
string shapeFly = "Fly";
string shapeHeart = "Heart";
string shapeImpossible = "Impossible";
string shapeSeuss = "Seuss";
string shapeWorry = "Worry";
int UserInput;
cout << "What do you want to draw?\n";
cout << "1. Cupid\n2. Fly\n3. Heart\n4. Impossible\n5. Seuss\n6. Worry\nNumber: ";
cin >> UserInput;
if (UserInput == 1){
cout << shapeCupid;}
else if (UserInput == 2){
cout << shapeFly;}
else if (UserInput == 3){
cout << shapeHeart;}
else if (UserInput == 4){
cout << shapeImpossible;}
else if (UserInput == 5){
cout << shapeSeuss;}
else if (UserInput == 6){
cout << shapeWorry;}
else if (UserInput != 1 || 2 || 3 || 4 || 5 || 6)
cout << "Please select proper value.\n";
system("pause");
return 0;
}
Here's a quick and dirty version that might help. I didn't add a ton of error checking, but it works for the 2 examples I used. If you do plan to embed the data as string literals you'll want to be sure to escape any special characters like backslash and double quotes. The Boop example has both.
For the embedded string literal data the first line must be padded to be as long as the longest line since it is used to write the width. An enhancement would be to iterate through all lines to get the max width and use that.
#include <string>
#include <iostream>
#include <vector>
#include <fstream>
using StringVec = std::vector<std::string>;
StringVec shapeCupid =
{
" ",
" .. ",
" $. ,o$$$o. ",
" $. $$$$$$$o. .. ",
" .$. $' $$$$$$ ,o'' ",
" .$' $ '$$$$$,o'.,' .oo ' ",
" .$' $. $$$$' ,, .o'. ",
" .$' '$o. 'O$ .. ooo''',oo ' ",
" .$' .o$' '$$'' ,,o' ",
" .%$,,,,,ooO' ' ,,o'' ",
" .$o. ,o' $o ..oo' ",
" ''O'''''''''',' $'$. .o' "
};
//Borrowed from http://www.chris.com/ascii/index.php?art=cartoons/betty%20boop
StringVec boop =
{
" _(,__ __), ",
" (_,d888888888b,d888888888b",
" d888888888888/888888888888b_)",
" (_8888888P'\"\"'`Y8Y`'\"\"'\"Y88888b",
" Y8888P.-' ` '-.Y8888b_)",
" ,_Y88P (_(_( )_)_) d88Y_,",
" Y88b, (o ) (o ) d8888P",
" `Y888 '-' '-' `88Y`",
" ,d/O\\ c /O\\b,",
" \\_/'.,______w______,.'\\_/",
" .-` `-.",
" / , d88b d88b_ \\",
" / / 88888bd88888`\\ \\",
" / / \\ Y88888888Y \\ \\",
" \\ \\ \\ 88888888 / /",
" `\\ `. \\d8888888b, /\\\\/",
" `.//.d8888888888b; |",
" |/d888888888888b/",
" d8888888888888888b",
" ,_d88p\"\"q88888p\"\"q888b,",
" `\"\"'`\\ \"`| /`'\"\"`",
" `. |===/",
" > | |",
" / | |",
" | | |",
" | Y /",
" \\ / /",
" jgs | /| /",
" / / / |",
" /=/ |=/"
};
void Write(const std::string& filename, StringVec& data)
{
if(data.empty())
{
return;
}
std::ofstream out(filename);
if(out)
{
out << data[0].size() << " x " << data.size() << "\n";
for(size_t row = 0; row < data.size(); ++row)
{
const std::string& line = data[row];
size_t col_start = 0;
size_t col_end = 0;
char col_char = line[0];
for(size_t col = 0; col < line.size(); ++col)
{
//If we hit a new character write the previous run to the file
if(col_char != line[col])
{
//but only if it wasn't a run of spaces.
if(col_char != ' ')
{
out << row << ", " << col_start << ", " << col_char << ", " << col_end - col_start + 1 << "\n";
}
col_char = line[col];
col_start = col;
col_end = col;
}
col_end = col;
}
//write the last run
if(col_char != ' ')
{
out << row << ", " << col_start << ", " << col_char << ", " << col_end - col_start + 1 << "\n";
}
}
}
}
StringVec Read(const std::string& filename)
{
StringVec data;
std::ifstream in(filename);
if(in)
{
char dummy;
size_t width;
size_t height;
if(in >> width >> dummy >> height)
{
data.resize(height, std::string(width, ' '));
}
if(!data.empty())
{
size_t row;
size_t col;
char ch;
size_t len;
while(in >> row >> dummy >> col >> dummy >> ch >> dummy >> len)
{
for(size_t i = col; i < col + len; ++i)
{
data[row][i] = ch;
}
}
}
}
return data;
}
void Print(const StringVec& data)
{
for(const std::string& s : data)
{
std::cout << s << "\n";
}
std::cout << "\n";
}
int main()
{
Write("cupid.art", shapeCupid);
Print(Read("cupid.art"));
Write("boop.art", boop);
Print(Read("boop.art"));
return 0;
}