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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C Primer Plus 第5章 运算符、表达式和语句 编程练习及答案

發(fā)布時間:2025/5/22 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C Primer Plus 第5章 运算符、表达式和语句 编程练习及答案 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標準>>>

1、編寫一個程序,將用分鐘表示的時間轉換成以小時和分鐘表示的時間。使用#define或const來創(chuàng)建一個代表60的符號常量。使用while循環(huán)來允許用戶重復鍵入值,并且當鍵入一個小于等于0的時間終止循環(huán)。

#include #define HOUR 60int main() {int minutes;printf("Please enter the minutes:");scanf("%d",&minutes);while(minutes>0){printf("%d hour,%d minutes.\n",minutes/HOUR,minutes%HOUR);printf("Please enter the minutes again:");scanf("%d",&minutes);}return 0; }

2、編寫一個程序,此程序要求輸入一個整數(shù),然后打印從輸入(包含)到比輸入大10(包含)的所有整數(shù)值(也就說,如果輸入是5,那么輸出就從5到15)。要求在各個輸出之間用空格、制表符或換行符分開。

#include int main(void) {int num,i;i=0;printf("Please input the number.\n");scanf("%d",&num);while(i++<11){printf("%d ",num++); }return 0; }

3、編寫一個程序,該程序要求用戶輸入天數(shù),然后將該值轉換為周數(shù)和天數(shù)。例如,該程序將把18天轉換為2周4天。用下面的格式顯示結果:

18 days are 2 weeks,4 days.

#include #define DAYS_W 7int main(void) {int days;printf("Please input the days:\n");scanf("%d",&days);while(days>0){printf("%d days are %d weeks,%d days.\n",days,days/DAYS_W,days%DAYS_W);printf("Please input the days:\n");scanf("%d",&days);}return 0; }

4、編寫一個程序,讓用戶按厘米輸入高度值,然后程序按厘米和英尺英寸顯示這個高度值。允許厘米和英尺英寸的值出現(xiàn)小數(shù)部分。程序允許用戶輸入,直到用戶輸入一個非正的數(shù)值。程序運行示例如下:

Enter a height in centemeters:182

182.0 cm = 5 feet,11.7 inches

Enter a height in centimeters? (<=0 to quit): 168

168.0 cm = 5 feet ,6.1 inches

Enter a height in centimeters? (<=0 to quit): 0

bye

#include #define INCH 2.54 //1 INCH = 2.54 CMint main(void) {float cm;printf("Enter a height in centimeters:");scanf("%f",&cm);while (cm>0){printf("%.1f cm = %d feet,%.1f inches\n",cm,int(cm/INCH/12),cm/INCH-int(cm/INCH/12)*12);printf("Enter a height in centimeters(<=0 to quit):");scanf("%f",&cm);}printf("bye\n");return 0; }

5、改寫用來找到前20個整數(shù)之后的程序addemup.c(程序清單5.13)(如果你愿意,可以把addemup.c程序看作是一個計算如果您第一天得到$1,第二天得到$2,以此類推,您在20天內(nèi)會賺多少錢的程序)。修改該程序,目的是您能交互地告訴程序,計算將進行到哪里。也就是說,有一個讀入的變量來代替20.

#include int main(void) {int count,sam,max;count=0;sam=0;printf("Input the max:\n");scanf("%d",&max);while(count++

6、現(xiàn)在修改編程練習5中的程序,使它能夠計算整數(shù)平方的和。C沒有平方函數(shù),但是您可以利用n的平方是n*n的事實。

#include int main(void) {int count,sam,max;count=0;sam=0;printf("Input the max:\n");scanf("%d",&max);while(count++

?7、編寫一個程序,該程序要求輸入一個float型值,并打印該值的立方數(shù)。使用您自己設計的函數(shù)來計算該值的立方數(shù)并將其立方數(shù)打印出來。main()函數(shù)把輸入的值傳遞給該函數(shù)。

#include<stdio.h> float cube(float n); int main(void) {float num;printf("Input a float:\n");scanf("%f",&num);printf("The cube of %f is %f.\n ",num,cube(num));return 0; } float cube(float n) {return(n*n*n); }

8、編寫一個程序,該程序要求用戶輸入一個華氏溫度。程序以double類型讀取溫度值,并將它作為一個參數(shù)傳遞給用戶提供的函數(shù)Temperatures()。該函數(shù)將計算相應的攝氏溫度和絕對溫度,并以小數(shù)點右邊有兩位數(shù)字的精度顯示這三種溫度。它應該用每個值所代表的溫度刻度來標識這3個值。下面是將華氏溫度轉換成攝氏溫度的方程:

Celsius = 1.8*Fahrenheit + 32.0;

通常用在科學上的絕對溫度的刻度是0代表絕對零,是可能溫度的下界。下面是將攝氏溫度轉換為絕對溫度的方程:

kelvin = Celsius + 273.16

Temperatures()函數(shù)使用const來創(chuàng)建代表該轉換里的3個常量的符號。main()函數(shù)將使用一個循環(huán)來允許用戶重復的輸入溫度,當用戶輸入Q或其他非數(shù)字值時,循環(huán)結束。

#include<stdio.h> void Temperatures(double);int main(void) {double fahrenheit;printf("Please input a fahrenheit:\n");while(scanf("%lf",&fahrenheit)==1){Temperatures(fahrenheit);printf("Please input a fahrenheit:\n");}printf("End.\n");return 0; } void Temperatures(double num) {const double a=1.8,b=32.0,c=273.16;printf("Fahrenheit = %lf\t",num);printf("Celsius = %lf\t",a * num + b);printf("Kelvin = %lf\n",a * num + b + c);}

?

轉載于:https://my.oschina.net/idreamo/blog/680766

總結

以上是生活随笔為你收集整理的C Primer Plus 第5章 运算符、表达式和语句 编程练习及答案的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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