Xin's Blog
Sunday, March 31, 2013
setBit and copyBit
void setBit(unsigned int& V, int bitNo, bool val){
if(val){
V=(1<<(bitNo-1))|V;
}
else{
V=(1<<(bitNo-1))&V;
}
}
void copyBits(unsigned int& V, int bitFrom, int length, unsigned int mask){
int m=1<<(bitFrom-1);
for(int i=(bitFrom);i<bitFrom+length;i++){
setBit(V,i,!!(mask&m));
m=m<<1;
}
}
Monday, March 18, 2013
Add method of the Queue
queue.h:#ifndef __XL_QUEUE_H__#define __XL_QUEUE_H__class Queue;class Node{int _data;Node* _next;Node(int data, Node* next=(Node*)0);friend class Queue;};class Queue{Node* _head;public:Queue();void add(int data);int remove();bool isEmpty();virtual ~Queue();};#endifqueue.cpp:#include "queue.h"Node::Node(int data, Node* next){_data = data;_next = next;}Queue::Queue(){_head = (Node*)0;}void Queue::add(int data){Node* toAdd = _head;Node* new = new Node(data);while(toAdd->_next){last=last->_next;}tail->_next = new;}int Queue::remove(){int ret = _head->_data;Node* toDel = _head;_head = _head->_next;delete toDel;return ret;}bool Queue::isEmpty(){return true;}Queue::~Queue(){while(!isEmpty()){remove();}}
Sunday, February 3, 2013
Xmover and display()
Xmover:
case DOWN:
if((row == console.getRows()-2) && (col == console.getCols()-1)){
console.alarm();
}
else if(row < console.getRows()-1){
row++;
}
else{
console.alarm();
}
break;
case RIGHT:
if( (col == console.getCols()-2) && (row == console.getRows()-1)){
console.alarm();
}
else if(col < console.getCols()-1){
col++;
}
else{
console.alarm();
}
break;
case ESCAPE:
done = true;
break;
}
display():
void Console::display(const char* str, int row, int col, int fieldLen){
console.setPos(row, col);
for(int i = 0;(fieldLen == 0)?(str[i]!=0):((str[i]!=0)?(str[i]!=0 && i<fieldLen):(i<fieldLen));(str[i]!=0)?(putChar(str[i]),i++):(putChar(' '),i++));
}
case DOWN:
if((row == console.getRows()-2) && (col == console.getCols()-1)){
console.alarm();
}
else if(row < console.getRows()-1){
row++;
}
else{
console.alarm();
}
break;
case RIGHT:
if( (col == console.getCols()-2) && (row == console.getRows()-1)){
console.alarm();
}
else if(col < console.getCols()-1){
col++;
}
else{
console.alarm();
}
break;
case ESCAPE:
done = true;
break;
}
display():
void Console::display(const char* str, int row, int col, int fieldLen){
console.setPos(row, col);
for(int i = 0;(fieldLen == 0)?(str[i]!=0):((str[i]!=0)?(str[i]!=0 && i<fieldLen):(i<fieldLen));(str[i]!=0)?(putChar(str[i]),i++):(putChar(' '),i++));
}
Saturday, January 19, 2013
argc and argv[] simple programs
Now we can make some simple programs using argc and argv[].
1: ADD, num1 + num2. If we type in C:\path\Debug\ADD 2 3 at the command line, the output will be 5. If you input more than two numbers an error will be displayed. Let's just assume argv[1] and argv[2] are both integers.
#include <iostream>
using namespace std;
int main(int argc, char* argv[]){
if(argc != 3)
cout << "Format is not correct. Please enter: ADD num1 num2" << endl;
else{
cout << "ADD: num1 + num2 = " << atoi(argv[1]) + atoi(argv[2]) << endl;
}
return 0;
}
2. SUB, num1 - num2.
#include <iostream>
using namespace std;
int main(int argc, char* argv[]){
if(argc != 3)
cout << "Format is not correct. Please enter: SUB num1 num2" << endl;
else{
cout << "SUBTRACT: num1 - num2 = " << atoi(argv[1]) - atoi(argv[2]) << endl;
}
return 0;
}
3. MUL, num1 * num2.
#include <iostream>
using namespace std;
int main(int argc, char* argv[]){
if(argc != 3)
cout << "Format is not correct. Please enter: MUL num1 num2" << endl;
else{
cout << "MULTIPLY: num1 * num2 = " << atoi(argv[1]) * atoi(argv[2]) << endl;
}
return 0;
}
4. DIV, num1 \ num2. num2 cannot be 0, or an error will be displayed
#include <iostream>
using namespace std;
int main(int argc, char* argv[]){
if(argc != 3)
cout << "Format is not correct. Please enter: DIV num1 num2" << endl;
else{
if (atoi(argv[2]) == 0)
cout << "error: num2 cannot be 0" << endl;
else
cout << "DIV: num1 / num2 = " << atoi(argv[1]) / atoi(argv[2]) << endl;
}
return 0;
}
1: ADD, num1 + num2. If we type in C:\path\Debug\ADD 2 3 at the command line, the output will be 5. If you input more than two numbers an error will be displayed. Let's just assume argv[1] and argv[2] are both integers.
#include <iostream>
using namespace std;
int main(int argc, char* argv[]){
if(argc != 3)
cout << "Format is not correct. Please enter: ADD num1 num2" << endl;
else{
cout << "ADD: num1 + num2 = " << atoi(argv[1]) + atoi(argv[2]) << endl;
}
return 0;
}
2. SUB, num1 - num2.
#include <iostream>
using namespace std;
int main(int argc, char* argv[]){
if(argc != 3)
cout << "Format is not correct. Please enter: SUB num1 num2" << endl;
else{
cout << "SUBTRACT: num1 - num2 = " << atoi(argv[1]) - atoi(argv[2]) << endl;
}
return 0;
}
3. MUL, num1 * num2.
#include <iostream>
using namespace std;
int main(int argc, char* argv[]){
if(argc != 3)
cout << "Format is not correct. Please enter: MUL num1 num2" << endl;
else{
cout << "MULTIPLY: num1 * num2 = " << atoi(argv[1]) * atoi(argv[2]) << endl;
}
return 0;
}
4. DIV, num1 \ num2. num2 cannot be 0, or an error will be displayed
#include <iostream>
using namespace std;
int main(int argc, char* argv[]){
if(argc != 3)
cout << "Format is not correct. Please enter: DIV num1 num2" << endl;
else{
if (atoi(argv[2]) == 0)
cout << "error: num2 cannot be 0" << endl;
else
cout << "DIV: num1 / num2 = " << atoi(argv[1]) / atoi(argv[2]) << endl;
}
return 0;
}
Friday, January 18, 2013
Basic examples of using argc and argv[]
#include <iostream>
using namespace std;
int main(int argc, char* argv[]){
cout << "argc :" << argc << endl;
for(int i=0;i<argc;i++){
cout << "argv[" << i << "]: " << argv[i] << endl;
}
return 0;
}
I'm using Visual Studio 2012. After build solution (Ctrl+Shift+b), you will find a debug folder under you project folder. Find out the path of the application you just built, then you will be able to run it using windows cmd.
At the command line, type C:\path\Debug\application.exe one 2 three, the output will be:
argc: 4
argv[0]: C:\path\Debug\application.exe
argv[1]: one
argv[2]: 2
argv[3]: three
argc: 4
argv[0]: C:\path\Debug\application.exe
argv[1]: one
argv[2]: 2
argv[3]: three
Subscribe to:
Posts (Atom)