实验三 选择结构基础(2学时)

This commit is contained in:
xtaodada 2021-10-25 13:26:59 +08:00
parent b6f70b9f40
commit 48e7778b82
No known key found for this signature in database
GPG Key ID: EE4DC37B55E24736
10 changed files with 247 additions and 0 deletions

1
.gitignore vendored
View File

@ -50,3 +50,4 @@ modules.order
Module.symvers
Mkfile.old
dkms.conf
.idea/

20
03/1/1.c Normal file
View File

@ -0,0 +1,20 @@
#include <stdio.h>
int sign(int x){
if (x < 0)
return -1;
else
if (x > 0)
return 1;
else
return x;
}
int main(){
int x;
printf("Enter x: ");
scanf("%d", &x);
printf("\nsign(%d)=%d",x,sign(x));
}

BIN
03/1/1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

44
03/1/README.md Normal file
View File

@ -0,0 +1,44 @@
【问题描述】 输入x,计算并输出下列分段函数sign(x)的值。要求定义和调用函数sign(x) 实现该分段函数。
![1](1.png)
【输入形式】从键盘输入整数x
【输入输出样例1】下划线部分表示输入
Enter x: <u>10</u>
sign(10)=1
【输入输出样例2】下划线部分表示输入
Enter x: <u>-5</u>
sign(-5)=-1
【输入输出样例3】下划线部分表示输入
Enter x: <u>0</u>
sign(0)=0
【样例说明】
输入提示符后要加一个空格。例如Enter x:,其中:后要加一个且只能一个空格。
输出语句的=两边无空格。
英文字母区分大小写。必须严格按样例输入输出。
# 运行结果
成功通过编译, 且无编译警告
共有测试数据:3
平均占用内存:1.943K
平均CPU时间:0.00549S
平均墙钟时间:0.00548S
测试数据 评判结果
测试数据1 完全正确
测试数据2 完全正确
测试数据3 完全正确

26
03/2/2.c Normal file
View File

@ -0,0 +1,26 @@
#include<stdio.h>
int main()
{
float a,b;
char c;
scanf("%f %c %f",&a,&c,&b);
switch (c) {
case '+':
printf("=%.2f",a+b);
break;
case '-':
printf("=%.2f",a-b);
break;
case '*':
printf("=%.2f",a*b);
break;
case '/':
printf("=%.2f",a/b);
break;
default:
printf("无效运算符");
break;
}
return 0;
}

31
03/2/README.md Normal file
View File

@ -0,0 +1,31 @@
【问题描述】 编程题:输入一个形式如“操作数 运算符 操作数”的四则运算表达式,输出运算结果。
【输入形式】 “操作数 运算符 操作数”,三者之间没有任何符号,操作数可以是整数或小数,运算符为“+ - * /”共4种情况若为其他运算符则不计算直接输出“无效运算符”
【输出形式】 有效运算符,输出“=结果”结果保留2位小数其他运算符直接输出“无效运算符”
【样例输入1】3.5+4.5
【样例输出1】=8.00
【样例输入2】5/3
【样例输出2】=1.67
【样例输入3】3^4
【样例输出3】无效运算符
# 运行结果
成功通过编译, 且无编译警告
共有测试数据:3
平均占用内存:1.982K
平均CPU时间:0.00460S
平均墙钟时间:0.00459S
测试数据 评判结果
测试数据1 完全正确
测试数据2 完全正确
测试数据3 完全正确

52
03/3/3.c Normal file
View File

@ -0,0 +1,52 @@
#include<stdio.h>
int number(int num){
int a = num % 3,b = num % 5,c = num % 7;
if (a == 0) {
if (b == 0) {
if (c == 0) {
printf("Can be divisible by 3,5,7.");
}
else {
printf("Can be divisible by 3,5.");
}
}
else {
if (c == 0) {
printf("Can be divisible by 3,7.");
}
else {
printf("Can be divisible by 3.");
}
}
}
else {
if (b == 0) {
if (c == 0) {
printf("Can be divisible by 5,7.");
}
else {
printf("Can be divisible by 5.");
}
}
else {
if (c == 0) {
printf("Can be divisible by 7.");
}
else {
printf("Can not be divisible by 3,5,7.");
}
}
}
return 0;
}
int main()
{
int num;
scanf("%d",&num);
number(num);
return 0;
}

44
03/3/README.md Normal file
View File

@ -0,0 +1,44 @@
【问题描述】 编程从键盘输入一个整数判断它能否被3,5,7整除并输出以下信息之一
⑴ Can be divisible by 3,5,7.
即能同时被3,5,7整除
⑵ Can be divisible by ?,?. (其中?为3,5,7之一)
即能被其中两个数(要指出哪两个)整除;
⑶ Can be divisible by ?. (其中?为3,5,7之一)
即能被其中一个数(要指出哪一个)整除;
⑷ Can not be divisible by 3,5,7.
即不能被3,5,7任一个整除。
【输入形式】 从键盘输入一个整数。
【输出形式】 按题目要求及样例输出该整数能否被3,5,7整除。
【样例输入1】 441
【样例输出1】 Can be divisible by 3,7.
【样例输入2】 1024
【样例输出2】 Can not be divisible by 3,5,7.
# 运行结果
成功通过编译, 且无编译警告
共有测试数据:4
平均占用内存:1.935K
平均CPU时间:0.00395S
平均墙钟时间:0.00394S
测试数据 评判结果
测试数据1 完全正确
测试数据2 完全正确
测试数据3 完全正确
测试数据4 完全正确

25
03/README.md Normal file
View File

@ -0,0 +1,25 @@
# 实验三 选择结构基础2学时
> 作业时间: 2021-10-24 22:32:00 至 2021-10-31 23:55:00
# 实验目的
1. 学会正确使用逻辑运算符和逻辑表达式。
2. 了解条件与程序流程的关系。
3. 熟练掌握if语句和switch语句。
# 实验器材
计算机硬件环境:`PIII 667以上计算机软件环境Dev C++, Visual C++。`
# 技能要点
1. if语句三种形式的使用
2. switch语句的格式以及控制流程
3. 在switch中break语句的作用
4. 缩进格式程序的编写。
# 思考题
1. 分支结构中,决定分支的条件一般是什么表达式?
2. 在C语言中如何判断逻辑真、假又如何表示逻辑真、假

View File

@ -24,3 +24,7 @@ A repo to record my study life.
- [4. 时间换算](https://github.com/xtaodada/C-study/tree/master/02-1/4)
- [5. 求复数之积](https://github.com/xtaodada/C-study/tree/master/02-1/5)
- [6. 计算时间差](https://github.com/xtaodada/C-study/tree/master/02-1/6)
- [实验三 选择结构基础2学时](https://github.com/xtaodada/C-study/tree/master/03)
- [1. 求分段函数的值](https://github.com/xtaodada/C-study/tree/master/03/1)
- [2. 求解四则运算表达式](https://github.com/xtaodada/C-study/tree/master/03/2)
- [3. 能否被3,5,7整除](https://github.com/xtaodada/C-study/tree/master/03/3)