实验3

#include <math.h>
#include <stdlib.h>
#include <stdio.h>
int main() 
{
	float a, b, c, x1, x2;
	float delta, real, imag;
	printf("Enter a, b, c: ");
	while(scanf("%f%f%f", &a, &b, &c) != EOF) 
	{
		if(a == 0)
		printf("not quadratic equation.\n\n");
		else 
		{
			delta = b*b - 4*a*c;
			if(delta >= 0) 
			{
				x1 = (-b + sqrt(delta)) / (2*a);
				x2 = (-b - sqrt(delta)) / (2*a);
				printf("x1 = %.2f, x2 = %.2f\n\n", x1, x2);
			}
			else 
			{
				real = -b/(2*a);
				imag = sqrt(-delta) / (2*a);
				printf("x1 = %.2f + %.2fi, x2 = %.2f - %.2fi\n\n", real,imag, real, imag);
			}
		}
	printf("Enter a, b, c: ");
	}
	system ("pause");
	return 0;
}

  

任务2:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
	int a,b,n,N;
	printf("给N赋值,输出N个0~9的整数\n"); 
	scanf("%d",&N);
	n=0;
	srand(time(NULL));
	do
	{
		a=rand()%10;
		printf("%-3d",a);
		n++;
	}while(n<=N);	
	printf("\n");
	system("pause");
	return 0;
} 

  

任务3:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
	int a,c,i;
	printf("输出101到200之间所有的素数\n");
	c=0;
	for(i=101;i<=200;i++)
	{
		for(a=2;a<=sqrt(i);a++)
		{
			
			if(i%a==0)
			{	
				break;
			}
		}
		if(a>sqrt(i))
		{
			printf("%d  ",i);
			c=c+1;
			if(c%5==0)
				printf("\n");
		}
	}
	printf("\n");
	printf("101到200之间有%d个素数",c);
	system("pause");
	return 0;
}

  

任务4:

#include <stdio.h>
#include <stdlib.h>
int main()
{
	int a,c;
	long s,xin;
	printf("enter a number:");
	
	while(scanf("%ld",&s)!=EOF)
	{	
		
		xin=0;
		c=1;
		while(s>0)
		{	
			
			a=s%10;
			s=s/10;
			if(a%2!=0)
			{
				xin=xin+c*a;
				c=c*10;	
			}
		}
		if (xin==0)
			printf("输入数据中不含奇数\n");
		else	 
			printf("new number is:%ld\n",xin);
		
		printf("enter a number:");
	}
	
	system("pause");
	return 0;
	
		
} 

  

思路:

关于如何取出数位上为奇数的数字,其实只要在经典的逆序输出的代码的基础上加上一个判定条件,即加入一个if语句,若为奇数则取出,反之不取出。
而如何保证高位的任在高位,低位的任在地位,我首先想到的一个笨方法是:两次逆序输出,即可将目标结果正序输出。然后,深入思考后,其实只要变换逆序输出代码中的
sum=sum*10+取出数字 为取出数字乘以一个每次翻十倍的整十数加上原先的结果即可。

任务5:

#include <stdio.h>
#include <stdlib.h>
int main()
{
	int n,i;
	double a,b,s;
	printf("Enter n(1~10):"); 
	scanf("%d",&n);
	do
	{
		if(n<1||n>10)
			printf("输入数据有误\n"); 
		else
		{
			b=1;
			s=0;
			a=1;
			for(i=1;i<=n;i++)
			{
				b=b*i;
				s=s+a/b;
				a=-a;		
			}
			printf("n=%d,s=%lf\n",n,s);
		}
		
		printf("Enter n(1~10):");
		
	}while(scanf("%d",&n)!=EOF);
	
	system("pause");
	return 0;
	
}

  

任务6:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
	srand(time(NULL));
	int a,b,day;
	printf("猜猜2020年12月哪一天会是你的luck day\n");
	day=rand()%31+1;
	printf("开始咯,你有三次机会,猜吧(1~31):");
	b=1;
	scanf("%d",&a);
	do
	{
	
		if(a==day)
		{
			printf("恭喜你,猜对了,你的luck day是%d号",a);
			break;
		}
		
		else if (a>day)
		{
			printf("你猜的日期晚了,Luck day 悄悄溜到前面去了\n");
			b++;
		}
		
		else if(a<day)
		{
			printf("你猜的日期早了,luck day还没到呢\n");
			b++;
		}
		
		
		
		if(b==4)
		{
			printf("次数用完了,偷偷告诉你,你的luck day是%d号",day);
			break;
		}
		
			
		printf("再猜(1~31):");
		
	}while(scanf("%d",&a)!=EOF);
	

	system("pause");
	return 0;
}

  

实验总结:三种代码结构的综合应用可以解决大多数问题,且相同的问题可以有多种解法。但是,实际在操作时,我常常容易见缝插针,缺什么补什么,最后看代码的时候会发现自己写的
代码有点冗长。所以,平时对同一问题的思考,应该尝试去优化程序代码与解决方案。

尚存的问题:首先关于随机数的生存,srand函数似乎可以根据时间点产生一个种子,从而使rand函数可以实现随机生成。但是,当将srand函数放在循环当中却无法实现随机数的生成,解释是
for循环速度太快,srand实际是生成一个数表,rand根据随机数表按顺序生成数字。我希望有更为具体形象的解释。另一个问题是在循环中加入scanf函数时,例如:while(scanf() )当输入ctrl+z时返回值是-1,
根据逻辑规则非0为真,所以执行循环,但似乎只是将上一次的数据结果再输出一遍,这里存在疑虑。最后的一个问题则是,循环中输入某些特定的字符会使循环进入无限重复的输出。

 

posted @ 2020-11-16 21:35  三尺有明  阅读(61)  评论(0)    收藏  举报