日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

40题计算机程序设计基础(C语言)编程习题

發(fā)布時(shí)間:2023/12/10 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 40题计算机程序设计基础(C语言)编程习题 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

注意:部分代碼雙引號(hào)中英文有問題,自己調(diào)整!

1.輸入2個(gè)整數(shù),求兩數(shù)的平方和并輸出。?

??#include <stdio.h>

???main()

{ int ?a,b,s; ?

?printf("please input a,b:\n");

?scanf("%d%d",&a,&b);

?s=a*a+b*b;

????printf("the result ?is %d\n",s);

?} ?

2. 輸入一個(gè)圓半徑(r)當(dāng)r>0時(shí),計(jì)算并輸出圓的面積和周長,否則,輸出提示信息。

#include <stdio.h>

???#define PI 3.14 <stdio.h>

main()

{ float ?r ,s , l; ?

?printf("please input r:\n");

?scanf("%f",&r);

if (r>=0) ???????????????????????

??{s=pi*r*r;

??????l=2*i*r ;

??printf("the area is %f\n",s);

printf("the circumference is %f\n",l);}

else ?

printf("input error!\n");

?} ?

3、函數(shù)y=f(x)可表示為:

???????????????????2x+1 ??(x<0)

?????????????y= ???0 ????(x=0)

???????????????????2x-1 ?(x>0)

編程實(shí)現(xiàn)輸入一個(gè)x值,輸出y值。??

main()

{int x,y;

scanf("%d",&x);

If(x<0)y=2*x+1;

If(x>0)y=2*x-1;

If(x==0) y=0;

printf(“%d”,y);}

4、編寫一個(gè)程序,4個(gè)整數(shù)中找出最小的數(shù),并顯示此數(shù)。

main( )

{int a,b,c,d,t;

?scanf ("%d,%d,%d,%d",&a,&b,&c,&d);

?if (a>b)

???{t=a; a=b; b=t;}

?if (a>c)

???{t=a; a=c; c=t;}

?if (a>d)

???{t=a; a=d; d=t;}

?printf ("min = %d \n",a);

}

5.有一函數(shù)當(dāng)x<0時(shí)y=1,當(dāng)x>0時(shí),y=3,當(dāng)x=0時(shí)y=5,編程,從鍵盤輸入一個(gè)x值,輸出y值。

main()

{int x,y;

scanf("%d",&x);

if (x<0) y=1;

else if(x==0) y=5;

else y=3;

printf("x=%d,y=%d\n",x,y);}

?

6.從鍵盤輸入兩個(gè)數(shù),求出其最大值(要求使用函數(shù)完成求最大值,并在主函數(shù)中調(diào)用該函數(shù))

main()

{float max(float x,float y); ????

float a,b,m;

scanf("%f,%f",&a,&b);

m=max(a,b);

printf("Max is %f\n",m);

}

float max(float x,float y)

{

float temp;

if (x<y)

{temp=x;

???????x=y;

?y=temp;

}

return(x);

}

7、從鍵盤輸入你和你朋友的年齡,編成判斷誰的年齡最大,并打印最大者的年齡。

#include ?<stdio.h>

main()

{ int ?yourAge, hisAge;

printf("Please enter your age:");

scanf("%d", &yourAge); ??????/*輸入你的年齡yourAge*/

printf("Please enter your friend's age:");

scanf("%d", &hisAge); ??????/*輸入你朋友的年齡hisAge*/

if (yourAge >= hisAge)

{

printf("You are older! Your age is = %d\n", yourAge); ?

}

if (hisAge > yourAge)

{

printf("Your friend is older! HisAge age is = %d\n", hisAge);

}}

8、鍵盤輸入2個(gè)加數(shù),再輸入答案,如果正確,顯示“right”,否則顯示“error”

#include “stdio.h”

main( )

{int a,b,c;

?printf(“please input a and b\n”);

scanf (%d,%d”,&a,&b);

?printf(“please input the answer for a+b\n”);

scanf (%d”,&c);

?if (c==a+b)

?????printf(“right\n”);

?else

?????printf(“error\n”);

}?

9. 編一程序每個(gè)月根據(jù)每個(gè)月上網(wǎng)時(shí)間計(jì)算上網(wǎng)費(fèi)用,計(jì)算方法如下:? ? ? ? ? ? ??

要求當(dāng)輸入每月上網(wǎng)小時(shí)數(shù),顯示該月總的上網(wǎng)費(fèi)用(6)

main()

?{ int hour;

??float fee;

??printf(“please input hour:\n”);

?scanf(“%d”,&hour);

?if(hour<=10)

???fee=30;

?else if(hour>=10&&hour<=50)

??fee=3*hour;

?else fee=hour*2.5;

?printf(“The total fee is %f”,fee);

}

10.神州行用戶無月租費(fèi),話費(fèi)每分鐘0.6元,全球通用戶月租費(fèi)50元,話費(fèi)每分鐘0. 4元。輸入一個(gè)月的通話時(shí)間,分別計(jì)算出兩種方式的費(fèi)用,判斷哪一種合適。

????main()

??{float a,x,y;

???printf(“\n請(qǐng)輸入您的話費(fèi):”);

???scanf(“%f,”,&a);

???x= 0.6*a;

???y=50+0.4*a; ????

???printf (“神州行話費(fèi)為: %f\n”,x);

printf (“全球通話費(fèi)為: %f\n”,y);

if (x>=y)

??????printf(“建議使用全球通”);

???else printf(“建議使用神州行);

????}

11.個(gè)人所得稅計(jì)算,應(yīng)納稅款的計(jì)算公式如下:

收入

稅率

收入<=1000元部分

0%

2000元>=收入>1000元的部分

5%

3000元>=收入>2000元的部分

10%

6000元>=收入>3000元的部分

15%

收入>6000元的部分

20%

輸入某人的收入,計(jì)算出應(yīng)納稅額及實(shí)際得到的報(bào)酬。(7分)

(如需連續(xù)計(jì)算多個(gè)人的納稅情況,直到輸入負(fù)數(shù)為止,程序應(yīng)如何改進(jìn)?試寫出程序)

#include “stdio.h”

main()

{

??int grade;

??float income,tax,money;

??printf(“please input your income\n”);

scanf (“%f”,&income);

??if (income<0)

??????printf(“the input is error”);

??else

{ grade=(int)income/1000;

??switch(grade)

??????{ case 0 : tax=0;break;

????????case 1 : tax=(income-1000)*0.05;break;

????????case 2 : tax=50+(income-2000)*0.1;break;

????????case 3 :

case 4 :

case 5 : tax=150+(income-3000)*0.15;break;

default: tax=600+(income-6000)*0.2;

?????????????????}

???????????money=income-tax;

???????printf(“\n tax=%f, money=%f”,tax, money);

??????}

}

12.從鍵盤上輸入一個(gè)百分制成績score,按下列原則輸出其等級(jí):score≥90,等級(jí)為A80≤score<90,等級(jí)為B70≤score<80,等級(jí)為C60≤score<70,等級(jí)為Dscore<60,等級(jí)為E

?#include <stdio.h>

main()

{

int ???data; ?????????

char ?grade; ???????????????????

printf("Please enter the score:");

scanf("%d”, &data); ?

switch(data/10) ??????????????????

{ ??case 10: ?

???????case 9 : ?grade=’A’; ?break;

case 8: ?grade=’B’; ??break;

case 7: ?grade=’C’; ??break;

???????case 6: ?grade=’D’; ??break;

default: ?grade=’E’;

}

????printf("the grade is %c”,grade);

}

?

*13. 編程設(shè)計(jì)一個(gè)簡單的計(jì)算器程序。從鍵盤輸入2個(gè)操作數(shù),1個(gè)運(yùn)算符,當(dāng)運(yùn)算符為加(+)、減(-)、乘(*)、除(/)時(shí),輸出計(jì)算結(jié)果

??#include <stdio.h>

main()

{ int ?data1, data2; ?????????/*定義兩個(gè)操作符*/

char ?op; ?????????????????????/*定義運(yùn)算符*/

printf("Please enter the expression:");

scanf("%d%c%d", &data1, &op, &data2); ?/*輸入運(yùn)算表達(dá)式*/

switch(op) ???????????????????/*根據(jù)輸入的運(yùn)算符確定要執(zhí)行的運(yùn)算*/

{ ?case '+': ????????????????????????/*處理加法*/

printf("%d + %d = %d \n", data1, data2, data1 + data2);

break;

case '-': ????????????????????????/*處理減法*/

printf("%d - %d = %d \n", data1, data2, data1 - data2);

break;

case '*': ????????????????????????/*處理乘法*/

printf("%d * %d = %d \n", data1, data2, data1 * data2);

break;

case '/': ????????????????????????/*處理除法*/

if (0 == data2) ?/*為避免出現(xiàn)溢出錯(cuò)誤檢驗(yàn)除數(shù)是否為0*/

printf("Division by zero!\n");

else ?

??printf("%d / %d = %d \n", data1, data2, data1 / data2);

break;

default:

printf("Unknown operator! \n");

}

}

14. 從鍵盤輸入10個(gè)整數(shù),統(tǒng)計(jì)其中正數(shù)、負(fù)數(shù)和零的個(gè)數(shù),并在屏幕上輸出。

main( )

?{int a[10], i,p=0,n=0,z=0;

??printf(“please input number”);

for(i=0;i<10;i++)

{scanf(“%d,”,&a[i]);

??if (a[i]>0)

??????p++;

else if (a[i]<0)

??????n++;

else z++}

??printf(“正數(shù):%5d, 負(fù)數(shù):%5d,零:%5d\n”,p,n,z);

}

}

15、編程序?qū)崿F(xiàn)求1-200之間的所有數(shù)的乘積并輸出。

#include <stdio.h>

???main( )

???{ ?int ?i, sum=1

??????for(i=1; i<200 i=i+1)

??????????sum=sum*i;

??????printf(“the sum of odd is :%d”,sum);

}

16. 從鍵盤上輸入10個(gè)數(shù),求其平均值。

?main()

{

int ?a[10],i,s=0; ????????????????

float ave;; ?????????????????

for(i=0;i<10;i++)

scanf(“%d”,&a[i]);

for(i=0;i<10;i++)

???sum+=a[i];

????ave=(float)sum/10;

printf("ave = %f\n", ave);

}

17、編程序?qū)崿F(xiàn)求1-1000之間的所有奇數(shù)的和并輸出。

???#include <stdio.h>

???main( )

???{ ?int ?i, sum=0;

??????for(i=1; i<1000; i=i+2)

??????????sum=sum+i;

??????printf(“the sum of odd is :%d”,sum);

}

?

18.有一個(gè)分?jǐn)?shù)序列:2/13/25/38/513/821/13……

編程求這個(gè)序列的前20項(xiàng)之和。

main()

{

?int i,t,n=20;

??float a=2,b=1,s=0;

for(i=1;i<=n;i++)

{s=s+a/b;

t=a;

a=a+b;

b=t;

}

?printf("sum=%9.6f",s);

}

19. 用數(shù)組實(shí)現(xiàn)以下功能:輸入5個(gè)學(xué)生成績,而后求出這些成績的平均值并顯示出來。??

main()

{float ?a[5],i;

float s=0;

for(i=0;i<5;i++)

scanf(“%f”,&a[i]);

for(i=0;i<5;I++)

s=s+a[i];

printf(“result=%f”,s/5);

}

*20、用循環(huán)的方法構(gòu)造一個(gè)55列的二維數(shù)組,使主對(duì)角線上的變量為1,其它為0,并將數(shù)組中所有項(xiàng)按行按列顯示出來。

main()

{int ?a[5][5],i,j, s=0;

for(i=0;I<5;i++)

for(j=0;j<5;j++)

if(i= =j) a[i][j]=1;

else a[i][j]=0;

for(i=0;i<5;i++)

for(j=0;j<5;j++)

{if(j= =0) ?printf(“\n”);

printf(“%d ?”, a[i][j]);

}

}

21.求一個(gè)3×3矩陣對(duì)角線元素之和。從鍵盤輸入矩陣元素的值并輸出和的值.

main()

??{ int a[3][3],sum=0;

????int i,j;

????printf("Enter data:\n");

????for(i=0;i<3;i++)

???????for(j=0;j<3;j++)

?????????scanf("%d",&a[i][j]);

?????for(i=0;i<3;i++)

????????sum=sum+a[i][i];

?????printf("sum=%d",sum);

}

22.輸入n的值,n代表行數(shù),輸出如圖所示的圖形。(6分)

?????*

?????* ?* ?*

?????* ?* ?* ?* ?*

?????* ?* ?* ?* ?* ?* ?* ???(此圖為n4時(shí)的輸出結(jié)果)

?

#include <stdio.h>

main()

{int ?i , j , k; ?

for (i = 1; i <= 4; i++) ?????????????/*控制行數(shù)*/ ??????????

{ for (k = 1; k <= (2 * i - 1); k++) ???/*控制每行輸出的*號(hào)個(gè)數(shù)*/

{ ?printf("*"); }

printf("\n"); ? }} ????????????/*輸出一行后換行*/

23、從鍵盤輸入30名學(xué)生的成績數(shù)據(jù),求其中的最高分、最低分和平均分。

(提示:用數(shù)組存放成績數(shù)據(jù))

???#include<stdio.h>

???#define ?M ?30

???main ( )

???{ float score[M], max , min, aver;

?????int ?i ;

?????printf(“please input score: \n”);

?????for(i=0; i<M ; i++)

????????scanf(“%f”, &score[i]);

?????max=score[0];

?????min=score[0];

?????aver=score[0];

?????for(i=1; i<M; i++)

?????{ ?if (max < score[i]) ?max= score[i];

????????if (min>score[i]) ??min=score[i];

????????aver+=score[i];

?????}

?????printf(“max=%f, min=%f,aver=%f”, max, min, aver/M);

}

24.?從鍵盤輸入某班學(xué)生某門課的成績及其學(xué)號(hào)(班級(jí)人數(shù)最多40人,具體人數(shù)由鍵盤輸入),輸出該班最高分和最低分及其學(xué)生學(xué)號(hào);并輸出該班該課程的總分和平均分。請(qǐng)編寫程序。

#include <stdio.h>

#define ?ARR_SIZE ?40

main()

{ ?float ?score[ARR_SIZE], maxScore,minScore,sum;

int ???n, i;

long ??maxNum, minNum,num[ARR_SIZE];

printf("Please enter total number:");

scanf("%d", &n); ???????????????????

printf("Please enter the number and score:\n");

for (i=0; i<n; i++) ???

?scanf("%ld%f", &num[i], &score[i]);

maxScore = score[0];minScore= score[0];

maxNum = num[0]; minNum= num[0]; ??

??sum=score[0];

for (i=1; i<n; i++) ???????????????????????

{ if (score[i] > maxScore) ?????????????????????

??? { ? maxScore = score[i]; ????????????????????

????? ????maxNum = num[i]; ????????????????

??? }

???????else ?if (score[i] < minScore)

{ ?minScore = score[i]; ????????????????????

????? ???minNum = num[i];

??????????}

????sum=sum+score[i];

}

printf("maxScore = %.0f, maxNum = %ld\n", maxScore, maxNum); ?

??????printf("minScore = %.0f, minNum = %ld\n", minScore, minNum);

printf("sum = %.1f, average = %.1f\n", sum, sum/n);

}

*25.將一個(gè)有5個(gè)元素的數(shù)組中的值(整數(shù))按逆序重新存放。

: 原來順序?yàn)?/span>:86541,要求改為14568

?define N 5

main()

?{int a[N],I,temp;

??printf(“enter array a:\n”);

??for(I=0;I<N;I++)

???scanf(“%d”,$a[i]);

??for(I=0;I<N;I++)

{ temp=a[i];

?a[i]=a[N-I-1];

?a[N-I-1]=temp;

}

?????????printf(“\n Now, array a:\n”);

?????????for(I=0;I<N;I++)

???????????printf(“%4d”,a[i]);

???????????printf(“\n”);

???????}

?

*26.從鍵盤上輸入一個(gè)2*3的矩陣,將其轉(zhuǎn)后形成3*2的矩陣輸出。

????main()

???{int a[2][3], b[3][2],i,j;

????for(i=0;i<2;i++)

??????for(j=0;j<3;j++)

???????scanf(“%d”,&a[i][j]);

????for(i=0;i<3;i++)

??????for(j=0;j<2;j++)

?????????b[i][j]=a[j][i];

????for(i=0;i<3;i++)

??????{for(j=0;j<2;j++)

??????????printf("%5d",b[i][j]);

???????printf("\n”);

???????}

????????}

*27.編寫兩個(gè)函數(shù)分別求兩個(gè)整數(shù)的最小公倍數(shù)和最大公約數(shù),用主函數(shù)調(diào)用這兩個(gè)函數(shù)并輸出結(jié)果。兩個(gè)整數(shù)由鍵盤輸入。

???#include "stdio.h"

mingb(x,y)

int x,y;

{int z,i,t;

z=1;

i=1;

if(x>y)

???????{t=x;x=y;y=t;}

while(z<=x*y)

{

?z=i*y;

????????if((z%x==0)&&(z%y==0)) break;

?i++;

}

return(z);

}

maxgy(x,y)

int x,y;

{int z,t;

????if(x>y)

?{t=x;x=y;y=t;}

z=x;

while(z>1)

{ if((x%z==0)&&(y%z==0)) break;

??z--;

}

return(z);

}

main()

{

int a,b,c;

char ch;

printf("\nmingb(1)/maxgy(2)?");

ch=getchar();

printf("\ninput:");

scanf("%d,%d",&a,&b);

if(ch=='1') c=mingb(a,b);

else if(ch='2') c=maxgy(a,b);

printf("the result is %d",c);

getch();

}

*28. 輸入一個(gè)3*3矩陣,求出其轉(zhuǎn)置矩陣,并求出兩個(gè)矩陣的和.

main()

{

int a[3][3];

int b[3][3];

int c[3][3]

int i,j;

printf(“please input 6 numbers!”)

for (i=1;i<3;i++)

for(j=1;j<3;j++)

{

scanf(“%d”,&a[i][j]);

b[j][i]=a[i][j];

}

for (i=1;i<3;i++)

for(j=1;j<3;j++)

{

c[i][j]=a[i][j]+b[i][j];

}

for (i=1;i<3;i++)

for(j=1;j<3;j++)

?{

printf(“%d”,a[i][j]);

??}

}

29、從鍵盤輸入10名學(xué)生的成績數(shù)據(jù),按成績從高到低的順序排列并輸出。(提示:用數(shù)組存放成績數(shù)據(jù))

main()

{ int a[10];

?int i,j,temp;

?printf("input score:\n");

?for(i=0;i<10;i++)

scanf("%d",&a[i]);

?printf("\n");

for(i=1;i<10;i++)

for(j=0;j<9;j++)

if(a[j]<a[j+1])

{temp=a[j];

?a[j]=a[j+1];

?a[j+1]=temp;

}

for(i=0;i<10;i++)

??????printf("%d,",a[i]);

}

30. 定義一個(gè)53列的數(shù)組,從鍵盤輸入各數(shù)組元素的值,計(jì)算各數(shù)組元素之和。

#include ?<stdio.h>

main( )

{ int i, j ,a[5][3];

printf(“Enter data:\n”);

??for(i=0;i<5;i++)

????for(j=0;j<3;j++)

?????scanf(“%d”,&a[i][j]);

??for(i=0;i<5;i++)

????for(j=0;j<3;j++)

sum=sum+a[i][j];

??printf(“sum=%5d\n”,sum);

}

31、編寫程序,交換兩個(gè)數(shù)組中的對(duì)應(yīng)元素。

???#include<stdio.h>

???#define N 20

???main( )

???{ ??int a[N], b[N], i, j, temp;

???????printf(“please input a:\n”);

???????for(i=0; i<N; i++)

??????????scanf(“%d”, &a[i]);

???????printf(“please input b:\n”);

???????for(j=0; j<N; j++)

??????????scanf(“%d”, &b[i]);

???????for(i=0; i<N; i++)

???????{ ?temp=a[i];

??????????a[i]=b[i];

??????????b[i]=temp;

????????}

????????for(j=0; j<N; j++)

??????????printf(“%d,”, a[j]);

????????printf(“\n”);

????????for(j=0; j<N; j++)

??????????printf(“%d,”,b[j] );

??????}

*32、從鍵盤上輸入一個(gè)4*3的整型數(shù)組,找出數(shù)組中的最小值及其在數(shù)組中的下標(biāo)。

#include ?<stdio.h>

???main()

{ ?int a[4][3], i , j ,min,m,n;

???printf("Please enter data:");

???for (i=0; i<4; i++)

????????for (j=0; j<3; j++)

???????????scanf(“%d”,& a[i][j]);

??????min=a[0][0];

??????m=0; n=0;

???for (i=0; i<4; i++)

????????for (j=0; j<3; j++)

????????????if (a[i][j]<min)

{min= a[i][j];

?m=i;

?n=j;

}

printf("the min is %d\n, min);

printf("posion is %d ?%d \n, m,n);

}

33.編程實(shí)現(xiàn)如下功能:從鍵盤輸入一行字符,統(tǒng)計(jì)其中大寫英文字符,小寫英文字符和其他字符的個(gè)數(shù)。

#include <stdio.h>

#include <string.h>

#define ARR_SIZE 80

main()

{

char str[ARR_SIZE];

int ?len, i, letter = 0, digit = 0, space = 0, others = 0;

????printf("Please input a ?string:");

????gets(str);

????len = strlen(str); ???

for (i=0; i<len; i++)

{ ?if (str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z') ??

?????????? letter ++; ???????????????/*統(tǒng)計(jì)英文字符*/

????????else if (str[i] >= '0' && str[i] <= '9' ) ??

????????????digit ++; ?????????????????/*統(tǒng)計(jì)數(shù)字字符*/

????????else

others ++; ????????????/*統(tǒng)計(jì)其它字符的個(gè)數(shù)*/

????}

?

printf("English character: ?%d\n", letter);

??? printf("digit character: ?%d\n", digit);

printf("other character: ?%d\n", others);

}

?

*34編程實(shí)現(xiàn)如下功能

1)在主函數(shù)中,實(shí)現(xiàn)從鍵盤輸入10名學(xué)生某門課的成績,保存在一維數(shù)組中;調(diào)用排序函數(shù);對(duì)排序后的數(shù)組中的元素按從高到低打印輸出。

2)編寫排序函數(shù),使用數(shù)組名做函數(shù)參數(shù),實(shí)現(xiàn)對(duì)該成績的排序。

#include ?<stdio.h>

#define ARR_SIZE 40

void ?Sort(float score[], long num[], int n); ??

main()

{ ?float ?score[ARR_SIZE];

?? int ???n, i;

?? long ??num[ARR_SIZE];

?? printf("Please enter total number:");

?? scanf("%d", &n); ????????????????????

?? printf("Please enter the number and score:\n");

?? for (i=0; i<n; i++) ?

?? {

??? scanf("%ld%f",&num[i],&score[i]); ?????

}

?? Sort(score, num, n); ????????

?? printf("Sorted results:\n");

?? for (i=0;i<n;i++) ???????????????????

?? { printf("%ld\t%4.0f\n",num[i],score[i]);

}

}

void ?Sort(float score[], long num[], int n) ?

{ ??int ???i, j;

?? float ?temp1; ?

long ??temp2; ?????????????

?? for (i=0; i<n-1; i++) ?????????????????

?? { ? for (j=i+1; j<n; j++) ???????????????

?????? { if (score[j] > score[i]) ??????????????????

?????????? { temp1 = score[j];

?? score[j] = score[i]; ?

score[i] = temp1;

/*交換學(xué)號(hào)*/

????????? temp2 = num[j]; ??

num[j] = num[i]; ???

num[i] = temp2;

?????????? } ??

} ?

} ?

} ???????????

*35.編程實(shí)現(xiàn)如下功能

實(shí)現(xiàn)從鍵盤輸入兩個(gè)字符串,分別存入兩個(gè)不同的字符數(shù)組中;將兩個(gè)字符串連接為一個(gè)字符串,并打印輸出連接后的整個(gè)字符。

#include <stdio.h>

#include <string.h>

#define ARR_SIZE 80

void MyStrcat(char dstStr[], char srcStr[]);

main()

{ ?char ?s[ARR_SIZE], t[ARR_SIZE];

printf("Please enter source string: ");

gets(s);

printf("Please enter destination string: ");

gets(t);

MyStrcat(s,t);

printf("The concatenate string is: ");

puts(s);

}

void MyStrcat(char dstStr[], char srcStr[])

{ ??int i = 0, j;

????while (dstStr[i] != '\0') ? { ??

i++;

????}

????for (j=0; srcStr[j]!='\0'; j++, i++)

????{

???????? dstStr[i] = srcStr[j];

????}

???? dstStr[i] = '\0';

}

?

*36、猜數(shù)游戲。系統(tǒng)隨機(jī)產(chǎn)生一個(gè)整數(shù),通過鍵盤輸入數(shù)據(jù)猜數(shù),猜對(duì)為止,并要求統(tǒng)計(jì)猜的次數(shù)。

注:rand()函數(shù)可以產(chǎn)生032767間的正整數(shù),程序中需包含stdlib.h

#include ?<stdio.h>

???#include ?<stdlib.h>

main()

{

int ?magic; ????????????????

int ?guess; ?????????????????

int ?counter; ???????????????????

magic = rand() % 100 + 1; ??

counter = 0; ???????????

do{

printf("Please guess a magic number:");

scanf("%d", &guess); ??????

counter ++; ??????????????

if (guess > magic) ????

printf("Wrong!Too high!\n");

else if (guess < magic) ?

printf("Wrong!Too low!\n");

}while (guess != magic);

?

printf("Right!\n"); ??????????

printf("counter = %d\n", counter);

}

37.輸入兩個(gè)整數(shù),利用指針變量作為函數(shù)參數(shù),編程實(shí)現(xiàn)兩數(shù)互換功能,并將交換后的數(shù)據(jù)重新輸出。?

#include ?<stdio.h>

void ?Swap(int *x, int *y);

main()

{ int ?a, b;

printf("Please enter a,b:");

scanf("%d,%d", &a, &b);

printf("Before swap: a = %d,b = %d\n", a,b);

Swap(&a, &b);

printf("After swap: a = %d,b = %d\n", a, b); ?

}

void ?Swap(int *x, int *y)

{

int ?temp;

temp = *x;

*x = *y;

*y = temp; ??}

38.隨機(jī)輸入若干個(gè)學(xué)生的體重,以輸入負(fù)數(shù)或零結(jié)束,分別求最重和最輕的體重,并計(jì)算平均體重。?

????#include ?<stdio.h>

main()

?{ int n=0;

??float weight,max=0,min=10,sum=0,ave;

??printf(“please input the weight:”);

??scanf(“%f”,& weight);

?while(weight>0)

????{ sum=weight+sum;

?????n++;

?????if (weight<min)

????????min=weight;

?????else if(weight>max)

????????max=weight;

?????scanf(“%f”,& weight);}

if (n>0)

{ ave=sum/n;

printf("maxweight = %f\n " , max); ?

??????printf("minweight = %f\n", min);

printf("ave = %f\n",ave);

???????else ?printf("NO VALID DATA”);

}

39.輸入mk的值,編程求下面表達(dá)式的值:(要求編寫一個(gè)求階乘的函數(shù),調(diào)用函數(shù)實(shí)現(xiàn)本題)

#include <stdio.h>

unsigned long Factorial(unsigned int number);

main()

{ unsigned int m, k;

double p;

??printf("Please input m, k:");

??scanf("%u, %u", &m, &k);

??p = (double)Factorial(k) / Factorial (m-k);

??printf("p=%f\n", p);

}

???

unsigned long Factorial(unsigned int number)

{ unsigned long i, result = 1;

????for (i=2; i<=number; i++)

result *= i;

???return result;

}

*40. 編寫程序,其中自定義一函數(shù),用來判斷一個(gè)整數(shù)是否為素?cái)?shù),主函數(shù)輸入一個(gè)數(shù),輸出是否為素?cái)?shù)。

#include <math.h>

int IsPrimeNumber(int number)

{ int i;

if (number <= 1)

return 0;

for (i=2; i<sqrt(number); i++)

{ if ((number % i) == 0)

return 0; }

return 1;}

?main()

{ int n;

??printf(“Please input n:”);

??scanf(“%d”,&n);

??if(IsPrimeNumber(n))

???printf(“\n%d is a Prime Number”,n);

??else ?printf(“\n%d is not a Prime Number”,n);}

下載:https://download.csdn.net/download/edogawa_konan/10415376

總結(jié)

以上是生活随笔為你收集整理的40题计算机程序设计基础(C语言)编程习题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。