C语言程序设计实验报告——实验四

实验四 选择结构

一、实验目的及要求

掌握选择和逻辑运算符优先级和表达式用法。
熟练掌握if语句、if else语句、switch语句和break语句的使用。

二、实验环境

硬件要求:计算机一台。
软件要求:Windows操作系统,Dev-C++或VC++6.0编译环境

三、实验内容

实验题目(1)

编写程序判定给定年份是否为闰年。年份由键盘输入。

博主表示,判断闰年的算法很简单,选择结构对于初学者来说应该不算什么难事,多加练习应该很快就能熟练。

源代码如下:

#include<stdio.h>
int main()
{
	int year;
	printf("请输入年份:");
	scanf("%d",&year);
	if(year%4==0&&year%100!=0||year%400==0)
		printf("%d年是闰年",year);
	else
		printf("%d年是平年",year);
    return 0;
}


实验题目(2)

在这里插入图片描述
(1)用if语句编程序;

源代码如下:

#include<stdio.h>
int main()
{
	long int I,bonus;
	printf("Please enter I:");
	scanf("%ld",&I);
	if(I<=100000)
	{
		bonus=I*1.10;
		printf("bonus is:%ld",bonus);
	}
	else if(100000<=I<=200000){	
		bonus=100000*1.10+(I-100000)*1.075;
		printf("bonus is:%ld",bonus);
	}	
	else if(200000<=I<=400000) {
		bonus=100000*1.10+100000*1.075+(I-200000)*1.05;
		printf("bonus is:%ld",bonus);										
	}
	else if(400000<=I<=600000) {
		bonus=100000*1.10+100000*1.075+200000*1.05+(I-400000)*1.03;
		printf("bonus is:%ld",bonus);
	}
	else if(600000<=I<=1000000){
		bonus=100000*1.10+100000*1.075+200000*1.05+200000*1.03+(I-600000)*1.015;
		printf("bonus is:%ld",bonus);
	}
	else if(I>1000000){
		bonus=100000*1.10+100000*1.075+200000*1.05+200000*1.03+400000*1.015+(I-1000000)*1.01;
		printf("bonus is:%ld",bonus); 
	}
 	return 0;
}

(2)用switch语句编程序。

源代码如下:

#include<stdio.h>
int main() {
	int I,bonus;
	printf("Pleaase enter I:");
	scanf("%d",&I);
	switch(I/100000) {
		case 0: {
			bonus=I*1.10;
			printf("bonus is:%ld",bonus);
			break;
		}
		case 1: {
			bonus=100000*1.10+(I-100000)*1.075;
			printf("bonus is:%ld",bonus);
			break;
		}
		case 2:{
			bonus=100000*1.10+100000*1.075+(I-200000)*1.05;
			printf("bonus is:%ld",bonus);
			break;
		}
		case 3:{
			bonus=100000*1.10+100000*1.075+(I-200000)*1.05;
			printf("bonus is:%ld",bonus);
			break;
		}
		case 4:{
			bonus=100000*1.10+100000*1.075+200000*1.05+(I-400000)*1.03;
			printf("bonus is:%ld",bonus);
			break;
		}
		case 5:{
			bonus=100000*1.10+100000*1.075+200000*1.05+(I-400000)*1.03;
			printf("bonus is:%ld",bonus);
			break;
		}
		case 6:{
			bonus=100000*1.10+100000*1.075+200000*1.05+200000*1.03+(I-600000)*1.015;
			printf("bonus is:%ld",bonus);
			break;
		}
		case 7:{
			bonus=100000*1.10+100000*1.075+200000*1.05+200000*1.03+(I-600000)*1.015;
			printf("bonus is:%ld",bonus);
			break;
		}
		case 8:{
			bonus=100000*1.10+100000*1.075+200000*1.05+200000*1.03+(I-600000)*1.015;
			printf("bonus is:%ld",bonus);
			break;
		}
		case 9:{
			bonus=100000*1.10+100000*1.075+200000*1.05+200000*1.03+(I-600000)*1.015;
			printf("bonus is:%ld",bonus);
			break;
		}
		default:{
			bonus=100000*1.10+100000*1.075+200000*1.05+200000*1.03+400000*1.015+(I-1000000)*1.01;
			printf("bonus is:%ld",bonus);
		}
	}
	return 0;
}


实验题目(3)

有4个圆塔,圆心分别为(2,2)、(-2,2)、(-2,-2)、(2,-2),圆半径为1,如图所示。这个塔的高度为10 m,塔以外无建筑物。今输入任一点坐标,求该点的建塔高度(塔外的高度为零)。

源代码如下:

#include <stdio.h>
#include <math.h>
int main() {
	int x, y, h;
	double p1, p2, p3, p4;
	printf("Please enter coordinate: ");
	scanf("%d %d", &x, &y);
	p1 = pow(x-2, 2) + pow(y-2, 2);
	p2 = pow(x-2, 2) + pow(y+2, 2);
	p3 = pow(x+2, 2) + pow(y-2, 2);
	p4 = pow(x+2, 2) + pow(y+2, 2);
	if(p1<=1 || p2<=1 || p3<=1 || p4<=1)
		h = 10;
	else
		h = 0;
	printf("The building height on this point is %d\n", h);
	return 0;
}


实验题目(4)

给出一百分制成绩,要求输出成绩等级‘A’,‘B’,‘C’,‘D’,‘E’。90分以上为‘A’,80-89 为‘B’,70-79分为‘C’,60-69分为‘D’,60分一下为‘E’。

这道题目,算法很简单,可以选择用if-else语句,也可以选择switch语句。博主还是比较喜欢switch语句。

源代码如下:

#include<stdio.h>
int main() {
	int score;
	printf("Please privide me with a score:");
	scanf("%d",&score);
	if(score>=0&&score<=100) {
		switch(score/10) {
			case 10:
			case 9:
				printf("A");
				break;
			case 8:
				printf("B");
				break;
			case 7:
				printf("C");
				break;
			case 6:
				printf("D");
				break;
			default:
				printf("E");
		}
	} else {
		printf("ERROR!");
	}
	return 0;
}

  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刚入坑的软件猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值