控制台程序源碼
1. 用C#寫一個類似cmd的控制台程序,實現ping的功能!!!(求完整程序源代碼!)
using System;
using System.Net;
using System.Net.NetworkInformation;
class Program
{
static void Main(string[] args)
{
int count = 4;
IPAddress addr = IPAddress.Parse("220.181.111.86");
Ping ping = new Ping();
long timeSum = 0;
int succCount = 0;
//發送
Console.WriteLine("正在ping {0}", addr);
for (int i = 0; i < count; ++i)
{
PingReply pr = ping.Send(addr);
if (pr.Status == IPStatus.TimedOut)
Console.WriteLine("超時");
else if (pr.Status == IPStatus.Success)
{
Console.WriteLine("延時:{0}毫秒", pr.RoundtripTime);
++succCount;
timeSum += pr.RoundtripTime;
}
}
if (timeSum != 0)
Console.WriteLine("平均延時{0}毫秒,丟包率{1}%", 1.0 * timeSum / succCount, (count - succCount) * 100.0 / count);
else
Console.WriteLine("丟包率100%");
return;
}
}
2. 控制台C或者C++程序,列印出自身源代碼
#include <iostream>
#include <fstream>
#define FILENAME "example.cpp" // 這里指定文件名
using namespace std;
int main()
{
ifstream ifs(FILENAME);
char data[1024];
while (!ifs.eof())
{
ifs >> data;
cout << data;
}
ifs.close();
return 0;
}
3. 求用控制台應用程序(不使用MFC)C++語言編寫的坦克大戰游戲源代碼 . 本人新手 寫了一個
C++實例編程:簡單坦克大戰2010-11-2212:40:22作者:佚名來源:IT哇咔搜集整理我要評論進入論壇字型大小:T|T核心提示:因為有些東西以前練過了,而有些東西沒練過,還有很多不足之處.#include#include#include#include#include#defineKEY_ESC0x01#defineKEY_SPACE0x39#defineKEY_UP0x48#defineKEY_LEFT0x4b#defineKEY_RIGHT0x4d#defineKEY_DOWN0x50/*1石頭,2磚塊,3水,5老家,8玩家,9敵人*/intmap[20][20]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,2,2,2,2,0,0,2,2,2,2,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,2,0,0,2,0,1,1,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,2,2,2,2,2,2,2,0,0,0,0,0,0,0,2,2,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,0,1,1,0,1,1,1,1,3,3,3,3,0,0,0,0,0,0,0,2,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,3,3,3,0,1,1,0,0,0,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,3,3,3,1,1,1,1,1,1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,2,2,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,1,1,0,2,2,0,0,0,0,2,2,2,0,0,0,2,2,0,0,0,1,1,0,0,0,0,0,0,8,2,5,2,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};strUCtf{intx;inty;intdirection;};structplay{intx;/*行坐標*/inty;/*列坐標*/intdirection;/*方向*/structffire[5];/*5顆子彈*/intscore;/*分數*/}Playone;structa{intx;inty;intcolor;intdirection;intdirectiontwo;/*用來判斷走的路是否往返*/intfireplay;/*是否攻擊的變數,隨機生成*/structffire;}amy[5];/*敵人的結構體,其實可以添加不同的顏色來表示不同種類的坦克*/charkey_state[128],key_pressed[128];voidInit();/*圖象驅動開始*/voidEnd();/*圖象驅動結束*/voidDrawMap();/*畫地圖*/voidDrawWater(intx,inty);/*畫水*/voidDrawBrick(intx,inty);/*畫磚*/voidDrawTone(intx,inty);/*畫石頭*/voidDrawHome(intx,inty);/*畫老家*/voidDrawBlack(intx,inty);/*去除內容*/voidDrawPlay(intx,inty);/*畫玩家*/voidDrawAmy(intx,inty,inti);/*畫敵人*/voidScore();/*輸出分數*/voidGamePlay();/*玩游戲過程*/voidGameOver();/*游戲失敗*/voidTimeDelay(unsignedlongmicrosec);/*延時函數傳入微秒數*/intGetKey(intScanCode);/*這里開始都是按鍵函數*/voidinterruptfar(*OldInt9Handler)();voidfarinterruptNewInt9();voidInstallKeyboard();voidShutDownKeyboard();voidmain(void){Init();DrawMap();GamePlay();End();}voidTimeDelay(unsignedlongmicrosec)/*延時函數傳入微秒數*/{unionREGSr;r.h.ah=0x86;r.x.cx=microsec>>16;r.x.dx=microsec;int86(0x15,&r,&r);}voidInit()/*圖象驅動開始*/{intgd=DETECT,gm;initgraph(&gd,&gm,"d:\\tc\\tc");cleardevice();InstallKeyboard();}voidEnd()/*圖象驅動結束*/{ShutDownKeyboard();closegraph();}voidDrawTone(intx,inty)/*畫石頭*/{setfillstyle(SOLID_FILL,7);bar(100+x*20-9,50+y*20-9,100+x*20+9,50+y*20+9);}voidDrawWater(intx,inty)/*畫水*/{setfillstyle(SOLID_FILL,BLUE);bar(100+x*20-9,50+y*20-9,100+x*20+9,50+y*20+9);}voidDrawBrick(intx,inty)/*畫磚*/{setfillstyle(SOLID_FILL,6);bar(100+x*20-9,50+y*20-9,100+x*20+9,50+y*20+9);setcolor(15);line(100+x*20-9,50+y*20-4,100+x*20+9,50+y*20-4);line(100+x*20-9,50+y*20+4,100+x*20+9,50+y*20+4);line(100+x*20-4,50+y*20-9,100+x*20-4,50+y*20+9);line(100+x*20+4,50+y*20-9,100+x*20+4,50+y*20+9);}voidDrawHome(intx,inty)/*畫老家*/{setcolor(0);setfillstyle(SOLID_FILL,GREEN);fillellipse(100+x*20,50+y*20,9,9);}voidDrawBlack(intx,inty)/*去除內容*/{setcolor(0);setfillstyle(SOLID_FILL,0);bar(100+x*20-9,50+y*20-9,100+x*20+9,50+y*20+9);}voidDrawPlay(intx,inty)/*畫玩家*/{setcolor(4);/*玩家為紅色*/circle(100+x*20,50+y*20,7);switch(Playone.direction)/*判斷玩家方向*/{case1:line(100+x*20,50+y*20,100+x*20,50+y*20-9);break;/*上*/case2:line(100+x*20,50+y*20,100+x*20+9,50+y*20);break;/*右*/case3:line(100+x*20,50+y*20,100+x*20,50+y*20+9);break;/*下*/case4:line(100+x*20,50+y*20,100+x*20-9,50+y*20);break;/*左*/}}voidDrawAmy(intx,inty,inti)/*畫敵人*/{if(amy[i].color==12)setcolor(12);elseif(amy[i].color==13)setcolor(13);else/*這里是判斷三種顏色的坦克*/setcolor(14);circle(100+x*20,50+y*20,7);switch(amy[i].direction)/*判斷玩家方向*/{case1:line(100+x*20,50+y*20,100+x*20,50+y*20-9);break;/*上*/case2:line(100+x*20,50+y*20,100+x*20+9,50+y*20);break;/*右*/case3:line(100+x*20,50+y*20,100+x*20,50+y*20+9);break;/*下*/case4:line(100+x*20,50+y*20,100+x*20-9,50+y*20);break;/*左*/}}voidScore()/*輸出分數*/{chars[10];Playone.score+=10;sprintf(s,"%d",Playone.score);setfillstyle(SOLID_FILL,0);bar(550,100,640,130);settextstyle(0,0,2);setcolor(YELLOW);outtextxy(550,115,s);}voidDrawMap()/*畫地圖*/{inti,j,k;for(i=0;i<20;i++){for(j=0;j<20;j++)if(map[i][j]==1)DrawTone(j,i);elseif(map[i][j]==2)DrawBrick(j,i);elseif(map[i][j]==3)DrawWater(j,i);elseif(map[i][j]==5)DrawHome(j,i);elseif(map[i][j]==8){Playone.x=i;Playone.y=j;Playone.direction=1;DrawPlay(j,i);for(k=0;k<5;k++)Playone.fire[k].direction=-1;/*5顆子彈的方向都為-1,表示不存在*/}elseif(map[i][j]==9){amy[0].x=1;amy[0].y=1;amy[0].direction=amy[0].directiontwo=3;/*第一個敵人*/--