当前位置:主页 > 查看内容

最简单的C程序举例——选择结构设计

发布时间:2021-05-08 00:00| 位朋友查看

简介:最简单的C程序举例——选择结构设计 1输入两个实数按从小到大的顺序输出这两个数 # include stdio.h int main ( ) { float a , b , t ; scanf ( %f%f , a , b ) ; if ( a b ) { t a ; a b ; b t ; } printf ( %4.2f\n%4.2f , a , b ) ; return 0 ; } 输入内……

最简单的C程序举例——选择结构设计

1,输入两个实数,按从小到大的顺序输出这两个数

#include"stdio.h"
int main()
{
	float a,b,t;
	scanf("%f%f",&a,&b);
	if(a>b)
	{
		t=a;
		a=b;
		b=t;
	}
	printf("%4.2f\n%4.2f",a,b);
	return 0;
 } 

输入内容:

3.6
3.2

运行结果:

3.20
3.60

//同理,判断三个的输出顺序。

#include"stdio.h"
int main()
{
	float a,b,c,t;
	scanf("%f%f%f",&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("%4.2f\n%4.2f\n%4.2f",a,b,c);
	return 0;
 } 

输入内容:

3.9
3.6
3.7

运行结果:

3.60
3.70
3.90

2,编一程序,输入一个小于1000的正数,输出其平方根(平方根不为整数,则输出其整数部分)

#include"stdio.h"
#include"math.h"
#define m 1000
int main()
{
	int i,k;
	printf("输入一个小于%d的整数i:",m);
	scanf("%d",&i);
	if(i>m)
	{
		printf("输入的数据不符合要求,请重新输入一个小于%d的整数i:",m);
		scanf("%d",&i);
	}
	k=sqrt(i);
	printf("%d的平方根的整数部分是%d\n",i,k);
	return 0;
 } 

输入内容:

输入一个小于1000的整数i:999

运行结果:

999的平方根的整数部分是31

3,写程序,输入x的值,输出y相应的值

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

输入内容:

4

运行结果:

x=4
y=7

4,给出一百分制成绩,要求输出成绩等级“A,B,C,D,E”.90以上为A,以此类推,60以下为E。

#include"stdio.h" 
int main()
{
	float score;
	char grade;
	printf("请输入学生成绩:");
	scanf("%f",&score);
	while(score>100 || score<0)
	{
	    printf("成绩有误,请重新输入"); 
	    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("成绩是%3.1f,相应等第为%c\n",score,grade);
	return 0; 
}

输入内容:

请输入学生成绩:90.5

运行结果:

成绩是90.5,相应等第为A

5,给出一个不多于5位的正整数,要求:

1)求出它是几位数;
2)分别输出每一位数字;
3)按逆序输出各位数字;

#include"stdio.h" 
#include"math.h"
int main()
{
	int num,indiv,ten,hundred,thousand,ten_thousand,place;
	printf("请输入一个整数(0~99999):");
	scanf("%d",&num);
	if(num>9999)
	place=5;
	else if(num>999)
	place=4;
	else if(num>99)
	place=3;
	else if(num>9)
	place=2;
	else
	place=1;
	printf("位数:%d\n",place);
	printf("每位数字为:");
	ten_thousand=num/10000; 
	thousand=(int)(num-ten_thousand*10000)/1000;
	hundred=(int)(num-ten_thousand*10000-thousand*1000)/100;
	ten=(int)(num-ten_thousand*10000-thousand*1000-hundred*100)/10;
	indiv=(int)(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10);
	switch(place)
	{
		case 5:
			printf("%d,%d,%d,%d,%d\n",ten_thousand,thousand,hundred,ten,indiv);
			printf("反序数为:");
			printf("%d%d%d%d%d",indiv,ten,hundred,thousand,ten_thousand);
			break;
		case 4:	 
		    printf("%d,%d,%d,%d\n",thousand,hundred,ten,indiv);
			printf("反序数为:");
			printf("%d%d%d%d",indiv,ten,hundred,thousand);
			break;
		case 3:
			printf("%d,%d,%d\n",hundred,ten,indiv);
			printf("反序数为:");
			printf("%d%d%d",indiv,ten,hundred);
			break;
		case 2:
			printf("%d,%d\n",ten,indiv);
			printf("反序数为:");
			printf("%d%d",indiv,ten);
			break;
		case 1:
			printf("%%d\n",indiv);
			printf("反序数为:");
			printf("%d",indiv);
			break;
	}
	return 0;
}

输入内容:

请输入一个整数(0~99999):96584

运行结果:

位数:5
每位数字分别为:9,6,5,8,4
反序数为:48569
;原文链接:https://blog.csdn.net/qq_45824568/article/details/115415304
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐