给我一个C语言程序要求百行以上,谢谢

Python026

给我一个C语言程序要求百行以上,谢谢,第1张

红黑树的一个演示程序,不一定完全正确

大概300+吧

// Copyright 1988-2006 By Zor X. Liu

#include <stdio.h>

#include <conio.h>

enum Color { red,black }

struct RBTNode

{

int dat

Color c

RBTNode *p,*l,*r

}

RBTNode *root,*nil

RBTNode* Insert(int)

void Delete(RBTNode*)

RBTNode* Successor(RBTNode*)

RBTNode* Minium(RBTNode*)

RBTNode* Search(int)

void LeftRotate(RBTNode*)

void RightRotate(RBTNode*)

void InorderWalk(RBTNode*)

void PreorderWalk(RBTNode*)

void Free(RBTNode*)

int main()

{

nil=new RBTNode

nil->c=black,nil->dat=0

nil->p=nil->r=nil->l=NULL

RBTNode *tmp

int choice,dat

while(true)

{

clrscr()

printf("------------------------------\n")

printf("Current:")InorderWalk(root)printf("\n")

printf("------------------------------\n")

printf(" 1.Insert\n")

printf(" 2.Delete\n")

printf(" 3.Preorder Walk\n")

printf(" q.Quit\n")

printf("------------------------------\n")

do {

choice=getch()

} while(choice!='q'&&(choice>'3'||choice<'1'))

if(choice=='q')

break

if(choice=='1'||choice=='2')

{

printf("Data:")

scanf("%d",&dat)

}

switch(choice)

{

case '1':

Insert(dat)

printf("Inserted.\n")

break

case '2':

tmp=Search(dat)

if(!tmp)

{

printf("Cannot find it.\n")

break

}

Delete(tmp)

printf("Deleted.\n")

break

case '3':

PreorderWalk(root)

break

}

getch()

}

return 0

}

void InorderWalk(RBTNode* x)

{

if(x!=nil&&x)

{

InorderWalk(x->l)

printf("%d",x->dat)

if(x->c==red)

printf("r ")

else

printf("b ")

InorderWalk(x->r)

}

}

void PreorderWalk(RBTNode* x)

{

if(x!=nil&&x)

{

printf("%d",x->dat)

if(x->c==red)

printf("r ")

else

printf("b ")

PreorderWalk(x->l)

PreorderWalk(x->r)

}

}

void Free(RBTNode* x)

{

if(x!=nil&&x)

{

Free(x->l)

Free(x->r)

delete x

}

}

RBTNode* Search(int dat)

{

RBTNode *x=root

if(root)

{

while(x!=nil)

{

if(dat==x->dat)

return x

else

if(dat>x->dat)

x=x->r

else

x=x->l

}

}

return NULL

}

RBTNode* Minium(RBTNode* x)

{

while(x->l!=nil)

x=x->l

return x

}

RBTNode* Successor(RBTNode* x)

{

if(x->r)

return Minium(x->r)

RBTNode *y=x->p

while(y&&x==y->r)

{

x=y

y=y->p

}

return y

}

void LeftRotate(RBTNode* x)

{

RBTNode *y=x->r,*z=x->p

if(y!=nil)

{

x->p=y

x->r=y->ly->l->p=x

y->p=z

y->l=x

if(y->p)

if(x==y->p->l)

y->p->l=y

else

y->p->r=y

else

root=y

}

}

void RightRotate(RBTNode* x)

{

RBTNode *y=x->l,*z=x->p

if(y!=nil)

{

x->p=y

x->l=y->ry->r->p=x

y->p=z

y->r=x

if(y->p)

if(y->p->l==x)

y->p->l=y

else

y->p->r=y

else

root=y

}

}

RBTNode* Insert(int dat)

{

RBTNode *n

n=new RBTNode

n->dat=dat,n->c=red

n->p=NULL,n->l=n->r=nil

if(!root)

{

root=n

n->c=black

return n

}

RBTNode *x=root,*y=NULL

while(x!=nil)

{

y=x

if(dat>x->dat)

x=x->r

else

x=x->l

}

n->p=y

if(dat>y->dat)

y->r=n

else

y->l=n

if(y->c==red)

{

x=n

while(x->p->p&&x->c==red&&x->p->c==red)

{

if(x->p->p->l==x->p)

{

y=x->p->p->r

if(y->c==red)

{

x->p->p->c=red

x->p->c=y->c=black

x=x->p->p

}

else

{

if(x==x->p->r)

{

LeftRotate(x->p)

x=x->l

}

x->p->c=black

x->p->p->c=red

RightRotate(x->p->p)

}

}

else

{

y=x->p->p->l

if(y->c==red)

{

x->p->p->c=red

x->p->c=y->c=black

x=x->p->p

}

else

{

if(x==x->p->l)

{

RightRotate(x->p)

x=x->r

}

x->p->c=black

x->p->p->c=red

LeftRotate(x->p->p)

}

}

}

}

root->c=black

}

void Delete(RBTNode* x)

{

RBTNode *y,*z

if(x->l==nil||x->r==nil)

y=x

else

y=Successor(x)

if(y->l!=nil)

z=y->l

else

z=y->r

z->p=y->p

if(y->p)

if(y==y->p->r)

y->p->r=z

else

y->p->l=z

else

root=z,z->p=NULL

if(y!=x)

x->dat=y->dat

if(y->c==black)

{

x=z

while(x!=root&&x->c==black)

{

if(x==x->p->l)

{

z=x->p->r

if(z->c==red)

{

x->p->c=red

z->c=black

LeftRotate(x->p)

}

if(z->c==black)

{

if(z->l->c==z->r->c==black)

{

z->c=red

x=x->p

}

else if(z->l->c==red&&z->r->c==black)

{

RightRotate(z)

z=z->p

}

if(z->r->c==red)

{

z->r->c=black

z->c=z->p->c

z->p->c=black

LeftRotate(z->p)

x=root

}

}

}

else

{

z=x->p->l

if(z->c==red)

{

x->p->c=red

z->c=black

RightRotate(x->p)

}

if(z->c==black)

{

if(z->r->c==z->l->c==black)

{

z->c=red

x=x->p

}

else if(z->r->c==red&&z->l->c==black)

{

LeftRotate(z)

z=z->p

}

if(z->l->c==red)

{

z->l->c=black

z->c=z->p->c

z->p->c=black

RightRotate(z->p)

x=root

}

}

}

}

}

delete y

}

这是以前我自己写的一个加密解密的程序,一共有175行,不过因为其中写了两种加密方法和两种对应的解密方法,而且用函数将他们区分开来,还用菜单做了选项,所以你只要直接删除其中一种方法的加解密函数就可以了,这个字符串加解密是对字符串比较全的操作,对你学习c会有帮助,而且也不难看懂,你可以看下,不懂可以通过百度hi问我(encrypt函数是加密函数decrypt是解密函数,数字1和2表示第一种加密方式,第二种加密方式)

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

FILE *file=NULL

void main()

{

void set(char*,int*,int*)

void encrypt1(char*,const int*)

void decrypt1(char*,const int*)

void encrypt2(char*,const int*)

void decrypt2(char*,const int*)

char content[100000]=""

int key1[10]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}

int key2[2]={0}

char press='\0'

do

{

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

printf("1.设置加密方法\n")

printf("2.加密方法1\n")

printf("3.加密方法2\n")

printf("4.解密方法1\n")

printf("5.解密方法2\n")

printf("6.退出\n")

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

printf("选择操作步骤(1-6):\n")

switch(press=getch())

{

case '1':set(content,key1,key2)break

case '2':encrypt1(content,key1)break

case '3':encrypt2(content,key2)break

case '4':decrypt1(content,key1)break

case '5':decrypt2(content,key2)break

default:break

}

}while(press!='6')

if(file!=NULL)

{

rewind(file)

fputs(content,file)

printf("文件或字符串的内容为:\n")

puts(content)

fclose(file)

}

}

void set(char content[100000],int key1[10],int key2[2])

{

long int key

char ch

int i

char str[100]=""

if(file!=NULL)

{

fclose(file)

file=NULL

}

printf("输入字符串或者文件完整路径:")

gets(str)

if(file=fopen(str,"r+"))

{

printf("输入的是文件完整路径,内容为:\n")

while(fgetc(file)!=EOF)

{

fseek(file,-1,1)

fgets(content,100000,file)

printf("%s",content)

}

}

else

{

file=fopen("content.txt","w+")

strcpy(content,str)

printf("输入的是字符串,内容为:\n")

puts(content)

}

printf("\n设置密码:")

gets(str)

for(i=0(i<strlen(str))&&(i<10)i++)

*(key1+i)=*(str+i)-'0'

key=atoi(str)

key2[0]=(key%11>=3)?(key%11):3

for(key2[1]=1,i=0i<10i++)

if((*(key1+i)>key2[1])&&(*(key1+i)<key2[0]))

key2[1]=*(key1+i)

}

void encrypt1(char content[100000],const int key1[10])

{

int j,k

for(j=0,k=0j<strlen(content)j++)

{

*(content+j)+=*(key1+k)

k++

if(k==9||*(key1+k)==-1)

k=0

if(*(content+j)>=122)

*(content+j)%=122

}

}

void decrypt1(char content[100000],const int key1[10])

{

int j,k

for(j=0,k=0j<strlen(content)j++)

{

*(content+j)-=*(key1+k)

k++

if(k==9||*(key1+k)==-1)

k=0

if(*(content+j)>=122)

*(content+j)%=122

}

}

void encrypt2(char content[100000],const int key2[2])

{

char precontent[100000],str[11]="",ch

int i,j,p

p=key2[0]

strcpy(precontent,content)

strcpy(content,"")

for(i=0i<strlen(precontent))

{

if(strlen(precontent)-i<key2[0])

p=strlen(precontent)-i

for(j=p-1j>=0j--)

*(str+j)=*(precontent+i++)

if(p==key2[0])

{

ch=*(str)

*(str)=*(str+key2[1])

*(str+key2[1])=ch

}

else

{

ch=*(str)

*(str)=*(str+p-1)

*(str+p-1)=ch

}

strcat(content,str)

for(j=0j<pj++)

*(str+j)='\0'

if(p!=key2[0])

break

}

}

void decrypt2(char content[100000],const int key2[2])

{

char precontent[100000],str[11]="",ch

int i,j,p

p=key2[0]

strcpy(precontent,content)

strcpy(content,"")

for(i=0i<strlen(precontent))

{

if(strlen(precontent)-i<key2[0])

p=strlen(precontent)-i

for(j=p-1j>=0j--)

*(str+j)=*(precontent+i++)

if(p==key2[0])

{

ch=*(str+p-1)

*(str+p-1)=*(str+p-1-key2[1])

*(str+p-1-key2[1])=ch

}

else

{

ch=*(str)

*(str)=*(str+p-1)

*(str+p-1)=ch

}

strcat(content,str)

for(j=0j<11j++)

*(str+j)='\0'

if(p!=key2[0])

break

}

}

//学生信息管理系统

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

struct student

{

char num[20]

char name[20]

int score1

int score2

int score3

}stu[100]={"01","zhangmingming",67,78,82,"02","lichengyou",78,91,88,"03","zhanghuican",68,82,56,"04","wanglu",56,45,77,"05","chendongming",67,38,47}

int main()

{

int i

int s=5

void cjtj()

void xxwh()

void xxcx()

void cjpx()

FILE *fp1

if((fp1=fopen("stu_list","w"))==NULL) /*以只写的方式打开一个文件stu_list*/

{

printf("Can not open file!\n")

exit(1)

}

fwrite(&stu,sizeof(struct student),5,fp1)/*将stu中的数据写到文件中*/

fclose(fp1)

do

{

printf("\n")

printf("\n")

printf(" +--------------------+\n")

printf(" |欢迎使用学生管理系统|\n")

printf(" ||\n")

printf(" | 请按照步骤操作 |\n")

printf(" | 1.信息维护 |\n")

printf(" | 2.信息查询 |\n")

printf(" | 3.成绩统计 |\n")

printf(" | 4.成绩排序 |\n")

printf(" | 0.退出程序 |\n")

printf(" +--------------------+\n")

printf(" 请选择您要进行的操作:")

scanf("%d",&i)

if(i==1)

{

xxwh()

}

else if(i==2)

{

xxcx()

}

else if(i==3)

{

cjtj()

}

else if(i==4)

{

cjpx()

}

else

break

}while(i!=0)

printf("谢谢使用!\n")

return 0

}

void cjtj() /*成绩统计子函数*/

{

char name[20]

system("cls")

printf("请输入您想统计的课程名称:\n")

scanf("%s",name)

int q,w,u,s,g,p,k,l,m

if(strcmp(name,"yuwen")==0)

{

printf("请输入您想统计的分数段(例60,70):\n")

scanf("%d,%d",&q,&w)

for(u=0u<5u++)

{

if((stu[u].score1>=q)&&(stu[u].score1<=w))

printf("%s %s %d %d %d\n",stu[u].num,stu[u].name,stu[u].score1,stu[u].score2,stu[u].score3)

}

}

if(strcmp(name,"shuxue")==0)

{

printf("请输入您想统计的分数段(例60,70):\n")

scanf("%d,%d",&s,&g)

for(p=0p<5p++)

{

if(stu[p].score2>=s&&g>=stu[p].score2)

printf("%s %s %d %d %d\n",stu[p].num,stu[p].name,stu[p].score1,stu[p].score2,stu[p].score3)

}

}

if(strcmp(name,"yingyu")==0)

{

printf("请输入您想统计的分数段(例60,70):\n")

scanf("%d,%d",&k,&l)

for(m=0m<5m++)

{

if(stu[m].score3>=k&&stu[m].score3<=l)

printf("%s %s %d %d %d\n",stu[m].num,stu[m].name,stu[m].score1,stu[m].score2,stu[m].score3)

}

}

return

}

void xxwh() /* 信息维护子函数*/

{

int y

int s=5

void zengjia()

void shanjian()

void xiugai()

do

{

printf(" --------------------\n")

printf(" 欢迎进入信息维护模块\n")

printf(" 1.增加学生信息 \n")

printf(" 2.删减学生信息 \n")

printf(" 3.修改学生信息 \n")

printf(" 4.退出本模块 \n")

printf(" --------------------\n")

printf(" 请输入您要进行的操作(1-4):")

scanf("%d",&y)

if(y==1)

{

zengjia()

s=s+1

}

else if(y==2)

{

shanjian()

s=s-1

}

else if(y==3)

{

xiugai()

}

else

break

}while(y!=4)

printf("谢谢使用!\n")

return

}

void zengjia() /*增加学生信息*/

{

struct student student1

FILE *fp1

int n=5

printf("输入您想添加的学生信息:\n")

scanf("%s %s%d%d%d",student1.num,student1.name,&student1.score1,&student1.score2,&student1.score3)

strcpy(stu[n+1].num,student1.num)

strcpy(stu[n+1].name,student1.name)

stu[n+1].score1=student1.score1

stu[n+1].score2=student1.score2

stu[n+1].score3=student1.score3

if((fp1=fopen("stu_list","r+"))==NULL) /*以读写的方式打开一个文件stu_list*/

{

printf("Can not open file!\n")

exit(1)

}

fwrite(&stu,sizeof(struct student),6,fp1)/*将stu中的数据写到文件中*/

fclose(fp1)

printf("\n")

printf("添加成功!\n")

printf("\n")

printf("\n")

return

}

void shanjian() /*删除学生信息*/

{

char a[20]

int h,j,e

FILE *fp1

printf("请输入您想删除的学生学号:\n")

scanf("%s",a)

for(h=0h<5h++)

{

if(strcmp(a,stu[h].num)==0)

j=h

}

if(j!=4)

{

for(e=h-1e<5e++,j++)

{

strcpy(stu[j].num,stu[j+1].num)

strcpy(stu[j].name,stu[j+1].name)

stu[j].score1=stu[j+1].score1

stu[j].score2=stu[j+1].score2

stu[j].score3=stu[j+1].score3

}

}

if((fp1=fopen("stu_list","r+"))==NULL) /*以读写的方式打开一个文件stu_list*/

{

printf("Can not open file!\n")

exit(1)

}

fwrite(&stu,sizeof(struct student),6,fp1)/*将stu中的数据写到文件中*/

fclose(fp1)

printf("\n")

printf("删除成功!\n")

printf("\n")

printf("\n")

return

}

void xiugai() /*修改学生信息*/

{

char b[20]

int c,n=5

FILE *fp1

struct student student2

printf("请输入您想修改的学生学号:\n")

scanf("%s",b)

for(c=0c<nc++)

{

if(strcmp(b,stu[c].num)==0)

{

printf("请输入修改后的学生信息:\n")

scanf("%s %s%d%d%d",student2.num,student2.name,&student2.score1,&student2.score2,&student2.score3)

strcpy(stu[c].num,student2.num)

strcpy(stu[c].name,student2.name)

stu[c].score1=student2.score1

stu[c].score2=student2.score2

stu[c].score3=student2.score3

}

} if((fp1=fopen("stu_list","r+"))==NULL) /*以读写的方式打开一个文件stu_list*/

{

printf("Can not open file!\n")

exit(1)

}

fwrite(&stu,sizeof(struct student),6,fp1)/*将stu中的数据写到文件中*/

fclose(fp1)

fclose(fp1)

printf("\n")

printf("修改成功!\n")

printf("\n")

printf("\n")

return

}

void cjpx() /*排序函数*/

{

int z,x,c,v,o

struct student t

do

{

printf("\n")

printf("\n")

printf("[1]按升序排序:\n")

printf("[2]按降序排序:\n")

printf("[3]返回\n")

printf("请选择:")

scanf("%d",&z)

if(z==1)

{

printf("选择的科目:")

printf("[1]语文:[2]数学:[3]英语:")

scanf("%d",&x)

if(x==1)

{

for(c=1c<5c++)

for(v=0v<5-cv++)

if(stu[v].score1>stu[v+1].score1)

{

t=stu[v]

stu[v]=stu[v+1]

stu[v+1]=t

}

for(o=0o<5o++)

printf("%s %s %d %d %d\n",stu[o].num,stu[o].name,stu[o].score1,stu[o].score2,stu[o].score3)

}

else if(x==2)

{

for(c=1c<5c++)

for(v=0v<5-cv++)

if(stu[v].score2>stu[v+1].score2)

{

t=stu[v]

stu[v]=stu[v+1]

stu[v+1]=t

}

for(o=0o<5o++)

printf("%s %s %d %d %d\n",stu[o].num,stu[o].name,stu[o].score1,stu[o].score2,stu[o].score3)

}

else if(x==3)

{

for(c=1c<5c++)

for(v=0v<5-cv++)

if(stu[v].score3>stu[v+1].score3)

{

t=stu[v]

stu[v]=stu[v+1]

stu[v+1]=t

}

for(o=0o<5o++)

printf("%s %s %d %d %d\n",stu[o].num,stu[o].name,stu[o].score1,stu[o].score2,stu[o].score3)

}

}

else

if(z==2)

{

printf("选择的科目:")

printf("[1]语文:[2]数学:[3]英语:")

scanf("%d",&x)

if(x==1)

{

for(c=1c<5c++)

for(v=0v<5-cv++)

if(stu[v].score1<stu[v+1].score1)

{

t=stu[v+1]

stu[v+1]=stu[v]

stu[v]=t

}

for(o=0o<5o++)

printf("%s %s %d %d %d\n",stu[o].num,stu[o].name,stu[o].score1,stu[o].score2,stu[o].score3)

}

else

if(x==2)

{

for(c=1c<5c++)

for(v=0v<5-cv++)

if(stu[v].score2<stu[v+1].score2)

{

t=stu[v+1]

stu[v+1]=stu[v]

stu[v]=t

}

for(o=0o<5o++)

printf("%s %s %d %d %d\n",stu[o].num,stu[o].name,stu[o].score1,stu[o].score2,stu[o].score3)

}

else

if(x==3)

{

for(c=1c<5c++)

for(v=0v<5-cc++)

if(stu[v].score3<stu[v+1].score3)

{

t=stu[v+1]

stu[v+1]=stu[v]

stu[v]=t

}

for(o=0o<5o++)

printf("%s %s %d %d %d\n",stu[o].num,stu[o].name,stu[o].score1,stu[o].score2,stu[o].score3)

}

}

else

break

}while(z!=3)

printf("\n")

printf("\n")

return

}

void xxcx() /*信息查询子函数*/

{

int g,i

char m[20]

system("cls")

do

{

printf("[1]查询学生信息:\n")

printf("[2]退出查询:\n")

printf("请选择:")

scanf("%d",&g)

if(g==1)

{

printf("请输入学号:")

scanf("%s",m)

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

{

if(strcmp(m,stu[i].num)==0)

{

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

printf("学号:%s\n",stu[i].num)

printf("姓名:%s\n",stu[i].name)

printf("语文:%d\n",stu[i].score1)

printf("数学:%d\n",stu[i].score2)

printf("英语:%d\n",stu[i].score3)

printf("\n")

printf("\n")

}

}

}

else

break

}while(g!=2)

printf("查找完毕!\n")

system("pause")

return

}