欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 旅游 > Linux期末考试编程题汇编

Linux期末考试编程题汇编

2024/10/23 23:22:58 来源:https://blog.csdn.net/r2931887650/article/details/143096693  浏览:    关键词:Linux期末考试编程题汇编

1、编写 Shell 脚本,输入一个数字 N,使用 until 语句,计算 1~N 的和。

#!/bin/bash# 输入一个数字Nread -p "请输入一个数字 N: " N# 初始化和为0sum=0# 初始化计数器i=1# 使用until循环计算1到N的和until [ $i -gt $N ]dosum=$((sum + i))i=$((i + 1))done# 输出结果echo "1 到 $N 的和为:$sum"

2、编写 Shell 脚本,使用 case 语句,要求实现如下功能:当执行一个程序的时候, 这个程序会让使用者选择 boy 或者 girl;如果使用者输入 B 或者 b 时,就显示: This is a boy;如果使用者输入 G 或者 g 时,就显示:This is a girl;如果是除了 B/b/G/g 以外的其他字符,就显示:Sorry, I dont know。

#!/bin/bash# 提示用户选择echo "Please choose 'boy' or 'girl' (B/b for boy, G/g for girl):"read choice# 使用 case 语句判断用户输入case "$choice" inB|b)echo "This is a boy.";;G|g)echo "This is a girl.";;*)echo "Sorry, I don't know.";;Esac

3、编写 Shell 脚本,使用 case 语句,实现两个变量之间的加减乘除运算。 

#!/bin/bash# 输入两个数字read -p "请输入第一个数字: " num1read -p "请输入第二个数字: " num2# 提示用户选择运算符read -p "请选择运算符:+(加法)、-(减法)、*(乘法)、/(除法):" operatorcase "$operator" in+)result=$((num1 + num2))echo "$num1 + $num2 = $result";;-)result=$((num1 - num2))echo "$num1 - $num2 = $result";;\*)result=$((num1 * num2))echo "$num1 * $num2 = $result";;/)if [ "$num2" -ne 0 ]; thenresult=$(echo "scale=2; $num1 / $num2" | bc)echo "$num1 / $num2 = $result"elseecho "除数不能为零"fi;;*)echo "无效的运算符";;Esac

4、编写 Shell 脚本,从键盘输入三个整数,使用 if 语句,求三个整数中的最大数。

#!/bin/bash
echo "请输入三个整数:"
read -p "第一个整数:" num1
read -p "第二个整数:" num2
read -p "第三个整数:" num3
if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ]; thenecho "最大数为:$num1"
elif [ $num2 -gt $num1 ] && [ $num2 -gt $num3 ]; thenecho "最大数为:$num2"
elseecho "最大数为:$num3"
fi

5、编写 Shell 脚本,使用 for 语句,求[0,50]区间内所有奇数的和。

#!/bin/bash# 初始化奇数和为0sum=0# 使用for循环遍历[0,50]区间内的所有数字for (( i=0; i<=50; i++ )); do# 如果当前数字是奇数,则将其添加到总和中if (( i % 2 != 0 )); thensum=$(( sum + i ))fidoneecho "奇数和为:$sum"

6、编写Shell脚本,计算n的阶乘。 

#!/bin/bash# 提示用户输入一个整数echo "请输入一个整数 n,以计算 n 的阶乘:"read n# 检查输入是否为非负整数if ! [[ "$n" =~ ^[0-9]+$ ]]; thenecho "请输入一个有效的非负整数。"exit 1Fi# 计算阶乘factorial=1for (( i=1; i<=n; i++ )); dofactorial=$(( factorial * i ))doneecho "$n 的阶乘是:$factorial"

7、分别使用until语句和while语句编写Shell脚本程序,使脚本运行结果如下: The loop is running 1 times. The loop is running 2 times. The loop is running 3 times. The loop is running 4 times. The loop is running 5 times.

使用until语句:

#!/bin/bashcount=1until [ $count -gt 5 ]; doecho "The loop is running $count times."((count++))done

使用while语句:

#!/bin/bashcount=1while [ $count -le 5 ]; doecho "The loop is running $count times."((count++))done

8、编写Shell 脚本,进行密码比对,用户有3次机会输入用户名和密码,如果输 入正确,显示“hellosdut!”;如果输入错误,继续输入用户名和密码,直到三 次机会用完,程序结束。(要求使用while语句)

#!/bin/bashcorrect_username="username"correct_password="password"attempts=3while [ $attempts -gt 0 ]; doecho "请输入用户名:"read usernameecho "请输入密码:"read -s passwordif [ "$username" = "$correct_username" ] && [ "$password" = "$correct_password" ]; thenecho "hellosdut!"exit 0else((attempts--))if [ $attempts -gt 0 ]; thenecho "用户名或密码错误,请重新输入。您还有 $attempts 次尝试机会。"elseecho "您已用尽所有尝试机会。"exit 1fifiDone

9、编写Shell脚本,定义一个数组包含元素-1,-3,6,7,2,-6,并输出大于0的所有元素。 

#!/bin/bash# 定义数组arr=(-1 -3 6 7 2 -6)echo "大于0的元素:"# 遍历数组中的元素for num in "${arr[@]}"; do# 检查元素是否大于0if [ "$num" -gt 0 ]; thenecho "$num"fidone

10、编写Shelll 程序,计算3个整数值的和,需要计算的各个数值由用户在执行脚 本时作为命令行参数给出。

#!/bin/bash# 检查是否提供了足够的参数if [ "$#" -ne 3 ]; thenecho "错误:请提供三个整数值作为参数。"exit 1finum1=$1num2=$2num3=$3# 检查参数是否为整数if ! [[ "$num1" =~ ^[0-9]+$ ]] || ! [[ "$num2" =~ ^[0-9]+$ ]] || ! [[ "$num3" =~ ^[0-9]+$ ]]; thenecho "错误:参数必须为整数值。"exit 1fi# 计算和sum=$((num1 + num2 + num3))echo "三个整数值的和为:$sum"

11、新建一个名为“circle.c”的文件,使用C语言编写计算圆面积的程序。使用 gcc 对其进行编译,生成可执行文件运行程序。

#include <stdio.h>#define PI 3.14159int main() {float radius, area;// 提示用户输入半径printf("请输入圆的半径:");scanf("%f", &radius);// 计算面积area = PI * radius * radius;// 输出面积printf("圆的面积为:%.2f\n", area);return 0;}

12.#!/bin/bash num=0 while ______ do echo $num num=$((num+1)) done 为显示从0到9的所有数字,Shell脚本程序中下划线处应填入:____[ $num -lt 10 ]  __

#!/bin/bashnum=0while [ $num -lt 10 ]doecho $numnum=$((num+1))done

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com