0){q=p[j];p[j]=p[j+1];p[j+1]=q;}}ma" />

c语言-简单小程序-简单算法

c语言-简单小程序-简单算法
c语言-简单小程序-简单算法

以下小程序都在TC2.0中运行通过,程序没来得及加注释。程序比较简单,都是一些初学c语言时写的程序,请通过运行看效果。

5个字符串排序.c

#include "stdio.h"

#include "stdilb.h"

void paixu(char *p[5])

{

int i,j;

char *q;

for(i=0;i<4;i++)

for(j=0;j<4-i;j++)

if(strcmp(p[j],p[j+1])>0)

{

q=p[j];

p[j]=p[j+1];

p[j+1]=q;

}

}

main()

{

char *p[5];

int i;

for(i=0;i<5;i++)

{

p[i]=(char *)malloc(10);

scanf("%s",p[i]);

}

paixu(p);

for(i=0;i<5;i++)

printf("%s\n",p[i]);

}

******************************************************************************* ***

//21根常胜.c

#include "stdio.h"

#include "conio.h"

stick(int left)

{

int take;

if(left==1)

{

loop1:printf("How many stick do you wish to take(1~1)?");

flushall();

if(scanf("%d",&take)!=1||take>1||take<=0)

{

printf(" Is error!\n");

goto loop1;

}

printf(" Y ou have taken the last stick.\n* * * Y ou lose!\n");

}

else

{

loop: printf("How many stick do you wish to take(1~4)?");

flushall();

if(scanf("%d",&take)!=1||take>4||take<=0)

{

printf(" Is error!\n");

goto loop;

}

printf(" %d stick left in the pile.\n",left-take);

printf(" Compute take %d stick.\n",5-take);

printf(" %d stick left in the pile.\n",left-5);

stick(left-5);

}

}

main()

{

system("cls");

textbackground(GREEN);

textcolor(YELLOW);

clrscr();

printf("*turn.Each one each time takes 1 to 4 sticks. The *\n");

printf("*one who takes the last stick will lose the game. *\n");

printf("***************************************************\n");

printf(">>-------------------Game Begin--------------------\n");

stick(21);

printf(">>-------------------Game Over!--------------------\n");

}

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

//猜数字游戏

#include "stdio.h"

#include "stdlib.h"

int yuan[4],shu[4];

yuan_fen(int num)

{

yuan[0]=num/1000;

yuan[1]=num/100%10;

yuan[2]=num/10%10;

yuan[3]=num%10;

}

shu_fen(int num)

{

shu[0]=num/1000;

shu[1]=num/100%10;

shu[2]=num/10%10;

shu[3]=num%10;

}

int shudui_panduan()

{

int shu1[4],shudui=0,i,j,t=0,q,biao=0;

for(i=0;i<4;i++) shu1[i]=-1;

for(i=0;i<4;i++)

{

for(j=0;j<4;j++)

{

if(shu[i]==yuan[j])

{

biao=0;

for(q=0;q

{

if(shu[i]==shu1[q]) {biao=1;break;}

}

if(biao==0)

{

shu1[t]=shu[i];

t++;

shudui++;

}

}

}

}

return shudui;

}

int weidui_panduan()

{

int weidui=0,i;

for(i=0;i<4;i++)

{

if(shu[i]==yuan[i]) weidui++;

}

return weidui;

}

main()

{

int dui,manin;

g2:

system("cls");

printf("**********************************************\n");

printf("*************Input 10000 Eixt!****************\n");

printf("**********************************************\n");

dui=0;

randomize();

yuan_fen(1000+random(9000));

gotoxy(3,5);

printf("Y our Input");

gotoxy(20,5);

printf("ji ge shu dui");

gotoxy(37,5);

printf("ji wei dui");

while(1)

{

g1:

gotoxy(1,6+dui);

printf("

");

gotoxy(3,6+dui);

if(scanf("%d",&manin)!=1) goto g1;

if(manin==10000) break;

if(manin>9999||manin<1000) goto g1;

shu_fen(manin);

gotoxy(23,6+dui);

printf("%d",shudui_panduan());

gotoxy(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 s[30][30] ;/* 定义一个足够大的数组*/

printf("shu ru yi ge ji shu:");

scanf("%d",&b);

if(b%2!=0)/*输入一个数,不是偶数才执行下面的代码*/

{

for ( i = 0; i < b; i++)/*将数组里赋初值0,以便后面的比较*/

for ( j = 0; j < b; j++)

s[i][j] = 0;

d = (b - 1) / 2;

c = 0;

/*下面是向数组里填数,填成魔方*/

s[0][d] = 1;

for (a = 2; a <= b * b; a++)

{

if (c - 1 >= 0)

{

if (d + 1 > b - 1)

{

c = c - 1;

d = 0;

}

else

{

if (s[c - 1][ d + 1] != 0)

c = c + 1;

else

{

c = c - 1;

d = d + 1;

}

}

}

else

{

if (d + 1 > b - 1)

c = c + 1;

else

{

c = b - 1;

d = d + 1;

}

}

s[c][ d] = a;

}

/*下面输出数组*/

for (i = 0; i < b; i++)

{

for (j = 0; j < b; j++)

{

printf("%d\t",s[i][j]);/*这里用了\t所以不要输入的数太大,不然输出对不齐*/

}

printf("\n");

}

}

}

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

//输出菱形数组.c

#include

#include

main()

{

int i,j,n;

char s[20]="1234567890987654321";

printf("\n\n");

n=19;

for(i=-n/2; i<=n/2; i++)

{

for(j=0;j<=abs(i); j++)printf("%c",' ');

for(j=0;j<=n/2-abs(i);j++)printf("%c",s[j]);

for(j=19-(n/2-abs(i)); j<19;j++)printf("%c",s[j]);

printf("\n");

}

}

*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

//商人和强盗过河游戏.C

#include "stdio.h"

#include "conio.h"

int inman=0,inret=0,thisman=3,thisret=3,thatman=0,thatret=0;

this_inmans()

{

l1:gotoxy(20,17);

printf(" ");

gotoxy(20,17);

printf("Input the number of men to that bank:");

flushall();

if(scanf("%d",&inman)!=1)

goto l1;

gotoxy(15,20);

printf(" ");

if(inman>2||inman>thisman)

{

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();

if(scanf("%d",&inret)!=1)

goto l2;

gotoxy(15,20);

printf(" ");

if(inret>2||inret>thisret)

{

gotoxy(15,20);

printf("Input error!Press any key to restart...");

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();

if(scanf("%d",&inman)!=1)

goto l3;

gotoxy(15,20);

printf(" ");

if(inman>2||inman>thatman)

{

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:");

flushall();

if(scanf("%d",&inret)!=1)

goto l4;

gotoxy(15,20);

printf(" ");

if(inret>2||inret>thatret)

{

gotoxy(15,20);

printf("Input error!Press any key to restart...");

getchar();

gotoxy(15,20);

printf(" ");

goto l4;

}

}

int this_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&&thisman

{

thisman=thisman1;

thisret=thisret1;

thatman=thatman1;

thatret=thatret1;

return 0;

}

else

{

if(thatman!=0&&thatman

{

thisman=thisman1;

thisret=thisret1;

thatman=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&&thisman

{

thisman=thisman1;

thisret=thisret1;

thatman=thatman1;

thatret=thatret1;

return 0;

}

else

{

if(thatman!=0&&thatman

{

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;i

printf("Man ");

gotoxy(12,16);

printf(" ");

gotoxy(12,16);

for(i=0;i

printf("Retainer ");

gotoxy(42,15);

printf(" ");

gotoxy(42,15);

for(i=0;i

printf("Man ");

gotoxy(42,16);

printf(" ");

gotoxy(42,16);

for(i=0;i

printf("Retainer ");

}

main()

{

int j=0;

system("cls");

textbackground(BLUE);

textcolor(YELLOW);

clrscr();

printf("********************************************************************* **********\n");

printf("*

*\n");

printf("* ^_^ Welcome to the game! ^_^ *\n");

printf("*

*\n");

printf("* Game Rules: *\n");

printf("* 3 men take 3 retainers and goods to pass the river, the boat carrys *\n");

printf("* 2 persons each time. In passing the river, at any bank number of *\n");

printf("* men must be more than the number of the retainers, or the retainers *\n");

printf("* will kill the men and take the goods. *\n");

printf("* Game operations :(1) Input the number of men and retainers in turn; *\n");

printf("* (2) Input error keys, the game will restart. *\n");

printf("*

*\n");

printf("********************************************************************* **********\n");

printf("*

*\n");

printf("*

*\n");

printf("*

*\n");

printf("*

*\n");

printf("*

*\n");

printf("*

*\n");

printf("*

*\n");

printf("*

*\n");

printf("*

*\n");

printf("********************************************************************* **********\n");

gotoxy(16,17);

printf("Press any key to start game.(Q key to quit)...");

if(getchar()=='Q') return 0;

w1:

j=0;

thisman=3;

thatman=0;

thisret=3;

thatret=0;

gotoxy(16,17);

printf(" ");

gotoxy(10,14);

printf("This bank");

gotoxy(40,14);

printf("That bank");

gotoxy(12,15);

printf("Man Man Man");

gotoxy(12,16);

printf("Retainer Retainer Retainer");

gotoxy(42,15);

printf(" ");

gotoxy(42,16);

printf(" ");

while(thisman!=0||thisret!=0)

{

t1:

gotoxy(27,14);

printf("--->");

if(thisman!=0) this_inmans();

else inman=0;

if(thisret!=0&&inman!=2) this_inrets();

else inret=0;

if(inret+inman>2)

{

gotoxy(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(thisman==0&&thisret==0) break;

gotoxy(27,14);

printf("<---");

if(thatman!=0) that_inmans();

if(thatret!=0&&inman!=2) that_inrets();

if(inret+inman>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=1;

break;

}

t_printf();

}

if(j==1) goto w1;

else

{

gotoxy(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(;a

{

b=*a;

*a=*p;

*p=b;

}

return d;

}

char *delect(char *p,char c)

{

char *a,*d;

a=p;

d=p;

while(*p!='\0')

{

if(*p!=c)

{

*a=*p;

a++;

}

p++;

}

*a='\0';

return d;

}

main()

{

char *p,*q,ch;

int c;

do

{

printf("***********************************\n");

printf("1. Reverse a string.\n");

printf("2. Delete a character in a string.\n");

printf("***********************************\n");

printf("Please input your choice: \n");

scanf("%d",&c);

switch(c)

{

case 1:

{

printf("Enter a string: \n");

scanf("%s",p);

q=fan(p);

printf("Reversed string is:%s \n",q);

break;

}

case 2:

{

printf("Enter a string:\n");

scanf("%s",p);

printf("Enter a character you want to delete: \n");

flushall();

scanf("%c",&ch);

q=delect(p,ch);

printf("Deleted string is:%s\n",q);

break;

}

}

printf("Would you like to continue? (y/n):");

/*flushall();*/

scanf("%c",&ch);

printf("\n");

}while(ch=='y');

}

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

有序二叉树.c

#include "stdio.h"

#include "malloc.h"

#define LEN sizeof(struct ercha)

#define NULL 0

struct ercha

{

int data;

struct ercha *lchild;

struct ercha *rchild;

};

struct ercha *head;

add(struct ercha *p,struct ercha *q)

{

if(p->data>q->data)

if(p->lchild==NULL)

p->lchild=q;

else

add(p->lchild,q);

else

if(p->rchild==NULL)

p->rchild=q;

else

add(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->data=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 0

struct ercha *head;

struct ercha

{

struct ercha * rch;

struct ercha * lch;

};

qianbianli(struct ercha *p)

{

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,*q;

while(1)

{

p=(struct ercha *)malloc(LEN); printf("shu ru shu ju:\n"); flushall();

scanf("%c",&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;printf("cha ru cheng gong,qing zai ci ");}

break;

}

case 'l':

{

if(q->lch!=NULL) {q=q->lch;printf("yi you shu ju,qing zai ci ");}

else {q->lch=p;printf("cha ru cheng gong,qing zai ci ");}

break;

}

default:

printf("shu ru you wu,qing chong xin ");

}

if(q->rch==p||q->lch==p) break;

}while(1);

}

}

}

main()

{

struct ercha *q;

char c;

while(1)

{

printf("*************************************\n");

printf("1 cha ru:\n");

printf("2 bianli:\n");

printf("**************************************\n");

printf("you chose:\n");

flushall();

scanf("%c",&c);

switch(c)

{

case '1':

{charu();break;}

case '2':

{

printf("**************************************\n");

printf("qian xu:\n");

qianbianli(head);

printf("\n**************************************\n");

printf("zhong xu:\n");

zhongbianli(head);

printf("\n**************************************\n");

printf("hou xu:\n");

houbianli(head);

printf("\n**************************************\n");

break;

}

}

printf("ni xian zai xiang ji xu ma?(y/n)\n");

flushall();

scanf("%c",&c);

if(c!='y')

break;

}

}

相关主题
相关文档
最新文档