1. 求一个比较复杂的C语言程序
#include "stdio.h"
#include "ctype.h"
#include "malloc.h"
#include "stdlib.h"
#define M 100
#define ZERO 0
#define SUCC 1
#define DEFT 0
#define MIN -1
#define MAX 2001
typedef int valuetype;
typedef struct Bnode
{
valuetype data;
int layer ;
struct Bnode *Lson,*Rson;
}Bnode,*Bptr;
void writeT(Bptr root)
{
int first=0,last=1;
Bptr p,q[M];
if(root->data==MIN)p=root->Rson;
else p=root->Lson;
if(p==NULL)
{ printf(" 当前二叉树为空,没有结点。\n");return;}
printf(" 当前二叉树的结点为:\n");
printf(" 层号 当前结点 左儿子 右儿子\n");
p->layer=1;
q[0]=p;
while(first!=last)
{
p=q[first++];
printf("%6d%10d ",p->layer,p->data);
if(p->Lson==NULL)printf("%12c",'\040');
else
{
printf("%12d",p->Lson->data);
p->Lson->layer=p->layer+1;
q[last++]=p->Lson;
}
if(p->Rson!=NULL)
{
printf("%12d",p->Rson->data);
p->Rson->layer=p->layer+1;
q[last++]=p->Rson;
}
printf("\n");
}
}
void inorder(Bptr p)
{
if(!p)return;
inorder(p->Lson);
printf("%5d",p->data);
inorder(p->Rson);
}
void sortT(Bptr root)
{
if(root->data==MIN) inorder(root->Rson);
else inorder(root->Rson);
printf("\n");
}
Bptr search (valuetype x,Bptr p)
{
while (p!=NULL)
{
if(x==p->data)return p;
if(x<p->data) p=p->Lson;
else p=p->Rson;
}
return NULL;
}
void searchT(Bptr root)
{
int x;
printf("请输入要查找的结点值x>0,x=");
scanf("%d",&x);
if(search(x,root)==NULL)printf("数中没有%d!\n",x);
else printf("%d 已经找到!\n",x);
}
void insert(valuetype x,Bptr &root)
{
Bptr f,p;
f=NULL;p=root;
while(p!=NULL)
{
if(x<p->data)f=p,p=p->Lson;
else f=p,p=p->Rson;
}
p=new Bnode;
p->data=x;p->Lson=p->Rson=NULL;
if(f==NULL)root=p;
else
if(x<=f->data)f->Lson=p;
else f->Rson=p;
}
void insertT(Bptr p)
{
int x;
printf("请输入要插入的结点的值x>0,x=");
scanf("%d",&x);
insert(x,p);
printf("%d已经被插入了\n",x);
}
Bptr creatST()
{
Bptr root ;valuetype x;
root =NULL;
printf(" 构造初始检索树,请输入元素序列,元素个数不得超过%d,要求:\n",M);
printf("序列以%d或%d开始,以0结束,元素值均为小于%d的正整数\n",MIN,MAX,MAX);
scanf("%d",&x);
while(x!=ZERO)
{
insert(x,root);
scanf("%d",&x);
}
return root;
}
int deleteST(valuetype x,Bptr root)
{
Bptr f,p,s,r;
for (p=root;;)
{
if(p==NULL)return DEFT;
if (x==p->data)break;
if(x<p->data)
{
f=p;p=p->Rson;
}
else
{
f=p;p=p->Rson;
}
}
if (p->Rson==NULL)
{
if(p==f->Lson)
f->Lson=p->Rson;
else
f->Rson=p->Lson;
free (p);
return SUCC;
}
s=p->Lson;
if (s->Rson==NULL)
{
p->data=s->data;
p->Lson=s->Lson;
free (s);
return SUCC;
}
r=s->Rson;
while (r->Rson!=NULL)
{
s=r;
r=r->Rson;
}
p->data=r->data;
s->Rson=r->Lson;
free (r);
return SUCC;
}
void deleteT(Bptr root)
{
int x;
printf("请输入要删除的结点值x>0,x=");
scanf("%d",&x);
if(deleteST(x,root))
printf("%d 已经被删除!\n",x);
else
printf(" %d不在树中,无法删除!\n",x);
}
char getalpha()
{
char c;
while(1)
{
c=getchar();
if(isalpha(c))
return c;
}
}
void treeT(Bptr root)
{
char c;
printf(" 对检索树可以进行下列操作:\n");
while (1)
{
printf("请输入操作码:查找F/f 插入I/i 删除D/d 显示P/p 结点排序S/s 终止E/e\nC=");
c=getalpha();
switch(c)
{
case 'f':
case 'F': searchT(root);break;
case 'p':
case 'P': writeT(root);break;
case 'i':
case 'I': insertT(root);break;
case 'd':
case 'D': deleteT(root);break;
case 's':
case 'S': sortT(root);break;
case 'e':
case 'E': writeT(root);return;
default:printf("输入的操作码不正确,请重新输入!\n");
continue;
}
}
}
void main()
{
Bptr root;
root=creatST();
treeT(root);
printf("程序结束,再见!\n");
}
2. 有一道很复杂的C语言代码想请教大佬,不会勿点
//随便分享一个C语言代码吧!
#include<stdio.h>
#include<conio.h>
#include<pthread.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#define print(a) switch(a)\
{\
case 0:\
printf("⬜");\
break;\
case 2:\
printf("🚧");\
break;\
case 1:\
printf("🍞");\
break;\
case 3:\
printf("💩");\
break;\
default:\
if(a-4)\
{\
if(a-snake-4<0)\
{\
printf("🎾");\
}\
else\
{\
if(shit)\
{\
if(rand()%shit)\
{\
printf("⬜");\
a=-1;\
}\
else\
{\
printf("💩");\
a=2;\
}\
}\
else\
{\
printf("⬜");\
a=-1;\
}\
}\
}\
else printf("🎃");\
a=a+1;\
break;\
}
//#define high 12
//#define wide 17
int shit=100,score=0,i=0,r=0,l=2,snake=2,snake1=-2,direction=4;
float speed=5;
int tick,p,q,x,y;
int contrl=0,food=2,bug=0;
int map[1024][1024];
int high=12,wide=17;
int growLong=1,growScore=1;
int Print()
{
while(1)
{
snake1=-snake;
switch(direction)
{
case 1:
if(map[y-1][x]>1) contrl=1;
else y=y-1;
break;
case 2:
if(map[y+1][x]>1) contrl=1;
else y=y+1;
break;
case 3:
if(map[y][x-1]>1) contrl=1;
else x=x-1;
break;
case 4:
if(map[y][x+1]>1) contrl=1;
else x=x+1;
break;
}
if(map[y][x]-1);
else
{
snake=snake+growLong;
score=score+growScore;
A:
p=rand()%(high-2)+1;
q=rand()%(wide-2)+1;
if(map[p][q]) goto A;
else map[p][q]=1;
}
map[y][x]=4;
if(contrl) return 0;
clrscr();
for(i=0; i<high; i++)
{
for(r=0; r<wide; r++)
{
print(map[i][r])
}
printf("\n");
}
printf("当前分数:%d\n",score);
usleep(1000000/speed);
}
}
int Speed()
{
clrscr();
printf("单位:格/秒,默认:5,当前:%g\n请输入速度:",speed);
scanf("%f",&speed);
return main();
}
int Food()
{
clrscr();
printf("单位:个,默认:2,当前:%d\n请输入食物数量:",food);
scanf("%d",&food);
return main();
}
int High()
{
clrscr();
printf("单位:格,默认:12,当前:%d\n请输入高度:",high);
scanf("%d",&high);
return main();
}
int Wide()
{
clrscr();
printf("单位:格,默认:17,当前:%d\n请输入宽度:",wide);
scanf("%d",&wide);
return main();
}
int Shit()
{
clrscr();
printf("单位:^-1/步,默认:100,当前:%d\n若为0,则不拉屎\n请输入拉屎概率:",shit);
scanf("%d",&shit);
return main();
}
int L()
{
clrscr();
printf("单位:格,默认:2,当前:%d\n请输入初始长度:",l);
scanf("%d",&l);
return main();
}
int GrowLong()
{
clrscr();
printf("单位:长度/个食物,默认:1,当前:%d\n请输入单个食物增加长度:",growLong);
scanf("%d",&growLong);
return main();
}
int GrowScore()
{
clrscr();
printf("单位:分数/个食物,默认:1,当前:%d\n请输入单个食物分数:",growScore);
scanf("%d",&growScore);
return main();
}
int BugContrl()
{
clrscr();
printf("0:关闭外挂,其他:开启外挂\n默认:0,当前:%d\n请输入外挂控制数:",bug);
scanf("%d",&bug);
return main();
}
int Bug()
{
while(1)
{
if(map[1][15]-4);
else direction=3;
if(map[1][1]-4);
else direction=2;
if(map[2][1]-4);
else direction=4;
if(map[2][14]-4);
else direction=2;
if(map[3][14]-4);
else direction=3;
if(map[3][1]-4);
else direction=2;
if(map[4][1]-4);
else direction=4;
if(map[4][14]-4);
else direction=2;
if(map[5][14]-4);
else direction=3;
if(map[5][1]-4);
else direction=2;
if(map[6][1]-4);
else direction=4;
if(map[6][14]-4);
else direction=2;
if(map[7][14]-4);
else direction=3;
if(map[7][1]-4);
else direction=2;
if(map[8][1]-4);
else direction=4;
if(map[8][14]-4);
else direction=2;
if(map[9][14]-4);
else direction=3;
if(map[9][1]-4);
else direction=2;
if(map[10][1]-4);
else direction=4;
if(map[10][15]-4);
else direction=1;
}
}
int main()
{
C:
clrscr();
printf("按对应键开始\n");
printf("0:开始游戏\n");
printf("1:设置速度\n");
printf("2:设置食物数量\n");
printf("3:设置高度\n");
printf("4:设置宽度\n");
printf("5:设置拉屎概率\n");
printf("6:设置初始长度\n");
printf("7:设置单个食物增加长度\n");
printf("8:设置单个食物分数\n");
printf("9:设置外挂\n");
tick=getch();
switch(tick)
{
case 48:
break;
case 49:
return Speed();
case 50:
return Food();
case 51:
return High();
case 52:
return Wide();
case 53:
return Shit();
case 54:
return L();
case 55:
return GrowLong();
case 56:
return GrowScore();
case 57:
return BugContrl();
default:
goto C;
}
srand(time(0));
for(i=0; i<high; i++)
{
for(r=0; r<wide; r++)
{
map[i][r]=0;
}
}
for(i=0; i<food; i++)
{
B:
p=rand()%(high-2)+1;
q=rand()%(wide-2)+1;
if(map[p][q]) goto B;
else map[p][q]=1;
}
x=wide/2;
y=high/2;
for(i=0; i<high; i++) map[i][0]=2;
for(i=0; i<high; i++) map[i][wide-1]=2;
for(r=0; r<wide; r++) map[0][r]=2;
for(r=0; r<wide; r++) map[high-1][r]=2;
contrl=0;score=0;snake=l;direction=4;
pthread_t print;
pthread_create(&print,NULL,Print,NULL);
if(bug)
{
pthread_t bug;
pthread_create(&bug,NULL,Bug,NULL);
}
while(contrl-1)
{
A:
tick=getch();
switch(tick)
{
case 50:
if(direction-2) direction=1;
else goto A;
break;
case 52:
if(direction-4) direction=3;
else goto A;
break;
case 54:
if(direction-3) direction=4;
else goto A;
break;
case 56:
if(direction-1) direction=2;
else goto A;
break;
}
usleep(1000000/speed);
}
printf("按任意键回到主菜单");
getch();
goto C;
}
3. 谁可以给我一个复杂点的C语言例子,要无错的,我刚学编程,很好奇
这是我做的课程设计,虽然不是很复杂,但对初学者来说已经足够做参考了。 #include #include sbit speaker=P2^3; sbit RS=P2^4; //P2.4 sbit RW=P2^5; //P2.5 sbit E=P2^6; //P2.6 sbit set=P3^4; //设置键 sbit enter=P3^5; //确认键 sbit add1=P3^6; //加1键 sbit sub1=P3^7; //减1键 bit k=0,f=0,alarmflag1,alarmflag2,xx;//k为0表示运行状态,k为1表示设置状态;f为0表示第一行显示,f为1表示第二行显示,alarmflag表示闹钟设置的标号,xx用来给speaker响 char sec,min,hour,week,day,month,year,n,m,hourn,minn,alarm; unsigned char count,key; unsigned char lcdd[]="0123456789"; bit naozhong; /*声明调用函数*/ void dispd(); //日期显示函数 void dispt(); //时间显示函数 void disalarm(); unsigned char keys();//按键扫描函数 void lcd_w_cmd(unsigned char com); //写命令字函数 void lcd_w_dat(unsigned char dat); //写数据函数 unsigned char lcd_r_start(); //读状态函数 void int1(); //LCD初始化函数 void delay(unsigned char t); //可控延时函数 void delay1(); //软件实现延时函数,5个机器周期 void main() { TMOD=0x01; //设置为定时器模式1 TH0=0x3c; //晶振12MHz,定时时间50ms TL0=0xb0; IE=0x82; //开全局中断和定时中断 TR0=1; //启动定时器 sec=0; //秒 min=0; minn=0;//分 hour=0; hourn=0;//时 week=0; //星期 day=13; //日 month=6; //月 year=11; //年 count=0; //中断次数清0 n=-3; //设置键按下次数,第一行日期 m=-3; //设置键按下次数,第二行时间 xx=1; P0=0xff; // 送全1到P0口 int1(); // 初始化LCD delay(255); while(1) { key=keys(); //读取按键 switch(key) { case 0xef: //按下设置键 { TR0=0; k=1; if(f==0) { n=n+3; if(n==9) { n=0; m=0; f=1; } } else { m=m+3; if(alarm==0) { if(m==12) { m=0; n=0; f=0; } } else { if(m==6) m=0; } } if(f==0) { lcd_w_cmd(0x0d); lcd_w_cmd(0x86+n); } else { lcd_w_cmd(0x0d); if(alarm==0) lcd_w_cmd(0xc4+m); else lcd_w_cmd(0xc7+m); } } break; case 0xdf: //按下确认键 { k=0; TR0=1; n=-3; m=-3; f=0; if(alarm==1) { alarm=0; alarmflag1=1; lcd_w_cmd(0x01); } if(alarmflag2==1) { alarmflag1=0; alarmflag2=0; lcd_w_cmd(0x01); } } break; case 0xbf: //按下加1键 { if(k==1) { if(f==0) { if(n==0){year++;if(year==100) year=0;} else if(n==3) {month++;if(month==13) month=1;} else {day++;if(day==32) day=1;} dispd(); //调用第一行显示函数 lcd_w_cmd(0x0d); //光标闪烁 lcd_w_cmd(0x86+n);//返回设置值显示地址 } else { if(alarm==0) { if(m==0){hour++;if(hour==24) hour=0;} else if(m==3) {min++;if(min==60) min=0;} else if(m==6){sec++;if(sec==60) sec=0;} else {week++;if(week==7) week=0;} dispt(); //调用第二行显示函数 lcd_w_cmd(0x0d); //光标闪烁 lcd_w_cmd(0xc4+m);//返回设置值显示地址 } else { if(m==0){hourn++;if(hourn==24) hourn=0;} else if(m==3) {minn++;if(minn==60) minn=0;} disalarm(); //调用第二行显示函数 lcd_w_cmd(0x0d); //光标闪烁 lcd_w_cmd(0xc7+m);//返回设置值显示地址 } } } } break; case 0x7f: //按下减1键 { if(k==1) { if(f==0) { if(n==0){year--;if(year
4. 如何把一个复杂的C语言源代码分成几个文件,然后在dev c++上进行多文件编译
假设我们要更改的源代码如下:
即“No such file or directory”就是没有文件或者路径。说明你的路径写错了,在编程时务必要注意一下。
5. 给我一个较复杂的C语言程序
好,给你一个有趣的程序,下面程序的作用是,输入一个4位数,之后将这个数分解成4个数字,并将这4个数字组合成一个最大数和一个最小数,并将两者相减,结果作为新的4位数,重复前面的步骤,最后的结果是什么?自己测试一下。
#include <stdio.h>
void mysort(int *a, int size)
{
int i, j, k, t;
for (i = 0; i < size - 1; ++i)
{
k = i;
for (j = i; j < size; ++j)
{
if (a[k] > a[j])
k = j;
}
if (k != i)
{
t = a[i];
a[i] = a[k];
a[k] = t;
}
}
}
void main()
{
int num, a[4], max, min;
do
{
scanf("%d", &num); /* 输入最初的4位数 */
} while (num < 1000 || num > 9999);
while (num)
{
/* 将4位数分解成4个独立的数字,并保存在a数组中 */
a[0] = num % 10;
num /= 10;
a[1] = num % 10;
num /= 10;
a[2] = num % 10;
num /= 10;
a[3] = num;
/* 排序数组元素 */
mysort(a, 4);
/* 从数组的4个元素中产生最大数字和最小数字 */
max = a[3] * 1000 + a[2] * 100 + a[1] * 10 + a[0];
min = a[0] * 1000 + a[1] * 100 + a[2] * 10 + a[3];
/* 产生新的4位数 */
num = max - min;
printf("%d\n", num);
if (num == 6174)
break;
}
printf("The magic number is: %d\n", num);
}