for(j=0;j
{
if(x[j].key>x[j+1].key)
{
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
}
}
}
//补充一个改进的冒泡算法:
void BubbleSort(elemtype x[],int n)
{
Int i,j;
BOOLexchange; //记录交换标志
for(i=1;i //最多做n-1趟排序
{
Exchange = false;
For(j=n-1;j>=i;--j)
{
If(x[j]>x[j+1])
{
x[0] =x[j];
X[j] =x[j+1];
X[j+1]= x[0];
Exchange = true; //发生了交换,设置标志为真.
}
}
if (!Exchange) //为发生替换,提前终止算法
return;
}
}
6. c语言 文件读写
#include "stdio.h"
main()
{
FILE *fp;
char ch,filename[10];
scanf("%s",filename);
if((fp=fopen(filename,"w")==NULL)
{
printf("cann't openfilen");
exit(0);
}
ch=getchar();
while(ch!='#')
{
fputc(ch,fp);
putchar(ch);
ch=getchar();
}
fclose(fp);
}
7. winsocket编程 //这个不错
// 服务器代码
#include
#include
void main()
{
WORD wVersionRequested; //版本号
WSADATA wsaData; //数据
int err;
wVersionRequested = MAKEWORD(1,1);
err =WSAStartup(wVersionRequested,&wsaData);
if( err != 0)
{
return;
}
if(LOBYTE( wsaData.wVersion ) != 1||
HIBYTE( wsaData.wVersion) != 1)
{
WSACleanup();
return;
}
SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0); //建立套接字
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR)); //绑定端口
listen(sockSrv,5); //转换socket套接子为侦听套接子
SOCKADDR_IN addrClient;
int len=sizeof(SOCKADDR);
while(1) //无限循环
{
SOCKETsockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);
char sendBuf[100];
sprint(sendBuf,"Welcome %s to ",
inet_ntoa(addrClient.sin_addr));
send(sockConn,sendBuf,strlen(sendBuf)+1,0);
char recvBuf[100];
recv(sockConn,recvBuf);
printf("%sn",recvBuf);
closesocket(sockConn);
WSACleanup();
}
}
注:这是Server端;File->New->Win32ConsoleApplication,工程名:TcpSrv;然后,File->New->C++Source File,文件名:TcpSrv;在该工程的Setting的Link的Object/librarymodules项要加入ws2_32.lib
#include
#include
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(1,1);
err = WSAStartup(wVersionRequested,&wsaData);//启动winsock Dll
if( err != 0)
{
return;
}
if(LOBYTE( wsaData.wVersion ) != 1||
HIBYTE( wsaData.wVersion) != 1)
{
WSACleanup();
return;
}
SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
addrSrv.sin_family=AF_INET;
addrSrv.sin_port = htons(6000);
connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
char recvBuf[100];
recv(sockClient,recvBuf,100,0);
printf("%sn",recvBuf);
send(sockClient,"This iszhangsan",strlen("This is zhangsan")+1,0);
closesocket(sockClient);
WSACleanup();
}
注:这是Client端;File->New->Win32ConsoleApplication,工程名:TcpClient;然后,File->New->C++Source File,文件名:TcpClient;同理,在该工程的Setting的Link的Object/librarymodules项要加入ws2_32.lib
8. 类的知识 (非常不错的一道题目)..
C++
#include
class human
{
public:
human(){ human_num++;};//默认构造函数
static inthuman_num; //静态成员
~human()
{
human_num--;
print();
}
voidprint() //
{
cout<<"human num is:"<
}
protected:
private:
};
int human::human_num =0; //类中静态数据成员在外部定义,仅定义一次
human f1(human x)
{
x.print();
return x;
}
int main(int argc, char* argv[])
{
human h1;//调用默认构造函数,human_num变为1
h1.print(); //打印Human_man:1
human h2 = f1(h1);//先调用函数f1(),输出human_num:1,而后输出human_num为0,
h2.print();//打印输出:human_num:0
return 0;
} //依次调用两个析构函数:输出:human_num:-1,human_num:-2;
输出:
1
1
0
0
-1
-2
----------------------------
分析:
human h1; //调用构造函数,---hum_num = 1;
h1.print(); //输出:"human is 1"
human h2 = f1(h1); //再调用f1(h1)的过程中,由于函数参数是按值传递对象,调用默认的复制构造函数,h2并没有调用定义的构造函数.
阅读(851) | 评论(0) | 转发(1) |