Tuesday, April 17, 2012

my answer for test2 Q3

not exactly correct, just give the idea

#include "tqueue.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[]){
    Tqueue<int> Q;
    if(argv[1][0]== '-' && argv[1][1] == 'a'){
        int i;
        for(i=0;i<argc-2;i++){
            if(sscanf(argv[i],"%d")
                Q.append(argv[i]);
            else{
                count<<"Incorrect call use as follows:"<<endl;
                count<<"tq -[a][d] 99  [99 99 ...]"<<endl;
                break;
            }
            Q.sort(true);
            do{
                cout<<Q.visit()<<" ";
            }while(Q.goNext());
    }
    else if(argv[1][0]== '-' && argv[1][1] == 'd'){
        int i;
        for(i=0;i<argc-2;i++){
            if(sscanf(argv[i],"%d")
                Q.append(argv[i]);
            else{
                count<<"Incorrect call use as follows:"<<endl;
                count<<"tq -[a][d] 99  [99 99 ...]"<<endl;
                break;
            }
            Q.sort(true);
            do{
                cout<<Q.visit()<<" ";
            }while(Q.goPrev());
    }
    return 0;
}

Wednesday, March 14, 2012

Using cout to print binary

#include <cstdio>
#include<iostream>
using namespace std;

char* bits(int val){
    const int m = sizeof(int)*8;
    static char str[m];
    int i;
    int j;
    for(i = m-1, j = 0; i>=0;i--,j++){
        if(val & (1<<i))
            str[j] = '1';
        else
            str[j] = '0';
    }
    return str;
}

int main () {
    int i = 2345;    
    cout << " i: " << bits(i) << endl; 
    return 0;
}

Monday, March 12, 2012

my setOn and setOff

void setOn(unsigned int& val, int bitNo){
    retuen val = val | (1 << bitNo);
}

void setOff(unsigned int& val, int bitNo){
    retuen val = val & ~(1 << bitNo);

}

Sunday, February 12, 2012

week 5 wiki answer

int validMonth(int mon, char* errmes){
  int res = 0;
  if(mon > 0 && mon <=12){
    res  = 1;
  }
  else{
    strcpy(errmes, "Invalid month (1<=month<=12)");
  }
  return res;
}
write the above function in one line:

int validMonth(int mon, char* errmes){
  return yada yada;
}
 
answer:
return((mon>0 && mon <=12) || !strcpy(errmes, "Invalid month!"));

Tuesday, January 31, 2012

problem with test 4.17

Can anyone help me to get through test 4.17?

- console.edit() was supposed to return Unknown Keycode,  Dec: 0   Hex: 0
  but instead it returned: ENTER,  Dec: 10   Hex: A

What does it mean? Anyone had a some problem and fixed it already?

Tuesday, September 27, 2011

Where is the problem?

I can't do anything after the window showed.
After i pressed keys(No matter what the key) twice, window closed.
Anyone can help me?
Here is my test code..

#include "keys.h"
#include "console.h"
using namespace cio;

int main(){
  bool insertMode = true;
  int strOffset = 10;
  int curPosition = 5;
  char str[80] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  str[20] = 0;
  Console C;
  C.display("****************************", 9, 9, 20);
  //C.display("*****************", 10, 9, 20);
  C.display("****************************", 11, 9, 20);
  C.edit(str, 10, 10, 15, 79, &insertMode, &strOffset, &curPosition);
  //C.display(str, 15, 0);
  C.pause();
  return 0;
}

Monday, April 18, 2011

Shape up notes for Feb 17

const: happen in running
#define: happen at complie time, before program running

const int i = ______ ->have to write a value

const char *p; ---> const
char* const q = name; ---->const, and q points name.

int& foo(int& R)  RENAME, foo(i) = i;