c語言-簡單小程序-簡單算法_第1頁
c語言-簡單小程序-簡單算法_第2頁
c語言-簡單小程序-簡單算法_第3頁
c語言-簡單小程序-簡單算法_第4頁
c語言-簡單小程序-簡單算法_第5頁
已閱讀5頁,還剩15頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、 以下小程序都在TC2.0中運行通過,程序沒來得及加注釋。程序比較簡單,都是一些初學c語言時寫的程序,請通過運行看效果。5個字符串排序.c#include stdio.h#include stdilb.hvoid paixu(char *p5) int i,j; char *q; for(i=0;i4;i+) for(j=0;j0) q=pj; pj=pj+1; pj+1=q; main() char *p5; int i; for(i=0;i5;i+) pi=(char *)malloc(10); scanf(%s,pi); paixu(p); for(i=0;i1|take4|take-G

2、ame Begin-n);stick(21);printf(-Game Over!-n);&/猜數字游戲#include stdio.h#include stdlib.hint yuan4,shu4;yuan_fen(int num)yuan0=num/1000;yuan1=num/100%10;yuan2=num/10%10;yuan3=num%10;shu_fen(int num)shu0=num/1000;shu1=num/100%10;shu2=num/10%10;shu3=num%10;int shudui_panduan()int shu14,shudui=0,i,j,t=0,q,

3、biao=0;for(i=0;i4;i+) shu1i=-1;for(i=0;i4;i+)for(j=0;j4;j+)if(shui=yuanj)biao=0;for(q=0;qt;q+)if(shui=shu1q) biao=1;break;if(biao=0)shu1t=shui;t+;shudui+;return shudui;int weidui_panduan()int weidui=0,i;for(i=0;i9999|manin1000) goto g1;shu_fen(manin);gotoxy(23,6+dui);printf(%d,shudui_panduan();gotox

4、y(40,6+dui);printf(%d,weidui_panduan();if(weidui_panduan()!=4) dui+;goto g1;else break;gotoxy(10,7+dui);printf(yi gong cai le %d ci!,dui+1);gotoxy(10,8+dui);printf(ni xuan ze ji xu?(y/n);flushall();if(getchar()=y) goto g2;&/九宮格.c main() int a,b, c, d,i,j; int s3030 ;/* 定義一個足夠大的數組*/ printf(shu ru yi

5、ge ji shu:); scanf(%d,&b); if(b%2!=0)/*輸入一個數,不是偶數才執(zhí)行下面的代碼*/ for ( i = 0; i b; i+)/*將數組里賦初值0,以便后面的比較*/for ( j = 0; j b; j+)sij = 0;d = (b - 1) / 2;c = 0; /*下面是向數組里填數,填成魔方*/s0d = 1;for (a = 2; a = 0)if (d + 1 b - 1)c = c - 1;d = 0;elseif (sc - 1 d + 1 != 0)c = c + 1;elsec = c - 1;d = d + 1;elseif (d +

6、 1 b - 1)c = c + 1;elsec = b - 1;d = d + 1;sc d = a;/*下面輸出數組*/for (i = 0; i b; i+) for (j = 0; j b; j+)printf(%dt,sij);/*這里用了t所以不要輸入的數太大,不然輸出對不齊*/printf(n);&/輸出菱形 數組.c#include #include main() int i,j,n; char s20=; printf(nn); n=19; for(i=-n/2; i=n/2; i+) for(j=0;j=abs(i); j+)printf(%c, ); for(j=0;j=

7、n/2-abs(i);j+)printf(%c,sj); for(j=19-(n/2-abs(i); j2|inmanthisman) gotoxy(15,20);printf(Input error!Press any key to restart.);flushall();getchar();gotoxy(15,20);printf( );goto l1;this_inrets() l2:gotoxy(20,17);printf( ); gotoxy(20,17);printf(Input the number of retainer to that bank:);flushall();i

8、f(scanf(%d,&inret)!=1)goto l2;gotoxy(15,20);printf( );if(inret2|inretthisret) gotoxy(15,20);printf(Input error!Press any key to restart.);flushall();getchar();gotoxy(15,20);printf( );goto l2;that_inmans() l3:gotoxy(20,17);printf( );gotoxy(20,17);printf(Input the number of men to this bank:);flushall

9、();if(scanf(%d,&inman)!=1)goto l3;gotoxy(15,20);printf( );if(inman2|inmanthatman)gotoxy(15,20);printf(Input error!Press any key to restart.);flushall();getchar();gotoxy(15,20);printf( );goto l3;that_inrets() l4:gotoxy(20,17);printf( );gotoxy(20,17);printf(Input the number of retainer to this bank:);

10、flushall();if(scanf(%d,&inret)!=1)goto l4;gotoxy(15,20);printf( );if(inret2|inretthatret)gotoxy(15,20);printf(Input error!Press any key to restart.);flushall();getchar();gotoxy(15,20);printf( );goto l4;int this_panduan()int thisman1,thisret1,thatman1,thatret1;thisman1=thisman;thisret1=thisret;thatma

11、n1=thatman;thatret1=thatret;thisman=thisman-inman;thisret=thisret-inret;thatman=thatman+inman;thatret=thatret+inret;if(thisman!=0&thismanthisret) thisman=thisman1; thisret=thisret1; thatman=thatman1; thatret=thatret1;return 0;elseif(thatman!=0&thatmanthatret)thisman=thisman1;thisret=thisret1;thatman

12、=thatman1;thatret=thatret1;return 0;else return 1;int that_panduan()int thisman1,thisret1,thatman1,thatret1;thisman1=thisman;thisret1=thisret;thatman1=thatman;thatret1=thatret;thisman=thisman+inman;thisret=thisret+inret;thatman=thatman-inman;thatret=thatret-inret;if(thisman!=0&thismanthisret) thisma

13、n=thisman1; thisret=thisret1; thatman=thatman1; thatret=thatret1;return 0;elseif(thatman!=0&thatmanthatret)thisman=thisman1;thisret=thisret1;thatman=thatman1;thatret=thatret1;return 0;else return 1;t_printf()int i;gotoxy(12,15);printf( );gotoxy(12,15); for(i=0;ithisman;i+) printf(Man );gotoxy(12,16)

14、;printf( );gotoxy(12,16); for(i=0;ithisret;i+) printf(Retainer );gotoxy(42,15);printf( );gotoxy(42,15); for(i=0;ithatman;i+) printf(Man );gotoxy(42,16);printf( );gotoxy(42,16); for(i=0;i);if(thisman!=0) this_inmans();else inman=0;if(thisret!=0&inman!=2) this_inrets();else inret=0;if(inret+inman2)got

15、oxy(15,20);printf(Input error!Press any key to restart.);flushall();getchar();gotoxy(15,20);printf( );goto t1;if(this_panduan()!=1) gotoxy(15,20);printf( );gotoxy(15,20);printf(The man were killed.Press any key to restart.);flushall();getchar();gotoxy(15,20);printf( );j=1;break;t_printf();t2:if(this

16、man=0&thisret=0) break; gotoxy(27,14);printf(2)gotoxy(15,20);printf( );gotoxy(15,20);printf(Input error!Press any key to restart.);goto t2;if(that_panduan()!=1) gotoxy(15,20);printf( );gotoxy(15,20);printf(The man were killed.Press any key to restart.);flushall();getchar();gotoxy(15,20);printf( );j=

17、1;break;t_printf();if(j=1) goto w1;elsegotoxy(15,20);printf( );gotoxy(15,20);printf(Congratulations! Mission Completed!Any key to quit.);flushall();getchar();return 0;&字符倒置 刪除.C#include char *fan(char *p)char *a,*d,b;a=p;d=p;while(*p!=0)p+;p-;for(;adataq-data) if(p-lchild=NULL) p-lchild=q;elseadd(p-

18、lchild,q);else if(p-rchild=NULL)p-rchild=q;elseadd(p-rchild,q);bianli( struct ercha *p)if(p!=NULL)bianli(p-lchild);printf(t%d,p-data);bianli(p-rchild);main()int pr_num,i=0;struct ercha *p;while(1)g1:flushall();p=(struct ercha *)malloc(LEN);if(scanf(%d,&pr_num)!=1) goto g1;if(pr_num=-1111) break;p-da

19、ta=pr_num;p-lchild=NULL;p-rchild=NULL;if(i=0) head=p;i+;continue;add(head,p);bianli(head);$二叉樹.c#include stdio.h#include conio.h#include malloc.h#define LEN sizeof(struct ercha)#define NULL 0struct ercha *head;struct ercha char data; struct ercha * rch; struct ercha * lch;qianbianli(struct ercha *p)

20、 if(p!=NULL) printf(%c ,p-data); qianbianli(p-lch); qianbianli(p-rch); zhongbianli(struct ercha *p) if(p!=NULL) zhongbianli(p-lch); printf(%c ,p-data); zhongbianli(p-rch); houbianli(struct ercha *p) if(p!=NULL) houbianli(p-lch); houbianli(p-rch); printf(%c ,p-data); charu() char c,b; struct ercha *p

21、,*q;while(1) p=(struct ercha *)malloc(LEN); printf(shu ru shu ju:n); flushall(); scanf(%c,&c); p-data=c; p-rch=NULL; p-lch=NULL; if(c=0) break; if(head=NULL) head=p; else q=head; do printf(xuan ze cha ru wei zhi:n); flushall(); scanf(%c,&b); switch(b) case r: if(q-rch!=NULL) q=q-rch;printf(yi you shu ju,qing zai ci ); else q-rch=p

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論