C语言实验


## C语言第一次实验

#include<stdio.h>
#include <stdlib.h>
#include <time.h>
#include<string.h>
#define N 200
void mouse();
void printfscreen();
void computernumber();
void guess();
void stringorder();
void Ab(char a[]);
void goaway(int *p);
void open(int *p);
void goguess();
void experiment01()
{
	int a=1,b=1,c=0;
	while(a)
	{
		if(b)
		{
			printf("*****************\n");
			printf("1: 老鼠咬坏账本\n2: 打印乘法口诀表\n3: 参加计算机竞赛人选\n4: 猜数游戏\n5: 字符串倒序输出\n6:退出程序\n7:  打开关闭提示    \n8:  质子的随机运动\n");
			printf("*****************\n");
		}
		printf("Please input number:");
		scanf("%d",&c);
		getchar();
		switch(c)
		{
		   case 1:/*老鼠咬坏账本*/
			   mouse();
			   break;
		   case 2:/*打印乘法口诀表*/
			   printfscreen();
			   break;
		   case 3:/*参加计算机竞赛人选*/
			   computernumber();
			   break;
		   case 4:/*猜数游戏*/
			   guess();
			   break;
		   case 5:/*字符串倒序输出*/
			   stringorder();
			   break;
		   case 6:/*退出程序*/
			   goaway(&a);
			   break;
		   case 7:/*打开关闭提示*/
			   open(&b);
			   break;
		   case 8:/*(质子的随机运动)*/
			   goguess();
			   break;
		   default:
			   printf("Invalid operator!\n");
		}
	}
	
}
void mouse()
{
	int i,j; //i表示第一个空上的数字,因为在个位上,取值0~9,j表示第二个空上的数字,因为在十位上,取值1~9
	long result;
	for(i=0;i<=9;i++)
		for(j=1;j<=9;j++)
		{
			result=(30+i)*4356+(10*j+8)*832;
			if(result==196360)
				printf("第一个数字=%d,第二个数字=%d\n",i,j);
		}
}
void printfscreen()
{
	int i,j;
	printf("******************************九九乘法口诀表******************************\n\n");
	for(i=1;i<10;i++)
	{
		for(j=1;j<=i;j++)
		{
			printf("%d*%d=%d   ",i,j,i*j);
		}
		putchar('\n\n');
	}
	printf("******************************九九乘法口诀表******************************\n");
}
void computernumber()
{
	int a,b,c,d,e,i;
	char w;
	for (a=0;a<2;a++)
		for(b=0;b<2;b++)
			for(c=0;c<2;c++)
				for(d=0;d<2;d++)
					for(e=0;e<2;e++)
					{
						if(a==1)
							if(b==1)
								if(b=c)
									if(d||e)
										if(e=1)
										{
											if(a==1&&d==1)
												goto END;
										}
										if(a!=1)
											if(b==c&&c!=d)
											{
												if(e==1)
													if(a==1&&d==1)
														goto END;
												if(e!=1)
													goto END;
											}
					}
END:printf("a=%d;b=%d;c=%d;d=%d;e=%d",a,b ,c,d,e);
					if(a==1)
					{
						w='a';
						printf("%c是参加",w);
					}
					if(b==1)
					{
						w='b';
						printf("%c是参加",w);
					}
					if(c==1)
					{
						w='c';
						printf("%c是参加",w);
					}
					if(d==1)
					{
						w='d';
						printf("%c是参加",w);
					}
					if(e==1)
					{
						w='e';
						printf("%c是参加",w);
					}
}
void guess()
{
	int a,i,b,n,c;
	srand((unsigned int)time(NULL));//设置当前时间为种子
	a=rand()%300+100;
	n=0;
	for(i=0;i<10;i++)
	{
		n++;
		printf("请输入你心中的300-100数字:");
		scanf("%d",&b);
		if(b<100||b>300)
		{
			c=3;
			printf("超出范围\n");
			continue;
		}
		if(a==b)
		{
			printf("恭喜你猜对了!猜了%d次,得分为:%d",(10-n)*10);
			c=1;
			break;
		}
		else
		{
			printf("抱歉答错了!还有%d次\n",10-n);
			c=0;
		}
	}
	if(c==0)
		printf("抱歉三次都没有答对!答案是:%d,得分为:0\n",a);
}
void stringorder()
{
	char str[N];
	printf("please input'.'end:");
	fgets(str,N,stdin);
	str[strlen(str)-1]='\0';
	Ab(str);
	printf("倒序后为:");
	puts(str);
}
void Ab(char a[])
{
	int n,i,j,t;
	n=strlen(a);
	for(i=0,j=n-2;i<=j;i++,j--)
	{
		t=a[i];
		a[i]=a[j];
		a[j]=t;
	}
	a[n-1]='\0';
}
void goaway(int *p)
{
	*p=0;
}
void open(int *p)
{
	*p=0;
}
void goguess()
{
	int z,r;
	int x=0,y=0,count=0;/*初始为坐标原点*/
	srand((unsigned int)time(NULL));/*设置当前时间为种子*/
	printf("Please input one of your heart number:");
	scanf("%d",&r);
	while((x*x)+(y*y)<(r*r))
	{
		z=rand()%4+1;/*根据时间随机生成一个方向*/
		if(z==1)
		{
			++x;
		}
		else if(z==2)
		{
			++y;
		}
		else if(z==3)
		{
			--x;
		}
		else
		{
			--y;
		}
		count++;
		printf("%d,%d\n",x,y);
	}
	printf("commen all is:%d\n",count);
}

## C语言第二次实验

#include<stdio.h>
#include <stdlib.h>
struct LNode Createlist_NULL();
void Createlist();
void print(struct LNode*L);
void insert_list(struct LNode *L,int x);
void delete_List(struct LNode *L,int x);
void reverse_List(struct LNode *L);
typedef struct LNode
{
	int data;
	struct LNode *next;
};
masain()
{
	struct LNode *L;
	int a=0,i,x,z;
	printf("菜单一:创建头结点\n菜单二:数据的输入\n菜单三:数据的插入\n菜单四:输入一个数并删除相同的数\n菜单五:数据的逆至置\n菜单六:结束\n");
	for(i=0;a!=6;i++)
	{
		printf("Please input one of number:");
		scanf("%d",&a);
		switch(a)
		{
		case 1:
			{
				L=(struct LNode*)malloc(sizeof(struct LNode));
			    if(L == NULL)
			   {
				   printf("No enough memory to allocate!\n");
				   exit(0);
			   }
			   L->next=NULL;
			   printf("Create new allocate successful\n");
			   break;
			}
		case 2:
			Createlist(L);
			break;
		case 3:
			{
				printf("please input insert number:");
				scanf("%d",&x);
				insert_list(L,x);
				print(L);
				break;
			}
		case 4:
			{
				printf("please input delate number:");
				scanf("%d",&z);
				delete_List(L,z);
				print(L);
				break;
			}
		case 5:
			{
				reverse_List(L);
				print(L);
				break;
			}
		case 6:
			continue;
		}
	}
	
}
struct LNode Createlist_NULL()
	{
		struct LNode *p;
		p=(struct LNode*)malloc(sizeof(struct LNode));
		if(p == NULL)
		{
			printf("No enough memory to allocate!\n");
			exit(0);
		}
		p->next=NULL;
		printf("Create new allocate successful\n");
		return *p;
	}
void Createlist(struct LNode *L)
{
	struct LNode *p,*q;
	int b;
	q=L;
	printf("Please enter the first number");
	scanf("%d",&b);
	while(b!=-1)
	{
		p=(struct LNode*)malloc(sizeof(struct LNode));
		if(p == NULL)
		{
			printf("No enough memory to allocate!\n");
			exit(0);
		}
		p->data=b;
		L->next=p;
		L=L->next;
		scanf("%d",&b);
	}
	L=q;
	p->next=NULL;
}
void print(struct LNode *L)
{
	struct LNode *p;
	p=L->next;
	if(!p)
	{
		printf("This number is empty\n");
		return;
	}
	while(p)
	{
		printf("%d ",p->data);
		p=p->next;
	}
	putchar('\n');
}
void insert_list(struct LNode *L,int x)
{
	struct LNode *p,*q;
	q=L;
	while(q->next)
	{
		q=q->next;
	}
	p=(struct LNode*)malloc(sizeof(struct LNode));
	if(p == NULL)
	{
		printf("No enough memory to allocate!\n");
		exit(0);
	}
	p->data=x;
	q->next=p;
	p->next=NULL;
};
void delete_List(struct LNode *L,int x)
{
	struct LNode *p,*q,*y;
	q=L;
	while(q)
	{
		if(q->data==x)
		{
			p=q->next;
			y->next=p->next;
		}
		y=q;
		q=q->next;
	}
}
void reverse_List(struct LNode *L)
{
	struct LNode *p,*q;
	p=L->next;
	L->next=NULL;
	while(p)
	{
		q=p->next;
		p->next=L->next;
		L->next=p;
		p=q;
	}
}



``


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值