实验三Linux_C_程序设计_第1页
实验三Linux_C_程序设计_第2页
实验三Linux_C_程序设计_第3页
实验三Linux_C_程序设计_第4页
实验三Linux_C_程序设计_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、?Linux程序开发环境?实验报告实验成绩:批阅教师:年 月 日实验3 Linux C程序设计1、实验目的(1) 掌握Linux shell程序运行方法 (2) 掌握Linux Shell程序根本语法(3) 了解Linux环境变量(4) 编写简单Linux shell程序 2、实验内容使用curses库完成CD唱片管理系统的C语言简单用户界面版。实验步骤见附录。3、实验记录(1) 实验报告中提交系统的完整脚本;#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string

2、.h>#include <curses.h>#define MAX_STRING (80)/* Longest allowed response */#define MAX_ENTRY (1024)/* Longest allowed database entry */#define MESSAGE_LINE 6/* Misc. messages go here */#define ERROR_LINE 22/* The line to use for errors */#define Q_LINE 20/* Line for Questions */#define PROM

3、PT_LINE 18/* Line for prompting on */static char current_cdMAX_STRING = "0"static char current_catMAX_STRING;const char *title_file = "title.cdb"const char *tracks_file = "tracks.cdb"const char *temp_file = "cdb.tmp"/* Prototypes for local functions */void cle

4、ar_all_screen(void);void get_return(void);int get_confirm(void);int getchoice(char *greet, char *choices);void draw_menu(char *options, int highlight, int start_row, int start_col);void insert_title(char *cdtitle);void get_string(char *string);void add_record(void);void count_cds(void);void find_cd(

5、void);void list_tracks(void);void remove_tracks(void);void remove_cd(void);void update_cd(void);/* 菜单项 */char *main_menu = "aadd new CD", "ffind CD", "ccount CDs and tracks in the catalog", "qquit", 0,;/* The extended menu is displayed when a CD is currently s

6、elected */char *extended_menu = "aadd new CD", "ffind CD", "ccount CDs and tracks in the catalog", "llist tracks on current CD", "rremove current CD", "uupdate track information", "qquit", 0,;int main() int choice; initscr(); do c

7、hoice = getchoice("Options:", current_cd0 ? extended_menu : main_menu);switch (choice) case 'q': break;case 'a': add_record(); break;case 'c': count_cds(); break;case 'f': find_cd(); break;case 'l': list_tracks(); break;case 'r': remove_cd();

8、 break;case 'u': update_cd(); break; while (choice != 'q'); endwin(); exit(EXIT_SUCCESS);/* main */add menu/* getchoice - ask the user to choose passed: greet, an introduction choices, an array of strings, NULL at end */int getchoice(char *greet, char *choices) static int selected_ro

9、w = 0; int max_row = 0; int start_screenrow = MESSAGE_LINE, start_screencol = 10; char *option; int selected; int key = 0; option = choices; while (*option) max_row+;option+; /* protect against menu getting shorted when CD deleted */ if (selected_row >= max_row)selected_row = 0; clear_all_screen(

10、); mvprintw(start_screenrow - 2, start_screencol, greet); keypad(stdscr, TRUE); cbreak(); noecho(); key = 0; while (key != 'q' && key != KEY_ENTER && key != 'n') if (key = KEY_UP) if (selected_row = 0)selected_row = max_row - 1; elseselected_row-;if (key = KEY_DOWN) i

11、f (selected_row = (max_row - 1)selected_row = 0; elseselected_row+;selected = *choicesselected_row;draw_menu(choices, selected_row, start_screenrow, start_screencol);key = getch(); keypad(stdscr, FALSE); nocbreak(); echo(); if (key = 'q')selected = 'q'return (selected);void draw_menu

12、(char *options, int current_highlight, int start_row, int start_col) int current_row = 0; char *option_ptr; char *txt_ptr; option_ptr = options; while (*option_ptr) if (current_row = current_highlight) attron(A_STANDOUT);txt_ptr = optionscurrent_row;txt_ptr+;mvprintw(start_row + current_row, start_c

13、ol, "%s", txt_ptr);if (current_row = current_highlight) attroff(A_STANDOUT);current_row+;option_ptr+; mvprintw(start_row + current_row + 3, start_col, "Move highlight then press Return "); refresh();/* clear_all_screen Clear the screen and re-write the title. If a CD is selected

14、then display the information. */void clear_all_screen() clear(); mvprintw(2, Q_LINE, "%s", "CD Database Application"); if (current_cd0) mvprintw(ERROR_LINE, 0, "Current CD: %s: %sn", current_cat, current_cd); refresh();/* get_return Prompt for and read a carriage return

15、. Ignore other characters. */void get_return() int ch; mvprintw(23, 0, "%s", " Press return "); refresh(); while (ch = getchar() != 'n' && ch != EOF);/* get_confirm Prompt for and read confirmation. Read a string and check first character for Y or y. On error or o

16、ther character return no confirmation. */int get_confirm() int confirmed = 0; char first_char = 'N' mvprintw(Q_LINE, 5, "Are you sure? "); clrtoeol(); refresh(); cbreak(); first_char = getch(); if (first_char = 'Y' | first_char = 'y') confirmed = 1; nocbreak(); if (

17、!confirmed) mvprintw(Q_LINE, 1, " Cancelled");clrtoeol();refresh();sleep(1); return confirmed;/database operation/* Database File Manipulation Functions */* insert_title Add a title to the CD database Simply add the title string to the end of the titles file */void insert_title(char *cdtit

18、le) FILE *fp = fopen(title_file, "a"); if (!fp) mvprintw(ERROR_LINE, 0, "cannot open CD titles database"); else fprintf(fp, "%sn", cdtitle);fclose(fp); /* get_string At the current screen position prompt for and read a string Delete any trailing newline. */void get_stri

19、ng(char *string) int len; wgetnstr(stdscr, string, MAX_STRING); len = strlen(string); if (len > 0 && stringlen - 1 = 'n')stringlen - 1 = '0'/* add_record Add a new CD to the collection */void add_record() char catalog_numberMAX_STRING; char cd_titleMAX_STRING; char cd_type

20、MAX_STRING; char cd_artistMAX_STRING; char cd_entryMAX_STRING; int screenrow = MESSAGE_LINE; int screencol = 10; clear_all_screen(); mvprintw(screenrow, screencol, "Enter new CD details"); screenrow += 2; mvprintw(screenrow, screencol, "Catalog Number: "); get_string(catalog_numb

21、er); screenrow+; mvprintw(screenrow, screencol, " CD Title: "); get_string(cd_title); screenrow+; mvprintw(screenrow, screencol, " CD Type: "); get_string(cd_type); screenrow+; mvprintw(screenrow, screencol, " Artist: "); get_string(cd_artist); screenrow+; mvprintw(15,

22、5, "About to add this new entry:"); sprintf(cd_entry, "%s,%s,%s,%s", catalog_number, cd_title, cd_type, cd_artist); mvprintw(17, 5, "%s", cd_entry); refresh(); move(PROMPT_LINE, 0); if (get_confirm() insert_title(cd_entry);strcpy(current_cd, cd_title);strcpy(current_cat

23、, catalog_number); /* count_cds - scan the database and count titles and tracks */void count_cds() FILE *titles_fp, *tracks_fp; char entryMAX_ENTRY; int titles = 0; int tracks = 0; titles_fp = fopen(title_file, "r"); if (titles_fp) while (fgets(entry, MAX_ENTRY, titles_fp) titles+;fclose(t

24、itles_fp); tracks_fp = fopen(tracks_file, "r"); if (tracks_fp) while (fgets(entry, MAX_ENTRY, tracks_fp) tracks+;fclose(tracks_fp); mvprintw(ERROR_LINE, 0, "Database contains %d titles, with a total of %d tracks.", titles, tracks); get_return();/* find_cd - locate a CD in the dat

25、abase prompt for a substring to match in the database set current_cd to the CD title */void find_cd() char matchMAX_STRING, entryMAX_ENTRY; FILE *titles_fp; int count = 0; char *found, *title, *catalog; mvprintw(Q_LINE, 0, "Enter a string to search for in CD titles: "); get_string(match);

26、titles_fp = fopen(title_file, "r"); if (titles_fp) while (fgets(entry, MAX_ENTRY, titles_fp) /* Skip past catalog number */ catalog = entry; if (found = strstr(catalog, ",") *found = 0;title = found + 1;/* Zap the next comma in the entry to reduce it to title only */if (found = s

27、trstr(title, ",") *found = '0' /* Now see if the match substring is present */ if (found = strstr(title, match) count+;strcpy(current_cd, title);strcpy(current_cat, catalog); fclose(titles_fp); if (count != 1) if (count = 0) mvprintw(ERROR_LINE, 0, "Sorry, no matching CD found

28、. ");if (count > 1) mvprintw(ERROR_LINE, 0, "Sorry, match is ambiguous: %d CDs found. ", count);current_cd0 = '0'get_return(); /* remove_tracks - delete tracks from the current CD */void remove_tracks() FILE *tracks_fp, *temp_fp; char entryMAX_ENTRY + 1; int cat_length; if

29、(current_cd0 = '0')return; cat_length = strlen(current_cat); tracks_fp = fopen(tracks_file, "r"); if (tracks_fp = (FILE *)NULL) return; temp_fp = fopen(temp_file, "w"); while (fgets(entry, MAX_ENTRY, tracks_fp) /* Compare catalog number and copy entry if no match */if (st

30、rncmp(current_cat, entry, cat_length) != 0) fputs(entry, temp_fp); fclose(tracks_fp); fclose(temp_fp); unlink(tracks_file); rename(temp_file, tracks_file);/* remove_cd - delete the current CD from the database */void remove_cd() FILE *titles_fp, *temp_fp; char entryMAX_ENTRY; int cat_length; if (cur

31、rent_cd0 = '0')return; clear_all_screen(); mvprintw(PROMPT_LINE, 0, "About to remove CD %s: %s. ", current_cat, current_cd); if (!get_confirm()return; cat_length = strlen(current_cat); /* Copy the titles file to a temporary, ignoring this CD */ titles_fp = fopen(title_file, "r

32、"); temp_fp = fopen(temp_file, "w"); while (fgets(entry, MAX_ENTRY, titles_fp) /* Compare catalog number and copy entry if no match */if (strncmp(current_cat, entry, cat_length) != 0) fputs(entry, temp_fp); fclose(titles_fp); fclose(temp_fp); /* Delete the titles file, and rename the

33、temporary file */ unlink(title_file); rename(temp_file, title_file); /* Now do the same for the tracks file */ remove_tracks(); /* Reset current CD to 'None' */ current_cd0 = '0'/* Some defines we use only for showing or entering the track information */#define BOXED_LINES 11#define

34、BOXED_ROWS 60#define BOX_LINE_POS 8#define BOX_ROW_POS 2/* list_tracks - list the tracks for the current CD */void list_tracks() FILE *tracks_fp; char entryMAX_ENTRY; int cat_length; int lines_op = 0; WINDOW *track_pad_ptr; int tracks = 0; int key; int first_line = 0; if (current_cd0 = '0')

35、mvprintw(ERROR_LINE, 0, "You must select a CD first. ", stdout);get_return();return; clear_all_screen(); cat_length = strlen(current_cat); /* First count the number of tracks for the current CD */ tracks_fp = fopen(tracks_file, "r"); if (!tracks_fp)return; while (fgets(entry, MAX

36、_ENTRY, tracks_fp) if (strncmp(current_cat, entry, cat_length) = 0) tracks+; fclose(tracks_fp); /* Make a new pad, ensure that even if there is only a single track the PAD is large enough so the later prefresh() is always valid. */ track_pad_ptr = newpad(tracks + 1 + BOXED_LINES, BOXED_ROWS + 1); if

37、 (!track_pad_ptr)return; tracks_fp = fopen(tracks_file, "r"); if (!tracks_fp)return; mvprintw(4, 0, "CD Track Listingn"); /* write the track information into the pad */ while (fgets(entry, MAX_ENTRY, tracks_fp) /* Compare catalog number and output rest of entry */if (strncmp(curr

38、ent_cat, entry, cat_length) = 0) mvwprintw(track_pad_ptr, lines_op+, 0, "%s", entry + cat_length + 1); fclose(tracks_fp); if (lines_op > BOXED_LINES) mvprintw(MESSAGE_LINE, 0, "Cursor keys to scroll, RETURN or q to exit"); else mvprintw(MESSAGE_LINE, 0, "RETURN or q to ex

39、it"); wrefresh(stdscr); keypad(stdscr, TRUE); cbreak(); noecho(); key = 0;while (key != 'q' && key != KEY_ENTER && key != 'n') if (key = KEY_UP) if (first_line > 0)first_line-;if (key = KEY_DOWN) if (first_line + BOXED_LINES + 1 < tracks)first_line+;/* now

40、draw the appropriate part of the pad on the screen */prefresh(track_pad_ptr, first_line, 0, BOX_LINE_POS, BOX_ROW_POS, BOX_LINE_POS + BOXED_LINES, BOX_ROW_POS + BOXED_ROWS);/*wrefresh(stdscr); */key = getch(); delwin(track_pad_ptr); keypad(stdscr, FALSE); nocbreak(); echo();/* update_cd - re-enter t

41、racks for current CD deletes all tracks for the current CD in the database and then prompts for new ones. */void update_cd() FILE *tracks_fp; char track_nameMAX_STRING; int len; int track = 1; int screen_line = 1; WINDOW *box_window_ptr; WINDOW *sub_window_ptr; clear_all_screen(); mvprintw(PROMPT_LI

42、NE, 0, "Re-entering tracks for CD. "); if (!get_confirm()return; move(PROMPT_LINE, 0); clrtoeol(); remove_tracks(); mvprintw(MESSAGE_LINE, 0, "Enter a blank line to finish"); tracks_fp = fopen(tracks_file, "a"); /* Just to show how, enter the information in a scrolling,

43、 boxed, window. The trick is to set-up a sub-window, draw a box around the edge, then add a new, scrolling, sub-window just inside the boxed sub-window. */ box_window_ptr = subwin(stdscr, BOXED_LINES + 2, BOXED_ROWS + 2, BOX_LINE_POS - 1, BOX_ROW_POS - 1); if (!box_window_ptr)return; box(box_window_

44、ptr, ACS_VLINE, ACS_HLINE); sub_window_ptr = subwin(stdscr, BOXED_LINES, BOXED_ROWS, BOX_LINE_POS, BOX_ROW_POS); if (!sub_window_ptr)return; scrollok(sub_window_ptr, TRUE); werase(sub_window_ptr); touchwin(stdscr); do mvwprintw(sub_window_ptr, screen_line+, BOX_ROW_POS + 2, "Track %d: ", track);clrtoeol();refresh();wgetnstr(sub_window_ptr, track_name, MAX_STRING);len = strlen(track_name);if (len

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论