Linux课程大作业

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Linux课程设计报告题目Linux课程大作业院系班级姓名指导教师一、基础篇(给出源程序和编译运行的结果)1、编写一个简单的c语言程序:根据输入的两个整数求平均值并且在终端输出,通过gcc编译器得到它的汇编程序文件。源代码(c):#includestdio.hdoubleaverage(intm,intn){return(m+n)/2.0;}intmain(void){intm,n=0;printf(请输入两个数,回车分割\n);scanf(%d,&m);scanf(%d,&n);printf(%d与%d的平均值是:%lf\n,m,n,average(m,n));}源代码(汇编):.filesum.c.text.globlaverage.typeaverage,@functionaverage:.LFB0:.cfi_startprocpushl%ebp.cfi_def_cfa_offset8.cfi_offset5,-8movl%esp,%ebp.cfi_def_cfa_register5subl$8,%espmovl12(%ebp),%eaxmovl8(%ebp),%edxaddl%edx,%eaxmovl%eax,-4(%ebp)fildl-4(%ebp)fldl.LC0fdivrp%st,%st(1)leave.cfi_restore5.cfi_def_cfa4,4ret.cfi_endproc.LFE0:.sizeaverage,.-average.section.rodata.align4.LC2:.string\350\257\267\350\276\223\345\205\245\344\270\244\344\270\252\346\225\260\357\274\214\345\233\236\350\275\246\345\210\206\345\211\262.LC3:.string%d.LC4:.string%d\344\270\216%d\347\232\204\345\271\263\345\235\207\345\200\274\346\230\257\357\274\232%lf\n.text.globlmain.typemain,@functionmain:.LFB1:.cfi_startprocpushl%ebp.cfi_def_cfa_offset8.cfi_offset5,-8movl%esp,%ebp.cfi_def_cfa_register5andl$-16,%espsubl$48,%espmovl$0,44(%esp)movl$.LC2,(%esp)callputsmovl$.LC3,%eaxleal40(%esp),%edxmovl%edx,4(%esp)movl%eax,(%esp)call__isoc99_scanfmovl$.LC3,%eaxleal44(%esp),%edxmovl%edx,4(%esp)movl%eax,(%esp)call__isoc99_scanfmovl44(%esp),%edxmovl40(%esp),%eaxmovl%edx,4(%esp)movl%eax,(%esp)callaveragemovl44(%esp),%ecxmovl40(%esp),%edxmovl$.LC4,%eaxfstpl12(%esp)movl%ecx,8(%esp)movl%edx,4(%esp)movl%eax,(%esp)callprintfleave.cfi_restore5.cfi_def_cfa4,4ret.cfi_endproc.LFE1:.sizemain,.-main.section.rodata.align8.LC0:.long0.long1073741824.identGCC:(Ubuntu/Linaro4.6.3-1ubuntu5)4.6.3.section.note.GNU-stack,,@progbits执行结果:2、编写一个c语言程序:打印输出所有“水仙花数”,用gdb调试程序(给出步骤,至少十步以上)。所谓“水仙花数”是指一个3位数,其各位数字立方和等于该数本身。例如,153是一水仙花数,因为153=1³+5³+3³。源代码:#includestdio.hintisnumber(intnumber){intge=(number%100)%10;intshi=(number/10)%10;intbai=(number/100);printf(%d\n,ge);printf(%d\n,shi);printf(%d\n,bai);inttemp=ge*ge*ge+shi*shi*shi+bai*bai*bai;if(temp==number){return1;}else{return0;}}intmain(){intnumber=0;printf(inputnumber!\n);scanf(%d,&number);if(number1000&&number0){printf(numberlimitsoutbounds!\n);}if(isnumber(number)){printf(numberisformat!\n);}else{printf(numberisnotformat!\n);}return0;}执行结果:3、设计一个程序,要求输出n个整数(n也由键盘输入)中的最大值,并为它编写makefile文件,用make编译后修成返回最小值,再编译,观察有多少文件不需要重新编译。源代码:max.cdoublemax(doublem,doublen){if(mn){returnn;}else{returnm;}}main.c#includestdio.h#includedefine.hintmain(void){doublem,n=0;printf(请输入两个实数、\n);scanf(%lf,&m);scanf(%lf,&n);printf(%lf与%lf最大值是:%lf\n,m,n,max(m,n));}define.hdoublemax(doublem,doublen);makefile文件:test:max.omain.ogccmax.omain.o-otestmain.o:main.cdefine.hgccmain.c-cmax.o:max.cgccmax.c-c结果图:改变程序后,只有max.c,main.c文件程序需要重新编译;4、编写一个程序,求2-n间的素数,n由键盘输入,循环变量分别从2到n、2到(int)sqrt(n),分别测出两个循环的所用时间。源代码:#includestdio.h#includemath.hintmain(){intt1=0,t2=0;intn;intj,k,j2,k2;inti,i2;printf(输入n的值:);scanf(%d,&n);for(i=2;i=n;i++){t1++;k=sqrt(i);for(j=2;jk+1;j++){if(i%j==0)break;}if(j=k+1){printf(%d是素数\n,i);}}printf(\nn循环时间%d,t1);for(i2=2;i2=(int)sqrt(n);i2++){t2++;k2=sqrt(i2);for(j2=2;j2k2+1;j2++){if(i2%j2==0)break;}if(j2=k+1){printf(%d是素数\n,i2);}}printf(\nsqrt(n)循环时间%d\n,t2);return0;}编译运行效果:5、设计一个程序,要求将10分别以十进制、八进制和十六进制输出。程序设计:#includestdio.hintmain(){intnumber=10;printf(十进制值:%d\n,number);printf(八进制值:%o\n,number);printf(十六进制值:%x\n,number);}运行结果:一、提高篇(三选二,划出程序流程图,给出程序源代码和编译运行的结果)1、设计两个程序,要求用命名管道FIFO,实现简单的文本文件或图片文件的传输功能。流程图:源代码:send.c#includestdio.h#includesignal.h#includestdlib.h#includeunistd.h#includesys/types.h#includesys/stat.h#includefcntl.h#includestring.hintmain(){chars[128];intfd;FILE*fp;fp=fopen(./a.txt,r);mkfifo(/tmp/fifo.tst,0644);fd=open(/tmp/fifo.tst,O_WRONLY);while(fgets(s,127,fp)!=NULL){write(fd,s,strlen(s));printf(%s,s);}close(fd);fclose(fp);unlink(/tmp/fifo.tst);return0;}get.c/******************get.c*****************/#includestdio.h#includesignal.h#includestdlib.h#includeunistd.h#includesys/types.h#includesys/stat.h#includestring.h#includefcntl.hintmain(){chars[128];intfd=open(/tmp/fifo.tst,O_RDONLY);intfd2=open(./b.txt,O_CREAT|O_WRONLY);memset(s,0,128);while(read(fd,s,128)0){printf(%s,s);write(fd2,s,128);printf(fd2=%d\n,fd2);}close(fd2);close(fd);return0;}运行结果:2、设计两个程序,要求用消息队列,实现聊天程序,每次发言后自动在后面增加当前系统时间。增加结束字符,比如最后输入“88”后结束进程。流程图:发送源源代码:#includestdio.h#includestdlib.h#includesys/ipc.h#includesys/msg.h#includestring.hstructmsgbuf{inttype;charptr[0];};intmain(intargc,char*argv[]){key_tkey;key=ftok(argv[1],100);intmsgid;msgid=msgget(key,IPC_CREAT|0600);pid_tpid;pid=fork();if(pid==0){while(1){printf(plsinputmsgtosend:);charbuf[128];fgets(buf,128,stdin);structmsgbuf*ptr=malloc(sizeof(structmsgbuf)+strlen(buf)+1);ptr-type=1;memcpy(ptr-ptr,buf,strlen(buf)+1);msgsnd(msgid,ptr,strlen(buf)+1,0);free(ptr);}}else{structmsgbuf{inttype;charptr[1024];};while(1){structmsgbufmybuf;memset(&mybuf,'\0',sizeof(mybuf));msgrcv(msgid,&mybuf,1024,2,0);printf(recvmsg:%s\n,mybuf.ptr);}}}接收端源代码:#includestdio.h#includestdlib.h#inclu

1 / 21
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功