C++ QT convert list to one of overloaded functions - c++

I am looking for a way to convert a list of QVariants into one of overloaded functions that takes either 1, 2, 3 or 4 QVariants.
My initial idea is to just make a switch statement on the list's length and call functions based on it, but I am not sure if it is the only solution for this problem, and it doesn't look very scalable:
switch(list.length())
{
case 0:
fun();
case 1:
fun(list[0]);
break;
case 2:
fun(list[0], list[1]);
break;
case 3:
fun(list[0], list[1], list[2]);
break;
...
}

Related

Does this count as recursion?

I'm learning about recursions and I have to later apply it a project. My project is going to include a deck of cards and I'm just curious if this counts as recursion? To me it seems like it since it calls itself. However since i'm still learning I want to make sure i'm understanding the concept and i'm not wrong. Please let me know.
string Card::suitName(Suit S) {
string suits;
switch (S) {
case clubs:
suits = "Clubs";
break;
case diamonds:
suits = "Diamonds";
break;
case hearts:
suits = "Hearts";
break;
case spades:
suits = "Spades";
break;
}
return (suits);
}
// declare the value of the cards
string Card::cardValue(Value Card) {
string card;
switch (Card) {
case two:
card = "Two";
break;
case three:
card = "Three";
break;
case four:
card = "Four";
break;
case five:
card = "Five";
break;
case six:
card = "Six";
break;
case seven:
card = "Seven";
break;
case eight:
card = "Eight";
break;
case nine:
card = "Nine";
break;
case ten:
card = "Ten";
break;
case jack:
card = "Jack";
break;
case queen:
card = "Queen";
break;
case king:
card = "King";
break;
case ace:
card = "Ace";
break;
}
return (card);
}
You've defined two functions Card::suitName() and Card::cardValue(). Neither function calls itself or any other user-defined function (excluding any std::string functions). There's no recursion anywhere in your code.
Recursion is defined as a function calling itself within itself. So suitName would need to have a call to suitName or cardValue to cardValue. Neither make such a call, so
There is no recursion in your code
Recursion requires a function to call itself (most commonly directly but it can be via another function).
Example:
bool doesHandContainAceRecursion(std::vector<Card> const& playersCards, int pos) {
// If we have tried all the cards and gone past the end
// Then return false.
//
// This is a classic part of recursion, checking if you have reached the end.
if (pos >= playersCards.size()) {
return false;
}
// Otherwise do the work you want to do.
if (playersCards[pos].value == 1) {
return true;
}
// This is the recursive call.
// It calls itself. This is a classic example of tail recursion.
return doesHandContainAceRecursion(playersCards, pos + 1);
}
// This is the function you call and it sets up a call to the
// recursive function above.
bool doesHandContainAce(std::vector<Card> const& playersCards) {
return doesHandContainAceRecursion(playersCards, 0);
}
No this is not recursions. Recursions is when method is calling itself
Example:
string Card::suitName(Suit S) {
string suits;
switch (S) {
case clubs:
suitName(diamonds); // Recursions
break;
case diamonds:
suits = "Diamonds";
break;
}
return (suits);
}
No, the code you provided does not involve recursion. Recursion is a programming technique where a function calls itself, either directly or indirectly, to solve a problem. In the code you provided, there are no function calls that reference the function itself.
The functions suitName and cardValue are simply taking in an input and returning a corresponding output based on a switch statement. They do not involve any kind of iterative or recursive process to arrive at the output.
In the context of a deck of cards, you might consider using recursion for a function that shuffles the cards, as shuffling involves repeatedly swapping pairs of cards until the deck is randomized. However, the code you provided does not involve recursion.

Using a switch in a do..while loop, in C++

A simple programm that reads strings, and responds using a switch;
in this do-while loop containing a switch, I am able to run case 1-4 with no issues, but once i hit the default case, the programme simply loops the default case over and over again the code is as follows;
do { switch ( switchstring (entry, input) )
/*the switchstring function is one 1 wrote to convert a given entry(string),
into an input(integer)*/
{
case 1:
//code
repeat = 2;
break;
case 2:
//code
repeat = 2;
break;
case 3:
//code
repeat = 2;
break;
case 4:
//code
repeat = 2;
break;
default:
//code
repeat = 1;
break;}} while(repeat == 1);
the 2nd question is regarding my switchstring() function; is there a way to change the switch function such that it reads;
case (insert string):
i.e. so that I can remove the entire switchstring() function
thanks in advance!
Show us how switchstring (entry, input) works.
The problem you are facing is because, in default you do the following:
repeat = 1;
Which makes while(repeat == 1) always true. And then switchstring (entry, input) always return something that makes your switch block always go the the default case again.
When no case will be true in switch, then it will go in default case of switch and you are specifying repeat=1; in default. After that while condition will be checked and it will be true because repeat is 1, again it will go to do and check condition, your switch function will return something and it will go to default.
To solve 2nd question regarding your switchstring() function, you have to show your code what you are doing in that function, So that i can give you best suggestion.

qt textbrowser name save in an array

I am trying to show my 4by4 matrix in qt gui, there for I have used one text browser for each element of matrix. Right now I am able to display matrix using switch case but I dont like this method. I want to make an array in which I can save the name of textbrowser and willing to access them using for loop. below is the my current code. please guide me how can I get what I am willing to do.
for (i = 0; i <= 3; i++)
{
for (j = 0; j <= 3;j++)
{
switch(no){
case 1:
ui->textBrowser_200->setText(text1);
break;
case 2:
ui->textBrowser_201->setText(text1);
break;
case 3:
ui->textBrowser_202->setText(text1);
break;
case 4:
ui->textBrowser_203->setText(text1);
break;
case 5:
ui->textBrowser_204->setText(text1);
break;
case 6:
ui->textBrowser_205->setText(text1);
break;
case 7:
ui->textBrowser_206->setText(text1);
break;
case 8:
ui->textBrowser_207->setText(text1);
break;
case 9:
ui->textBrowser_208->setText(text1);
break;
case 10:
ui->textBrowser_209->setText(text1);
break;
case 11:
ui->textBrowser_210->setText(text1);
break;
case 12:
ui->textBrowser_211->setText(text1);
break;
case 13:
ui->textBrowser_212->setText(text1);
break;
case 14:
ui->textBrowser_213->setText(text1);
break;
case 15:
ui->textBrowser_214->setText(text1);
break;
case 16:
ui->textBrowser_215->setText(text1);
break;
}
no++;
}
}
Here is a simple solution which will allow you to keep your current Designer ui:
Add this member variable to your own class, to have two-dimensional matrix of widget pointers:
std::array< std::array <QTextBrowser *, 4>, 4> mTextBrowserMatrix;
Then initialize it in the constructor, with 16 lines of code like this, after you have called setupUi() for the Designer ui:
mTextBrowserMatrix[0][0] = ui->textBrowser_200;
// repeat above for all 16 widgets.
And then just access them like mTextBrowserMatrix[0][0] etc.
You could use any container or even plain C arrays for this, above is just an example.
Of course you will save some repetitive copy-paste style code and have cleaner Designer design, if you just create the QTextBrowser matrix in code, instead of using Designer for it. But since you already have them, might as well stick with it for now, 16 lines of repeated code is not that horrible.

Switch Statement continue

Is the following possible in C++?
switch (value) {
case 0:
// code statements
break;
case 1:
case 2:
// code statements for case 1 and case 2
/**insert statement other than break here
that makes the switch statement continue
evaluating case statements rather than
exit the switch**/
case 2:
// code statements specific for case 2
break;
}
I want to know if there is a way to make the switch statement continue evaluating the rest of the cases even after it has hit a matching case. (such as a continue statement in other languages)
How about a simple if?
switch (value)
{
case 0:
// ...
break;
case 1:
case 2:
// common code
if (value == 2)
{
// code specific to "2"
}
break;
case 3:
// ...
}
Once the case label is decided, there is no way to have the switch continue to search for other matching labels. You can continue to process the code for the following label(s) but this doesn't distinguish between the different reasons why a case label was reached. So, no, there is no way to coninue the selection. In fact, duplicate case labels are prohibited in C++.
Yep, just don't put in a break. It will naturally fall down to the other switch statements.

Dynamically changing size of switch-case

Following situation:
My system gets an hardware signal and writes a time value to a buffer in my
signal handler routine. Afterwards a (software) signal is sent with the time value as argument to the appropriate slot function.
The slot routine gets called correctly, but here my problem lays in:
In the slot function I have a simple switch-case statement like this:
switch(id) {
case 1:
do something..
id = 2;
break;
case 2:
start_time = val;
id = 3;
break;
case 3:
end_time = val;
id = 1;
break;
}
In those three cases I store a start and end time value between case 2 and 3 and
out of those time values I determine the elapsed time between the hardware
signals. This works fine, but now I have to measure the time sometimes "longer",
depening on parameter. This means, I can't stop the measurement at case 3 instead
I have case 4, 5, 6 and so on . What is an elegant and optimal solution for this "problem"
instead of writing:
if (param < xy) {
switch(id) {
case 1:
...
break;
case 2:
...
break;
} else if (param > xy) {
switch(id) {
case 1:
...;
break;
case 2:
...;
break;
case 3:
...;
break;
case 4:
...;
break;
case 5:
...;
break;
}
}
}
What you are describing is called a finite state machine there are a large number of excellent state machine libraries out there that will take care of the heavy lifting for you.
Take a look at this question and some of the others that it references.
You can try following:
switch ((param - xy) >= 0 ? id : -id) {
// param >= xy cases
case 1:
...
break;
case 2:
...
break;
...
// param < xy cases
case -1:
...
break;
case -2:
...
break;
...
}
Or for something fun an exciting, you could write some self modifying code to dynamically change your swithc jump table as the parameters it receives differ. You'd have to allocate a large enough area for the largest table size and play around with funciton pointers or assembler, but it could be done.
Try using a std::map of function pointers, a.k.a. jump table, rather than a switch statement. The map allows flexibility during run-time.
Store a pointer to the function, along with the case value. Search the map for the case value, retrieve the pointer and dereference to call the function.