Ternary Operator Problems
Nov 2, 2008 at 11:31pm UTC
I've written a shift cipher encryption program and just for educational purposes I wanted to see if I could reduce all my if then statements to a big long ternary operator. I've reduced it by several lines but have been unable to get the entire thing on one line. I'm thinking it should be possible but I can't quite get the syntax right.
Here is what I started with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
if (y < 65 || y > 90 && y < 97 || y > 122)
xy = y;
else if (y < 123 && y > 96)
{
if (y + sVal < 123 && y + sVal > 96) //LCASE1
xy = y + sVal;
else if (y + sVal < 97) //LCASE2
xy = y + sVal + 26;
else if (y + sVal > 122) //LCASE3
xy = y + sVal - 26;
}
else if (y < 91 && y > 64)
{
if (y + sVal < 91 && y + sVal > 64) //CAPS1
xy = y + sVal;
else if (y + sVal < 65) //CAPS2
xy = y + sVal + 26;
else if (y + sVal > 90) //CAPS3
xy = y + sVal - 26;
}
Here is what I've since reduced it down to (I want to get this on one line):
1 2 3 4 5 6 7
if (y < 65 || y > 90 && y < 97 || y > 122)
xy = y;
else if (y < 123 && y > 96)
xy = (y + sVal < 123 && y + sVal > 96) ? y + sVal : (y + sVal < 97) ? y + sVal + 26 : (y + sVal > 122) ? y + sVal - 26 : false ;
else if (y < 91 && y > 64)
xy = (y + sVal < 91 && y + sVal > 64) ? y + sVal : (y + sVal < 65) ? y + sVal + 26 : (y + sVal > 90) ? y + sVal -26 : false ;
And here's the entire program to stick it in:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string line;
string crypt = "" ;
string iFile;
string oFile;
int sVal;
cout << "Input source file: " ;
cin >> iFile;
cout << "Input destination file: " ;
cin >> oFile;
cout << "Input shift value: " ;
cin >> sVal;
cout << "Reading from file: " << iFile << endl;
const char * v = iFile.c_str();
ifstream myfile(v);
cout << "Writing to file: " << oFile << " with shift value of: " << sVal << endl;
const char * z = oFile.c_str();
ofstream myofile(z);
if (myfile.is_open())
{
while (!myfile.eof())
{
getline (myfile,line);
//cout << "line gotten is: " << line << endl;
for (unsigned int i=0; i < line.length(); i++)
{
char y = (line.at(i));
char xy = 12;
/** Code starts here **
if (y < 65 || y > 90 && y < 97 || y > 122)
xy = y;
else if (y < 123 && y > 96)
xy = ((y + sVal < 123 && y + sVal > 96) ? y + sVal : (y + sVal < 97) ? y + sVal + 26 : (y + sVal > 122) ? y + sVal - 26) : false
else if (y < 91 && y > 64)
xy = ((y + sVal < 91 && y + sVal > 64) ? y + sVal : (y + sVal < 65) ? y + sVal + 26 : (y + sVal > 90) ? y + sVal -26 : false);
** Code ends here **/
crypt += xy;
}
myofile << crypt << endl;
crypt = "" ;
}
myfile.close();
}
else cout << "Unable to open file" << endl;
cout << "EOF" << endl;
}
Nov 3, 2008 at 12:50am UTC
Just use some some more () around it to specifiy what everything should be and it should be fine...Ex:
1 2 3 4 5 6 7 8 9
if (x == 2) {
x = 1;
} else if (x == 1) {
x = 0;
} else {
x = 5;
}
//becomes
(x == 2) ? x = 1 : (x == 1 ? x = 0 : (x = 5));
Nov 3, 2008 at 3:48am UTC
I'm happy to report that I solved my problem. I have included the finalized code below for anyone who is interested. =)
From This:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
if (y < 65 || y > 90 && y < 97 || y > 122)
xy = y;
else if (y < 123 && y > 96)
{
if (y + sVal < 123 && y + sVal > 96) //LCASE1
xy = y + sVal;
else if (y + sVal < 97) //LCASE2
xy = y + sVal + 26;
else if (y + sVal > 122) //LCASE3
xy = y + sVal - 26;
}
else if (y < 91 && y > 64)
{
if (y + sVal < 91 && y + sVal > 64) //CAPS1
xy = y + sVal;
else if (y + sVal < 65) //CAPS2
xy = y + sVal + 26;
else if (y + sVal > 90) //CAPS3
xy = y + sVal - 26;
}
To This:
xy = (y < 65 || y > 90 && y < 97 || y > 122) ? y : (y < 123 && y > 96) ? ((y + sVal < 123 && y + sVal > 96) ? y + sVal : (y + sVal < 97) ? y + sVal + 26 : (y + sVal > 122) ? y + sVal - 26 : false ) : (y < 91 && y > 64) ? ((y + sVal < 91 && y + sVal > 64) ? y + sVal : (y + sVal < 65) ? y + sVal + 26 : (y + sVal > 90) ? y + sVal -26 : false ) : false ;
Nov 3, 2008 at 8:58am UTC
Why?
Nov 3, 2008 at 7:11pm UTC
Code obfuscation I suppose.
Topic archived. No new replies allowed.