好友
阅读权限 10
听众
最后登录 1970-1-1
布莱克
发表于 2017-3-16 13:54
本帖最后由 布莱克 于 2017-4-8 19:41 编辑
c语言自动关机小程序,挺好玩的,和大家分享一下, ,楼主小白会每天更新新学到的有意思的小程序
代码如下:
#include<stdlib.h>
#include<stdio.h>
main()
{
system("title 背景");
system("color 4F");
system("shutdown /s");
getchar();
system("shutdown /a");
}
今天楼主小白又学会了定时关机功能,现在把代码发给大家共享!
#include<windows.h>
#include<stdio.h>
void main()
{
char cmd[20]="shutdown -s -t ";
char t[5]="0",g[10];
printf("-现在系统时间是:\n");
system("TIME /T");
printf(" -请选择定时长(秒):\n");
scanf("%s",t);
system(strcat(cmd,t));
printf(" -如果你不想关机的话请输入不关机:\n");
scanf("%s",&g);
if(strcmp(g,"不关机")==0)
system("shutdown /a");
getchar();
}
今天楼主花了一下午时间构思了狼人杀代码,其中链表部分是最近所学的,用来储存数据,但是这程序较复杂的我觉得在人物的身份不能重复上,为此,楼主对程序再三调试,终于黄天不负苦心人,成功啦,我也自学到了如何使赋值不能重复这一方法呢,所以来和大家分享一下啦,狼人杀只是一例,我觉得还可以做个彩票号码分析器啥的,哈哈!其中代码在附件里,欢迎下载!代码行少一行pEnd->pNext=pNew;猜猜放在哪里合适(提示在create函数里)
#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
struct players
{
char name[10];
char role[10];
struct players* pNext;
};
struct players* create()
{
struct players* pHead=NULL;
struct players* pNew,*pEnd;
int i=1;
pNew=pEnd=(struct players*)malloc(sizeof(struct players));
printf("请输入第%d位玩家姓名:",i);
scanf("%s",&pNew->name);
while(i!=9)
{
if(i==1)
{
pNew->pNext=NULL;
pEnd=pNew;
pHead=pNew;
}
else
{
pNew->pNext=NULL;
pEnd->pNext=pNew;
pEnd=pNew;
}
pNew=(struct players*)malloc(sizeof(struct players));
i++;
printf("请输入第%d位玩家姓名:",i);
scanf("%s",&pNew->name);
}
pNew->pNext=NULL;
return pHead;
}
void print(struct players* pHead)
{
struct players* pTemp=pHead;
int i;
while(pTemp!=NULL)
{
printf("%s:%s\n",pTemp->name,pTemp->role);
pTemp=pTemp->pNext;
}
}
struct players* change(struct players* pHead)
{
int i=0,a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,j=0,n=0,m,r=1;
struct players* pTemp=pHead;
srand((unsigned)time(NULL));
while(i<8)
{
m=rand()%9;
n++;
switch(m)
{
case 0:if(a==0)
{
a++;i++;
strcpy(pTemp->role,"预言家");
} break;
case 1:
if(b==0)
{
b++;i++;
strcpy(pTemp->role,"女巫");
} break;
case 2:
if(c==0)
{
c++;i++;
strcpy(pTemp->role,"猎人");
} break;
case 3:
if(d==0)
{
d++;i++;
strcpy(pTemp->role,"狼人");
} break;
case 4:
if(e==0)
{
e++;i++;
strcpy(pTemp->role,"狼人");
} break;
case 5:
if(f==0)
{
f++;i++;
strcpy(pTemp->role,"狼人");
} break;
case 6:
if(g==0)
{
g++;i++;
strcpy(pTemp->role,"村民");
} break;
case 7:
if(h==0)
{
h++;i++;
strcpy(pTemp->role,"村民");
} break;
case 8:
if(j==0)
{
j++;i++;
strcpy(pTemp->role,"村民");
} break;
}
if(r==i)
pTemp=pTemp->pNext;
r=i+1;
}
return pHead;
}
int main()
{
char a[10];
struct players* pHead;
system("title 黑仔背景");
system("color 4F");
one:
pHead=create();
pHead=change(pHead);
print(pHead);
printf("如果想继续的话,请输入【继续】,否则输入【退出】\n");
scanf("%s",&a);
if(strcmp(a,"继续")==0)
goto one;
}
楼主看完位运算啦,暂时的理解是在储存单元中再次划分区域进行储存,希望理解错的话大家不吝赐教,其中位运算的^(异或)还是挺有意思的,比如x=x^y;y=y^x;x=x^y;就实现了x和y值的交换,x,y,x^y三个数的数据我的理解是有两个1,就必有一个0,若有两个零,则全为0,z这样重复进行异或位运算时就实现了数值的传递与交换,我觉得这样理解挺方便的。
接下来分享给大家楼主新学的网络套接字代码,利用这个程序可以实现局域网内客户端和服务器端的互动,先贴出服务器端的代码:
#include<stdio.h>
#include<winsock.h>
int main()
{
char Sendbuf[100];
char Receivebuf[100];
int SendLen;
int ReceiveLen;
int Length;
SOCKET socket_server;
SOCKET socket_receive;
SOCKADDR_IN Server_add;
SOCKADDR_IN Client_add;
WORD wVersionRequested;
WSADATA wsaData;
int error;
wVersionRequested=MAKEWORD(2,2);
error=WSAStartup(wVersionRequested,&wsaData);
if(error!=0)
{
printf("加载套接字失败!");
return 0;
}
if(LOBYTE(wsaData.wVersion)!=2||HIBYTE(wsaData.wVersion)!=2)
{
WSACleanup();
return 0;
}
Server_add.sin_family=AF_INET;
Server_add.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
Server_add.sin_port=htons(5000);
socket_server=socket(AF_INET,SOCK_STREAM,0);
if(bind(socket_server,(SOCKADDR*)&Server_add,sizeof(SOCKADDR))==SOCKET_ERROR)
{
printf("绑定失败!");
}
if(listen(socket_server,5)<0)
{
printf("失败!\n");
}
Length=sizeof(SOCKADDR);
socket_receive=accept(socket_server,(SOCKADDR*)&Client_add,&Length);
if(socket_receive==SOCKET_ERROR)
{
printf("连接失败!");
}
while(1)
{
ReceiveLen=recv(socket_receive,Receivebuf,100,0);
if(ReceiveLen<0)
{
printf("连接失败!\n");
printf("程序退出!\n");
break;
}
else
{
printf("client say:%s\n",Receivebuf);
}
printf("please enter message:");
scanf("%s",Sendbuf);
SendLen=send(socket_receive,Sendbuf,100,0);
if(SendLen<0)
{
printf("发送失败!\n");
}
}
closesocket(socket_receive);
closesocket(socket_server);
WSACleanup();
return 0;
}
然后给出客户端的代码,运行时先运行服务器端再运行客户端
#include<stdio.h>
#include<winsock.h>
int main()
{
char Sendbuf[100];
char Receivebuf[100];
int SendLen;
int ReceiveLen;
SOCKET socket_send;
SOCKADDR_IN Server_add;
WORD wVersionRequested;
WSADATA wsaData;
int error;
wVersionRequested=MAKEWORD(2,2);
error=WSAStartup(wVersionRequested,&wsaData);
if(error!=0)
{
printf("加载套接字失败!\n");
return 0;
}
if(LOBYTE(wsaData.wVersion)!=2||HIBYTE(wsaData.wVersion)!=2)
{
WSACleanup();
return 0;
}
Server_add.sin_family=AF_INET;
Server_add.sin_addr.S_un.S_addr=inet_addr("192.168.1.110");
Server_add.sin_port=htons(5000);
socket_send=socket(AF_INET,SOCK_STREAM,0);
if(connect(socket_send,(SOCKADDR*)&Server_add,sizeof(SOCKADDR))==SOCKET_ERROR)
{
printf("连接失败!\n");
}
send(socket_send,"客户端连接成功!",100,0);
while(1)
{
printf("please enter message:");
scanf("%s",Sendbuf);
SendLen=send(socket_send,Sendbuf,100,0);
if(SendLen<0)
{
printf("发送失败!\n");
}
ReceiveLen=recv(socket_send,Receivebuf,100,0);
if(ReceiveLen<0)
{
printf("接收失败!\n");
printf("程序退出!\n");
break;
}
else
{
printf("Server say:%s\n",Receivebuf);
}
}
closesocket(socket_send);
WSACleanup();
return 0;
}
注意其中的物理网络地址与局域网有关,每次会变动,可以用cmd命令提示符然后输入arp -a查看。还有就是上面的代码在编译后,要在设置中添加一个库文件ws2_32.lib进入方法(楼主用的是vc++6.0)project->设置->连接,在库模块中输入,切记!否则编译不会通过!
自动关机.zip
33.6 KB, 下载次数: 32, 下载积分: 吾爱币 -1 CB
压缩后为.exe文件
吾爱定时关机.zip
37.38 KB, 下载次数: 12, 下载积分: 吾爱币 -1 CB
免费评分
查看全部评分