Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tenary operator in QB64 with 4 numbers
#1
These _Max/_Min functions are excellent. Applying the tenary operator to more than two numbers is much easier in QB64 than in C, for example. Even determining four numbers is easy in Basic, but a monster in C.

This is interesting if, for example, one want to determine maximum/minimum for temperature values etc., and the program should then react accordingly.

Basic rulez!  Tongue

Code: (Select All)

'Tenaeren Operators in QB64 ab Vers. 4.0 - 16. Dez. 2024
'Dank an a740g und RhoSigma - 17/18 Dez 2024

Option _Explicit

Dim As Long zahl1, zahl2, zahl3, zahl4, max, min, mittelWert

Locate 3, 3
Print "Vierfach tenaerer Operator in QB64"
Locate 4, 3
Print "=================================="

Locate 6, 3
Input "Zahl 1: ", zahl1

Locate 7, 3
Input "Zahl 2: ", zahl2

Locate 8, 3
Input "Zahl 3: ", zahl3

Locate 9, 3
Input "Zahl 4: ", zahl4

'Anwendung des tenaeren Operators auf 4 Zahlen
max = _Max(_Max(_Max(zahl1, zahl2), zahl3), zahl4)
min = _Min(_Min(_Min(zahl1, zahl2), zahl3), zahl4)

Locate 11, 3
Print Using "Die groesste Zahl ist: ####"; max

Locate 13, 3
Print Using "Die kleinste Zahl ist: ####"; min

Locate 15, 3
mittelWert = (max + min) / 2
Print Using "Der mittlere Wert betraegt: ###"; mittelWert

End

And here with the call to the corresponding C program
Code: (Select All)

'Aufruf des tenaeren Operators in C mit 4 Zahlen - 18. Dez. 2024

Option _Explicit

'In QB64 mit "Declare Library" wie angegeben
Declare Library "D:\Lab\QuickBasic64\Extern-nicht-Basic\Basic-ruft-C\btenaerVierfach"
  Function btenaerVierfach& (ByVal Zahl1 As Long, Byval Zahl2 As Long, Byval Zahl3 As Long, Byval Zahl4 As Long)
End Declare

Dim As Long zahl1, zahl2, zahl3, zahl4

Locate 3, 3
Print "Vierfachen tenaeren Operator in C aufrufen"
Locate 4, 3
Print "=========================================="

Locate 6, 3
Input "Zahl 1: ", zahl1

Locate 7, 3
Input "Zahl 2: ", zahl2

Locate 8, 3
Input "Zahl 3: ", zahl3

Locate 9, 3
Input "Zahl 3: ", zahl4


Locate 11, 3
Print Using "Die groesste Zahl ist      : ####"; btenaerVierfach&(zahl1&, zahl2&, zahl3&, zahl4&)

End

And the C-function (a monster!):
Code: (Select All)

//Maximum + Minimum von 4 Zahlen mit tenären Operator - 18. Dez. 2024

#include <stdio.h>
#include <stdlib.h>

long btenaerVierfach(long zahl1, long zahl2, long zahl3, long zahl4)
{
long max;

max = zahl1 >= zahl2
? zahl1 >= zahl3
? zahl1 >= zahl4 ? zahl1 : zahl4
: zahl3 >= zahl4 ? zahl3 : zahl4
: zahl2 >= zahl3
? zahl2 >= zahl4 ? zahl2 : zahl4
: zahl3 >= zahl4 ? zahl3 : zahl4;

return(max);
}
Reply
#2
(12-18-2024, 05:28 PM)Kernelpanic Wrote: These _Max/_Min functions are excellent. Applying the tenary operator to more than two numbers is much easier in QB64 than in C, for example. Even determining four numbers is easy in Basic, but a monster in C.

This is interesting if, for example, one want to determine maximum/minimum for temperature values etc., and the program should then react accordingly.

Basic rulez!  Tongue

Code: (Select All)

'Tenaeren Operators in QB64 ab Vers. 4.0 - 16. Dez. 2024
'Dank an a740g und RhoSigma - 17/18 Dez 2024

Option _Explicit

Dim As Long zahl1, zahl2, zahl3, zahl4, max, min, mittelWert

Locate 3, 3
Print "Vierfach tenaerer Operator in QB64"
Locate 4, 3
Print "=================================="

Locate 6, 3
Input "Zahl 1: ", zahl1

Locate 7, 3
Input "Zahl 2: ", zahl2

Locate 8, 3
Input "Zahl 3: ", zahl3

Locate 9, 3
Input "Zahl 4: ", zahl4

'Anwendung des tenaeren Operators auf 4 Zahlen
max = _Max(_Max(_Max(zahl1, zahl2), zahl3), zahl4)
min = _Min(_Min(_Min(zahl1, zahl2), zahl3), zahl4)

Locate 11, 3
Print Using "Die groesste Zahl ist: ####"; max

Locate 13, 3
Print Using "Die kleinste Zahl ist: ####"; min

Locate 15, 3
mittelWert = (max + min) / 2
Print Using "Der mittlere Wert betraegt: ###"; mittelWert

End

And here with the call to the corresponding C program
Code: (Select All)

'Aufruf des tenaeren Operators in C mit 4 Zahlen - 18. Dez. 2024

Option _Explicit

'In QB64 mit "Declare Library" wie angegeben
Declare Library "D:\Lab\QuickBasic64\Extern-nicht-Basic\Basic-ruft-C\btenaerVierfach"
  Function btenaerVierfach& (ByVal Zahl1 As Long, Byval Zahl2 As Long, Byval Zahl3 As Long, Byval Zahl4 As Long)
End Declare

Dim As Long zahl1, zahl2, zahl3, zahl4

Locate 3, 3
Print "Vierfachen tenaeren Operator in C aufrufen"
Locate 4, 3
Print "=========================================="

Locate 6, 3
Input "Zahl 1: ", zahl1

Locate 7, 3
Input "Zahl 2: ", zahl2

Locate 8, 3
Input "Zahl 3: ", zahl3

Locate 9, 3
Input "Zahl 3: ", zahl4


Locate 11, 3
Print Using "Die groesste Zahl ist      : ####"; btenaerVierfach&(zahl1&, zahl2&, zahl3&, zahl4&)

End

And the C-function (a monster!):
Code: (Select All)

//Maximum + Minimum von 4 Zahlen mit tenären Operator - 18. Dez. 2024

#include <stdio.h>
#include <stdlib.h>

long btenaerVierfach(long zahl1, long zahl2, long zahl3, long zahl4)
{
long max;

max = zahl1 >= zahl2
? zahl1 >= zahl3
? zahl1 >= zahl4 ? zahl1 : zahl4
: zahl3 >= zahl4 ? zahl3 : zahl4
: zahl2 >= zahl3
? zahl2 >= zahl4 ? zahl2 : zahl4
: zahl3 >= zahl4 ? zahl3 : zahl4;

return(max);
}

The min/max functions also exist in C, so you could do it the same way as in BASIC there.

I think the problem is, that many people think the tenary operator in C is a holy magican, but it's not.
It's perfect if you have exactly two different cases to distinguish. If you've more cases, then there are other language constructs to be preferred.
Reply
#3
Quote:@RhoSigma - The min/max functions also exist in C, so you could do it the same way as in BASIC there.
I haven't found a max/min function in my C/C++ manuals, only something like this, via "#define ...": https://stackoverflow.com/questions/3437...d-max-in-c

1. The old C macro way:
Code: (Select All)

#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))

Where else can you find this selection option as it is possible in QB64, and so simple? In a programming language for "everyday use"?
Reply
#4
(12-18-2024, 08:33 PM)Kernelpanic Wrote:
Quote:@RhoSigma - The min/max functions also exist in C, so you could do it the same way as in BASIC there.
I haven't found a max/min function in my C/C++ manuals, only something like this, via "#define ...": https://stackoverflow.com/questions/3437...d-max-in-c

1. The old C macro way:
Code: (Select All)

#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))

Where else can you find this selection option as it is possible in QB64, and so simple? In a programming language for "everyday use"?

https://legacy.cplusplus.com/reference/algorithm/min/
https://legacy.cplusplus.com/reference/algorithm/max/
Reply
#5
Yes, in C++, that's what I suspected. But I can't find anything about it in my books.

The C++ programming language, 2nd edition, Addison-Wesley
C++ - Introduction and Guide, 2nd edition, Addison-Wesley
MS-Visual C++, Reference Volume 1
C++ for Dummies, Volumes 1 + 2 . . . etc

I can't find any reference to this function anywhere.

Maybe it's because this is a supplement to C++ 98 and isn't in my books yet. They're all from around 1994 to 2006, when I was interestin/working with C/C++.
Reply
#6
std::min and std:max were added to C++11, which is probably why you do not see them in your books.
Reply
#7
I have a Windows book that might help. Oh wait, I can't use it... it's updating.

Pete Big Grin
Shoot first and shoot people who ask questions, later.
Reply
#8
@a740g & RhoSigma - Yes, it is not in my books yet. Otherwise, in C++ it works like in QB64 with three numbers:

Basic & C++ rulez!  Big Grin
Code: (Select All)

//max example - https://legacy.cplusplus.com/reference/algorithm/max/
//Beispiel in C++ zu QB64 MAX/MIN - 19. Dez. 2024

#include <iostream>  // std::cout
#include <algorithm>  // std::max
#include <iomanip>    //setw() und cin.width(int) 
#include <string>

using namespace std;

int main(void)
{
double a, b, c, maximum;

system("cls");
cout << '\n' << "Maximum dreier Zahlen" << '\n' << '\n';

cout << "Zahl 1: "; cin >> a;
cout << "Zahl 2: "; cin >> b;
cout << "Zahl 3: "; cin >> c;
maximum = max(max(a, b), c);

cout << '\n' << "Die groesste Zahl ist: ";
cout << fixed << setprecision(2) << setw(4) << maximum << '\n';

  cout << endl;
  return 0;
}

[Image: Maximum-C-2024-12-19.jpg]
Reply




Users browsing this thread: 2 Guest(s)