實驗六_指針[1]_第1頁
實驗六_指針[1]_第2頁
實驗六_指針[1]_第3頁
實驗六_指針[1]_第4頁
全文預覽已結束

下載本文檔

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

文檔簡介

1、C語言程序設計實驗報告實驗六 使用指針的程序設計學號 姓名 一、實驗目的1、 掌握指針的概念,會定義和使用指針變量;2、 能正確使用數(shù)組的指針和指向數(shù)組的指針變量;3、 熟悉指針作為函數(shù)參數(shù)的定義和調(diào)用方法;4、 能正確使用字符串的指針和指向字符串的指針變量。二、實驗內(nèi)容1. 分析下面的程序并上機運行程序,要求寫出3組以上程序的運行結果。#include <stdio.h> void main()int *p1,*p2,*p;int a,b;printf("Input a b please");scanf("%d%d",&a,&

2、;b);p1=&a;p2=&b;if(a>b)p=p1;p1=p2;p2=p;printf("*p1=%d, *p2=%dn",*p1,*p2);printf("a=%d, b=%dn",a,b);運行結果:Input a b please23 12*p1=12, *p2=23a=23, b=12Press any key to continueInput a b please67 87*p1=67, *p2=87a=67, b=87Press any key to continue2. 下列程序的功能是分別求出數(shù)組中所有奇數(shù)之和以

3、及所有偶數(shù)之和。形參n給了數(shù)組3 #include <stdio.h>#define N 10void fun(int *a,int n,int *odd,int *even) for(n=0;n<6;n+)if(*(a+n)%2=1)*odd=*odd+*(a+1);else*even=*even+*(a+1) ;void main() int aN=1,10,2,3,19,6,i,n=6,odd,even; printf("The original data is:n"); for(i=0;i<n;i+) printf("%5d&quo

4、t;,*(a+i); printf("nn"); fun(a,n,&odd,&even); printf("The sum of odd numbers:%dn",odd); printf("The sum of even number:%dn",even);#include <stdio.h>#define N 10void fun( int *a,int n,int *odd,int *even)int i;for(i=0;i<n;i+)if(ai%2=1)*odd=*odd+ai;else*ev

5、en=*even+ai;void main() int aN=1,10,2,3,19,6,i,n=6,odd=0,even=0; printf("The original data is:n"); for(i=0;i<n;i+) printf("%5d",*(a+i); printf("nn"); fun(a,n,&odd,&even); printf("The sum of odd numbers:%8dn",odd); printf("The sum of even number

6、:%8dn",even); 運行結果:3. 編程實現(xiàn)從鍵盤輸入一個字符ch和一個字符串str,利用字符指針實現(xiàn)刪除字符串str中和字符ch相等所有字符,然后輸出字符串str。運行程序并寫出2組以上程序運行結果。程序代碼:#include<stdio.h>#define N 5void main()char ch='a',strN,*p;int i;printf("input str:");scanf("%s",str);p=str;for(i=0;i<5;i+)if(*(p+i)=ch)for(i=i;i<N;i+)*(p+i)=*(p+i+1);printf("%sn",str);input str:hgjashgjsPress any key to continueinput str:greatgretPress any key to continue運行結果:三

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論