2018-2019-2 Linux程序设计 期中考试
姓名: 学号: 班级:计算机科学与技术165
- 写一个脚本程序,它带一个参数,若该参数是一个普通文件,则把该文件的索引结点号、大小以及最近一次修改时间等详细信息输出来;如果该参数是一个目录,则将该目录(包含子目录)下的所有普通文件的文件名以及上述文件信息输出来。
要求:
(1)如果参数个数大于1,或者参数既不是普通文件也不是目录文件,则提示出错
(2)给出程序在两种不同类型参数(普通文件/目录)下的运行结果 - 写一个脚本程序,提示用户输入12个整数,其中前6个整数放到数组array1中,后6个整数放到数组array2中,接下来将两个数组对应位置的元素求和,将两个数组array1,array2以及其求和结果均输出到文件result.txt中。
- 写一个脚本程序,计算1-1/2+1/3-1/4+…-1/N的值
要求:
1)N由用户输入
2)保留小数点后3位 - 编写c程序,利用随机数生成器生成N个100-200之间的整数,并将这N个整数存放在数组array中,接下来对数组array做如下处理:
1)求数组array中元素的次大值
2)求数组array中所有元素的标准差
要求:
(1)N由用户输入
(2)将上述1)和2)的实现分别放在一个.c文件中
(3)利用make工程管理器进行编译 - 编写c程序,统计附件wordCount.txt中每个单词出现的次数
要求:分别使用带缓存以及不带缓存的文件读写方式
第1题
代码如下
#!/bin/sh
if test $# -gt 1
then
echo "error! More than one parameter."
exit 1
fi
if [ -f $1 ]
then
#cat $1
ls -il $1
exit 0
elif [ -d $1 ]
then
ls -il $1
exit 0
else
echo "Parameter is neither a file nor a directory."
fi
以下是我的目录结构:
运行结果:
第2题
代码如下:
#!/bin/sh
rm result.txt
echo "please input 12 numbers"
echo "array1:" >>result.txt
for (( i=0;i<6;++i ))
do
read arr1[$i]
echo -n "${arr1[$i]} " >> result.txt
done
echo " ">>result.txt
echo "array2:">>result.txt
for (( i=0;i<6;++i ))
do
read arr2[$i]
echo -n "${arr2[$i]} " >>result.txt
done
echo " ">>result.txt
echo "result:" >>result.txt
for((i=0;i<6;++i))
do
echo -n `expr ${arr1[$i]} + ${arr2[$i]} ` >>result.txt
echo -n " ">>result.txt
done
下面是我的目录结构:
下面是运行结果:
result.txt文件的内容:
第3题
代码如下:
#!/bin/sh
while true
do
result=0
a=0
echo "please input the number n:"
read n
for((i=1;i<=n;++i))
do
flag=$(( $i % 2 ))
if [ $flag -eq 0 ]
then
a=`echo "scale=3;-1.000/$i" | bc`
else
a=`echo "scale=3;1.000/$i" | bc`
fi
result=`echo "scale=3;$result+$a" | bc`
done
echo $result
done
下面是运行结果:
第4题
下面是目录结构:
下面是main.c
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<math.h>
#include "functions.h"
int main() {
int n;
int array[100000];
int i,j;
printf("please input the number of integers:\n");
scanf("%d",&n);
printf("Original sequence\n");
for(i=0; i<n; i++) {
array[i]=rand()%101+100;
printf("%d ",array[i]);
}
printf("\nBubble-sorted sequence:\n");
for(i=0; i<n-1; ++i) {
for(j=n-1; j>=i+1; --j) {
if(array[j]>array[j-1]) {
int temp=array[j];
array[j]=array[j-1];
array[j-1]=temp;
}
}
}
下面是secondBiggest.c
int secondBiggest(int *array,int n) {
// printf("\n第二大的数: %d",array[1]);
return array[1];
}
下面是standardDeviation.c
float standardDeviation(int *array,int n) {
int i;
float ave,standDev;
ave=standDev=0;
for(i=0; i<n; ++i)
ave+=array[i]/n;
//printf("\naverage: %f",ave);
for(i=0; i<n; ++i)
standDev+=pow(ave-array[i],2)/n;
float temp=sqrt(standDev);
printf("\n(2).The standard deviation is: %f\n",temp);
//float temp=sqrt(standDev);
return temp;
}
下面是functions.h
int secondBiggest(int *array,int n);
int standardDeviation(int *array,int n);
下面是makefile
4: main.o secondBiggest.o standardDeviation.o
gcc main.o secondBiggest.o standardDeviation.o -o 4 -lm
main.o: main.c functions.h
gcc main.c -c
secondBiggest.o: secondBiggest.c
gcc secondBiggest.c -c
standardDeviation.o: standardDeviation.c
gcc standardDeviation.c -c
先运行make命令编译,下面是运行结果:
第5题
下面是目录结构
(1).不带缓存文件读写方式
代码如下:
#include<stdio.h>
#include<string.h>
#include<unistd.h>
struct word
{
char c[30];
int n;
} w[10000];
int main()
{
FILE *fp;
char b[30],ch;
int i=0,m=1,j=0,k=0,t=0,flag=0;
fp=fopen("wordCount.txt","r");
while((ch=fgetc(fp))!=EOF)
{
if('A'<=ch&&ch<='Z')
ch=ch+32;
if('a'<=ch && ch<='z')
{
b[i]=ch;
i++;
flag=1;
}
else
{
if(ch=='-'&&(ch=fgetc(fp))=='\n')
{
flag=0;
}
else
{
if(flag==1)
{
b[i]='\0';
i=0;
flag=0;
m=0;
for(j=0; j<k; j++)
{
if(strcmp(b,w[j].c)==0)
{
m=1;
break;
}
}
if(m)
w[j].n++;
else
{
w[k].n=1;
strcpy(w[k].c,b);
k++;
}
}
}
if('A'<=ch && ch<='Z')
ch+=32;
if('a'<=ch && ch<='z')
{
b[i]=ch;
i++;
flag=1;
}
}
}
for(i=0; i<k; i++)
{
printf("%s %d\n",w[i].c,w[i].n);
}
printf("\n");
for(i=0; i<k&&i<5; i++)
{
t=0;
while(w[t].n==0)
t++;
for(j=1; j<k; j++)
{
if(w[j].n>w[t].n)
t=j;
else if(w[j].n==w[t].n)
{
if(strcmp(w[j].c,w[t].c)<0)
t=j;
}
}
printf("%s %d\n",w[t].c,w[t].n);
w[t].n=0;
}
return 0;
}
运行结果:
(2).带缓存文件读写方式
代码如下:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
struct word {
char c[30];
int n;
} w[10000];
int main() {
FILE *fp;
char b[30],ch;
char buffer[100000];
int i=0,m=1,j=0,k=0,t=0,flag=0;
fp = open("wordCount.txt", "r");
int size = read(fp, buffer, sizeof(buffer));
int p=0;
while(p<sizeof(buffer)) {
ch=buffer[p++];
// printf("%c\n",buffer[p]);
if('A'<=ch&&ch<='Z')
ch=ch+32;
if('a'<=ch && ch<='z') {
b[i]=ch;
i++;
flag=1;
} else {
if(ch=='-'&&(ch=fgetc(fp))=='\n') {
flag=0;
} else {
if(flag==1) {
b[i]='\0';
i=0;
flag=0;
m=0;
for(j=0; j<k; j++) {
if(strcmp(b,w[j].c)==0) {
m=1;
break;
}
}
if(m)
w[j].n++;
else {
w[k].n=1;
strcpy(w[k].c,b);
k++;
}
}
}
if('A'<=ch && ch<='Z')
ch+=32;
if('a'<=ch && ch<='z') {
b[i]=ch;
i++;
flag=1;
}
}
}
// printf("%d\n",k);
for(i=0; i<k; i++) {
printf("%s %d\n",w[i].c,w[i].n);
}
printf("\n");
for(i=0; i<k&&i<5; i++) {
t=0;
while(w[t].n==0)
t++;
for(j=1; j<k; j++) {
if(w[j].n>w[t].n)
t=j;
else if(w[j].n==w[t].n) {
if(strcmp(w[j].c,w[t].c)<0)
t=j;
}
}
printf("%s %d\n",w[t].c,w[t].n);
w[t].n=0;
}
return 0;
}
下面是运行结果: