编程环境Visual Studio 2017
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
#include <stdio.h> /************************************************************************/ /* practice 1 */ /************************************************************************/ #define SECONDS_PER_MINUTE (60) void p5_1(void) { float hours = 0; int minutes = 0; int seconds = 0; printf("please input the number of minutes(<= 0 to quit):"); scanf_s("%d", &minutes); while (minutes > 0) { getchar(); hours = (float)minutes / SECONDS_PER_MINUTE; seconds = minutes * SECONDS_PER_MINUTE; printf("%d minute = %f hours or %d seconds\n", minutes, hours, seconds); printf("please input the number of minutes(<= 0 to quit):"); scanf_s("%d", &minutes); } return ; } /************************************************************************/ /* practice 2 */ /************************************************************************/ void p5_2(void) { int input_num = 0; printf("please enter an integer:"); scanf_s("%d", &input_num); getchar(); printf("the ten integers after %d are:", input_num); for (int i = 0; i <= 10; i++) { printf("%d ", (input_num + i)); } printf("\n"); return; } /************************************************************************/ /* practice 3 */ /************************************************************************/ const int DAY_PER_WEEK = 7; void p5_3(void) { int days = 0; int weeks = 0; int remain_day = 0; printf("please input the number of day:"); scanf_s("%d", &days); getchar(); weeks = days / DAY_PER_WEEK; remain_day = days % DAY_PER_WEEK; printf("%d days are %d weeks, %d days\n", days, weeks, remain_day); return ; } /************************************************************************/ /* practice 4 */ /************************************************************************/ const float CM_PER_INCH = 2.54f; const float CM_PER_FEET = 30.38f; void p5_4(void) { float height_cm = 0; int height_feet = 0; float height_inch = 0.0; printf("Enter a height in centimeters: "); scanf_s("%f", &height_cm); while (height_cm > 0) { getchar(); height_feet = (int)(height_cm / CM_PER_FEET); height_inch = (height_cm - CM_PER_FEET * height_feet) / CM_PER_INCH; printf("%.1f cm = %d feet, %.1f inches\n", height_cm, height_feet, height_inch); printf("Enter a height in centimeters(<=0 to quit): "); scanf_s("%f", &height_cm); } return ; } /************************************************************************/ /* practice 5 */ /************************************************************************/ void p5_5(void) { int count, sum; count = 0; sum = 0; printf("please enter work days:"); scanf_s("%d", &count); getchar(); do { sum += count; } while (count-- > 0); printf("sum = %d\n", sum); return; } /************************************************************************/ /* practice 6 */ /************************************************************************/ #define SQUARE(X) ((X) * (X)) void p5_6(void) { int count, sum; count = 0; sum = 0; printf("please enter work days:"); scanf_s("%d", &count); getchar(); do { sum += SQUARE(count); } while (count-- >0); printf("sum = %d\n", sum); return; } /************************************************************************/ /* practice 7 */ /************************************************************************/ void cube(double input) { double cube_num = input * input * input; printf("the cube of %lf is %lf\n", input, cube_num); return; } void p5_7(void) { double input = 0.0; printf("please enter a double number:"); scanf_s("%lf", &input); getchar(); cube(input); return; } /************************************************************************/ /* practice 8 */ /************************************************************************/ void p5_8(void) { int first_operand = 0; int second_operand = 0; printf("This program computes moduli.\n"); printf("Enter an integer to serve as the second operand:"); scanf_s("%d", &second_operand); getchar(); printf("Now enter the first operand:"); scanf_s("%d", &first_operand); while (first_operand > 0) { getchar(); printf("%d %% %d is %d\n", first_operand, second_operand, (first_operand % second_operand)); printf("Enter next number for first operand (<=0 to quit):"); scanf_s("%d", &first_operand); } printf("Done\n"); return; } /************************************************************************/ /* practice 9 */ /************************************************************************/ void Temperatures(const double temperature) { const double Fahrenheit_val = 32.0; const double Kelvin_val = 273.16; const double Fahrenheit_temp = 5.0 / 9.0 * (temperature - Fahrenheit_val); const double Kelvin_temp = temperature + Kelvin_val; printf("Celsius: %.2lf Fahrenheit:%.2lf Kelvin:%.2lf\n", temperature, Fahrenheit_temp, Kelvin_temp); return; } void p5_9(void) { double temperature = 0.0; printf("please enter the temperature (q to quit):"); while (scanf_s("%lf", &temperature) == 1) { getchar(); Temperatures(temperature); printf("please enter the temperature (q to quit):"); } return; } int main(int argc, char **argv) { p5_9(); getchar(); return 1; } |
此文为博主原创文章,转载请注明出处
emmmm 第三题有要求用循环重复输入 输入非正值的时候循环结束这样
呃......,还是那句话书不在身边,到时候拿书看一下,可能写的时候忘记看题的要求了
很厉害了!!!顶!
谢谢,大家一起努力
兄弟,第九题我还是没看懂
要是有注释就好了
难理解的地方才加有注释,简单点的按照练习意思理解就好
第四题计算height_feet,用到了强制转换int,但是我去掉了这个,结果输出是一样的,怎么回事啊
因为上面的代码里面我将height_feet定义为int,所以就算你将强制类型转换int去掉,当将最后计算出来的结果赋值给height_feet的时候还是会被截断为int的
210行到213行:题目是输入华氏度,输出华氏度、摄氏度、开氏度。
输出的字符串名字错了,Fahrenheit_temp改成Celsius_temp比较好吧,毕竟定义了temperature就代表了华氏度
我的妈呀最后一题怎样都看不懂有点崩溃
do while 还没学呢。。。。
Hi 楼主,我感觉第9题函数里的公式好像写错了
书上说的是:
摄氏温度=5.0/9.0*(华氏温度-32.0)
开氏温度=摄氏温度+273.16
还有就是变量命名有点混淆
Fahrenheit是华氏温度,Celsius是摄氏温度
麻烦楼主有时间看一下~
编译器打不出来啊