




已閱讀5頁,還剩20頁未讀, 繼續(xù)免費閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
(一)Shell編程一、實驗?zāi)康模?)掌握在Linux下的C編程基本方法。2)掌握shell編程方法。3)掌握dialog圖形化編程方法。二、 實驗內(nèi)容1、 編寫能輸出“Hello world!”問候語的C程序,并在終端中編譯、執(zhí)行。要求記錄所使用的命令及結(jié)果。#include stdio.hmain()printf(Hello world!n);2、編寫一個C程序并設(shè)置其在后臺執(zhí)行,其功能是在一段時間后(可自行設(shè)置),在屏幕上顯示信息:Time for play!,寫出相應(yīng)的程序、命令及結(jié)果。#include stdio.hmain()int time=0;printf(請輸入等待時間(單位:s):);scanf(%d,&time);sleep(time);printf(Time for play!n);3、編寫C程序,求1到100之間整數(shù)的階乘和,并對程序進行優(yōu)化。寫出程序、命令和結(jié)果。#include stdio.hmain( ) int i; double s = 1,sum = 0; for( i= 1;i= 100;i+) sum+=s*=i; printf( 1到100之間整數(shù)的階乘和:%fn,sum); printf( 1到100之間整數(shù)的階乘和:%en,sum); 4、編寫C程序,根據(jù)鍵盤輸入的半徑求圓面積,要求在命令行使用不同的圓周率(PI=3.14,PI=3.14159,PI=3.14159626等)進行編譯,寫出程序、命令和結(jié)果。 #include stdio.hint main()double r = 0.0 , Area = 0.0;printf(請輸入半徑: );scanf(%lf, &r);Area = PI * r * r;printf(圓面積: %fn, Area); 5、編寫shell程序sh.1, 完成向用戶輸出“你好!”的問候語。并根據(jù)實驗的時間,分別給出:“上午好!”或者“下午”,或“晚上”好的問候。#!/bin/shTIME=$(date +%H) if $TIME -ge 1 & $TIME -le 11 ;then echo 早上好!elif $TIME -ge 12 & $TIME -le 18 ;then echo 下午好!elif $TIME -ge 19 & $TIME -le 24 ;then echo 晚上好!fi6、編程sh.2:如果存在sh.1文件,則輸出信息:sh.1文件已經(jīng)存在,并詢問是否運行?如果用戶回答:要運行。那么結(jié)果是什么?如果sh.1不存在,則提示用戶先創(chuàng)建文件。#!/bin/shif -f sh.1 ; then echo 文件已經(jīng)存在,并詢問是否運行? now? Y/Nread aif $a = Y | $a = y ; then./sh.1elif $a = N | $a = n ; thenexit 2fielseecho sh.1不存在,先創(chuàng)建文件! exit 1#fi7、編程sh.3:循環(huán)顯示所有的sh.*文件。#!/bin/shfor i in $(ls sh.*)dols -l $idone8、教材第9頁靜態(tài)庫實驗,按步驟完成操作,并記錄結(jié)果。分析所出現(xiàn)的問題及如何解決的?#include void bill(char *arg)printf(bill: We passed %sn,arg);#include void fred(int arg)printf(fred: We passed %dn,arg);#include #include lib.hint main() bill(Hello World); exit(0);/* * This is lib.h.It declares the functions fred and bill for users */void bill(char *);void fred(int);#include #include lib.hint main()bill(Hello World);exit(0);9、教材第41頁實驗,從函數(shù)返回一個值。#!/bin/shyes_or_no()echo Is your name $* ?while truedoecho -n Enter yes or no :read xcase $x iny | yes ) return 0;n | no ) return 1;* ) echo Answer yes or noesacdoneecho Original parameters are $*if yes_or_no $1thenecho Hi $1, nice nameelseecho Never mingfiexit 010、Dialog 工具編程sh.4:實現(xiàn)用信息框+輸入框:顯示問候語“某同學,你好!”的信息。(提示:輸入信息請用英文。)#!/bin/shdialog -inputbox 請輸入名字 : 9 28 2name.txtNAME=$(cat name.txt)dialog -infobox $NAME同學,你好! 9 2811、設(shè)計一個圖形化的菜單查詢系統(tǒng)sh.5,標題名為“歡迎來到學生之家!”,調(diào)查內(nèi)容包括:姓名、性別、年齡和建議(提示:輸入信息請用英文。)。并將所得到的結(jié)果,用信息框顯示出來。(格式不限,自由發(fā)揮。)#!/bin/shdialog -title 歡迎來到學生之家! -inputbox 姓名 : 10 25 -inputbox 性別 : 10 25 -inputbox 年齡 : 10 25 -inputbox 建議 : 10 25 2info.txtset $(cat info.txt)dialog -title 歡迎來到學生之家! -infobox 姓名 : $1 性別 : $2 年齡 : $3 建議 : $4 10 30三、實驗結(jié)果與討論(根據(jù)實驗結(jié)果回答下列問題)1、 你所使用的實驗環(huán)境是什么?請寫出Linux的平臺、內(nèi)核版本號。2、 在Linux中,標準設(shè)備文件有哪些?這些設(shè)備文件在哪個目錄? Linux 中的設(shè)備類型:字符設(shè)備(無緩沖且只能順序存取)、塊設(shè)備(有緩沖且可以隨機存取),套接字設(shè)備設(shè)備文件目錄:在/dev下3、 對于上述各編程題目中所用到了的各個頭文件,請找到它們的位置。 C庫文件在/usr/include 下四、總結(jié)你在實驗遇到什么問題?如何解決的?如果沒有問題那么有什么體會或看法?1 做1-100的階乘時,結(jié)果用long int和int定義,會出現(xiàn)溢出,要用double類型;2 求圓面積時,本想直接定義但考慮的圓周率的精度問題,我選擇了手動輸入;3 執(zhí)行文件如果出現(xiàn)權(quán)限不足,要修改權(quán)限;4 vim 是vi的升級版本,它不僅兼容vi的所有指令,還有一些新的特性,如:多級撤消,在vi里,按 u只能撤消上次命令,而在vim里可以無限制的撤消;5 由于Linux沒有默認裝dialog,所以我們要先裝了才能用。先到Linux中找Packages文件夾中的dialog-1.1-9.20080819.1.el6.i686.rpm,然后放到虛擬機中安裝;6 第8題中要加上頭文件#include,就不會出現(xiàn)上面的警告了。 (二)Linux環(huán)境及文件操作編程一、實驗?zāi)康模?)掌握庫函數(shù)的制作與安裝2)掌握文件操作編程基本方法。3)熟悉Linux環(huán)境設(shè)置。三、 實驗內(nèi)容 1、應(yīng)用底層函數(shù)read( ), open( ), write( )函數(shù),分別編寫具有打開C源程序(*.C)的my_open( )函數(shù)、并能從指定文件復(fù)制文件內(nèi)容的my_copy( )函數(shù), 要求:1)編寫my_open.c和my_copy.c源程序- my_open.c- #include #include void my_open(const char *path) open(path, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR|S_IXUSR);- my_copy.c- void my_copy(int in, int out) char c;while(read(in, &c, 1) = 1) write(out, &c, 1);2) 再編譯成my_open.o 和my_copy.o2、將上題所得到的兩個目標代碼,用ar程序打包成庫文件名為libmy_copy.a,放在當前目錄,編寫能調(diào)用庫函數(shù)的源程序my_prog.c,編譯并運行。記錄命令和并分析運行結(jié)果。#include int main() char path147,path247,c; int choice,in,out; while(1) printf(Input your choice:n); printf(1.Open file.n); printf(2.Copy file.n); printf(3.Exitn); scanf(%d, &choice); if(choice = 1) printf(Input file path and name: ); scanf(%s, path1);in= my_open(path1);while(read(in, &c, 1) = 1) write(1, &c, 1); else if(choice = 2) printf(Input oldcopy path and newcopy path: ); scanf(%s %s, path1, path2);in=my_open(path1);out=my_open(path2);my_copy(in, out); else if(choice = 3) break; else printf(Error: Please input right number.n); 3、編寫教材第87頁與第98頁的復(fù)制程序,記錄運行結(jié)果,并比較這兩個程序的運行時間。-copy_system.c-#include #include #include #include int main()char c;int in, out ;in = open(file.in, O_RDONLY);out = open(file.out,O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);while(read(in,&c,1) = 1)write(out,&c,1);exit(0);-copy_stdio.c-#include #include int main()int c;FILE *in,*out;in = fopen(file.in,r);out = fopen(file.out,w);while(c =fgetc(in) != EOF)fputc(c,out);exit(0);4、編寫教材第103-105頁的目錄掃描程序。#include #include #include #include #include #include void printdir(char *dir, int depth) DIR *dp; struct dirent *entry; struct stat statbuf; if(dp = opendir(dir) = NULL) fprintf(stderr,cannot open directory: %sn, dir); return; chdir(dir); while(entry = readdir(dp) != NULL) lstat(entry-d_name, &statbuf); if(S_ISDIR(statbuf.st_mode) if(strcmp(.,entry-d_name) = 0 | strcmp(.,entry-d_name) = 0) continue; printf(%*s%s/n,depth,entry-d_name); printdir(entry-d_name, depth+4); else printf(%*s%sn,depth,entry-d_name); chdir(.); closedir(dp); int main() printf(Directory scan of /home:n); printdir(/home,0); printf(done.n); exit(0); 5、教材第128頁strftime( )函數(shù)與strptime( )函數(shù)編程,記錄運行結(jié)果并與date命令的輸出作比較。 #define _XOPEN_SOURCE#include #include #include int main() struct tm *tm_ptr, timestruct; time_t the_time; char buf256; char *result; (void) time(&the_time); tm_ptr = localtime(&the_time); strftime(buf,256, %A %d %B, %I:%S %P, tm_ptr); printf(strftime gives: %sn, buf); strcpy(buf, Thu 26 July 2007, 17:53 will do fine); printf(calling strptime with: %sn, buf); tm_ptr = ×truct; result = strptime(buf, %a %d %b %Y, %R, tm_ptr); printf(strptime consumed up to: %sn,result); printf(strptime gives:n); printf(date: %02d/%02d/%02dn); printf(date:%02d/%02d/%02dn,tm_ptr-tm_year%100,tm_ptr-tm_mon+1, tm_ptr-tm_mday); printf(time: %02d:%02dn,tm_ptr-tm_hour, tm_ptr-tm_min); exit(0); *6、教材第168頁終端控制程序。(*可選做)三、實驗結(jié)果與討論(根據(jù)實驗結(jié)果回答下列問題)4、 標準與非標準庫函數(shù)有什么區(qū)別? 標準庫函數(shù):libc.a它是標準C庫函數(shù),gcc等編譯程序能夠自動鏈接,在使用時,只需要包含其定義的頭文件即可;非標準庫函數(shù):在使用時,必須要包含頭文件外,還要加上鏈接庫函數(shù)的操作。5、 在Linux中,用戶帳號文件在哪個目錄?加密后的密碼文件在哪個目錄?用戶帳號文件目錄:/home/加密后的密碼文件目錄:/etc/shadow四、總結(jié)你在實驗遇到什么問題?如何解決的?如果沒有問題那么有什么體會或看法?1. 第五題中的警告,加個#include 頭文件就不會有警告了。 (三)MYSQL數(shù)據(jù)庫與Linux程序開發(fā)一、實驗?zāi)康模?)掌握MYSQL數(shù)據(jù)庫命令與應(yīng)用編程。2)了解Linux多模塊軟件編譯與鏈接過程。四、 實驗內(nèi)容1、 在你的Linux環(huán)境中安裝MYSQL數(shù)據(jù)庫系統(tǒng)。記錄安裝步驟和命令過程。答:解壓mysql-5.0.87-linux-i686-glibc23.tar.zip到/usr/local;tar vxf mysql-5.0.87-linux-i686-glibc23.tar#groupaddmysql #useradd-gmysql-s/bin/false-Mmysql #cd/usr/local/mysql#./configure -prefix=/usr/local/mysql -enable-thread-safe-client -enable-local-infile -with-charset=gbk -with-extra-charset=all -with-low-memory# cp support-files/f /etc/f #bin/mysql_install_db-user=mysql#chown-Rroot. #chown-Rmysql/var #bin/mysqld_safe-user=mysql& #cp support-files/mysql.server /etc/rc.d/init.d/mysql #chmod700/etc/rc.d/init.d/mysqld #cd#chown -R mysql /usr/local/mysql/data. #chgrp -R mysql /usr/local/mysql加入自動啟動服務(wù)隊列: #chkconfig-addmysql #chkconfig-level345mysqlon 測試#/usr/local/mysql/bin/mysqladminping #/usr/local/mysql/bin/mysqladminversion#/usr/local/mysql/bin/mysql添加root密碼 #/usr/local/mysql/bin/mysqladmin-uroot-p舊密碼password新密碼 說明:此時mysql的root用戶的密碼為空2、 編輯教材第298頁select4.c程序,記錄編譯命令和運行結(jié)果。create table children (childno int (11) NOT NULL auto_increment,fname varchar(30),age int(11),PRIMARY KEY (childno) );insert into children(childno, fname, age) values(1,Jenny,21);insert into children(childno, fname, age) values(2,Andrew,17);insert into children(childno, fname, age) values(3,Gavin,8);insert into children(childno, fname, age) values(4,Duncan,6);insert into children(childno, fname, age) values(5,Emma,4);insert into children(childno, fname, age) values(6,Alex,15);insert into children(childno, fname, age) values(7,Adrian,9);/*select4.c*/#include #include #include mysql.hMYSQL my_connection;MYSQL_RES *res_ptr;MYSQL_ROW sqlrow;void display_header();void display_row();int main(int argc,char *argv)int res;int first_row =1;mysql_init(&my_connection);if(mysql_real_connect(&my_connection,localhost,rick,secret,foo,0,NULL,0)printf(Connection success!n);res=mysql_query(&my_connection,SELECT childno,fname,age FROM children WHERE age5);if(res)fprintf(stderr,SELECT error:%sn,mysql_error(&my_connection);elseres_ptr = mysql_use_result(&my_connection);if(res_ptr)while(sqlrow=mysql_fetch_row(res_ptr)if(first_row)display_header();first_row=0;display_row();if(mysql_errno(&my_connection)fprintf(stderr,Retrive error:%sn,mysql_error(&my_connection);mysql_free_result(res_ptr);mysql_close(&my_connection);elsefprintf(stderr,Connection failedn);if(mysql_errno(&my_connection)fprintf(stderr,Connection error %d: %sn,mysql_errno(&my_connection),mysql_error(&my_connection);return EXIT_SUCCESS;void display_header()MYSQL_FIELD *field_ptr;printf(Column details:n);while(field_ptr = mysql_fetch_field(res_ptr) != NULL)printf(t Name:%sn,field_ptr-name);printf(t Type:);if(IS_NUM(field_ptr-type)printf(Numeric fieldn);elseswitch(field_ptr-type)case FIELD_TYPE_VAR_STRING:printf(VARCHARn);break;case FIELD_TYPE_LONG:printf(LONGn);break;default:printf(Type is %d,check in mysql_com.hn,field_ptr-type);printf(t Max width %ldn,field_ptr-length);if(field_ptr-flags & AUTO_INCREMENT_FLAG)printf(t Auto incrementsn);printf(n);void display_row()unsigned int field_count;field_count =0;while(field_count mysql_field_count(&my_connection)if(sqlrowfield_count)printf(%s ,sqlrowfield_count);elseprintf(NULL);field_count+;printf(n);3、 編寫程序p.c,其功能是從鍵盤輸入兩個實數(shù),輸出這兩個實數(shù)的平方和,生成可執(zhí)行文件為pow。編寫源程序,記錄編譯的命令和結(jié)果。#include #includemath.h int main() double x,y,z; printf(Please enter the two real Numbers:n); scanf(%lf,%lf,&x,&y); z =pow(x,2)+pow(y,2); printf(Sum of squares:n ); printf(%lfn,z); 4、 編程power.c,其功能是從鍵盤輸入兩個實數(shù)x和y,計算x的y次方,生成可執(zhí)行文件名為XpowerY。編寫源程序,記錄編譯命令及程序的運行結(jié)果。#include #includemath.h int main() double x,y,z; printf(Please enter the two real Numbers:n); scanf(%lf,%lf,&x,&y); z =pow(x,y); printf(Result:n ); printf(%lfn,z); 5、 完成P319頁的實驗過程,創(chuàng)建相關(guān)的文件,并記錄命令和結(jié)果。/*main.c*/#include #include a.hextern void function_two();extern void function_three();int main()function_two();function_three();exit (EXIT_SUCCESS);/*2.c*/#include a.h#include b.hvoid function_two()/*3.c*/#include b.h#include c.hvoid function_three()6、 完成P327頁管理函數(shù)庫的實驗,并將庫文件名改為libmy.a, 創(chuàng)建后將其安裝到系統(tǒng)的庫文件默認目錄。1) 完成P327的所有步驟;all: myappCC= gcc INSTDIR = /usr/local/binINCLUDE = .CFLAGS = -g -Wall -ansiMYLIB = mylib.amyapp: main.o $(MYLIB)$(CC) -o myapp main.o $(MYLIB)$(MYLIB): $(MYLIB)(2.o) $(MYLIB)(3.o)main.o: main.c a.h 2.o: 2.c a.h b.h 3.o: 3.c b.h c.hclean:-rm main.o 2.o 3.o $(MYLIB)install: myapp if -d $(INSTDIR); then cp myapp $(INSTDIR); chmod a+x $(INSTDIR)/myapp; chmod og-w $(INSTDIR)/myapp; echo Installed in $(INSTDIR); else echo Sorry,$(INSTDIR) does not exist; fi2) 安裝庫文件,并寫出安裝庫函數(shù)文件的步驟;3)寫出使用所創(chuàng)建庫libmy.a的命令。 /*main.c*/#include #include #include a.hvoid function_two()printf(function_two() n);void function_three()printf(function_three() n);int main()function_two();function_three();exit (EXIT_SUCCESS);三、實驗結(jié)果與討論(根據(jù)實驗結(jié)果回答下列問題)1、寫出使用C訪問MYSQL數(shù)據(jù)庫的步驟。你
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025屆葉城縣三上數(shù)學期末預(yù)測試題含解析
- 知識產(chǎn)權(quán)管理規(guī)范課件
- 2025屆內(nèi)蒙古莫力達瓦達斡爾族自治旗鐵堅中心校三上數(shù)學期末達標檢測試題含解析
- 響應(yīng)式Web開發(fā)項目教程(HTML5 CSS3 Bootstrap)(第3版) 課件 第8章 Bootstrap基礎(chǔ)入門
- 軟件界面設(shè)計分析
- 語言教育的組織與實施
- 互聯(lián)網(wǎng)教育平臺開發(fā)合作框架合同
- 農(nóng)業(yè)經(jīng)濟園區(qū)管理協(xié)議
- 股東合作協(xié)議書的和建議
- 農(nóng)業(yè)機械合作使用及維護合同
- GB/T 2423.18-2021環(huán)境試驗第2部分:試驗方法試驗Kb:鹽霧,交變(氯化鈉溶液)
- 11471勞動爭議處理(第7章)
- 糖尿病護理專題知識講座
- 聘用證明(共4篇)
- 留置導(dǎo)尿技術(shù)操作-課件
- 乳腺癌手術(shù)及重建知情同意書
- 桌面云規(guī)劃與最佳實踐
- IgG4相關(guān)性疾病的診治ppt課件
- 保健品會議營銷市場操作手冊(全)
- 設(shè)備(材料)供應(yīng)招標文件范本
- 220千伏線路無人機放線施工組織設(shè)計
評論
0/150
提交評論