Ubuntu相关程序(浙大版Linux程序设计实验题)

示例代码

1.删除指定文件到回收站

#!/bin/sh
if test $# -eq 0
  then
  echo "Please specify a file!"
else
  gzip $1
  mv $1.gz $HOME/.local/share/Trash/files
  echo "File $1 is deleted !"
fi

2.将指定文件从回收站恢复到指定路径

#! /bin/bash
ls -l $HOME/.local/share/Trash/files
echo "Please input a filename:"
read FILENAME
if [ -f $HOME/.local/share/Trash/files/$FILENAME ]
then
  sudo mv $HOME/.local/share/Trash/files/$FILENAME $HOME/$FILENAME
  gzip -d $HOME/$FILENAME
  echo "File $FILENAME is restore!"
else
  echo "error:$FILENAME not exist"
fi

3.清空回收站的内容

#! /bin/bash
for i in $HOME/.local/share/Trash/files/*
do
  if [ -d $i ]
    then 
    rm -r $i && echo "$i has been deleted!"
  else
    rm $i && echo "$i has been deleted!"
  fi
done


 

4.利用while循环计算1到100的和:

#! /bin/bash
total=0
num=0
while [ $num -le 100 ]
do
total=$(expr $total + $num)
num=$(expr $num + 1)
done
echo "The result is $total"

1.shell程序中赋值等号两边不能加空格,否则会看成命令。例如total = 0则会报错:total: 未找到命令

2.注意expr后面的求和都要用空格隔开。如果写成expr $total+$num最后输出

The result is 0+1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+94+95+96+97+98+99+100
喜欢C的编码方式的同学一定要注意!!

5.利用while循环计算1到100的和:

方法一:

#! /bin/bash
total=0
for num in {1..100}
do
   let "total = total + num"
done
echo "The result is $total"

方法二:
#! /bin/bash
total=0
for num in {1..100}
do
   total=$(expr $total + $num)
done
echo "The result is $total"

方法三:

#! /bin/bash
total=0
for ((num=1;num<=100;num++))
do
   total=$(expr $total + $num)
done
echo "The result is $total"

6.实现四则运算

#! /bin/bash
echo "四则运算计算器说明:"
echo "首先输入四则运算符(+ - * /)"
echo "再输入两个正整数"
echo "计算出结果并输出"
echo "输入非四则运算符退出程序"
echo "***********************"
check=0
until((0))
do
 echo "请输入运算符:"
 read op
 echo "请输入两个正整数:"
 read a b
 case $op in
  +) z=$(expr $a + $b) ;;
  -) z=$(expr $a - $b) ;;
  \*) z=$(expr $a \* $b) ;;
  /) z=$(expr $a / $b) ;;
  *) check=$(expr $check + 1) ;;
 esac
 if test $check = 0
  then
   echo "$a $op $b = $z"
 else
   echo "bye-bye"
   exit 0
 fi
done


注意:乘号在case里相当于dafault的意思,所以乘号需要转义/*才可以使用,否则会报错

7.编写一个bash脚本程序,用for循环实现将当前目录下的所有.c文件移到指定的目录下,最后在显示器上显示指定目录下的文件和目录

#! /bin/bash
if [ ! -d cdirectory ]
  then
  mkdir cdirectory
fi
for i in *.c
do
  sudo mv $i cdirectory
done
echo "文件夹cdirectory包含的文件和目录有:"
ls cdirectory


8.生成10个1-10之间的随机数

#include <stdlib.h>
#include <stdio.h>
int main ()
{
 int i,j;
 srand((int)time(0));
 for(i=0;i<10;i++)
 {
  j=1+(int)(10.0*rand()/(RAND_MAX+1.0));
  printf(" %d ",j);
 }
  printf("\n");
}

9.生成50个100-1000之间的随机数

#include <stdlib.h>
#include <stdio.h>
int main ()
{
 int i,j;
 srand((int)time(0));
 for(i=0;i<50;i++)
 {
  j=100+rand()%900;
  printf(" %d ",j);
 }
  printf("\n");
}

10.猜数游戏的程序,先产生一个随机数,要求被试输入一个数,计算机会提示猜大了,猜小了或恭喜您猜中了,直到猜中,退出程序

#include <stdlib.h>
#include <stdio.h>
int main(){
  int x,in;
  srand((int)time(0));
  x=1+(int)(100.0*rand()/(RAND_MAX+1.0));
  while(1){
    scanf("%d",&in);
    if(in > x){
      printf("大了\n");
    }else if(in < x){
      printf("小了\n");
    }else{
      printf("正确\n");
      break;
    }
  }
}
  

11.修改上述程序,限定猜数的次数作为难度系数,除了提示猜大了,猜小了或恭喜您猜中了外,还有次数已到,猜数失败

#include <stdlib.h>
#include <stdio.h>
int main(){
  int x,in,num,max;
  srand((int)time(0));
  x=1+(int)(100.0*rand()/(RAND_MAX+1.0));
  max=10;
  num=0;
  while(1){
    if(num==max){
      break; 
    }
    scanf("%d",&in);
    if(input > x){
      printf("大了\n");
    }else if(in < x){
      printf("小了\n");
    }else{
      printf("正确\n");
      break;
    }
    num++;
  }
}
  

12.编写一个简单的程序,其功能是实现输入任务及任务截止时间,通过
系统时间和日期函数的使用,可计算输出任务的剩余时间

#include <stdio.h>
#include <time.h>

int main(){
  int i,j,k,l;
  long int a,now;
  time_t timep;
  struct tm *p;
  while(1){
    time (&timep);
    p=localtime(&timep);
    a=(i-p->tm_mday)*24*3600+(j-p->tm_hour)*3600+(k-p->tm_min)*60+l-p->tm_sec;
    if(a==0){
      fputs(str,stdout);
      break;
    }else{
      if(now!=a){
        now=a;
      }
    }
  }
}

上述的i,j,k,l分别代表入日期1~31,小时0~23、分、秒对应的数字

13.编写一个程序,求2~n间的素数,n由键盘输入,循环变量分别从2到n、2到(int)sqrt(n),分别测出两个循环的所用时间。

#include<stdio.h>
#include<sys/time.h>
#include<math.h>
#include<unistd.h>
#include<stdlib.h>

int prime1(int x){
  int i;
  for(i=2;i<x;i++){
    if(x%i==0) return 0;
  }
  return 1;
}

int prime2(int x){
  int i;
  for(i=2;i<=(int)sqrt(x);i++){
    if(x%i==0) return 0;
  }
  return 1;
}

int main(){
  struct timeval tv1,tv2;
  struct timezone tz;
  int n,i;
  scanf("%d",&n);
  gettimeofday(&tv1,&tz);
  }
  gettimeofday(&tv2,&tv);
  printf("%f s\n",tv2.tv_sec-tv1.tv_sec+pow(10,-6)*(tv2.tv_usec-tv1.tv_usec));
  gettimeofday(&tv1,&tz);
  for(i=2;i<=n;i++){
    if(prime2(i)){

    }
  }
  gettimeofday(&tv2,&tz);
  printf(" %f s\n",tv2.tv_sec-tv1.tv_sec+pow(10,-6)*(tv2.tv_usec-tv1.tv_usec));
} 
  
  

报错情况以及解决:

1.隐式声明与内建函数‘exit’不兼容 [默认启用]

解决办法:添加头文件#include <stdlib.h>

笔记:

1.比较运算

-eq           //等于

-ne           //不等于

-gt            //大于 

-lt            //小于 

-ge            //大于等于

-le            //小于等于

2.for循环

for num in {1..100}  如果对奇数求和则for num in {1..100..2} 如果对偶数求和for num in {0..100..2}

for ((num=0;num<=100;num++)) 如果对奇数求和则for ((num=1;num<=100;num+=2)) 如果对偶数求和for ((num=0;num<=100;num+=2))

3.until 循环

until一直执行的循环条件until((0)) ,exit 0可退出程序

4.case分支

case中的;;相当于C语言中的break; case中的*相当于C语言中的default

5.生成随机数rand()

函数 rand( )会返回一个 0~ RAND_MAX(其值为 2147483647)之间的随机值。产生一个大于等于 0、小于 1的数,此数可表示为:rand()/(RAND_MAX+1.0)。

srand((int)time(0))表示以当前时间对应的int值为随机序列起点,这样每次运行程序,可以得到不同的随机数

1-100可以写成 1+(int)(10.0*rand()/(RAND_MAX+1.0));也可以写成1+rand()%100;

6.时间函数的相关说明:

头文件#include<time.h>

localtime()返回的是一个tm结构体

time函数给出从1970年1月1日00:00:00至今的秒数,它必须带一个参数,用来存储这个秒数,time()会导致语法错误,time(0)表示秒数不进行存储。

gettimeofday函数取得当前的时间

头文件 #include<sys/time.h>    #include<unistd.h>

tv_sec表示秒,tv_usec表示微秒,tz表示时差

7.c语言中字符串处理函数

strcpy(字符数组,字符串常量或字符数组) 将后者赋值给前者

strcat(字符数组,字符串常量或字符数组)将后者连接到前者的末尾

char *strcat(char *str1,const char *str2 );
char *strcat(char *strDestination,const char *strSource );
功能:函数将字符串str2 连接到str1的末端,并返回指针str1
注:连接前两个字符串的后面都有一个' \0 ',连接时将字符串1后面的 ' \0 ‘去掉,只在新串最后保留一个 ' \0 ‘
char *strcpy(char *str1,const char *2 );
char *strcpy(char *strDestination,const char *strSource );
功能:复制字符串strSource中的字符到字符串strDestination,包括空值结束符。返回值为指针strDestination。
注:1、“字符数组1”必须写成数组名形式,“字符串2"可以是字符数组名,也可以是一个字符串常量
       2、复制时连同字符串后面的 ' \0 ' 一起复制到数组1中
       3、不能用赋值语句直接将一个字符串常量或者字符数组直接赋给一个字符数组(同普通变量数组是一样的),而只能用strcpy函数处理。
       4、可以用strcpy函数将字符串2中的前若干个字符复制到字符数组1中去。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值