zoukankan      html  css  js  c++  java
  • 160809209_李梦鑫_C语言程序设计实验2 选择结构程序设计

    实验2-1 输入3个数,并按由大到小的顺序输出。

    实验要求:

    编写一个C程序,输入3个数,并按由大到小的顺序输出。

    源码:#include <stdio.h>

       int main()

    {
      int a,b,c,t;
      printf("请输入三个整数:");
      scanf("%d%d%d",&a,&b,&c);
      if(a<b){
        t = a;
        a = b;
        b = t;
      }
      if(b>c){
      printf("%d\t%d\t%d\n",a,b,c);
      }
      else if(c>a){
      printf("%d\t%d\t%d\n",c,a,b);
      }
      else{
      printf("%d\t%d\t%d\n",a,c,b);
      }
      return 0;
    }

     运行抓图:

           

    实验2-2 从键盘上输入x的值,并根据计算输出y的值

    实验要求:从键盘上输入x的值,并根据计算输出y的值

     

    提示:

    1. 使用数据函数需要#include <math.h>
    2. 开方函数:sqrt(x)
    3. 绝对值函数:fabs(x)

    源码:

    #include <math.h>

    int main(void)

    {

      double x,y;

       printf("输入X");

       scanf("%d",&x);

           if(x>4){

            y=sqrt(x-4);

            printf("%d\n",y);

        }

           else if(x<-5){

            y=fabs(x);

           printf("%d\n",y);

        }

            else{

            y=x+3;

            printf("%d\n",y);

        }

    }

     

    实验2-3从键盘上输入一个字母,如果是小写字母,将其转换成大写字母并输出。

    实验要求:从键盘上输入一个字母,如果是小写字母,将其转换成大写字母并输出。

    提示:

    1. 输入字符给变量c

        char c;

    方法一:c = getchar();

    方法二:scanf("%c",&c);

    1. 输出字符变量c

    方法一:putchar(c);

    方法二:printf("%c",c);

    程序源码:

    #include <stdio.h>

        int main(){

            char c;

            printf("请输入一个字母:");

            scanf("%c",&c);

            printf("%c\n",c-32);

        }

    #include <stdio.h>

        int main(){

        printf("请输入一个字母:");

        char c;

        c=getchar();

        if(c<='z' && c>='a')

        c=c-32;

        putchar(c);

    }

    运行截图:

    实验2-4从键盘上输入x的值,并根据计算输出y的值

    实验要求:从键盘上输入x的值,并根据计算输出y的值

     

     程序源码:

    #include <math.h>
    #include <stdio.h>
      int main(){
      int x,y;
      printf("输入X:");
      scanf("%d",&x);
      if(x<1){
      y=x;
      printf("%d\n",y);
    }
      else if(1<=x && x<10){
      y=(2*x-1);
      printf("%d\n",y);
    }
      else{
      y=(3*x-11);
      printf("%d\n",y);
          }
    }

     

    实验2-5 给出一个百分制的成绩,要求出成绩等级ABCDE,其中90分以上输出A8089输出B7079输出C6069输出D60分以下输出E

    实验要求:给出一个百分制的成绩,要求出成绩等级ABCDE,其中90分以上输出A8089输出B7079输出C6069输出D60分以下输出E

    提示:

    本实验要求同学们采用两种方法来完成:

    方法一:使用if语句完成

    方法二:使用switch语句完成。

     程序源码: 

    #include <stdio.h>

        int main(void){

        int x;

        printf ("输入成绩:");

        scanf("%d",&x);

        if(x>=90)

            printf("A");

        else if(x>=80 && x<=89)

            printf("B");

        else if(x>=70 && x<=79)

            printf("C");

        else if(x>=60 && x<=69)

            printf("D");

        else

            printf("E");

       }

     

                                                                                                     实验心得

       

       自我感觉刚开始c语言的题目难度很大,做起来有些许的困难,询问了同学,还有上网搜了一些东西,加上书上的例子,才勉强完成了,但是还是很多不太明白或者不熟悉的地方,还没有真正理解C语言的表达方式,以后还需要自己努力再认真多思考。对于自己的学习C语言,我想以后多点努力多点付出,打好基础。

     

  • 相关阅读:
    java web项目打包.war格式
    version 1.4.2-04 of the jvm is not suitable for thi
    Sugarcrm Email Integration
    sharepoint 2010 masterpage中必须的Content PlaceHolder
    微信开放平台
    Plan for caching and performance in SharePoint Server 2013
    使用自定义任务审批字段创建 SharePoint 顺序工作流
    Technical diagrams for SharePoint 2013
    To get TaskID's Integer ID value from the GUID in SharePoint workflow
    how to get sharepoint lookup value
  • 原文地址:https://www.cnblogs.com/lmx0925/p/5909836.html
Copyright © 2011-2022 走看看