作业

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

实验要求:

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

参考:

源码:#include<stdio.h>

int main(void)

       {

       int a,b,c,t;

       printf("请输入三个整数:");

       scanf("%d%d%d",&a,&b,&c);

       if(a<b){//如果a<b,则a与b交换

              t=a;

              a=b;

              b=t;

       }

       //从此处开始,a>b

       if(b>c){

              printf("%d、t%d %d ",a,b,c);

       }

       else if(c>a){

              printf("%d %d %d ",c,a,b);

       }

       else{

              printf("%d %d %d ",a,c,b);

       }

}

运行结果抓图

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

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

提示:

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

源码

#include<stdio.h>

#include<math.h>

int main()

{

       double x,y;

       printf("Enter x:");

       scanf("%if",&x);

       if(x>4)

              y=sqrt(x-4);

       else if(x<-5)

              y=fabs(x);

       else

              y=x+3;

              printf("结果=%.2f ",y);

       return 0;

}

实验结果:

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

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

提示:

  1. 输入字符给变量c

    char c;

方法一:c = getchar();

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

  1. 输出字符变量c

方法一:putchar(c);

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

   

程序源码

运行结果抓图

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

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

程序源码

#include<stdio.h>

#include<math.h>

int main()

{

       double x,y;

       printf("输入 x:");

       scanf("%if",&x);

       if(x<1){

              y=x;

       }

       else if(x>=10){

              y=(x*3)-11;

       }

       else{

              y=(2*x)-1;

       }

       printf("结果=%.2f ",y);

       return 0;

}

运行结果抓图

实验2-5 给出一个百分制的成绩,要求出成绩等级’A’、’B’、’C’、’D’、’E’,其中90分以上输出’A’,80~89输出’B’,70~79输出’C’,60~69输出’D’,60分以下输出’E’。

实验要求:

给出一个百分制的成绩,要求出成绩等级’A’、’B’、’C’、’D’、’E’,其中90分以上输出’A’,80~89输出’B’,70~79输出’C’,60~69输出’D’,60分以下输出’E’。

提示:

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

方法一:使用if语句完成

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

程序源码

#include<stdio.h>

int main()

{    

       int x;

       printf("请输入成绩:");

while(scanf("%d",&x) && x>=0  &&  x<=100);

if(x>=90){

              printf("A");

       }

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

              printf("B;");

       }

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

              printf("C");

       }

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

              printf("D");

       }    

       else if(x<60)

{

       printf("E");

}

return 0;

}

运行结果抓图

实验心得

第四个不知道哪出错了,运行结果都为0

     第五个运行出来以后有一点小瑕疵,但是不知道问啥照片复制不过来,

虽然感觉这次的题比上次难,但是还是有许多的收获,我会认真的去学习这门课的

原文地址:https://www.cnblogs.com/Cynicisty/p/5910344.html