[LEFT][SIZE=3][SIZE=1][SIZE=3][FONT=Courier New][FONT=Courier New][SIZE=2][COLOR=black]
[COLOR=#186125]//**************************************
[/COLOR][COLOR=#186125]//
[/COLOR][COLOR=#186125]// Name: A reverse string function.
[/COLOR][COLOR=#186125]// Description:This is a simple function
[/COLOR][COLOR=#186125]// that I wrote to reverse a string. I'm a
[/COLOR][COLOR=#186125]// newbie to this language so please have a
[/COLOR][COLOR=#186125]// look and give feedback.
[/COLOR][COLOR=#186125]// By: Brutus
[/COLOR][COLOR=#186125]//
[/COLOR][COLOR=#186125]// Inputs:A string
[/COLOR][COLOR=#186125]//
[/COLOR][COLOR=#186125]// Returns:A reversed string
[/COLOR][COLOR=#186125]//
[/COLOR][COLOR=#186125]//This code is copyrighted and has[/COLOR][COLOR=#186125]// limited warranties.Please see http://
[/COLOR][COLOR=#186125]// [URL="http://www.planet-source-code.com/vb/scripts/Sh"]www.Planet-Source-Code.com/vb/scripts/Sh[/URL]
[/COLOR][COLOR=#186125]// owCode.asp?txtCodeId=2960&lngWId=3[/COLOR][COLOR=#186125]//for details.[/COLOR][COLOR=#186125]//**************************************
[/COLOR][COLOR=#186125]//
[/COLOR]
#include <iostream>
[COLOR=#000080]using[/COLOR] [COLOR=#000080]namespace[/COLOR] std;
[COLOR=#000080]int[/COLOR] main()
{
[COLOR=#000080]const[/COLOR] [COLOR=#000080]int[/COLOR] max = 80; //size of array
[COLOR=#000080]char[/COLOR] str[max]; //the array of characters
[COLOR=#000080]int[/COLOR] count = 0;
[COLOR=#000080]int[/COLOR] i = 0;
cout << "Enter a string:\n"; //Prompt user to enter a string
cin.getline(str,max,'\n'); //Read in the string
while(str[count] != '\0')
count++; //count the characters in the string, until a char 13 is found
for(i = count; i >=0; i--) /loop from length of string to 0
{
cout << str[i]; /Display the reversed string
}
[COLOR=#000080]return[/COLOR] 0;
}[/COLOR][/SIZE][/FONT][/FONT][/SIZE][/SIZE][/SIZE][/LEFT]
[SIZE=3][COLOR=DimGray][SIZE=2][COLOR=DarkOrchid]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=3][SIZE=1][SIZE=3][FONT=Courier New][FONT=Courier New][SIZE=2][COLOR=black][COLOR=#186125]//**************************************
[/COLOR][COLOR=#186125]//
[/COLOR][COLOR=#186125]// Name: ^A5*Calculator
[/COLOR][COLOR=#186125]// Description:A complete GUI scientific
[/COLOR][COLOR=#186125]// calculator for engineering applications.
[/COLOR][COLOR=#186125]//
[/COLOR][COLOR=#186125]// By: Wasim Haque
[/COLOR][COLOR=#186125]//
[/COLOR][COLOR=#186125]// Inputs:Inputs r made through mouse.
[/COLOR][COLOR=#186125]//
[/COLOR][COLOR=#186125]// Assumes:Angles r in radians
[/COLOR][COLOR=#186125]//
[/COLOR][COLOR=#186125]//This code is copyrighted and has[/COLOR][COLOR=#186125]// limited warranties.Please see http://
[/COLOR][COLOR=#186125]// www.Planet-Source-Code.com/vb/scripts/Sh
[/COLOR][COLOR=#186125]// owCode.asp?txtCodeId=5812&lngWId=3[/COLOR][COLOR=#186125]//for details.[/COLOR][COLOR=#186125]//**************************************
[/COLOR][COLOR=#186125]//
[/COLOR]
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
[COLOR=#000080]union[/COLOR] REGS i,o;
[COLOR=#000080]char[/COLOR] numc[]={'1','2','3','4','5','6','7','8','9','0','=','-','+','*','/'};
[COLOR=#000080]int[/COLOR] optr[]={48,61,45,43,42,246},sq[]={251,253,227};
[COLOR=#000080]int[/COLOR] button,xx,yy;
[COLOR=#000080]int[/COLOR] xco[15],k=-1,x=210;
[COLOR=#000080]int[/COLOR] xcor,ycor;
[COLOR=#000080]int[/COLOR] yco[15],num=0;
[COLOR=#000080]int[/COLOR] prevx,prevy;
[COLOR=#000080]int[/COLOR] xx1=210,yy1=328,up=0;//xx1=210,yy1=181;
[COLOR=#000080]int[/COLOR] numascii[]={49,50,51,52,53,54,55,56,57,48,61,45,43,42,246};
[COLOR=#000080]char[/COLOR] numstring[14];
[COLOR=#000080]void[/COLOR] main()
{
[COLOR=#000080]int[/COLOR] i,c=4,x1,y1=377,x2,n=0,k=0;//x=210;
[COLOR=#000080]int[/COLOR] gd=DETECT,gm;
[COLOR=#000080]int[/COLOR] key;
[COLOR=#186125]//struct stack exp;
[/COLOR] [COLOR=#000080]char[/COLOR] numcod[5];
initgraph(&gd,&gm," ");
[COLOR=#186125]//hidemouseptr();
[/COLOR] cleardevice();
setcolor(14);
settextstyle(4,HORIZ_DIR,4);outtextxy(185,7,"C a l c u l a t o r");
setlinestyle(0,0,3);
setcolor(8); settextstyle(0,HORIZ_DIR,0);
rectangle(200,60,440,345);
line(200,95,440,95); setfillstyle(1,4);setcolor(15);
bar(380,130,437,103);outtextxy(388,114,"On/Off");
setfillstyle(1,1);setcolor(14);
for(i=0;i<3;i++)
{
y1=y1-49;x1=210;
for(c=0;c<3;c++)
{
xco[num]=x1;yco[num]=y1;
num++;itoa(num,numcod,10);
bar(x1,y1,x1+12,y1+12);outtextxy(x1+4,y1+4,numcod);x1=x1+52;
}
}
y1=377;
for(i=0;i<3;i++)
{
y1=y1-49;x2=x1;
for(c=0;c<2;c++)
{
xco[num]=x2;yco[num]=y1;
bar(x2,y1,x2+12,y1+12);outtextxy(x2+4,y1+4,&optr[n]);n++;
x2=x2+52;num++;
}
}
setfillstyle(1,10);setcolor(6);
bar(290,207,340,220);outtextxy(297,210,"RESET");
bar(210,181,238,193);outtextxy(214,183,"exp");
bar(248,179,278,195);outtextxy(256,184,"y");outtextxy(264,180,"x");
bar(288,181,298,193);outtextxy(290,185,&sq[0]);
bar(308,179,334,195);outtextxy(316,184,"x");outtextxy(324,180,"2");
bar(344,179,374,195);outtextxy(360,185,"y");outtextxy(352,185,&sq[0]); outtextxy(348,180,"x");
bar(380,179,408,195);outtextxy(384,183,"1/x");
bar(416,181,430,193);outtextxy(417,183,"n!");
bar(210,140,228,152);outtextxy(213,144,"ln");
bar(238,138,268,156);outtextxy(246,143,"e");outtextxy(254,139,"x");
bar(276,140,300,152);outtextxy(277,143,"log");
bar(308,138,338,156);outtextxy(316,145,"10");outtextxy(329,138,"x");
bar(346,140,370,152);outtextxy(347,143,"Sin");
bar(378,140,402,152);outtextxy(379,143,"Cos");
bar(410,140,434,152);outtextxy(411,143,"Tan");
bar(210,99,250,112);outtextxy(213,103,"Tan");outtextxy(238,99,"-1");
bar(260,99,300,112);outtextxy(261,103,"Cos");outtextxy(286,99,"-1");
bar(308,99,348,112);outtextxy(309,103,"Sin");outtextxy(334,99,"-1");
bar(356,104,366,114);outtextxy(358,105,".");setfillstyle(1,7);
[COLOR=#186125]//bar(210,328,222,340);
[/COLOR] bar(203,63,437,93);
initmouse();
showmouseptr();
while(1)
{
mouse_ctrl();
}
}
calculate()
{
[COLOR=#000080]int[/COLOR] len=0,f1=0,f2=0,k,rp=0,st;
[COLOR=#000080]int[/COLOR] rest[4],nop=0;
[COLOR=#000080]double[/COLOR] res;
[COLOR=#000080]float[/COLOR] resd;
[COLOR=#000080]char[/COLOR] numstr1[7],numstr2[7],op,strres[14];
outtextxy(280,450,"calculating...");
for(rp=0;rp<strlen(numstr1);rp++)
{
numstr1[rp]='\0';
}
for(rp=0;rp<strlen(numstr2);rp++)
{
numstr2[rp]='\0';
}
numstring[strlen(numstring)]='\0';
len=strlen(numstring);puts(numstring);puts("hit a key");getch();
for(rp=0;rp<len;rp++)
{
if(numstring[rp]=='+' || numstring[rp]=='-' || numstring[rp]=='*' || numstring[rp]=='/')
{
op=numstring[rp];st=rp;
for(f2=rp+1;f2<len;f2++)
{
numstr2[f1]=numstring[f2];++f1;
}
}
}
for(rp=0;rp<st;rp++)
{
numstr1[rp]=numstring[rp];
}
numstr1[rp]='\0';numstr2[f1]='\0';
[COLOR=#186125]//puts(numstr1);puts(numstr2);printf("%c
[/COLOR] [COLOR=#186125]// ",op); getch();
[/COLOR] switch(op)
{
[COLOR=#000080]case[/COLOR] '+': res=atof(numstr1)+atof(numstr2);break;
[COLOR=#000080]case[/COLOR] '*': res=atof(numstr1)*atof(numstr2);break;
[COLOR=#000080]case[/COLOR] '/': res=atof(numstr1)/atof(numstr2);break;
[COLOR=#000080]case[/COLOR] '-': res=atof(numstr1)-atof(numstr2);break;
}
gcvt(res,6,strres); //puts(strres);
outtextxy(x+16,74,strres);x=x+16;
for(rp=0;rp<len;rp++)
{
numstring[rp]='\0';
}
for(rp=0;rp<strlen(strres);rp++)
{
numstring[rp]=strres[rp];
}
numstring[rp]='\0';
for(rp=0;rp<strlen(strres);rp++)
{
strres[rp]='\0';
}
for(rp=0;rp<strlen(numstr1);rp++)
{
numstr1[rp]='\0';
}
for(rp=0;rp<strlen(numstr2);rp++)
{
numstr2[rp]='\0';
}
k=strlen(numstring)-1;op='\0'; setfillstyle(1,7);//puts(numstring);
bar(203,63,437,93);x=210;outtextxy(x,72,numstring);x=x+8*(k);
return;
}
mouse_ctrl()
{
[COLOR=#000080]int[/COLOR] rp=0,sq,i=0;
[COLOR=#000080]double[/COLOR] sqf,ctof,xtof;
[COLOR=#000080]char[/COLOR] ntoc[5];
[COLOR=#000080]char[/COLOR] symb[1];
[COLOR=#000080]char[/COLOR] xstr[4];
[COLOR=#186125]//cleardevice();
[/COLOR] fflush(stdin);
initmouse();
showmouseptr();
outtextxy(280,450,"mouse activated...");
while(1)
{
getmousepos(&button,&xcor,&ycor);
if((button & 1)==1)
{
if(xcor>308 && xcor<334 && ycor>179 && ycor<195)
{
sqf=atof(numstring)*atof(numstring);
gcvt(sqf,5,ntoc);
[COLOR=#000080]goto[/COLOR] fun_call;
}
if(xcor>290 && xcor<340 && ycor>210 && ycor<220)
{
[COLOR=#186125]//reset(ntoc);
[/COLOR] gcvt(0.0,5,ntoc);goto fun_call;
}
if(xcor>380 && xcor<408 && ycor>179 && ycor<195)
{
ctof= atof(numstring);
sqf=1/ctof;gcvt(sqf,5,ntoc);goto fun_call;
}
if(xcor>210 && xcor<228 && ycor>140 && ycor<152)
{
ctof=log((atof(numstring)));
gcvt(ctof,5,ntoc);goto fun_call;
}
if(xcor>276 && xcor<300 && ycor>140 && ycor<152)
{
ctof=log(atof(numstring))/log(10);
gcvt(ctof,5,ntoc);goto fun_call;
}
if(xcor>288 && xcor<298 && ycor>181 && ycor<193)
{
ctof=sqrt(atof(numstring));
gcvt(ctof,5,ntoc);goto fun_call;
}
if(xcor>238 && xcor<268 && ycor>138 && ycor<156)
{
ctof=exp(atof(numstring));
gcvt(ctof,5,ntoc);goto fun_call;
}
if(xcor>416 && xcor<430 && ycor>181 && ycor<193)
{
ctof=atof(numstring);sqf=1.0;
for(rp=1;rp<=ctof;rp++)
{
sqf=sqf*rp;
}
gcvt(sqf,5,ntoc);goto fun_call;
}
if(xcor>308 && xcor<338 && ycor>138 && ycor<156)
{
ctof=pow(10,atof(numstring));
gcvt(ctof,5,ntoc);goto fun_call;
}
if(xcor>346 && xcor<370 && ycor>140 && ycor<152)
{
ctof=sin(atof(numstring));
gcvt(ctof,5,ntoc);goto fun_call;
}
if(xcor>378 && xcor<402 && ycor>140 && ycor<152)
{
ctof=cos(atof(numstring));
gcvt(ctof,5,ntoc);goto fun_call;
}
if(xcor>410 && xcor<434 && ycor>140 && ycor<152)
{
ctof=tan(atof(numstring));
gcvt(ctof,5,ntoc);goto fun_call;
}
if(xcor>210 && xcor<250 && ycor>99 && ycor<112)
{
ctof=atan(atof(numstring));
gcvt(ctof,6,ntoc);goto fun_call;
}
if(xcor>260 && xcor<300 && ycor>99 && ycor<112)
{
ctof=acos(atof(numstring));
gcvt(ctof,6,ntoc);goto fun_call;
}
if(xcor>308 && xcor<348 && ycor>99 && ycor<112)
{
ctof=asin(atof(numstring));
gcvt(ctof,6,ntoc);goto fun_call;
}
if(xcor>248 && xcor<278 && ycor>179 && ycor<195)
{
setfillstyle(1,7);bar(203,63,437,93);x=210;
xstr[i]=symb[0];outtextxy(x,72,xstr);++i;symb[0]='\0';puts(numstring);
getmousepos(&button,&xcor,&ycor);
if(xcor>418 && xcor<430 && ycor>328 && ycor<340)
{
ctof=atof(numstring);xtof=atof(xstr);puts(xstr);
sqf=pow(ctof,xtof); printf("\n%d",sqf);
gcvt(sqf,6,ntoc);goto fun_call;
}
[COLOR=#000080]else[/COLOR] return;
}
if(xcor>210 && xcor<222 && ycor>328 && ycor<340)
{
symb[0]='1';
[COLOR=#000080]goto[/COLOR] fun;
}
if(xcor>262 && xcor<274 && ycor>328 && ycor<340)
{
symb[0]='2';
[COLOR=#000080]goto[/COLOR] fun;
}
if(xcor>314 && xcor<326 && ycor>328 && ycor<340)
{
symb[0]='3';
[COLOR=#000080]goto[/COLOR] fun;
}
if(xcor>366 && xcor<378 && ycor>328 && ycor<340)
{
symb[0]='0';
[COLOR=#000080]goto[/COLOR] fun;
}
if(xcor>418 && xcor<430 && ycor>328 && ycor<340)
{
calculate();
}
if(xcor>210 && xcor<222 && ycor>279 && ycor<291)
{
symb[0]='4';
[COLOR=#000080]goto[/COLOR] fun;
}
if(xcor>262 && xcor<278 && ycor>279 && ycor<291)
{
symb[0]='5';
[COLOR=#000080]goto[/COLOR] fun;
}
if(xcor>314 && xcor<326 && ycor>279 && ycor<291)
{
symb[0]='6';
[COLOR=#000080]goto[/COLOR] fun;
}
if(xcor>366 && xcor<378 && ycor>279 && ycor<291)
{
symb[0]='-';
[COLOR=#000080]goto[/COLOR] fun;
}
if(xcor>418 && xcor<430 && ycor>279 && ycor<291)
{
symb[0]='+';
[COLOR=#000080]goto[/COLOR] fun;
}
if(xcor>210 && xcor<222 && ycor>230 && ycor<242)
{
symb[0]='7';
[COLOR=#000080]goto[/COLOR] fun;
}
if(xcor>262 && xcor<274 && ycor>230 && ycor<242)
{
symb[0]='8';
[COLOR=#000080]goto[/COLOR] fun;
}
if(xcor>314 && xcor<326 && ycor>230 && ycor<240)
{
symb[0]='9';
[COLOR=#000080]goto[/COLOR] fun;
}
if(xcor>366 && xcor<378 && ycor>230 && ycor<242)
{
symb[0]='*';
[COLOR=#000080]goto[/COLOR] fun;
}
if(xcor>418 && xcor<430 && ycor>230 && ycor<242)
{
symb[0]='/';
[COLOR=#000080]goto[/COLOR] fun;
}
if(xcor>356 && xcor<366 && ycor>104 && ycor<114)
{
symb[0]='.';
[COLOR=#000080]goto[/COLOR] fun;
}
if(xcor>380 && xcor<437 && ycor>103 && ycor<130)
{
closegraph();exit(1);
[COLOR=#186125]//main();
[/COLOR] }
}
}
fun_call:
rel_print(ntoc);
return;
fun:
store(symb);
return;
}
store(char sym[])
{
numstring[strlen(numstring)]=sym[0];
setfillstyle(1,7);setcolor(0);
k=strlen(numstring);
bar(203,63,437,93);x=210;outtextxy(x,72,numstring);x=x+8*(k-1);
sym[0]='\0';
return;
}
rel_print(char ntoc[])
{
[COLOR=#000080]int[/COLOR] rp=0;
for(rp=0;rp<strlen(numstring);rp++)
{
numstring[rp]='\0';
}
for(rp=0;rp<strlen(ntoc);rp++)
{
numstring[rp]=ntoc[rp];
}
numstring[rp]='\0';
numstring[rp+1]='\0';
for(rp=0;rp<strlen(ntoc);rp++)
{
ntoc[rp]='\0';
}
ntoc[rp+1]='\0';
setfillstyle(1,7);setcolor(0);
k=strlen(numstring);
bar(203,63,437,93);x=210;outtextxy(x,72,numstring);x=x+8*(k-1);
fflush(stdin);
return;
}
initmouse()
{
i.x.ax=0;
int86(0x33,&i,&o);return;
}
showmouseptr()
{
i.x.ax=1;
int86(0x33,&i,&o);return;
}
/*changecursor(int *cursor)
{
i.x.ax=9;
i.x.bx=0;
i.x.cx=0;
i.x.dx=(unsigned)cursor;
segread(&s);
s.es=s.ds;
int86x(0x33,&i,&i,&s);return;
}*/
getmousepos(int *button,int *x,int *y)
{
i.x.ax=3;
int86(0x33,&i,&o);
*button=o.x.bx;
*x=o.x.cx;
*y=o.x.dx;
return;
}
hidemouseptr()
{
i.x.ax=2;
int86(0x33,&i,&o);
return;
}
[/COLOR][/SIZE][/FONT][/FONT][/SIZE][/SIZE][/SIZE]
[SIZE=3][SIZE=1][SIZE=3][FONT=Courier New][FONT=Courier New][SIZE=2][COLOR=black][COLOR=#186125]//**************************************
[/COLOR][COLOR=#186125]//
[/COLOR][COLOR=#186125]// Name: A fast Computation of N! ( Fact
[/COLOR][COLOR=#186125]// orial) instead of recursion...
[/COLOR][COLOR=#186125]// Description:Quickly you can obtain yo
[/COLOR][COLOR=#186125]// ur factorial number, and of course this
[/COLOR][COLOR=#186125]// code explain the theory you can optimize
[/COLOR][COLOR=#186125]// more and more this.
[/COLOR][COLOR=#186125]// By: michele berardi
[/COLOR][COLOR=#186125]//
[/COLOR][COLOR=#186125]//This code is copyrighted and has[/COLOR][COLOR=#186125]// limited warranties.Please see http://
[/COLOR][COLOR=#186125]// www.Planet-Source-Code.com/vb/scripts/Sh
[/COLOR][COLOR=#186125]// owCode.asp?txtCodeId=4729&lngWId=3[/COLOR][COLOR=#186125]//for details.[/COLOR][COLOR=#186125]//**************************************
[/COLOR][COLOR=#186125]//
[/COLOR]
/*
Quick Factorial Computation
(you can use any types instead of long!)
(C) 2002 Berardi Michele
http://web.tiscali.it/mberardi
E-Mail(s): [email protected]
[email protected]
*/
#include <stdio.h>
[COLOR=#000080]void[/COLOR] main()
{
[COLOR=#000080]long[/COLOR] N , b , c , p ; // use [COLOR=#000080]int[/COLOR] [COLOR=#000080]for[/COLOR] fast calculation and small range of calculation..
// use [COLOR=#000080]double[/COLOR] [COLOR=#000080]for[/COLOR] factor bigger than 12!
Restart:
printf("Enter N!: ");
scanf("%d", &N);
if(N < 0)
{
printf("\t\n R.estart, I.nvalid P.arameter! (N > -1)\n");
[COLOR=#000080]goto[/COLOR] Restart;
}
[COLOR=#000080]else[/COLOR]
c = N - 1;
p = 1;
while(c>0)
{
p = 0;
b = c;
while(b>0) {
[COLOR=#000080]if[/COLOR] ( b & 1 )
{
p += N; // p = p + N;
}
// [COLOR=#000080]if[/COLOR] you would like to use [COLOR=#000080]double[/COLOR] choose the alternative forms instead shifts
// the code is fast even!
// you can use the same tips on [COLOR=#000080]double[/COLOR] or 64 bit [COLOR=#000080]int[/COLOR] etc.... but you must... ;-)
b >>=1; // b/=2; (b = b / 2;) ( b >> 1; a.s.r. is more efficent [COLOR=#000080]for[/COLOR] [COLOR=#000080]int[/COLOR] or long..!)
N <<=1; // N += N; N = N + N; N = N * 2; (N <<=1; a.s.l. is more efficent [COLOR=#000080]for[/COLOR] [COLOR=#000080]int[/COLOR] or long..!)
} // end of: while(b>0)
N = p;
c--; // c = c - 1;
} // end of: while(c > 0)
printf("[%d] is the factorial! \n", p);
}
[COLOR=#186125]//p.s.
[/COLOR] [COLOR=#186125]//using recursion is much slower!
[/COLOR] [COLOR=#186125]//for example:
[/COLOR] [COLOR=#186125]//
[/COLOR] [COLOR=#186125]//int fact(int);
[/COLOR] [COLOR=#186125]//void main()
[/COLOR]
[COLOR=#186125]//{
[/COLOR] [COLOR=#186125]// int N;
[/COLOR] [COLOR=#186125]// printf("Enter the number: ");
[/COLOR] [COLOR=#186125]// do
[/COLOR] [COLOR=#186125]// { scanf("%d",&N);}
[/COLOR] [COLOR=#186125]// while(N<0);
[/COLOR] [COLOR=#186125]// printf("\n %d", fact(N));
[/COLOR] [COLOR=#186125]// }
[/COLOR] [COLOR=#186125]// int fact(int N)
[/COLOR]
[COLOR=#186125]// {
[/COLOR] [COLOR=#186125]// int f;
[/COLOR] [COLOR=#186125]// if(N==0)f=1;
[/COLOR] [COLOR=#186125]// else f=N*(fact(N-1));
[/COLOR] [COLOR=#186125]// return f;
[/COLOR] [COLOR=#186125]// }
[/COLOR] [/COLOR][/SIZE][/FONT][/FONT][/SIZE][/SIZE][/SIZE]
[SIZE=3][SIZE=1][SIZE=3][FONT=Courier New][FONT=Courier New][SIZE=2][COLOR=black][COLOR=#186125]//**************************************
[/COLOR][COLOR=#186125]//
[/COLOR][COLOR=#186125]//INCLUDE files for :[ A Simple Text to
[/COLOR][COLOR=#186125]// Html File Converter ]
[/COLOR][COLOR=#186125]//**************************************
[/COLOR][COLOR=#186125]//
[/COLOR]#include <iostream>
#include <fstream>
#include <cassert>
#include <string>
[COLOR=#000080]using[/COLOR] std::fstream;
[COLOR=#000080]using[/COLOR] std::string;
[COLOR=#000080]using[/COLOR] std::ios;[/COLOR][/SIZE][/FONT][/FONT][/SIZE][/SIZE][/SIZE]
[SIZE=3][SIZE=1][SIZE=3][FONT=Courier New][FONT=Courier New][SIZE=2][COLOR=black][COLOR=#186125]//**************************************
[/COLOR][COLOR=#186125]//
[/COLOR][COLOR=#186125]//INCLUDE files for :A very simple Serve
[/COLOR][COLOR=#186125]// r/Client chat
[/COLOR][COLOR=#186125]//**************************************
[/COLOR][COLOR=#186125]//
[/COLOR]#include <windows.h>[/COLOR][/SIZE][/FONT][/FONT][/SIZE][/SIZE][/SIZE]
[SIZE=3][SIZE=1][SIZE=3][FONT=Courier New][FONT=Courier New][SIZE=2][COLOR=black][COLOR=#186125]//**************************************
[/COLOR][COLOR=#186125]//
[/COLOR][COLOR=#186125]// Name: Alternating Winsock Chat
[/COLOR][COLOR=#186125]// Description:This code is just a littl
[/COLOR][COLOR=#186125]// e beginning project of mine that I wrote
[/COLOR][COLOR=#186125]// when I first began learning to use winso
[/COLOR][COLOR=#186125]// ck. Because this was my first practical
[/COLOR][COLOR=#186125]// application of my winsock knowledge, it
[/COLOR][COLOR=#186125]// is not quite so sophisticated, and it is
[/COLOR][COLOR=#186125]// thusly an ALTERNATING chat program. . .
[/COLOR][COLOR=#186125]// sorry. . .anyway, even though I wrote th
[/COLOR][COLOR=#186125]// is code about 2 years ago, and it is use
[/COLOR][COLOR=#186125]// less to me now, I thought I would post i
[/COLOR][COLOR=#186125]// t for those who may wish to learn winsoc
[/COLOR][COLOR=#186125]// k and need a good example. I tried my be
[/COLOR][COLOR=#186125]// st to comment it, but I am really not us
[/COLOR][COLOR=#186125]// ed to using many comments in my codes, s
[/COLOR][COLOR=#186125]// o bear with me. If you need any explanat
[/COLOR][COLOR=#186125]// ions, mail me.
[/COLOR][COLOR=#186125]// By: Phillip Hopkins
[/COLOR][COLOR=#186125]//
[/COLOR][COLOR=#186125]// Assumes:you MUST add the "mpr.lib" an
[/COLOR][COLOR=#186125]// d "wsock32.lib" libraries to you project
[/COLOR][COLOR=#186125]// in order to compile this. If you need de
[/COLOR][COLOR=#186125]// tailed instructions on how to do this, E
[/COLOR][COLOR=#186125]// -mail me and I can help.
[/COLOR][COLOR=#186125]//
[/COLOR][COLOR=#186125]//This code is copyrighted and has[/COLOR][COLOR=#186125]// limited warranties.Please see http://
[/COLOR][COLOR=#186125]// www.Planet-Source-Code.com/vb/scripts/Sh
[/COLOR][COLOR=#186125]// owCode.asp?txtCodeId=5279&lngWId=3[/COLOR][COLOR=#186125]//for details.[/COLOR][COLOR=#186125]//**************************************
[/COLOR][COLOR=#186125]//
[/COLOR]
[COLOR=#186125]// Created by Phillip D. Hopkins
[/COLOR][COLOR=#186125]// of Philware Inc.
[/COLOR][COLOR=#186125]// sites
[/COLOR][COLOR=#186125]// www.philwareinc.com (soon to be launc
[/COLOR][COLOR=#186125]// hed)
[/COLOR][COLOR=#186125]// www.philwareinc.20m.com (already up)
[/COLOR]#include <iostream.h>
#include <winsock.h>
[COLOR=#000080]void[/COLOR] ClearScreen(); //A VERY cheap and fake "Clear Screen" function
[COLOR=#000080]int[/COLOR] DisplayMenu() //A simple function to display the menu and get the response
{
[COLOR=#000080]int[/COLOR] choice;
beginning:
ClearScreen();
cout << endl << endl << endl << endl << endl << endl << endl << "Philware WINSOCK Test V1.0" << endl;
cout << "Choose the correct Connection Type:" << endl;
cout << "1. Server" << endl;
cout << "2. Client" << endl;
cout << "3. Exit" << endl;
cout << "Choice: ";
cin >> choice;
[COLOR=#000080]if[/COLOR] (choice != 1 && choice != 2 && choice != 3)
{
cout << "That is not one of the choices." << endl;
[COLOR=#000080]goto[/COLOR] beginning;
}
[COLOR=#000080]return[/COLOR] choice;
}
[COLOR=#000080]int[/COLOR] main()
{
WSADATA WsaDat; //Declares what is necessary to use winsock
[COLOR=#000080]int[/COLOR] menuchoice; //Gets the integer from the "DisplayMenu()" function
[COLOR=#000080]if[/COLOR] (WSAStartup(MAKEWORD(1,1), &WsaDat) != 0) //Initiallizes the WSA
{
cout << "WSA initialization failed.";
cout << "Closing. . .";
Sleep(MAKEWORD(10,30));
[COLOR=#000080]goto[/COLOR] end;
}
SOCKET mysocket; //Our Handy Dandy Socket
mysocket = socket(AF_INET, SOCK_STREAM, 0); //Sets the properties of our socket
//Here, it is set to a STREAM Socket
[COLOR=#000080]if[/COLOR] (mysocket == INVALID_SOCKET) //Checks to see if it created correctly
{
cout << "Socket creation failed!";
cout << "Closing. . .";
Sleep(MAKEWORD(10,30));
[COLOR=#000080]goto[/COLOR] end;
}
[COLOR=#000080]int[/COLOR] ip1, ip2, ip3, ip4, yesno; //Holders [COLOR=#000080]for[/COLOR] IP address, and a
//"yesno" var [COLOR=#000080]for[/COLOR] confirmation later
reipadd: //Yah, I know it is bad, but I did use a nasty "goto" command
cout << "\nChoose your IP address:\n"; //It has you enter the server's
cout << "first numeral: "; //IP address one numeral at a time
cin >> ip1;
cout << "second numeral:";
cin >> ip2;
cout << "third numeral: ";
cin >> ip3;
cout << "fourth numeral:";
cin >> ip4;
cout << "\nIs [COLOR=#000080]this[/COLOR] your IP address? (1=yes, 2=no)\n" << ip1 << "." << ip2 << "." << ip3 << "." << ip4 << endl;
cin >> yesno; //this just shows the ip and ask [COLOR=#000080]for[/COLOR] verification
[COLOR=#000080]if[/COLOR] (yesno!=1)
{
[COLOR=#000080]goto[/COLOR] reipadd; //here is that nasty "goto" command. . .
}
SOCKADDR_IN SockAddr; //this keeps track of the socket's address info
SockAddr.sin_port = 50;
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.S_un.S_un_b.s_b1 = ip1;
SockAddr.sin_addr.S_un.S_un_b.s_b2 = ip2;
SockAddr.sin_addr.S_un.S_un_b.s_b3 = ip3;
SockAddr.sin_addr.S_un.S_un_b.s_b4 = ip4;
[COLOR=#000080]char[/COLOR] myscreenname[20], theirscreenname[20]; // self-explanatory :)
rescreenname: //another reference to that "goto" command
//lets you enter a screen name
cout << endl << "Type in a one-word \"Screen Name\" less than 20 letters" << endl;
cin.ignore(80, '\n');
cin.get(myscreenname, 20);
cin.ignore(80, '\n');
//checks to see [COLOR=#000080]if[/COLOR] you typed it right
cout << "Your screen name is :" << myscreenname << endl << "Do you want [COLOR=#000080]this[/COLOR] screen name?(1=yes 2=no)" << endl;
cin >> yesno;
[COLOR=#000080]if[/COLOR] (yesno!=1)
{
[COLOR=#000080]goto[/COLOR] rescreenname; //yup, [COLOR=#000080]this[/COLOR] will have you retype the screen name
}
menuchoice=DisplayMenu(); //shows the menu and gets a choice (defined at top of code)
[COLOR=#000080]if[/COLOR] (menuchoice==1) // if you chose to be a server. . .
{
// bind to IP address. . .
[COLOR=#000080]if[/COLOR] (bind(mysocket, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)) == SOCKET_ERROR)
{
cout << "Attempt to bind failed" << endl;
cout << "Closing. . .";
Sleep(MAKEWORD(10,30));
[COLOR=#000080]goto[/COLOR] end;
}
cout << "Waiting [COLOR=#000080]for[/COLOR] client. . ." << endl;
//go into "Listen" mode. . .
listen(mysocket, 1);
//when request received to connect, accept it
SOCKET TempSock = SOCKET_ERROR;
[COLOR=#000080]while[/COLOR] (TempSock == SOCKET_ERROR)
{
TempSock = accept(mysocket, NULL, NULL);
}
mysocket = TempSock;
//send your screen name to the client
cout << "Client Found. Sending screen name. . ." << endl;
send(mysocket, myscreenname, sizeof(myscreenname), 0);
cout << "Screen name sent successfully." << endl;
cout << "Retrieving screen name from client. . ." << endl;
//wait to receive screen name from client
[COLOR=#000080]int[/COLOR] WaitforScreenName = SOCKET_ERROR;
[COLOR=#000080]while[/COLOR] (WaitforScreenName == SOCKET_ERROR)
{
WaitforScreenName = recv(mysocket, theirscreenname, 20, 0);
[COLOR=#000080]if[/COLOR] ((WaitforScreenName == 0)||(WaitforScreenName == WSAECONNRESET))
{
cout << "Connection closed at the other end [COLOR=#000080]while[/COLOR] receiving screen name! RATS!" << endl;
cout << "Closing. . .";
Sleep(MAKEWORD(10,30));
[COLOR=#000080]goto[/COLOR] end;
}
cout << "Screen Name Acquired. You are talking with: " << theirscreenname << endl;
}
[COLOR=#000080]char[/COLOR] toclient[50], String2[50];
[COLOR=#000080]int[/COLOR] RetVal = SOCKET_ERROR;
send1: //type and send message to client
cout << myscreenname << ": ";
cin.ignore(80, '\n');
cin.get(toclient, 50);
cin.ignore(80, '\n');
send(mysocket, toclient, 50, 0);
[COLOR=#186125]// cout << "MESSAGE SENT" <<
[/COLOR] [COLOR=#186125]// ; endl << "Waiting for reply. . ."
[/COLOR] [COLOR=#186125]// << endl;
[/COLOR] //wait [COLOR=#000080]for[/COLOR] the client to send a message. . .
[COLOR=#000080]while[/COLOR] (RetVal == SOCKET_ERROR)
{
RetVal = recv(mysocket, String2, 50, 0);
[COLOR=#000080]if[/COLOR] ((RetVal == 0)||(RetVal == WSAECONNRESET))
{
cout << "Connection closed at the other end. . .Sorry. . ." << endl;
cout << "Closing. . .";
Sleep(MAKEWORD(10,30));
[COLOR=#000080]goto[/COLOR] end;
}
//display message
[COLOR=#186125]// cout << "Message Received:" &
[/COLOR] [COLOR=#186125]// lt;< endl;
[/COLOR] cout << theirscreenname << ": " << String2 << endl;
RetVal = SOCKET_ERROR;
[COLOR=#000080]goto[/COLOR] send1; // loop and send another message
}
[COLOR=#186125]// cout << "\nThis message was se
[/COLOR] [COLOR=#186125]// nt:" << endl;
[/COLOR] [COLOR=#186125]// cout << "THIS IS FROM THE SERV
[/COLOR] [COLOR=#186125]// ER, TEST SUCCESSFUL";
[/COLOR] [COLOR=#186125]// int pause;
[/COLOR] [COLOR=#186125]// cin >> pause;
[/COLOR] }
[COLOR=#000080]else[/COLOR] [COLOR=#000080]if[/COLOR] (menuchoice==2)
{
//attempt to connect to server. . .
cout << "Trying to connect to a server. . ." << endl;
[COLOR=#000080]if[/COLOR] (connect(mysocket, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)) == SOCKET_ERROR)
{
cout << "failed to establish connection with server" << endl;
cout << "Closing. . .";
Sleep(MAKEWORD(10,30));
[COLOR=#000080]goto[/COLOR] end;
}
//wait to receive screen name
cout << "Server Found! Waiting to receive Screen Name. . ." << endl;
[COLOR=#000080]int[/COLOR] WaitforScreenName = SOCKET_ERROR;
[COLOR=#000080]while[/COLOR] (WaitforScreenName == SOCKET_ERROR)
{
WaitforScreenName = recv(mysocket, theirscreenname, 20, 0);
[COLOR=#000080]if[/COLOR] ((WaitforScreenName == 0)||(WaitforScreenName == WSAECONNRESET))
{
cout << "Connection closed at the other end [COLOR=#000080]while[/COLOR] receiving screen name!" << endl;
cout << "Closing. . .";
Sleep(MAKEWORD(10,30));
[COLOR=#000080]goto[/COLOR] end;
}
cout << "Screen Name Acquired. You are talking with: " << theirscreenname << endl;
//send screen name to server
cout << "Sending your screen name. . ." << endl;
send(mysocket, myscreenname, sizeof(myscreenname), 0);
cout << "Screen Name Sent!" << endl;
//wait [COLOR=#000080]for[/COLOR] message from server
cout << "Waiting [COLOR=#000080]for[/COLOR] first Message from " << theirscreenname << ". . ." << endl;
}
[COLOR=#000080]int[/COLOR] RetVal = SOCKET_ERROR;
[COLOR=#000080]char[/COLOR] String[50], toserver[50];
send2:
[COLOR=#000080]while[/COLOR] (RetVal == SOCKET_ERROR)
{
RetVal = recv(mysocket, String, 50, 0);
[COLOR=#000080]if[/COLOR] ((RetVal == 0)||(RetVal == WSAECONNRESET))
{
cout << "Connection closed at the other end" << endl;
cout << "Closing. . .";
Sleep(MAKEWORD(10,30));
[COLOR=#000080]goto[/COLOR] end;
}
[COLOR=#186125]// cout << "Message Received:" &
[/COLOR] [COLOR=#186125]// lt;< endl;
[/COLOR] cout << theirscreenname << ": " << String << endl;
[COLOR=#186125]// int pause1;
[/COLOR] [COLOR=#186125]// cout << "\nThis message was r
[/COLOR] [COLOR=#186125]// eceived:" << endl;
[/COLOR] [COLOR=#186125]// cout << String;
[/COLOR] [COLOR=#186125]// cin >> pause1;
[/COLOR] }
//type and send message to server
cout << myscreenname << ": ";
cin.ignore(80, '\n');
cin.get(toserver, 50);
cin.ignore(80, '\n');
[COLOR=#186125]// cout << "Sending message. . ."
[/COLOR] [COLOR=#186125]// << endl;
[/COLOR] send(mysocket, toserver, 50, 0);
[COLOR=#186125]// cout << "Message Sent. Waiting
[/COLOR] [COLOR=#186125]// for reply from " << theirscreennam
[/COLOR] [COLOR=#186125]// e << ". . ." << endl;
[/COLOR] RetVal = SOCKET_ERROR;
[COLOR=#000080]goto[/COLOR] send2; //loop back and wait [COLOR=#000080]for[/COLOR] another message
}
[COLOR=#000080]else[/COLOR] [COLOR=#000080]if[/COLOR] (menuchoice==3)
{
//close the program
cout << "Closing. . .";
Sleep(MAKEWORD(10,30));
[COLOR=#000080]goto[/COLOR] end;
}
end:
[COLOR=#000080]return[/COLOR] 0;
}
[COLOR=#000080]void[/COLOR] ClearScreen() //Here be the cheesy "Screen Clear" function
{
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
[/COLOR][/SIZE][/FONT][/FONT][/SIZE][/SIZE][/SIZE]