当前位置: 首页 > article >正文

Linux期末考试编程题汇编

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

#!/bin/bash

# 输入一个数字N

read -p "请输入一个数字 N: " N



# 初始化和为0

sum=0

# 初始化计数器

i=1



# 使用until循环计算1到N的和

until [ $i -gt $N ]

do

sum=$((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" in

B|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 "请输入第一个数字: " num1

read -p "请输入第二个数字: " num2

# 提示用户选择运算符

read -p "请选择运算符:+(加法)、-(减法)、*(乘法)、/(除法):" operator

case "$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 ]; then

            result=$(echo "scale=2; $num1 / $num2" | bc)

            echo "$num1 / $num2 = $result"

        else

            echo "除数不能为零"

        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 ]; then
    echo "最大数为:$num1"
elif [ $num2 -gt $num1 ] && [ $num2 -gt $num3 ]; then
    echo "最大数为:$num2"
else
    echo "最大数为:$num3"
fi

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

#!/bin/bash



# 初始化奇数和为0

sum=0



# 使用for循环遍历[0,50]区间内的所有数字

for (( i=0; i<=50; i++ )); do

    # 如果当前数字是奇数,则将其添加到总和中

    if (( i % 2 != 0 )); then

        sum=$(( sum + i ))

    fi

done



echo "奇数和为:$sum"

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

#!/bin/bash

# 提示用户输入一个整数

echo "请输入一个整数 n,以计算 n 的阶乘:"

read n

# 检查输入是否为非负整数

if ! [[ "$n" =~ ^[0-9]+$ ]]; then

    echo "请输入一个有效的非负整数。"

    exit 1

Fi

# 计算阶乘

factorial=1

for (( i=1; i<=n; i++ )); do

factorial=$(( factorial * i ))

done

echo "$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/bash

count=1

until [ $count -gt 5 ]; do

    echo "The loop is running $count times."

    ((count++))

done

使用while语句:

#!/bin/bash

count=1

while [ $count -le 5 ]; do

    echo "The loop is running $count times."

    ((count++))

done

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

#!/bin/bash



correct_username="username"

correct_password="password"

attempts=3



while [ $attempts -gt 0 ]; do

    echo "请输入用户名:"

    read username

    echo "请输入密码:"

    read -s password

    if [ "$username" = "$correct_username" ] && [ "$password" = "$correct_password" ]; then

        echo "hellosdut!"

        exit 0

    else

        ((attempts--))

        if [ $attempts -gt 0 ]; then

            echo "用户名或密码错误,请重新输入。您还有 $attempts 次尝试机会。"

        else

            echo "您已用尽所有尝试机会。"

            exit 1

        fi

    fi

Done

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

    # 检查元素是否大于0

    if [ "$num" -gt 0 ]; then

        echo "$num"

    fi

done

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

#!/bin/bash

# 检查是否提供了足够的参数

if [ "$#" -ne 3 ]; then

    echo "错误:请提供三个整数值作为参数。"

    exit 1

fi

num1=$1

num2=$2

num3=$3

# 检查参数是否为整数

if ! [[ "$num1" =~ ^[0-9]+$ ]] || ! [[ "$num2" =~ ^[0-9]+$ ]] || ! [[ "$num3" =~ ^[0-9]+$ ]]; then

    echo "错误:参数必须为整数值。"

    exit 1

fi

# 计算和

sum=$((num1 + num2 + num3))

echo "三个整数值的和为:$sum"

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

#include <stdio.h>

#define PI 3.14159

int 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/bash

num=0

while [ $num -lt 10 ]

do

    echo $num

    num=$((num+1))

done

http://www.kler.cn/news/358369.html

相关文章:

  • Jenkins+Ant+Jmeter接口自动化集成测试
  • 基于x86_64汇编语言简单教程3: 一些概念的补充与整理
  • 基于Matlab进行H5读写操作
  • vue使用jquery的ajax,页面跳转
  • VMware虚拟机连不上网络,但VMware网络服务和网络适配器均正常
  • Android音视频 MediaCodec框架-创建流程(3)
  • resnetv1骨干
  • 阿里巴巴 | 推出升级版AI翻译工具:Marco MT 性能超越Google和ChatGPT
  • oracle创建用户与表空间,用户授权、以及导入dmp数据泵文件
  • Python----QT篇基础篇(一)
  • 图像中的融合
  • zotero文献管理学习
  • 柬埔寨旅游应该准备的高棉语翻译器《柬埔寨语翻译通》app语音翻译功能让你跟当地人无阻沟通交流,高棉语OCR识别技术分享
  • 桂林美景探索:SpringBoot旅游平台指南
  • 5.C++经典实例-判断输入的年份是否为闰年
  • go 中指针的执行效率比较
  • AI大模型开发架构设计(14)——基于LangChain大模型的案例架构实战
  • Windows环境apache控制台命令行启动、停止、重启httpd服务
  • 【Flutter】页面布局:线性布局(Row 和 Column)
  • mybatis针对枚举的处理的总结