Page 2 of 2 FirstFirst 1 2
Results 21 to 31 of 31

Thread: c++ switch statement     submit to reddit submit to twitter

  1. #21
    E. Body
    Join Date
    Sep 2007
    Posts
    2,019
    BG Level
    7
    FFXI Server
    Fenrir

    Quote Originally Posted by Zeith View Post
    Or, as I myself would be inclined to say, "Nested ternary operators are for clever jerks."
    But single Ternary operators are amazing!

  2. #22
    Murder machine with a motor in her nose
    Join Date
    Apr 2007
    Posts
    368
    BG Level
    4
    FFXI Server
    Carbuncle

    inline assembly...coz that's how we roll.

    Code:
    #include <iostream>
    
    using namespace std;
    
    void main() {
    	int myGrade = 75;
    
    _asm
    {
    	mov eax, dword ptr [myGrade]
    	mov ecx,'A'
    	cmp eax,89
    	setle ch
    	add cl,ch
    	cmp eax,79
    	setle ch
    	add cl,ch
    	cmp eax,69
    	setle ch
    	add cl,ch
    	cmp eax,59
    	setle ch
    	add cl,ch
    	add cl,ch
    	xor ch,ch
    	mov dword ptr [myGrade],ecx
    }
    
    cout<<(char)myGrade<<endl;
    
    }
    For the life of me I couldn't figure out how to "cout" in assembly or lord knows I'd have put that in there too.

    Seems like there should be a way to do it with 1 less jump, but I'm too sleepy to figure that out.

    as a comparison, that above nested comparison method expands to this in assembly:
    Code:
    myGrade=(myGrade>89?'A':myGrade>79?'B':myGrade>69?'C':myGrade>59?'D':'F');
    00401D31  cmp         dword ptr [myGrade],59h 
    00401D35  jle         main+7Dh (401D3Dh) 
    00401D37  mov         byte ptr [ebp-0Dh],41h 
    00401D3B  jmp         main+0AFh (401D6Fh) 
    00401D3D  cmp         dword ptr [myGrade],4Fh 
    00401D41  jle         main+89h (401D49h) 
    00401D43  mov         byte ptr [ebp-0Eh],42h 
    00401D47  jmp         main+0A9h (401D69h) 
    00401D49  cmp         dword ptr [myGrade],45h 
    00401D4D  jle         main+95h (401D55h) 
    00401D4F  mov         byte ptr [ebp-0Fh],43h 
    00401D53  jmp         main+0A3h (401D63h) 
    00401D55  cmp         dword ptr [myGrade],3Bh 
    00401D59  setle       dl   
    00401D5C  lea         edx,[edx+edx+44h] 
    00401D60  mov         byte ptr [ebp-0Fh],dl 
    00401D63  mov         al,byte ptr [ebp-0Fh] 
    00401D66  mov         byte ptr [ebp-0Eh],al 
    00401D69  mov         cl,byte ptr [ebp-0Eh] 
    00401D6C  mov         byte ptr [ebp-0Dh],cl 
    00401D6F  movsx       edx,byte ptr [ebp-0Dh] 
    00401D73  mov         dword ptr [myGrade],edx
    Bleh. I figured out what it was doing after the setle dl line. Seems overly complex to me, but what do I know.
    00401D59 setle dl -> if my grade was less than 59 this will be 1
    00401D5C lea edx,[edx+edx+44h] -> this adds 1+1 to 'D' based on the above and stores it in edx, of which the low order byte is used next (dl)
    the rest of the lines round-robin the values up, unwinding the nested comparisons until the final result gets put in myGrade. To be honest I have absolutely no idea why this unwinding was neccesary. Seems like it would have been faster just to push the grade into eax or ebx or something and just leave it there. Guessing there's some way eax, ecx, or edx could have been reused in some optimized way in the equation.

    I hate myself for spending 2 extra hours at work figuring this out.

    edit: Turns out the original asm I posted didn't work quiiiiet right. This one works but isn't as nice.
    Will spend more time on it just because...

  3. #23
    Relic Shield
    Join Date
    Jul 2008
    Posts
    1,951
    BG Level
    6
    FFXIV Character
    Audrey Weaver
    FFXIV Server
    Behemoth
    FFXI Server
    Asura

    Now write an SSE optimized version for calculating grades of a batch of students in a class!

    go,go,go

  4. #24
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    updated with new question.

  5. #25
    Banned.

    Join Date
    Oct 2006
    Posts
    10,115
    BG Level
    9

    How do you differentiate strings?; that code you are using would turn into lowercase every letter, which may not be what you're being asked to do: 'Turn the first letter of every sentence into uppercase'.

  6. #26
    Murder machine with a motor in her nose
    Join Date
    Apr 2007
    Posts
    368
    BG Level
    4
    FFXI Server
    Carbuncle

    string x="blah";
    if(x.size())
    x.at(0)=toupper(x.at(0));

    ?

  7. #27
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    yea im trying to change position 0 of every string i pull from the input file to upper case. also how would i go about implementing it? Would i set the input ed string = to a new variable then run the variable threw the function, then output it from there?

  8. #28
    Relic Shield
    Join Date
    Jul 2008
    Posts
    1,951
    BG Level
    6
    FFXIV Character
    Audrey Weaver
    FFXIV Server
    Behemoth
    FFXI Server
    Asura

    What are strings in memory? What's the difference between an uppercase character and a lower case character?

    How would you turn 'a' into 'A'?
    What about 'b' into 'B'?

    Hint:

    http://www.cdrummond.qc.ca/cegep/inf...ges/ASCII1.GIF

    You can get to the contents of a string by calling c_str()
    Code:
    std::string cppString("Hello World!");
    char* cString;
    
    cString = cppString.c_str();

  9. #29
    Very Sexy Nerd
    Join Date
    Oct 2005
    Posts
    8,732
    BG Level
    8
    FFXI Server
    Carbuncle

    I like how the solutions to such simple and trivial problems have to be so stupidly complicated. <_<

  10. #30
    Murder machine with a motor in her nose
    Join Date
    Apr 2007
    Posts
    368
    BG Level
    4
    FFXI Server
    Carbuncle

    Code:
    getline (input,line);
    if(line.size())
    line.at(0)=toupper(line.at(0));
    cout << line << endl;
    outfile << line << endl;
    It's cute and all to do the conversion by subtracting 32 off the ascii value...but why reinvent the wheel? you've got toupper(), use it.

  11. #31
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    thank you snicky... that was exactly what i needed lol thank you for the basic anwser.

Page 2 of 2 FirstFirst 1 2