第三次作业

实验一:

1. 字符判断

     输入一个字符,判断它如果是小写字母输出其对应大写字母;如果是大写字母输出其对应小写字母;如果是数字输出数字本身;如果是空格,输出“space”;如果不是上述情况,输出“other”。

#include <stdio.h>
int main()
{
	char c;
    printf("请输入:\n");
	scanf("%c",&c);
	if (c>='65'&&c<='90')
	{
	    c=c+32;
	    printf("%c",c);
	} 
	else if (c>='97'&&c<='122')
	{
	    c=c-32;
	    printf("%c",c);
	}
	else if (c==' ')
	{
		printf("space");
	}
	else if(c>='0'&&c<='9')
	{
        printf("%c",c);
	}
	else
	{
	    printf("other");
	}
	return 0; 
}

  

实验总结:

1:char字符记得带‘’ 

2:等号为==   ==  ==  ==

3:取地址符&&&&&&&&&&&&

4:char字符只能读取一个字符

实验二:

 2. 年龄问题

     输入一个学生的生日(年:月:日),并输入当前日期(年:月:日),计算该生的实际年龄(周岁)。

 

 

#include <stdio.h>
int main()
{
	int x,y,z,l,m,n,s;
	printf("输入:\n");
	scanf("%d%d%d",&l,&m,&n);
	printf("你的生日为%d年%d月%d日\n",l,m,n);
	scanf("%d%d%d",&x,&y,&z);
	printf("今天日期为%d年%d月%d日\n",x,y,z);
	if (x<l||x<=0||l<0)
	{
		printf("输入错误");
	}
	else if (x%4==0&&x%100!=0||x%400==0)
	{
		if (y<1||y>12||m<1||m>12)
		{
		    printf("输入错误");
		}
		else
		{
		    if (y==1||y==3||y==5||y==7||y==9||y==11||y==12)
		    {
			    if (z>=32||z<=0)
			    {
				    printf("输入错误"); 
			    }
			    else if (m==1||m==3||m==5||m==7||m==8||m==10||m==12)
			    {
			        if (n>31||n<=0)
			        {
			    	    printf("输入错误");
				    } 
				    else 
				    {   
					    if (y>m)
					    {
						    s=x-l;
						    printf("你今年%d岁了",s); 
					    }
					    else if (y<m)
					    {
						    s=x-l-1;
						    printf("你今年%d岁了",s); 
					    }
					    else 
					    {
						    if (z>=n)
						    {
							    s=x-l;
						        printf("你今年%d岁了",s); 
						    }
						    else 
						    {
							    s=x-l-1;
							    printf("你今年%d岁了",s); 
						    }
					    }
				    }
			    }
			    else if (m==4||m==6|m==9||m==11)
			    {
				    if (n>30||n<1)
				    {
				        printf("输入错误"); 
			        }
			        else
			        {
			    	    if (y>m)
					    {
						    s=x-l;
						    printf("你今年%d岁了",s); 
					    }
					    else 
					    {
						    s=x-l-1;
						    printf("你今年%d岁了",s); 
					    }
				    }
			    }
			    else
			    {
				    if (n<1||n>29)
				    {
					    printf("输入错误"); 
				    }
				    else if (y>m)
				    {
					    s=x-l;
					    printf("你今年%d岁了",s); 
				    }
				    else
				    {
					    s=x-l-1;
					    printf("你今年%d岁了",s); 
				    }
			    }
		    }
		    else if (y==4||y==11||y==6||y==9)
		    {
			    if (z>30||z<1||m<=0||m>12)
			    {
				    printf("输入错误");
			    }
			    else if (m==1||m==3||m==5||m==7||m==8||m==10||m==12)
			    {
			        if (n>31||n<1)
			        {
			    	    printf("输入错误");
				    }
				    else if (y>m)
				    {
					    s=x-l;
					    printf("你今年%d岁了",s);
				    }
				    else
				    {
					    s=x-l-1;
					    printf("你今年%d岁了",s); 
				    }  
		        }
		        else if (m=4||m==6||m==9||m==11)
		        {
		    	    if (n>30||n<1)
		    	    {
		    		    printf("输入错误");
				    }
				    else if (y>=m)
				    {
					    s=x-l;
					    printf("你今年%d岁了",s);
				    }
				    else 
				    {
					    if (z>=n)
					    {
						    s=x-l;
					        printf("你今年%d岁了",s);
					    }
					    else 
					    {
						    s=x-l-1;
					        printf("你今年%d岁了",s); 
					    }
				    }
			    }
			    else
			    {
				    s=x-l;
				    printf("你今年%d岁了",s);
			    }
		    }
		    else
		    {
			    if (z>29||z<1||m<=0||m>12)
			    {
				    printf("输入错误");
			    }
			    else if (y<m)
			    {
				    s=x-l-1;
				    printf("你今年%d岁了",s); 
			    }
			    else if (y>m)
			    {
				    s=x-l;
				    printf("你今年%d岁了",s); 
			    }
			    else
			    {
				    if (z>=n)
				    {
					    s=x-l;
				        printf("你今年%d岁了",s); 
				    }
				    else
				    {
					    s=x-l-1;
				        printf("你今年%d岁了",s);
				    }
		    	}
	    	}
    	} 
    }
    else
    {
    	if (y<1||y>12||m<1||m>12)
		{
		    printf("输入错误");
		}
		else
		{
		    if (y==1||y==3||y==5||y==7||y==9||y==11||y==12)
		    {
			    if (z>=32||z<=0)
			    {
				    printf("输入错误"); 
			    }
			    else if (m==1||m==3||m==5||m==7||m==8||m==10||m==12)
			    {
			        if (n>31||n<=0)
			        {
			    	    printf("输入错误");
				    } 
				    else 
				    {   
					    if (y>m)
					    {
						    s=x-l;
						    printf("你今年%d岁了",s); 
					    }
					    else if (y<m)
					    {
						    s=x-l-1;
						    printf("你今年%d岁了",s); 
					    }
					    else 
					    {
						    if (z>=n)
						    {
							    s=x-l;
						        printf("你今年%d岁了",s); 
						    }
						    else 
						    {
							    s=x-l-1;
							    printf("你今年%d岁了",s); 
						    }
					    }
				    }
			    }
			    else if (m==4||m==6|m==9||m==11)
			    {
				    if (n>30||n<1)
				    {
				        printf("输入错误"); 
			        }
			        else
			        {
			    	    if (y>m)
					    {
						    s=x-l;
						    printf("你今年%d岁了",s); 
					    }
					    else 
					    {
						    s=x-l-1;
						    printf("你今年%d岁了",s); 
					    }
				    }
			    }
			    else
			    {
				    if (n<1||n>29)
				    {
					    printf("输入错误"); 
				    }
				    else if (y>m)
				    {
					    s=x-l;
					    printf("你今年%d岁了",s); 
				    }
				    else
				    {
					    s=x-l-1;
					    printf("你今年%d岁了",s); 
				    }
			    }
		    }
		    else if (y==4||y==11||y==6||y==9)
		    {
			    if (z>30||z<1||m<=0||m>12)
			    {
				    printf("输入错误");
			    }
			    else if (m==1||m==3||m==5||m==7||m==8||m==10||m==12)
			    {
			        if (n>31||n<1)
			        {
			    	    printf("输入错误");
				    }
				    else if (y>m)
				    {
					    s=x-l;
					    printf("你今年%d岁了",s);
				    }
				    else
				    {
					    s=x-l-1;
					    printf("你今年%d岁了",s); 
				    }  
		        }
		        else if (m=4||m==6||m==9||m==11)
		        {
		    	    if (n>30||n<1)
		    	    {
		    		    printf("输入错误");
				    }
				    else if (y>=m)
				    {
					    s=x-l;
					    printf("你今年%d岁了",s);
				    }
				    else 
				    {
					    if (z>=n)
					    {
						    s=x-l;
					        printf("你今年%d岁了",s);
					    }
					    else 
					    {
						    s=x-l-1;
					        printf("你今年%d岁了",s); 
					    }
				    }
			    }
			    else
			    {
				    s=x-l;
				    printf("你今年%d岁了",s);
			    }
		    }
		    else
		    {
			    if (z>28||z<1||m<=0||m>12)
			    {
				    printf("输入错误");
			    }
			    else if (y<m)
			    {
				    s=x-l-1;
				    printf("你今年%d岁了",s); 
			    }
			    else if (y>m)
			    {
				    s=x-l;
				    printf("你今年%d岁了",s); 
			    }
			    else
			    {
				    if (z>=n)
				    {
					    s=x-l;
				        printf("你今年%d岁了",s); 
				    }
				    else
				    {
					    s=x-l-1;
				        printf("你今年%d岁了",s);
				    }
		    	}
	    	}
    	} 
	}
	return 0;
}

  

   耐心写。。。总能写出来。。。

实验三:

3. 判断三角形类型

  输入三个整数,判断由其构成的三角形的类型(等边三角形、等腰三角形、等腰直角三角形、直角三角形、一般三角形以及非三角形)

#include <stdio.h>
int main()
{
	int a,b,c;
	printf("请输入三个数:\n");
	scanf("%d%d%d",&a,&b,&c);
	if (c*c==a*a+b*b)
	{
		if (a==b||a==c||b==c)
		{
			printf("等腰直角三角形"); 
		}
		else
		{
			printf("直角三角形");
		}
	} 
	else if (a==b||a==c||b==c)
	{ 
		if (a==b&&a==c&&b==c)
		{
			printf("等边三角形"); 
		}
		else
		{
			printf("等腰三角形"); 
		}
	} 
	else if (a<b+c&&a>b-c||b<a+c&&b>a+c||c<a+b&&c>b+a)
	{
		printf("一般三角形");
	}
	else
	{
		printf("非三角形");
	} 
	return 0;
}

  

实验四:

 4. 看商品猜价格小游戏(图略)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int a,b;
srand(time(NULL));
printf("请输入你猜的价格:\n");
scanf("%d",&a);
b=rand()%100+1;
if (a==b)
{
printf("great");
}
else
{
printf("error !!much is %d",b);
}
return 0;
}

  

附加题:

你买了一箱n个苹果,很不幸的是买完时箱子里混进了一条虫子。虫子每x小时能吃掉一个苹果,假设虫子在吃完一个苹果之前不会吃另一个,那么经过y小时你还有多少个完整的苹果?

   输入n,x和y(均为整数),输出剩下的苹果个数。

   测试数据:  10  4  9

                    5  3  16

#include <stdio.h>
int main()
{
	int n,z,m;
	float a,x,y;
	printf("请输入:\n");
	scanf("%d%d%d",&n,&x,&y);
	z=y/x;
	a=y/x;
	if (z>=n)
	{
		printf("苹果已经吃光了!");
	}
	else
	{
		if (z<a)
		{
			m=n-z-1;
		    printf("还剩%d个苹果",m);
		}
		else 
		{
			m=n-z;
			printf("还剩%d个苹果",m);
		}
	}
	return 0;
}

  

知识点:

1:随机数的新函数库以及新的语句

2:if else  语句运用以及内嵌

3:char字符的细节问题

4:或与非字符

实验总结:

1:char字符记得带‘’ 

2:等号为==   ==  ==  ==

3:取地址符&&&&&&&&&&&&

4:char字符只能读取一个字符

5:char字符的细节问题

6:单词拼写错误

7:变量定义重复

原文地址:https://www.cnblogs.com/yanchao980817/p/5971626.html