• 第二次C语言作业


    实验一:判断成绩等级。

    给定一百分制成绩,要求输出成绩的等级。90以上为A,80-89为B,70-79为C,60-69为D,60分以下为E,输入大于100或小于0时输出“输入数据错误”。 分别用if和用switch语句实现

    #include <stdio.h>
    int main()
    {
    	int a;
    	printf("请输入你的分数:\n");
    	scanf("%d",&a); 
    	if (a<0||a>100)
    	{
    		printf("数据输入错误!\n");
    	}
    	else if(a>=90)
    	{
    		printf("你的成绩为A\n");
    	}
    	else if(a>=80)
    	{
    		printf("你的成绩为B\n");
    	}
    	else if(a>=70)
    	{
    		printf("你的成绩为C\n"); 
    	}
    	else if(a>=60)
    	{
    		printf("你的成绩为D\n");
    	}
    	else
    	{
    		printf("你的成绩为E\n");
    	}
            return 0;
    }
    

      

    知识点:

    1:if  else if语句

    #include <stdio.h>
    int main()
    {
    	int a,b;
    	printf("请输入你的成绩:\n");
    	scanf("%d",&a);
    	if (a>100||a<0)
    	{
    		printf("数据输入错误!\n");
    	}
    	else
    	{
    		switch (b=a/10)
    		{
    		    case 10 :
    			    printf("你的成绩为A\n");
    			    break;
    			case 9 :
    			    printf("你的成绩为A\n");
    			    break;
    		    case 8 :
    			    printf("你的成绩为B\n");
    			    break;
    		    case 7 :
    			    printf("你的成绩为C\n");
    			    break;
    		    case 6 :
    			    printf("你的成绩为D\n");
    			    break;
    		    default :
    			    printf("你的成绩为E\n");
    	    }
    	}
            return 0;
    }
    

      

    知识点;

    1:switch语句

    2:if语句,内套switch语句

    实验总结:
    1:else 语句同行结尾不加 ;
    2:记得验证
    3:case 语句后面只接字符。

    实验二:判断整数位数及逆序输出。

            输入一个不多于5位数的正整数,判断它是几位数并逆序输出。注意验证数据的合法性。

    #include <stdio.h>
    int main()
    {
    	int a, b,c,d,e,f,g;
    	printf("请输入一个数:\n");
    	scanf("%d",&a);
    	if (a>=10000&&a<100000)
    	{
    		printf("该数是五位数\n");
    		b=a%10;
    		c=a/10%10;
    		d=a/100%10;
    		e=a/1000%10;
    		f=a/10000;
    		g=10000*b+1000*c+100*d+10*e+1*f;
    		printf("倒序数为:%05d",g);
    	}
    	else if (a>=1000&&a<10000)
    	{
    		printf("该数是四位数\n");
    		b=a%10;
    		c=a/10%10;
    		d=a/100%10;
    		e=a/1000;
    		g=b*1000+c*100+d*10+e*1;
    		printf("该数的倒序数为:%04d",g);
        } 
        else if (a>=100&&a<1000)
        {
        	printf("该数是三位数\n");
        	b=a%10;
        	c=a/10%10;
        	d=a/100;
        	g=b*100+c*10+d*1;
        	printf("该数的倒序数是:%03d",g);
    	}
    	else if (a>=10&&a<100)
    	{
    		printf("该数是两位数\n");
    		b=a%10;
    		c=a/10;
    		g=b*10+c*1;
    		printf("该数的倒序数为:%02d",g);
    	}
    	else if (a>=0&&a<10)
    	{
    		printf("该数为个位数\n");
    		printf("该数的倒序数为%d",a);
    	}
    	else 
    	{
    		printf("intput error");
    	}
         return 0;
    }
    

      

    知识点:

    1:if  else if语句

    2:求某个数某一位数的值

    实验总结:
    1:注意分号的放置位置。
    2:修饰符的正确使用
    3:计算某位数取值时记得验证

     实验三:回文数问题

            给定一个5位数,判断它是否是回文数。例如:12321是回文数。回文数的特点是个位和万位相同,十位和千位相同。

    #include <stdio.h>
    int main()
    {
    	int a,b,c,d,e;
    	printf("请输入一个五位数:\n");
    	scanf("%d",&a);
    	b=a%10;
    	c=a/10000;
    	d=a/10%10;
    	e=a/1000%10;
    	if (b==c&&d==e)
    	{
    		printf("该数为回文数");
    	}
    	else 
    	{
    		printf("该数不是回文数");
    	}
            return 0;
     } 
    

      

    实验总结:
    1:scanf的取地址符&&&&&&&
    2:求某一位数的灵活多变

    实验四:计算分段函数

            y=-x+2.5                         0 <= x < 5

            y=2-1.5(x-3)(x-3)        5 <= x < 10

            y=x/2-1.5                     10 <= x < 20

           输入x的值(x为整数),输出y的值,结果保留3位小数。

    #include <stdio.h>
    int main()
    {
         int x;
         float y;
         printf("请输入一个数:\n");
    	 scanf("%d",&x);
    	 if (x<0||x>=20) 
    	 {
    	 	printf("数据输入错误!\n");
    	 }
    	 else if (x>=0&&x<5)
    	 {
    	 	y=x+2.5;
    	 	printf("y的值为%.03f",y);
    	 }
    	 else if (x>=5&&x<10)
    	 {
    	 	y=2-1.5*(x-3)*(x-3);
    	 	printf("y的值为%.03f",y);
    	 }
    	 else
    	 {
    	 	y=x/2-1.5;
    	 	printf("y的值为%.03f",y);
    	 }
          return 0;
    }

      

    知识点:if    else if 语句

    实验总结:
    1:float的格式字符为f
    2:整数做除,只输出整数。
    3:保留小数修饰符为 .n

  • 相关阅读:
    MP3/4维修全攻略
    看图学维修mp3之电源篇65Z8\65Z5
    CSS按钮样式之button标签与input type=button的区别详解
    【原】PNG的使用技巧
    【原】[webkit移动开发笔记]之禁止触发系统默认菜单
    【原】使用iScroll.js解决ios4下不支持position:fixed的问题
    【原】YUI压缩与CSS media queries下的bug
    【翻译】Building a Simple Blog Engine with ASP.NET MVC and LINQ Part 2
    【翻译】Building a Simple Blog Engine with ASP.NET MVC and LINQ Part 4
    .NET技术书籍推荐
  • 原文地址:https://www.cnblogs.com/yanchao980817/p/5954242.html
Copyright © 2020-2023  润新知