實驗八實驗報告卓越14彭佳偉_第1頁
實驗八實驗報告卓越14彭佳偉_第2頁
實驗八實驗報告卓越14彭佳偉_第3頁
實驗八實驗報告卓越14彭佳偉_第4頁
實驗八實驗報告卓越14彭佳偉_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、 C語言程序設(shè)計 實驗報告專業(yè) 計算機(jī)科學(xué)與技術(shù) 班級 卓越工程師班 日期 2014年12月17日 成績 第八次實驗 指針實驗 指導(dǎo)教師 李開 學(xué)生姓名 彭佳偉 學(xué) 號 U201414716 實驗組別 同組人姓名 實驗名稱 指針實驗 一、實驗?zāi)康模?) 熟練掌握指針的說明、賦值、使用。(2) 掌握用指針引用數(shù)組的元素,熟悉指向數(shù)組的指針的使用。(3) 熟練掌握字符數(shù)組與字符串的使用,掌握指針數(shù)組及字符指針數(shù)組的用法。(4) 掌握指針函數(shù)與函數(shù)指針的用法。(5) 掌握帶有參數(shù)的main函數(shù)的用法。二、實驗任務(wù)1. 源程序改錯下面的源程序中是否存在錯誤?如果存在,原因是什么?如果存在錯誤,要求在計

2、算機(jī)上對這個源程序進(jìn)行調(diào)試修改,使之能夠正確執(zhí)行。源程序1#includeint main(void) float *p; scanf(%f, p); printf(%fn, *p); return 0;2. 源程序完善、修改、替換(1) 下面的源程序的功能是:通過函數(shù)指針和菜單選擇來調(diào)用字符串拷貝函數(shù)或字符串連接函數(shù)。請在程序中的下劃線處填寫合適的表達(dá)式、語句或代碼片段來完善該程序。#include#includeint main(void) char a80, b80, c160, *result = c; int choice, i; do printf(tt1 copy string.

3、n); printf(tt2 connect string.n); printf(tt3 exit. n); printf(ttinput a number(1-3) please! n); scanf (%d, &choice); while(choice 5); switch(choice) case 1: p = strcpy; break; case 2: p = strcat; break; case 3: goto dowm; getchar(); printf(input the first string please! n); i = 0; printf(input the s

4、econd string please! n); i = 0; result = (a, b); printf(the result is %sn, result); dowm: ; return 0;(2) 請上機(jī)運行第(1)題程序,使之能按下面要求輸出結(jié)果:1 copy string.2 connet string.3 exitinput a number(1-2) please! 2(輸入)Input the first string please!the more you learn,(輸入)input the second string please!the more you get

5、.(輸入)the result is the more you learn, the more you get.3. 跟蹤調(diào)試源程序請按下面的要求對所給的源程序進(jìn)行操作,并回答問題和排除錯誤。(1) 單步執(zhí)行源程序。進(jìn)入strcpy時,watches窗口中s為何值?返回main時,watches窗口中s為何值?(2) 排除源程序中的錯誤,使程序輸出結(jié)果為:there is a bote on the lake.源程序3#includechar *strcpy(char *, char*);int main(void) char a20, b60 = there is a boat on the

6、 lake.; printf(%sn, strcpy(a, b); return 0;char *strcpy(char *s, char *t) while(*s+ = *t+) ; return (s);4. 程序設(shè)計編寫并上機(jī)調(diào)試運行能實現(xiàn)以下功能的程序或函數(shù):(1) 已知一個長整型變量占4個字節(jié),其中每個字節(jié)又分成高4位和低4位。試編寫一個程序,從該長整型變量的高字節(jié)開始,依次取出每個字節(jié)的高4位和低4位并以數(shù)字字符的形式記性顯示。(2) 利用大小為n的指針數(shù)組指向用gets函數(shù)輸入的n行,每行不超過80個字符。試編寫一個函數(shù),它將每一行中連續(xù)的多個空格字符壓縮為一個空格字符。在調(diào)用函

7、數(shù)中輸出壓縮空格后的各行,空行不予輸出。(3) 編寫一個程序,輸入n個整數(shù),排序后輸出。排序的原則由命令行可選參數(shù)-的-d決定,并且有參數(shù)-d時按遞減順序排序,否則按遞增順序排序。要求將排序算法定義成函數(shù),利用指向函數(shù)的指針使該函數(shù)實現(xiàn)遞增或遞減排序。(4) 設(shè)某個班有N個學(xué)生,每個學(xué)生修了M門課程。輸入M門課程的名稱,然后依次輸入N個學(xué)生中每個學(xué)生所修M門課程的成績,并且都存放到相應(yīng)的數(shù)組中。試編寫下列函數(shù): 計算每個學(xué)生各門課程平均成績。 計算全班沒門課程的平均成績。 分別統(tǒng)計低于全班各門課程平均成績的人數(shù)。 分別統(tǒng)計全班各門課程不及格的人數(shù)和90分以上的人數(shù)。在調(diào)用函數(shù)中輸出上面各函數(shù)的

8、計算結(jié)果(要求都用指針操作,不得使用下下標(biāo)操作)三、實驗步驟及結(jié)果1 源代碼:/ 2.8.1/ Created by apple on 14/12/17./ Copyright (c) 2014年 pjwhust. All rights reserved./#include int main(void) float *p; float a; scanf(%f,&a); p = &a; printf(%fn,*p); return 0;運行結(jié)果:2.1 源代碼:/ 2.8.2.1/ Created by apple on 14/12/17./ Copyright (c) 2014年 pjwhus

9、t. All rights reserved./#include #include int main(void) char *(*p)(char *s1, const char *s2); char a80,b80,c160,*result = c; int choice,i; do printf(tt1 copy string.n); printf(tt2 connect string.n); printf(tt3 exit.n); printf(ttinput a number(1-3) please!n); scanf(%d, &choice); while (choice 3); sw

10、itch (choice) case 1: p = strcpy; break; case 2: p = strcat; break; case 3: goto down; default: break; getchar(); printf(input the first string please!n); i = 0; while (ai+ = getchar() != n ); ai-1 = 0; printf(input the second string please!n); i = 0; while (bi+ = getchar() != n); bi-1 = 0; result =

11、 p(a,b); printf(the result is %sn,result);down: ; return 0;2.1.2 運行結(jié)果:3 源代碼:/ 2.8.3/ Created by apple on 14/12/17./ Copyright (c) 2014年 pjwhust. All rights reserved./#include char *mystrcpy(char *, char *);int main(void) char a60, b60 = there is a boat on the lake.; printf(%sn,mystrcpy(a,b); return

12、0;char *mystrcpy(char *s, char *t) char *p = s; while ( *t != 0) *p = *t; p+; t+; ; *p = 0; return s;運行結(jié)果:4.1 源代碼:/ 2.8.4.1/ Created by apple on 14/12/17./ Copyright (c) 2014年 pjwhust. All rights reserved./#include int main(void) long int n = 0x1234ABCD; char *p = (char *)&n; int high_half,low_half;

13、 int i; for (i=3; i=0; i-) low_half = pi & 0x0f; if (low_half 4; if (high_half 10) high_half += 0; else high_half += A - 10; printf(%dt%dn,high_half,low_half); return 0;運行結(jié)果:4.2 源代碼:/ 2.8.4.2/ Created by apple on 14/12/17./ Copyright (c) 2014年 pjwhust. All rights reserved./#include #define N 5#defin

14、e MAX 80void ChangeInput(void);int main(void) ChangeInput(); return 0;void ChangeInput(void) char sN80; char *pN; int i; printf(Please input %d lines:n,N); for (i=0; iN; i+) gets(si); pi = si; for (i=0; iN; i+) int j; for (j=0; pij!=0; j+) if (pij= & pij+1= ) int k=j+1; while (pik+= ); k-; int tmp=j

15、+1; while (pitmp+=pik+); for (i=0; iN; i+) printf(%sn,*(p+i);運行結(jié)果:4.3 源代碼:/ 2.8.4.3/ Created by apple on 14/12/17./ Copyright (c) 2014年 pjwhust. All rights reserved./#include#include#define n 5void sort1(int *, int);void sort2(int *, int);int main(int argc, char *argv) int an; int i; void (*p)(int *

16、, int); printf(input %d integers:n, n); for (i = 0; i 1 & !strcmp(argv1, -d) p = sort1; else p = sort2; p(a, n); printf(after sort:n); for(i = 0; i n; i+) printf(%d , ai); printf(n); return 0;void sort1(int *a, int m) int i, j, t; for(i = 0; i m - 1; i+) for(j = 0; j m - 1 - i; j+) if(*(a + j) *(a +

17、 j + 1) t = *(a + j); *(a + j) = *(a + j + 1); *(a + j + 1) = t; void sort2(int *a, int m) int i, j, t; for(i = 0; i m - 1; i+) for(j = 0; j * (a + j + 1) t = *(a + j); *(a + j) = *(a + j + 1); *(a + j + 1) = t; 運行結(jié)果:4.4 源代碼:/ 2.8.4.4/ Created by apple on 14/12/17./ Copyright (c) 2014年 pjwhust. All

18、rights reserved./#include #define M 3 /subjects#define N 4 /studentsstruct Student int scoreM; int sum;void AverageStudents(struct Student studentN); /每個學(xué)生各門課的平均成績void AverageSubjets(char subjectM10, struct Student studentN, float *average); /全班每門課的平均成績void BelowAverage(char subjectM10, struct Stude

19、nt studentN, float averageM); /低于全班各門課程平均成績的人數(shù)void Below60_Above90(char subjectM10, struct Student studentN); /各門課程低于60分和高于90(包含90)分的人數(shù)int main(void) char subjectM10; float averageM; struct Student studentN; int i,j; /初始化struct student for(i=0; iN; i+) for(j=0; jscore+j) = 0; (student+i)-sum = 0; pr

20、intf(Please inpt %d subjects:n,M); for (i=0; iM; i+) scanf( %s,*(subject+i); printf(please input score of %d students:n,N); for (i=0; iN; i+) for (j=0; jscore+j); AverageStudents(student); AverageSubjets(subject, student, average); BelowAverage(subject, student, average); Below60_Above90(subject, st

21、udent); return 0;void AverageStudents(struct Student studentN) /每個學(xué)生各門課的平均成績 int i,j; for (i=0; iN; i+) for (j=0; jsum += *(student+i)-score+j); printf(nAverage Score Of Each Studentn); for (i=0; isum/M); void AverageSubjets(char subjectM10, struct Student studentN, float averageM) /全班每門課的平均成績 int s

22、umM; int i,j; for (i=0; iM; i+) *(sum+i) = 0; for (i=0; iM; i+) for (j=0; jscore+i); printf(nAverage Score of Each Subjectn); for (i=0; iM; i+) printf(tsubject %s : %.2fn, *(subject+i), *(average+i) = (*(sum+i)*1.0/N); void BelowAverage(char subjectM10, struct Student studentN, float averageM) /低于全班各門課程平均成績的人數(shù) int i,j; int cntM; for (i=0; iM; i+) *(cnt+i) = 0; for (i=0; iM; i+) for (j=0; jscore+i) *(average+i) *(cnt+i) += 1; printf(nS

溫馨提示

  • 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論