#include <iostream> #include <stdlib.h> #include <cstdlib> using namespace std; class Stack { private: static const int max = 10; int A[max]; int top; public: Stack() {top = -1;} bool isEmpty(); bool isFull(); int pop(); void push(int x); int display(); }; bool Stack::isEmpty() { if(top == -1) return true; return false; } bool Stack::isFull() { if(top == max - 1) return true; return false; } int Stack::pop() { if(isEmpty()) { cout<<"Stack is Empty\n"; system("read"); } int x = A[top]; top--; return x; } void Stack::push(int x){ if(isFull()) { cout<<"Stack is Full!\n"; system("read"); } top++; A[top] = x; } int Stack::display(){ int i; //for (i=0;i<10;i++){ // cout << A[i] << endl; //} for(i=top;i>=0;i--){ //cout << "Stack ["+i+"] = "; cout << A[i] << endl; } } int main() { int x=0; int choice; Stack s; return 0; }
Tuesday, August 16, 2016
C++ Program demonstrate the Stacks in C++
Saturday, August 13, 2016
TCP Server with netcat
import sys import socket import getopt import threading import subprocess listen = False command = False upload = False execute = "" target = "" upload_destination = "" port = 0 def usage(): print "BHP Net Tool" print print "Usage: bhpnet.py -t target_host -p port" print "-l --listen - listen on [host]:[port] for incoming connections" print "-e --execute=file_to_run - execute the given file upon receiving a connections" print "-c --command - initialize a command shell" print "-u --upload=destination - upon receiving connection upload a file and write to [destination]" print print print "Examples: " print "bhpnet.py -t 192.168.0.1 -p 5555 -l -c" print "bhpnet.py -t 192.168.0.1 -p 5555 -l -u=c:\\target.exe" print "bhpnet.py -t 192.169.0.1 -p 5555 -l -e=\"cat /etc/passwd\"" print "echo 'ABCDEFGHI' | ./bhpnet.py -t 192.168.11.12 -p 135" sys.exit(); def client_sender(buffer): client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: client.connect((target,port)) if len(buffer): client.send(buffer) while True: recv_len = 1 response = "" while recv_len: data = client.recv(4096) recv_len = len(data) response += data if recv_len < 4096: break print response, buffer = raw_input("") buffer += "\n" client.send(buffer) except: print "[*] Exception! Exiting." client.close() def server_loop(): global target if not len(target): target = "0.0.0.0" server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((target,port)) server.listen(5) while True: client_socket, addr = server.accept() client_thread = threading.Thread(target=client_handler, args = (client_socket,)) client_thread.start() def run_command(command): command = command.rstrip() try: output = subprocess.check_output(command,stdrr=subprocess.STDOUT, shell=True) except: output = "Failed to execute command.\r\n" return output def client_handler(client_socket): global upload global execute global command if len(upload_destination): file_buffer = "" while True: data = client_socket.recv(1024) if not data: break else: file_buffer += data try: file_descriptor = open(upload_destination,"wb") file_descriptor.write(file_buffer) file_descriptor.close() client_socket.send("Successfully saved file to %s\r\n" % upload_destination) except: client_socket.send("Failed to save file to %s\r\n" % upload_destination) if len(execute): output = run_command(execute) client_socket.send(output) if command: while True: client_socket.send("<BHP:#> ") cmd_buffer = "" while "\n" not in cmd_buffer: cmd_buffer += client_socket.recv(1024) response = run_command(cmd_buffer) client_socket.send(response) def main(): global listen global port global execute global command global upload_destination global target if not len(sys.argv[1:]): usage() try: opts, args = getopt.getopt(sys.argv[1:],"hle:t:p:cu:", ["help","listen","execute","target","port","command","upload"]) except getopt.GetoptError as err: print str(err) usage() for o,a in opts: if o in ("-h","--help"): usage() elif o in ("-l","--listen"): listen = True elif o in ("-e","--execute"): execute = a elif o in ("-c","--commandshell"): command = True elif o in ("-u","--upload"): upload_destination = a elif o in ("-t","--target"): target = a elif o in ("-p","--port"): port = int(a) else: assert False,"Unhandled Option" if not listen and len(target) and port > 0: buffer = sys.stdin.read() client_sender(buffer) if listen: server_loop() main()
Tuesday, August 9, 2016
C++ Assignment Questions and Answers
Question [5 Marks]
Consider the following program:
int x,y;
cin >> x >> y;
while(x!=0 && y!=0){
if(x>y){
x = x-y;
}else if(x<y){
y = y-x;
}else y=0;
}
cout << x+y << endl;
What will this program print on the following inputs:
a. 12 5 b. 27 39 c. -2 12 d. 23 0
Question [5 Marks]
Write a function that takes as input a positive integer n and returns the n-th harmonic number. Reminder: the n-th harmonic number is equal to 1+(1/2)+(1/3)+(1/4)+...+(1/n)
Question 3 [10 Marks]
Recall that the % operator calculates the integer remainder of a division (e.g. 8%3 is equal to 2). Consider the following code:
void hail(int n)
{
if(n<=1) return;
2
cout n << “ -> ” ;
if(n%2) hail(3*n+1);
else hail(n/2);
}
What will this function print for n=5? For n=17? For n=6?
Question 4 [10 Marks]
Write a function which will be given as input an array, its size and an integer p. The function will then cyclically shift the array p positions to the right: each element is moved p positions to the right, while the last p elements are moved to the beginning of the array. For example: if we have the array [ 1 2 3 4 5 6], shifting 2 positions to the right should give the array [ 5 6 1 2 3 4 ]. Your function should work correctly for negative values of p.
Question 5 [10 Marks]
Write a function which, given an array of integers, returns the integer that appears most frequently in the array. E.g. for the array [ 1 2 3 2 3 4 2 5 ] your function should return 2.
Question 6 [10 Marks]
Write a function that is given two positive integers r,c and allocates a 2-dimensional array with r rows and c columns. The function then returns a pointer to the allocated array.
Question 7 [10 Marks]
For the following exercises recall the definition of a basic linked list node:
struct Node { int data; struct Node* next; };
Write a function which, given a pointer to the first element of a linked list returns true if all elements of the list are unique. In other words, the function should return false if and only if there exist two nodes in the list with the same data.
Question 8 [10 Marks]
Write a function which will be given a pointer to the first node of a linked list and add to the list, for each element, a copy of it appearing immediately after it. For example: suppose we have a list with the following numbers 1,2,3,4. Calling the function should produce the list 1,1,2,2,3,3,4,4.
Answers
Consider the following program:
int x,y;
cin >> x >> y;
while(x!=0 && y!=0){
if(x>y){
x = x-y;
}else if(x<y){
y = y-x;
}else y=0;
}
cout << x+y << endl;
What will this program print on the following inputs:
a. 12 5 b. 27 39 c. -2 12 d. 23 0
Question [5 Marks]
Write a function that takes as input a positive integer n and returns the n-th harmonic number. Reminder: the n-th harmonic number is equal to 1+(1/2)+(1/3)+(1/4)+...+(1/n)
Question 3 [10 Marks]
Recall that the % operator calculates the integer remainder of a division (e.g. 8%3 is equal to 2). Consider the following code:
void hail(int n)
{
if(n<=1) return;
2
cout n << “ -> ” ;
if(n%2) hail(3*n+1);
else hail(n/2);
}
What will this function print for n=5? For n=17? For n=6?
Question 4 [10 Marks]
Write a function which will be given as input an array, its size and an integer p. The function will then cyclically shift the array p positions to the right: each element is moved p positions to the right, while the last p elements are moved to the beginning of the array. For example: if we have the array [ 1 2 3 4 5 6], shifting 2 positions to the right should give the array [ 5 6 1 2 3 4 ]. Your function should work correctly for negative values of p.
Question 5 [10 Marks]
Write a function which, given an array of integers, returns the integer that appears most frequently in the array. E.g. for the array [ 1 2 3 2 3 4 2 5 ] your function should return 2.
Question 6 [10 Marks]
Write a function that is given two positive integers r,c and allocates a 2-dimensional array with r rows and c columns. The function then returns a pointer to the allocated array.
Question 7 [10 Marks]
For the following exercises recall the definition of a basic linked list node:
struct Node { int data; struct Node* next; };
Write a function which, given a pointer to the first element of a linked list returns true if all elements of the list are unique. In other words, the function should return false if and only if there exist two nodes in the list with the same data.
Question 8 [10 Marks]
Write a function which will be given a pointer to the first node of a linked list and add to the list, for each element, a copy of it appearing immediately after it. For example: suppose we have a list with the following numbers 1,2,3,4. Calling the function should produce the list 1,1,2,2,3,3,4,4.
Answers
Question
1
a.
Input
= 12 5 output = 1
b.
Input
= 27 39 output = 3
c.
Input
= -2 12 output = Program will loop
forever
d.
Input
= 23 0 output = 23
Question
2
float n,sum=0;
cin
>> n;
while(n!=0){
sum += (1/n);
n-=1;
}
cout
<< sum;
Question
3
input: 5
5->16->8->4->2
input: 17
17->52->26->13->40->20->10->5->16->8->4->2
input: 6
6->3->10->5->16->8->4->2
Question
4
void rotate(int *A, int s, int p){
int
i;
int
temp[s];
for(i
=0; i<s; i++){
temp[i]
= A[i];
}
for(i
=0; i<s; i++){
A[((i+p)%s)]
= temp[i];
}
}
Question 5
int
modefinder(int *A){
int freq=0;
int top = 0;
int i,j,mode;
for(i=0; i<sizeof(A); i++){
for(j=0;
j<sizeof(A); j++){
if(A[i]==A[j]){
freq++;
}
}
if(freq>top){
mode
= A[i];
top
= freq;
}
freq = 0;
}
return mode;
}
Question 6
int **make2dary(int r,int c){
int
i;
int
**ary = new int *[r];
for
(i=0; i<r; i++){
ary[i]
= new int[c];
}
return
ary;
}
Question
7
bool linklistchk(struct Node *head){
while(head!=0){
struct
Node *tmp;
for(tmp
= head->next; tmp!=0; tmp=tmp->next)
if(tmp->data==head->data)
return
false;
head
= head->next;
}
return
true;
}
Question
8
void dup_eli(struct Node *head){
while(head!=0){
struct
Node *cpy = new Node;
cpy->data
= head->data;
cpy->next
= head->next;
head->next
= cpy;
head
= cpy->next;
}
}
Singly linked list in C++
#include <iostream> using namespace std; // Node class class Node { int data; Node* next; public: Node() {}; void SetData(int aData) { data = aData; }; void SetNext(Node* aNext) { next = aNext; }; int Data() { return data; }; Node* Next() { return next; }; }; // List class class List { Node *head; public: List() { head = NULL; }; void Print(); void Append(int data); void Delete(int data); }; /** * Print the contents of the list */ void List::Print() { // Temp pointer Node *tmp = head; // No nodes if ( tmp == NULL ) { cout << "EMPTY" << endl; return; } // One node in the list if ( tmp->Next() == NULL ) { cout << tmp->Data(); cout << " --> "; cout << "NULL" << endl; } else { // Parse and print the list do { cout << tmp->Data(); cout << " --> "; tmp = tmp->Next(); } while ( tmp != NULL ); cout << "NULL" << endl; } } /** * Append a node to the linked list */ void List::Append(int data) { // Create a new node Node* newNode = new Node(); newNode->SetData(data); newNode->SetNext(NULL); // Create a temp pointer Node *tmp = head; if ( tmp != NULL ) { // Nodes already present in the list // Parse to end of list while ( tmp->Next() != NULL ) { tmp = tmp->Next(); } // Point the last node to the new node tmp->SetNext(newNode); } else { // First node in the list head = newNode; } } /** * Delete a node from the list */ void List::Delete(int data) { // Create a temp pointer Node *tmp = head; // No nodes if ( tmp == NULL ) return; // Last node of the list if ( tmp->Next() == NULL ) { delete tmp; head = NULL; } else { // Parse thru the nodes Node *prev; do { if ( tmp->Data() == data ) break; prev = tmp; tmp = tmp->Next(); } while ( tmp != NULL ); // Adjust the pointers prev->SetNext(tmp->Next()); // Delete the current node delete tmp; } } int main() { // New list List list; // Append nodes to the list list.Append(100); list.Print(); list.Append(200); list.Print(); list.Append(300); list.Print(); list.Append(400); list.Print(); list.Append(500); list.Print(); // Delete nodes from the list list.Delete(400); list.Print(); list.Delete(300); list.Print(); list.Delete(200); list.Print(); list.Delete(500); list.Print(); list.Delete(100); list.Print(); }
output
100 --> NULL
100 --> 200 --> NULL
100 --> 200 --> 300 --> NULL
100 --> 200 --> 300 --> 400 --> NULL
100 --> 200 --> 300 --> 400 --> 500 --> NULL
100 --> 200 --> 300 --> 500 --> NULL
100 --> 200 --> 500 --> NULL
100 --> 500 --> NULL
100 --> NULL
EMPTY
Sunday, August 7, 2016
Simple C++ calculator program
Code:
#include <iostream> #include <stdlib.h> using namespace std; int add(int a, int b); int sub(int a, int b); int multi(int a, int b); int divi(int a, int b); int mod(int a, int b); int main(){ int a,b,choose; system("cls"); cout << "\t\t=================================\n"; cout << "\t\tWelcome to the simple calculator!\n"; cout << "\t\t=================================\n\n\n"; cout << "--------------------------------------------------------------\n"; cout << "-> Enter the first number\t: "; cin >> a; cout << "--------------------------------------------------------------\n"; cout << "-> Enter the second number\t: "; cin >> b; cout << "--------------------------------------------------------------\n"; system("cls"); cout << "--------------------------------------------------------------\n"; cout << "What do you want to do? \n"; cout << "--------------------------------------------------------------\n\n"; cout << "--------------------------------------------------------------"; cout << "1. Addition\n"; cout << "2. Subtraction\n"; cout << "3. Multiplication\n"; cout << "4. Divition\n"; cout << "5. Modulo\n"; cout << "--------------------------------------------------------------\n\n"; cout << "--------------------------------------------------------------\n"; cout << "Type the number of your choice: "; cin >> choose; cout << "--------------------------------------------------------------\n\n"; if (choose == 1){ cout << "--------------------------------------------------------------\n"; cout << "The output is: " << add(a,b) << endl; cout << "--------------------------------------------------------------\n"; } else if (choose == 2) { cout << "--------------------------------------------------------------\n"; cout << "The output is: " << sub(a,b) << endl; cout << "--------------------------------------------------------------\n"; } else if (choose == 3){ cout << "--------------------------------------------------------------\n"; cout << "The output is: " << multi(a,b) << endl; cout << "--------------------------------------------------------------\n"; } else if (choose == 4) { cout << "--------------------------------------------------------------\n"; cout << "The output is: " << divi(a,b) << endl; cout << "--------------------------------------------------------------\n"; } else if (choose == 5) { cout << "--------------------------------------------------------------\n"; cout << "The output is: " << mod(a,b) << endl; cout << "--------------------------------------------------------------\n"; } else { cout << "--------------------------------------------------------------\n"; cout << "Your input is incorrect, try again!\n"; cout << "--------------------------------------------------------------\n"; system("pause"); main(); } system("Pause"); } int add (int a, int b){ return a+b; } int sub (int a, int b){ return a-b; } int multi(int a, int b){ return a*b; } int divi(int a, int b){ return a/b; } int mod (int a, int b){ return a%b; }
Subscribe to:
Comments (Atom)