符瑞艺 160809228_C语言程序设计实验2 选择结构程序设计

实验2-1 输入3个数,并按由大到小的顺序输出。
实验要求:
编写一个C程序,输入3个数,并按由大到小的顺序输出。
参考:
 
源码:
#include <stdio.h>
int     main(){
    int a,b,c,t;
    printf("10,20,111;");
    scanf("%d%d%d",&a,&b,&c);
    if(a<b){
        t=a;
        a=b;
        b=t;
        
    } 
        if(b>c){
            printf("%d	%d	%d
",a,b,c);
            
        }
        else if(c>a){
            printf("%d	%d	%d
",c,a,b);
        }
        else{
            
                printf("%d	%d	%d
",a,c,b);
                
            
        }
    return 0;        
}
运行结果抓图

实验2-2 从键盘上输入x的值,并根据计算输出y的值
实验要求:从键盘上输入x的值,并根据计算输出y的值
 
提示:
1.    使用数据函数需要#include <math.h>
2.    开方函数:sqrt(x)
3.    绝对值函数:fabs(x)
源码
#include <stdio.h>
#include <math.h>
    int     main(){
     float x,y;
     printf("请输入一个数"); 
         scanf("%f",&x);
         if(x>4){
         y=sqrt(x-4);
         printf("%f",y);
     }
         else if(x<-5){
         y=fabs(x);
         printf("%f",y);
     }
        else{
        
        y=x+3;
        printf("%f",y);
    }
    return 0;        
}

实验结果:

  
实验2-3从键盘上输入一个字母,如果是小写字母,将其转换成大写字母并输出。
实验要求:从键盘上输入一个字母,如果是小写字母,将其转换成大写字母并输出。
提示:
1.    输入字符给变量c
    char c;
方法一:c = getchar ();
方法二:scanf("%c",&c);
2.    输出字符变量c
方法一:putchar(c);
方法二:printf("%c",c);
    
程序源码
#include <stdio.h>
int main()
{
   char c;
   printf("请输入一个字符:
");
   scanf("%c",&c);
   printf("其大写字符是:%c
",c-32);
}
运行结果抓图

实验2-4从键盘上输入x的值,并根据计算输出y的值
实验要求:从键盘上输入x的值,并根据计算输出y的值
 
程序源码
#include <stdio.h>
int main()
{ 
    int x,y;
    printf("请输入一个数
");
    scanf("%d",&x);
        if(x<1){
        y=x;
        printf("%d",y);
    }
     else if(1<=x<10){
         y=2*x-1;
         printf("%d",y);
     }
     else{
         y=3*x-11;
         printf("%d",y);
     }
   return 0;
    }

实验心得
通过这次试验,我明白了C语言一些使用技巧,使我朝着C语言这大门又走进了一步,虽然这次实验比上次多了些步骤,但也让我感受到C语言的魅力,这次作业教得晚了,下次作业我会早点交
原文地址:https://www.cnblogs.com/furuiyi228/p/5915545.html