欧美日韩不卡一区二区三区,www.蜜臀.com,高清国产一区二区三区四区五区,欧美日韩三级视频,欧美性综合,精品国产91久久久久久,99a精品视频在线观看

c語言函數實習報告

時間:2022-08-03 13:23:52 實習報告 我要投稿
  • 相關推薦

c語言函數實習報告

程序設計(C語言)實驗報告

c語言函數實習報告

實驗目的

(1)掌握函數的定義方法,調用方法,參數說明以及返回值;

(2)掌握實參與形參的對應關系,一集參數之間的“值傳遞”的方式;

(3)掌握函數嵌套調用及遞歸調用的設計方法;

(4)在編寫過程中加深理解函數調用的程序設計思想。

實驗內容

(1)編輯,編譯,運行實驗指導中的程序,并分析輸出結果

(2)編寫一個函數primeNum(int x),功能是判別一個數是否是素數。

(3)編寫函數mulNum(int a,int b),功能是判定a是否是b的整數倍

實驗1方法一:

源程序:

#include<stdio.h>

#include<math.h>

int computeNum(int x)

{

int sum ,a,b,c,d,e;

sum=0;

x=abs(x);

a=x/10000;

b=(x%10000)/1000;

c=(x%1000)/100;

d=(x%100)/10;

e=(x%10)/1;

sum=a+b+c+d+e;

return sum;

}

main()

{

int a,b;

printf("Please input an integer:");

scanf("%d",&a);

b=computeNum(a);

printf("the sum of all digits is %d\n",b);

}

輸入一個整數123 運行結果如圖

輸入整數98341驗證 運行結果如圖

方法二:

#include<stdio.h>

#include<math.h>

int computeNum(int x)

{

int sum,i,t;

sum=0;

x=abs(x);

for(i=4;i>=0;i--)

{

t=pow(10,i);

if(x>=t)

{

sum=sum+x/t;

x=x-(x/t)*t;

}

}

return sum;

}

main()

{

int a,b;

printf("Please input an integer:");

scanf("%d",&a);

b=computeNum(a);

printf("The sum of all digits is %d\n:",b); }

輸入整數456運行結果如圖

輸入整數98341驗證運行結果如圖

實驗2:

源程序:

#include<stdio.h>

void move(char geton ,char puton)

{

printf("%c->%c\n",geton,puton);

}

void Hanoi(int n,char one,char two,char three) {

if (n==1)

move(one,three);

else

{

Hanoi(n-1,one,three,two);

move(one,three);

Hanoi(n-1,two,one,three);

}

}

void main()

{

int m ;

printf("Input the number of diskes:"); scanf("%d",&m);

printf("The steps of moving %d diskes:\n",m); Hanoi(m,'A','B','C');

}

輸入3運行結果如下:

輸入4運行結果如下:

實驗2:

源程序:

#include<stdio.h>

int i,a,x;

int primeNum(int x)

{

for(i=2;i<x;i++)

{

a=x%i;

if(a==0)

return 0;

}

return 1;

}

main()

{

printf("Please input x!\n");

scanf("%d",&x);

if(x<2)

printf("wrong in put!\n");

else

{

a=primeNum(x);

if(a==0)

printf("%d is not a prime number!\n",x); else

printf("%d is a prime number!\n",x);

}

}

輸入數據0運行結果如下:

輸入數據1運行結果如下:

輸入數據2運行結果如下:

輸入數據3運行結果如下:

輸入數據9運行結果如下:

輸入數據13運行結果如下:

輸入數據59運行結果如下:

輸入數據121運行結果如下:

實驗3: 源程序:

#include<stdio.h>

int mulNum(int a,int b) {

int i,c; c=a%b; if(c>0)

i=0; else i=1; return i; }

main() {

int a,b,s;

printf("please input a and b:\n"); scanf("%d %d",&a,&b); s=mulNum(a,b); if(s==1)

printf("%d is a multiple of %d\n",a,b); else

printf("%d is not a multiple of %d\n",a,b); }

輸入數據1和5運行結果如下:

輸入數據5和5運行結果如下:

輸入數據6和2運行結果如下:

輸入數據6和4運行結果如下:

輸入數據20和4運行結果如下:

輸入數據37和9運行結果如下:

出現(xiàn)的問題及解決方法:

編譯過程中常出現(xiàn)因錯漏而使語句不規(guī)范的現(xiàn)象。解決方法:更加認真以及注意檢查。

實驗心得:

通過本次試驗我掌握了函數的定義,調用方法,參數的說明以及返回值,函數遞歸調用的設計方法等,逐步理解了函數調用的程序設計思想,學習過程常會遇到問題,因此需要認真理解,多作練習。

【c語言函數實習報告】相關文章:

C語言函數的定義03-03

什么是C語言函數02-28

C語言常用的輸入函數03-04

C語言的函數分類03-24

c語言數學函數的介紹10-20

C語言中gets()函數知識04-02

C語言函數調用與參數傳遞10-10

C語言函數參數傳遞問題03-22

c語言隨機數生成函數和時間函數03-16

C語言中函數的區(qū)分有哪些04-27