本文目錄一覽:
誰有c語言一些運行起來結果很好玩的程序的代碼..
/*太大了估計你不想要,給你一個掃雷程序,turbo C下才能編譯成功 空格:打標記 回車:掃雷 方向鍵:改變方向 */ #includestdio.h #includeconio.h #includetime.h void adjust(int*,int*); int a[23][23],c[22][22],mm,nn; int roundmine(int,int); void spread(); main() { int i,j,b[22][22],minenum,mine; int x,y,yy,s; char g,h,z; int*x1; int*y1; time_t t; again:srand((unsigned) time(t)); textcolor(LIGHTGREEN); for(i=2;i=21;i++) for(j=2;j=21;j++) { gotoxy(i,j); putch(219); b[i][j]=1; c[i][j]=1; } minenum=0; for(i=1;i=22;i++) for(j=1;j=22;j++) { a[i][j]=0; if(i1i22j1j22) {if((s=rand()%10)0) a[i][j]=0; else if(s==0) a[i][j]=1; } if(a[i][j]) minenum++; } gotoxy(50,2); puts(“dirction”); gotoxy(50,3); puts(“SPACE:mark”); gotoxy(50,4); puts(“ENTER:dig”); gotoxy(50,8); printf(“mine num:%4d”,minenum); gotoxy(50,5); printf(“ESC:exit”); gotoxy(30,15); puts(” “); gotoxy(30,20); puts(” “); mine=minenum; gotoxy(2,2); x=2;y=2; while(mine) { h=getch(); if(h==’H’) gotoxy(x,–y); else if(h==’P’) gotoxy(x,++y); else if(h==’K’) gotoxy(–x,y); else if(h==’M’) gotoxy(++x,y); x1=x;y1=y; adjust(x1,y1); if(h==’ ‘) { if(c[wherex()][wherey()]) if(b[wherex()][wherey()]) { textcolor(LIGHTRED); putch(16); mine–; gotoxy(59,8); printf(“%4d”,mine); gotoxy(x,y); b[wherex()][wherey()]=0; } else { textcolor(LIGHTGREEN); putch(219); mine++; gotoxy(59,8); printf(“%4d”,mine); gotoxy(x,y); b[wherex()][wherey()]=1; } } if(h==’\015′) { mm=wherex();nn=wherey(); c[wherex()][wherey()]=0; if(a[wherex()][wherey()]) { for(i=2;i=21;i++) for(j=2;j=21;j++) {gotoxy(i,j); if(a[i][j]) {textcolor(LIGHTRED); putch(017); } } break; } else {spread();gotoxy(mm,nn);} } if(h==’\033′) exit(0); } yy=1; for(i=2;i=21;i++) for(j=2;j=21;j++) if (a[i][j]==1b[i][j]==1) {gotoxy(30,15); cprintf(“You lose!!”); yy=0; } if(yy) {gotoxy(30,15); cprintf(“You win!!”); } gotoxy(30,20); cprintf(“Once again(y/n)?”); z=getch(); if(z==’y’) goto again; } void adjust(int* x,int* y) { if(*x2) gotoxy(*x+=20,*y); else if(*x21) gotoxy(*x-=20,*y); else if(*y2) gotoxy(*x,*y+=20); else if(*y21) gotoxy(*x,*y-=20); } int roundmine(int x,int y) { int i,j,r=0; for(i=x-1;i=x+1;i++) for(j=y-1;j=y+1;j++) if(a[i][j]) r++; return r; } void spread() { int r,i,j,x,y; x=wherex(); y=wherey(); r=roundmine(x,y); if(r) {printf(“%d”,r);c[x][y]=0;} else if(r==0x1x22y1y22) { putchar(‘ ‘);c[x][y]=0; for(i=x-1;i=x+1;i++) for(j=y-1;j=y+1;j++) if(x1x22y1y22c[i][j]) {gotoxy(i,j); spread(); } } }
圖 (c語言)
我來做,等等
/*編寫無向圖的鄰接矩陣類AdjMWGraph,實現無向圖的廣度遍歷和深度遍歷。
其中,圖中頂點數據類型為字元。Input第一行圖中頂點的個數n(4=n=26)
第二行是圖中邊的條數m(3=m=351) 第三行是頂點信息(全為大寫字母)
後面的輸入數據是依附於一條邊的兩個頂點,有多少條邊,就輸入多少組信息。
注意:根結點都為A;並且所給字元連續,也就是說A B C D ……。
Output廣度優先搜索序列。 深度優先搜索序列。
Sample Input
7
6
A B C D E F G
A B
A C
B D
B E
C F
C G
Sample Output
A B C D E F G
A B D E C F G
*/
#include stdio.h#include stdlib.h
unsigned char arrMap[26][26] = {0};
int nV = 0;
void deep_prior_scan(int j);
void spread_prior_scan(int j);
void GetResult()
{
int nL = 0;//存儲邊數
int i;
char arrV[26] = {0};
char temp, pairV[2];
//輸入過程
printf(“\n請輸入頂點數:”);
scanf(“%d”, nV);
printf(“\n請輸入邊數:”);
scanf(“%d”, nL);
printf(“\n請依次輸入頂點:\n”);
for (i = 0, temp = ‘A’; i nV; )
{
temp = getchar();
if (temp = ‘A’ temp = ‘Z’)
{
arrV[i++] = temp;
}
}
printf(“\n請依次輸入邊:\n”);
for (i = 0; i nL*2; )
{
temp = getchar();
if (temp = ‘A’ temp = ‘Z’)
{
pairV[i%2] = temp;
if (i%2 == 1)
{
arrMap[pairV[0] – ‘A’][pairV[1] – ‘A’] = 1;
}
++i;
}
}
//printf(“\n廣度優先遍歷:\n”);
//spread_prior_scan(0);
//printf(“\n”);
printf(“\n深度優先遍歷:\n”);
deep_prior_scan(0);
printf(“\n”);
}
int main()
{
GetResult();
system(“pause”);
return 0;
}
void deep_prior_scan(int j)
{
int i;
printf(“%c “, j + ‘A’);
for (i = 0; i nV; i++)
{
if (arrMap[j][i] != 0)
{
arrMap[j][i] = 0;
deep_prior_scan(i);
}
}
}
void spread_prior_scan(int j)
{
int i;
if (j == 0)
{
printf(“\nA “);
}
if (j = 26)
{
printf(“\nj=%d\n”, j);
}
for (i = 0; i nV; i++)
{
if (arrMap[j][i] != 0)
{
printf(“%c “, i + ‘A’);
}
}
for (i = 0; i nV; i++)
{
if (arrMap[j][i] != 0)
{
arrMap[j][i] = 0;
spread_prior_scan(i);
}
}
}
因為懶得複製數組,所以兩種遍歷方法要分開運行,你可以通過打開和關閉對相應函數的注釋來得到兩次的結果
c語言 求程序
具體什麼公式不懂….
#include math.h
#include stdio.h
#include stdlib.h
#define KE 200
main()
{
float ex[KE],hy[KE],dx[KE],ix[KE];
float ga[KE],gb[KE];
float ex_low_m1=0, ex_low_m2=0, ex_high_m1=0, ex_high_m2=0;
int n,k,kc,ke,NSTEPS, kstart;
float ddx,dt,epsz,epsilon,sigma,eaf,freq_in,pi,arg;
float T,t0,spread,pulse;
FILE *fp;
// FILE *fopen(); 原程序有這個,錯誤,應刪掉
/*printf(“input freq(MHz):”);
scanf(“%f”,freq);
freq=freq*1e6;*/
//初始化常量
ddx = 0.01;
dt = ddx/(2*3e8);
kc=KE/2;
pi=3.14159;
epsz=8.85419e-12;
/*initialze to free space*/
for(k=0;kKE;k++) //初始化所有輸出空間
//提示輸入非傳導性
printf(“dielectric starta at –“);
scanf(“%d”,kstart);
//提示輸入epsilon
printf(“epsilon –“);
scanf(“%f”,epsilon);
//提示輸入傳導性
printf(“conductivity –“);
scanf(“%f”,sigma);
printf(“Kstart,epsilon,sigma:%d,%6.2f,%6.2f \n”,kstart,epsilon,sigma);
/* eaf=dt*sigma/(2*epsz*epsilon);
printf(“eaf:%6.4f \n”,eaf); */
//根據公式填充ga,gb
for(k=kstart;k=KE;k++)
{
ga[k]=1./(1.+epsilon+sigma*dt/epsz);
gb[k]=sigma*dt/epsz;
}
//輸出ga,gb
for(k=0;k=KE;k++)
//初始化所有空間為0
for(k=0;kKE;k++)
{
ex[k]=0; hy[k]=0;dx[k]=0;ix[k]=0;
}
/*these parametres specify the input pulse*/
//初始化參數
t0=50.0;
spread =20.0;
T=0;
NSTEPS=1;
//輸入正數就一直計算,否則退出
while(NSTEPS0)
{
printf(“NSTEPS—“);
scanf(“%d”,NSTEPS);
printf(“%d\n”,NSTEPS);
//這裡如果輸入非正數仍會執行 如果需要退出,添加如下代碼
// if (NSTEPS = 0) break;
// NSTEPS次循環
for(n=1;n=NSTEPS;n++)
{
T=T+1;
/*main fdtd loop*/
/*calculate the dx field*/
//根據公式填充dx
for(k=0;kKE;k++)
{
dx[k]=dx[k]+0.5*(hy[k-1]-hy[k]);
}
/* put gaussian pulse at the low end */
freq_in=3e8;
pulse=exp(-0.5*(pow( (t0-T)/spread,2.0)) );
/*pulse=sin(2*pi*freq*dt*T);*/
dx[kc]=dx[kc]+pulse;
//輸出
printf(“n=%d,T=%d,pulse=%6.2f\n”,n,T,pulse);
printf(“n=%d,dx[kc]=%6.2f\n”,n,dx[kc]);
/*ex field*/
//根據公式填充ex, ix
for(k=1;kKE-1;k++)
{
ex[k]=ga[k]*(dx[k]-ix[k]);
ix[k]=ix[k]+gb[k]*ex[k];
}
/* absorbing boundary conditions*/
ex[0]= ex_low_m2;
ex_low_m2= ex_low_m1;
ex_low_m1=ex[1];
ex[KE-1]= ex_high_m2;
ex_high_m2= ex_high_m1;
ex_high_m1=ex[KE-2];
//根據公式填充hy
for(k=1;kKE-1;k++)
}
/* end of main fdtd loop*/
/*print the ex and hy fields*/
for(k=1;k=KE;k++)
printf(“%3d %6.2f %6.2f\n”,k,ex[k],hy[k]);
/*print the ex and hy fields to files*/
//輸出ex到文件ex2.1
fp=fopen(“ex2.1″,”w”);
for(k=1;k=KE;k++)
fprintf(fp,” %6.2f\n”,ex[k]);
fclose(fp);
//輸出hy到文件hy2.1
fp=fopen(“hy2.1″,”w”);
for(k=1;k=KE;k++)
fprintf(fp,” %6.2f\n”,hy[k]);
fclose(fp);
printf(“T=%5.0f\n”,T);
}
}
原創文章,作者:LZQX,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/144749.html