d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Is This Guy Crazy?
12Next
Add Reply New Topic New Poll
Member
Posts: 22,502
Joined: Aug 5 2011
Gold: 0.00
Jan 17 2014 10:29pm
http://www.youtube.com/watch?v=-hv2i2F8Pnw

Probably so, but why not help out the crazy and make there lives easier. This is a simple program I made for doing the calculations.

Quote
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int ch2n(string word);
int char2num(string word);

int main(){
string inputWord, phrase("Word or Sentence: ");
ofstream theFile("Info.txt", ios_base::app);
cout << phrase;

while (getline(cin, inputWord)){
  if (inputWord.find_first_not_of(' ') == string::npos)return 0;

  cout << phrase;
  theFile << phrase << inputWord << endl;
  theFile << "Letter Value: " << ch2n(inputWord) << endl;
  theFile << "Across Value: " << char2num(inputWord) << endl;
  theFile << "Sum: " << ch2n(inputWord) + char2num(inputWord) << endl;
  theFile << "Difference: " << ch2n(inputWord) - char2num(inputWord) << endl;
  theFile << "Total: " << ch2n(inputWord) + char2num(inputWord) + (ch2n(inputWord) + char2num(inputWord)) + (ch2n(inputWord) - char2num(inputWord)) << endl;
}
}

int ch2n(string word){
int total = 0;

for (int i = 0; i != word.size(); i++){
  if (tolower(word[i]) == 'a')total += 1;
  else if (tolower(word[i]) == 'b')total += 2;
  else if (tolower(word[i]) == 'c')total += 3;
  else if (tolower(word[i]) == 'd')total += 4;
  else if (tolower(word[i]) == 'e')total += 5;
  else if (tolower(word[i]) == 'f')total += 6;
  else if (tolower(word[i]) == 'g')total += 7;
  else if (tolower(word[i]) == 'h')total += 8;
  else if (tolower(word[i]) == 'i')total += 9;
  else if (tolower(word[i]) == 'j')total += 10;
  else if (tolower(word[i]) == 'k')total += 11;
  else if (tolower(word[i]) == 'l')total += 12;
  else if (tolower(word[i]) == 'm')total += 13;
  else if (tolower(word[i]) == 'n')total += 14;
  else if (tolower(word[i]) == 'o')total += 15;
  else if (tolower(word[i]) == 'p')total += 16;
  else if (tolower(word[i]) == 'q')total += 17;
  else if (tolower(word[i]) == 'r')total += 18;
  else if (tolower(word[i]) == 's')total += 19;
  else if (tolower(word[i]) == 't')total += 20;
  else if (tolower(word[i]) == 'u')total += 21;
  else if (tolower(word[i]) == 'v')total += 22;
  else if (tolower(word[i]) == 'w')total += 23;
  else if (tolower(word[i]) == 'x')total += 24;
  else if (tolower(word[i]) == 'y')total += 25;
  else if (tolower(word[i]) == 'z')total += 26;
}
return total;
}

int char2num(string word){
int total = 0;

for (int i = 0; i != word.size(); i++){
  if (tolower(word[i]) == 'a')total += 1;
  else if (tolower(word[i]) == 'b')total += 2;
  else if (tolower(word[i]) == 'c')total += 3;
  else if (tolower(word[i]) == 'd')total += 4;
  else if (tolower(word[i]) == 'e')total += 5;
  else if (tolower(word[i]) == 'f')total += 6;
  else if (tolower(word[i]) == 'g')total += 7;
  else if (tolower(word[i]) == 'h')total += 8;
  else if (tolower(word[i]) == 'i')total += 9;
  else if (tolower(word[i]) == 'j')total += 1;
  else if (tolower(word[i]) == 'k')total += 2;
  else if (tolower(word[i]) == 'l')total += 3;
  else if (tolower(word[i]) == 'm')total += 4;
  else if (tolower(word[i]) == 'n')total += 5;
  else if (tolower(word[i]) == 'o')total += 6;
  else if (tolower(word[i]) == 'p')total += 7;
  else if (tolower(word[i]) == 'q')total += 8;
  else if (tolower(word[i]) == 'r')total += 9;
  else if (tolower(word[i]) == 's')total += 10;
  else if (tolower(word[i]) == 't')total += 2;
  else if (tolower(word[i]) == 'u')total += 3;
  else if (tolower(word[i]) == 'v')total += 4;
  else if (tolower(word[i]) == 'w')total += 5;
  else if (tolower(word[i]) == 'x')total += 6;
  else if (tolower(word[i]) == 'y')total += 7;
  else if (tolower(word[i]) == 'z')total += 8;
}
return total;
}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jan 17 2014 10:45pm
Code
if (tolower(word[i]) == 'a')total += 1;
else if (tolower(word[i]) == 'b')total += 2;
else if (tolower(word[i]) == 'c')total += 3;
else if (tolower(word[i]) == 'd')total += 4;
else if (tolower(word[i]) == 'e')total += 5;
else if (tolower(word[i]) == 'f')total += 6;
else if (tolower(word[i]) == 'g')total += 7;
else if (tolower(word[i]) == 'h')total += 8;
else if (tolower(word[i]) == 'i')total += 9;
else if (tolower(word[i]) == 'j')total += 10;
else if (tolower(word[i]) == 'k')total += 11;
else if (tolower(word[i]) == 'l')total += 12;
else if (tolower(word[i]) == 'm')total += 13;
else if (tolower(word[i]) == 'n')total += 14;
else if (tolower(word[i]) == 'o')total += 15;
else if (tolower(word[i]) == 'p')total += 16;
else if (tolower(word[i]) == 'q')total += 17;
else if (tolower(word[i]) == 'r')total += 18;
else if (tolower(word[i]) == 's')total += 19;
else if (tolower(word[i]) == 't')total += 20;
else if (tolower(word[i]) == 'u')total += 21;
else if (tolower(word[i]) == 'v')total += 22;
else if (tolower(word[i]) == 'w')total += 23;
else if (tolower(word[i]) == 'x')total += 24;
else if (tolower(word[i]) == 'y')total += 25;
else if (tolower(word[i]) == 'z')total += 26;


you know you didn't have to type all that, right? you could've used something similar to:
total += tolower(word[i]) - 'a' + 1

This post was edited by carteblanche on Jan 17 2014 10:45pm
Member
Posts: 22,502
Joined: Aug 5 2011
Gold: 0.00
Jan 17 2014 10:52pm
Quote (carteblanche @ Jan 17 2014 11:45pm)
you know you didn't have to type all that, right? you could've used something similar to:
total += tolower(word[i]) - 'a' + 1


how do I increase the total value to 2 if it's b then?? and 3 to c, 4 to d, etc.

This post was edited by sylz1014 on Jan 17 2014 10:57pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jan 17 2014 11:07pm
Quote (sylz1014 @ Jan 17 2014 11:52pm)
how do I increase the total value to 2 if it's b then?? and 3 to c, 4 to d, etc.


Read what i posted again. use different letters and see what happens.
Member
Posts: 22,502
Joined: Aug 5 2011
Gold: 0.00
Jan 17 2014 11:12pm
well actually I found a flaw. When writing a sentence the whitespaces make a dramatic change in results.

Example:
[Your Way]
Word or Sentence: Hello Mr Man
Across Value: 48
Letter Value: -17
Difference: -65
Sum: 31
Total: -3
[My Way]
Word or Sentence: Hello Mr Man
Across Value: 48
Letter Value: 111
Difference: 63
Sum: 159
Total: 381

Mine is the correct value.

This post was edited by sylz1014 on Jan 17 2014 11:31pm
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Jan 18 2014 01:30am
then use isalpha()
Member
Posts: 1,177
Joined: Feb 19 2006
Gold: 962.70
Jan 19 2014 01:41am
You can simplify the whole thing quite a bit to get the same values as you are getting currently.

Code

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int ch2n(string word);
int char2num(string word);

int main(){
string inputWord, phrase("Word or Sentence: ");
ofstream theFile("Info.txt", ios_base::app);
cout << phrase;

while (getline(cin, inputWord)){
if (inputWord.find_first_not_of(' ') == string::npos)return 0;

int letterValue = ch2n(inputWord);
int acrossValue = char2num(inputWord);
theFile << phrase << inputWord << endl;
theFile << "Letter Value: " << letterValue << endl;
theFile << "Across Value: " << acrossValue << endl;
theFile << "Sum: " << letterValue + acrossValue << endl;
theFile << "Difference: " << letterValue - acrossValue << endl;
theFile << "Total: " << 3 * letterValue + acrossValue << endl;

cout << phrase;
}
}

int ch2n(string word){
int total = 0;
for (int i = 0; i != word.size(); i++)
if (isalpha(word[i]))
total += tolower(word[i]) - 'a' + 1;
return total;
}

int char2num(string word){
int total = 0;
for (int i = 0; i != word.size(); i++) {
if (isalpha(word[i])) {
char c = tolower(word[i]);
if (c >= 'a' && c <= 'i') {
total += c - 'a' + 1;
} else if (c >= 'j' && c <= 's') {
total += c - 'j' + 1;
} else if (c >= 't' && c <= 'z') {
total += c - 't' + 2;
}
}
}
return total;
}


Also, what is your total line doing?
Code

theFile << "Total: " << ch2n(inputWord) + char2num(inputWord) + (ch2n(inputWord) + char2num(inputWord)) + (ch2n(inputWord) - char2num(inputWord)) << endl;


Why not just have:
Code

theFile << "Total: " << 3 * ch2n(inputWord) + char2num(inputWord) << endl;


Also, your functions and variables could use a bit more clarity, for instance: ch2n and char2num imply that they do the same thing, however they don't... maybe consider using something like "calculateAcrossValue" or "acrossValue" or something. Additionally, inputWord is not always a single word.

Lastly, and most importantly... I tried to make it through that YouTube video (please tell me that's not you), but what the hell is that guy smoking? I don't even have a single clue what he's trying to prove
Member
Posts: 22,502
Joined: Aug 5 2011
Gold: 0.00
Jan 19 2014 01:50pm
Quote (huskerfan113 @ Jan 19 2014 02:41am)
You can simplify the whole thing quite a bit to get the same values as you are getting currently.

Also, what is your total line doing?

Why not just have:

Also, your functions and variables could use a bit more clarity, for instance: ch2n and char2num imply that they do the same thing, however they don't... maybe consider using something like "calculateAcrossValue" or "acrossValue" or something. Additionally, inputWord is not always a single word.

Lastly, and most importantly... I tried to make it through that YouTube video (please tell me that's not you), but what the hell is that guy smoking? I don't even have a single clue what he's trying to prove


Was just doing the order of operations to output that line, didn't put much thinking into shrinking it down lol.
I made it quickly because a buddy of mine told me to do it. I haven't even been able to watch more than a minute of that video, he is definitely on something and it ain't weed lol. As for the functions, like I said made it quick and didn't put any thought into clarifying what the code is actually doing but to get it done.
e/
brilliant code though. spruced it right up

This post was edited by sylz1014 on Jan 19 2014 02:14pm
Member
Posts: 12,953
Joined: Mar 14 2004
Gold: 13,142.01
Jan 19 2014 04:59pm
That video just made my brain explode.
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Jan 21 2014 08:12am
nm

This post was edited by labatymo on Jan 21 2014 08:14am
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll