选择结构程序代码参考

这里写图片描述

*1,求y值*/
#include <stdio.h>
int main()
{
	float x,y;
	printf("please input a number x=\n");
	scanf("%f",&x);
	if(x<1)
	{
	    y=x;
	    printf("y=%5.2f\n",y);
	}
	else if(x>=10)
	{
		y=3*x-11;
printf("y=%5.2f\n",y);
	}
	else 
	{
		y=2*x-1;
		printf("y=%5.2f\n",y);
	}
	
	return 0;
}

/*2,输入三整数,由小到大输出*/

#include <stdio.h>
int main()
{
  int a,b,c;
int t;

  scanf("%d %d %d",&a,&b,&c);
  if (a>b) {t=a;a=b;b=t;}
  if(a>c)  { t=a; a=c; c=t; }
  if(b>c)  { t=b; b=c; c=t;}
   
 printf("a=%d,b=%d,c=%d\n",a,b,c);
}    
/*3,判断任意三个数能否构成三角形,如果能求三角形面积*/
#include<stdio.h>
#include<math.h>
int main()
{
	float a,b,c,s,area;
	printf("please input any digital a b c\n");
	scanf("%f,%f,%f",&a,&b,&c);
	if(a+b>c)
	{
	  s=(a+b+c)/2;
	  area=sqrt(s*(s-a)*(s-b)*(s-c));
	  printf("a=%f,b=%f,c=%f\narea=%7.1f",a,b,c,area);
	}
	else
	printf("a,b,c不能构成三角形"); 
	return 0;
}

/*4,关于ax²+bx+c中a,b,c的讨论*/
#include<stdio.h>
#include<math.h>
int main()
{ 
  float a,b,c,x,x1,x2,delta;
  printf("plese input a b c three number\n");
  scanf("%f,%f,%f",&a,&b,&c);
  if(a==0) 
  {
    if(b==0)
    {
  	 printf("it is not a quadratic equation\n");
    }
    else
      {
  	 x=-c/b;
  	 printf("it is a linear equation\nx=%7.2f",x);
      }
  }
  else
  {
  	delta=b*b-4*a*c;
  	if(delta>=0)
  	{
	  	 x1=(-b+sqrt(delta))/(2*a);
        x2=(-b-sqrt(delta))/(2*a);
    	printf("x1=%7.2f\nx2=%7.2f\n",x1,x2);
    }
  	else
  	{
	  	printf("no real root\n");
    } 	
  } 
  return 0;
}

/*5,输入一个至多五位数,判断他的位数,并将此数倒叙:*/
#include<stdio.h>
#include<math.h>
int main()
{
	int N,G,S,B,Q,W,P;
	printf("please input a number(0-99999):\n");
	scanf("%d",&N);
	if(N>9999)
	P=5;
	else if(N>999)
	P=4;
	else if(N>99)
	P=3;
	else if(N>9)
	P=2;
	else P=1;
	printf("place:%d\n",P);
	W=N/10000;
	Q=(int)(N-W*10000)/1000;
	B=(int)(N-W*10000-Q*1000)/100;
	S=(int)(N-W*10000-Q*1000-B*100)/10; 
	G=(int)(N-W*10000-Q*1000-B*100-S*10);
	switch(P){
		case(5):printf("the upside down number:");
		        printf("%d%d%d%d%d\n",G,S,B,Q,W);
		        break;
        case(4):printf("the upside down number:");
                printf("%d%d%d%d\n",G,S,B,Q);
                break;
        case(3):printf("the upside down number:");
                printf("%d%d%d\n",G,S,B);
                break;
        case(2):printf("the upside down number:");
                printf("%d%d\n",G,S);
                break;
        case(1):printf("the upside down number:");
                printf("%d\n",G);
                break;
	}
	return 0;
}

/*6、编写程序,要求从键盘输入成绩,输出对应的等级(100分为A,90-99为A,80-89为B,70-79为C,60-69为D,小于60为E)。*/
#include<stdio.h>
int main()
{   
    float score;
	char grade;
    printf("please input your score:");
    scanf("%f",&score);
    while(score>100||score<0)
    {
    	printf("your score is error!\n");
    	scanf("%f",&score);
    }
	switch((int)(score/10))
	{   
	    case 10:
		case 9:grade='A';break;
		case 8:grade='B';break;
		case 7:grade='C';break;
		case 6:grade='D';break;
		case 5:
		case 4:
		case 3:
		case 2:
		case 1:
		case 0:grade='E';
	}
	printf("your score is:%7.2f\nyour level is:%c\n",score,grade); 
 return 0;	
}


/*7、设计一个简单的计算机程序,用户输入运算数和四则运算符(+、-、*、/),输出计算结果。*/
#include<stdio.h>
#include<math.h>
int main()
{
 double a,b;
 char c;
 scanf("%lf%c%lf",&a,&c,&b);
 switch(c)
 {
 case'+':printf("=%g",a+b);break;
 case'-':printf("=%g",a-b);break;
 case'*':printf("=%g",a*b);break;
 case'/':printf("=%g",a/b);break;
 }
 return 0;
}
 , 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值