C语言第二次试验报告
C语言实验报告2
姓名:胡熙国 实验地点:一教524 实验时间:2021-4-15
一、实验目的与要求
- 了解C语言的基本语法要素,能熟练的将算法转化为语言程序
- 掌握各种形式的if语句语法和使用方法,注意if语句中if和else的匹配关系,以及if语句的嵌套
- 掌握switch语句的语法和使用方法
二、实验内容
实验一
1.问题描述
- 有一个分段函数,输入x的值计算出y
2.试验代码
#include <stdio.h>
void main()
{
float x, y;
printf("请输入x的值:");
scanf("%f", &x);
if (x < 1)
{
y = x;
}
else
{
if (x < 10)
{
y = 2 * x - 1;
}
else
{
y = 3 * x - 1;
}
}
printf("y的值为%.2f\n", y);
}
3.效果截图
4.问题分析
- 注意if和else的配对:与else配对的if是与else相邻的上一个if
试验二
1.问题描述
- 鸡兔同笼问题
2.试验代码
#include <stdio.h>
void main()
{
int h, f, y, x;
printf("请输入鸡和兔的总数,鸡和兔脚的总数:");
scanf("%d,%d", &h, &f);
if (x > 0 && y > 0)
{
y = (f - 2 * h) / 2;
x = h - y;
printf("鸡有%d 兔有%d\n", x, y);
}
else
{
printf("输入的信息有误\n");
}
}
3.效果截图
4.问题分析
- 先用方程将鸡和兔的表达式算出来
- 用if对输入的数据进行判断避免出错
试验三
1.问题描述
- 输入一个点的坐标判断是否在塔内,并打印出塔的高度
2.试验代码
#include <stdio.h>
#include <math.h>
void main()
{
int h = 10;
float x1, y1, x2, y2, x3, y3, x4, y4, x, y, d1, d2, d3, d4;
x1 = 2;
y1 = 2;
x2 = -2;
y2 = 2;
x3 = -2;
y3 = -2;
x4 = 2;
y4 = -2;
printf("请输入点(x,y):");
scanf("%f,%f", &x, &y);
d1 = sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
d2 = sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2));
d3 = sqrt((x - x3) * (x - x3) + (y - y3) * (y - y3));
d4 = sqrt((x - x4) * (x - x4) + (y - y4) * (y - y4));
if (d1 > 1 && d2 > 1 && d3 > 1 && d4 > 1)
{
h = 0;
printf("该塔的高度为%d\n", h);
}
else
{
printf("该塔的高度为%d\n", h);
}
}
3.效果截图
4.问题分析
- 在计算距离的时候用到sqrt函数要有math.h头文件
- 要输入的数据很多要细心
试验四
1.问题描述
- 模拟一个简单地计算器
2.试验代码
用switch实现
#include <stdio.h>
void main()
{
float x, y;
int input = 1;
while (input)
{
printf("1.add 2.sub \n 3.mul 4.div\n 0.exit\n");
printf("请选择>:");
scanf("%d", &input);
printf("请输入两个数操作数>:");
scanf("%f,%f", &x, &y);
switch (input)
{
case 1:
printf("%.2f\n", x + y);
break;
case 2:
printf("%.2f\n", x - y);
break;
case 3:
printf("%.2f\n", x * y);
break;
case 4:
if (y != 0)
{
printf("%.2f\n", x / y);
break;
}
else
{
printf("除数为零\n");
break;
}
case 0:
break;
default:
printf("输入的表达式有误,请重新选择\n");
}
}
}
用if实现
#include <stdio.h>
void main()
{
float x, y;
char ch;
int i;
printf("请输入要计算的表达式:");
scanf("%f%c%f", &x, &ch, &y);
if (ch == '+')
{
printf("%.2f\n", x + y);
}
else
{
if (ch == '-')
{
printf("%.2f\n", x - y);
}
else
{
if (ch == '*')
{
printf("%.2f\n", x * y);
}
else
{
if (ch == '/')
{
printf("%.2f\n", x / y);
}
}
}
}
}
3.效果截图
4.问题分析
- 程序的主要结构用的是switch语句
- 在case4里面用if判断了除数为0的情况,避免出错
- 在switch的外层用了while循环并且循环条件恒为真只有在选择到退出程序时才会为假从而退出程序,这样就使整个程序可以一直使用,并且在想退出是也能退出
试验五
1.问题描述
- 输入箱子的长宽高,判断箱子的形状
2.试验代码
#include <stdio.h>
void main()
{
int x,y,z;
printf("请输入箱子的长,宽,高\n");
scanf("%d,%d,%d",&x,&y,&z);
if(x==y&&y==z)
{
printf("该箱子是正方体\n");
}
else
{
printf("该箱子是长方体\n");
}
}
3.效果截图
试验六
1.问题描述
根据给出的条件计算应付款
2.试验代码
#include <stdio.h>
void main()
{
float x, y, z, sum;
int m;
printf("请输入打印纸,墨盒,光盘的数量:");
scanf("%f,%f,%f", &x, &y, &z);
sum = 18 * x + 132 * y + 4.5 * z;
m = sum / 100;
switch (m)
{
case 1:
printf("应付款:%.2f", sum * 0.95);
break;
case 2:
printf("应付款:%.2f", sum * 0.94);
break;
case 3:
printf("应付款:%.2f", sum * 0.93);
break;
case 4:
printf("应付款:%.2f", sum * 0.92);
break;
default:
if (m == 0)
{
printf("应付款:%.2f", sum);
}
else
{
printf("应付款:%.2f", sum * 0.90);
}
}
}
3.效果截图
试验七
1.问题描述
- 输入某一年和月,输出该月的天数
2.试验代码
#include <stdio.h>
void main()
{
int year, month, days;
printf("请输入年份和月份:");
scanf("%d,%d", &year, &month);
switch (month)
{
case 2:
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
days = 29;
}
else
{
days = 28;
}
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
}
printf("%d年%d月有%d天", year, month, days);
}
3.效果截图
4.问题分析
- 该问题作重要的是去判断输入的年份是否为闰年
- 闰年判断方式:年份能被4整除但是不能被100,或者是能被400整除
试验八
1.问题描述
- 模拟自动售货机
2.试验代码
#include <stdio.h>
void main()
{
int x, n, y;
float sum = 0.0;
printf("请选择: 1.日用品 2.文具 3.食品\n");
scanf("%1d", &x);
fflush(stdin); //刷新缓冲区
switch (x)
{
case 1:
printf("请选择:1.牙刷(3.5元/支) 2.牙膏(6.2元/支)\n\t3.肥皂(2元/块) 4.毛巾(8.6元/条)\n");
fflush(stdin);
scanf("%1d", &y);
printf("请输入你需要的数量:");
fflush(stdin);
scanf("%1d", &n);
switch (y)
{
case 1:
sum = 3.5 * n;
break;
case 2:
sum = 6.2 * n;
break;
case 3:
sum = 2 * n;
break;
case 4:
sum = 8.6 * n;
break;
}
break;
case 2:
printf("请选择:1.笔(3元/支) 2.笔记本(1.2/本)\n\t3.文件夹(12元/个) 4.文具盒(8.6元/个)\n");
fflush(stdin);
scanf("%1d", &y);
printf("请输入你需要的数量:");
fflush(stdin);
scanf("%1d", &n);
switch (y)
{
case 1:
sum = 3 * n;
break;
case 2:
sum = 1.2 * n;
break;
case 3:
sum = 12 * n;
break;
case 4:
sum = 8.6 * n;
break;
}
break;
case 3:
printf("请选择:1.白糖(3.6元/包) 2.盐(1/包)\n\t3.饼(2元/个) 4.方便面(3.6元/包)\n");
fflush(stdin);
scanf("%1d", &y);
printf("请输入你需要的数量:");
fflush(stdin);
scanf("%1d", &n);
switch (y)
{
case 1:
sum = 3.6 * n;
break;
case 2:
sum = 1 * n;
break;
case 3:
sum = 2 * n;
break;
case 4:
sum = 3.6 * n;
break;
}
break;
}
printf("总计:%.2f\n", sum);
}
3.效果截图
4.问题分析
- 这个程序中需要有多个scanf输入,这样会造成如果在某一个输入过程中多输入了数字,那么就会导致程序出错,于是我们要在每个scanf函数后面,下一个scanf之前输入fflush(stdin);函数去清除缓存区的数据;但是要注意这个函数在GCC编译器中无用的。
三、试验小结
1.总结
- 对switch和if的结构和语法有了更深刻的印象
- 掌握了switch与if之间的转化
- 了解到了scanf的一些相关问题
2.评论
- 实验内容完整,充分
- 对问题的分析很到位
- 实验的代码很整洁