章节号终于正常了
编程环境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 |
#include <stdio.h> #include <float.h> /************************************************************************/ /* practice 1 */ /************************************************************************/ void p4_1(void) { char first_name[40]; char last_name[40]; printf("What's your first name:"); scanf_s("%s", first_name); printf("What's your last name:"); scanf_s("%s", last_name); getchar(); printf("%*s,%*s\n", 10, first_name, 10, last_name); return; } /************************************************************************/ /* practice 2 */ /************************************************************************/ void p4_2(void) { char first_name[40]; char last_name[40]; int first_name_length = 0; int last_name_lenght = 0; printf("What's your first name:"); scanf_s("%s", first_name); printf("What's your last name:"); scanf_s("%s", last_name); getchar(); first_name_length = strlen(first_name); last_name_lenght = strlen(last_name); printf("a.\"%s,%s\"\n", first_name, last_name); printf("b.\"%20s,%20s\"\n", first_name, last_name); printf("c.\"%-20s,%-20s\"\n", first_name, last_name); printf("d.%*s,%*s\n", first_name_length+3, first_name, last_name_lenght+3, last_name); return; } /************************************************************************/ /* practice 3 */ /************************************************************************/ void p4_3(void) { double input = 0.0; printf("please input the float number:"); scanf_s("%lf", &input); getchar(); printf("%lf\n", input); printf("%e\n", input); } /************************************************************************/ /* practice 4 */ /************************************************************************/ #define CM_PER_INCH (2.54) void p4_4(void) { float height_inch = 0; float height_cm = 0; printf("What's your height:"); scanf_s("%f", &height_inch); getchar(); printf("Dabney, you are %f feet tall\n", height_inch); height_cm = height_inch * CM_PER_INCH; printf("Dabney, you are %f cm tall\n", height_cm); } /************************************************************************/ /* practice 5 */ /************************************************************************/ void p4_5(void) { float network_speed = 0; float file_size = 0; float download_time = 0; printf("How much your network speed:"); scanf_s("%f", &network_speed); getchar(); printf("How much is your download file:"); scanf_s("%f", &file_size); getchar(); download_time = file_size / (network_speed / 8); printf("At %f megabits per secnod, a file of %f magebytes\n download in %f seconds.\n", network_speed, file_size, download_time); } /************************************************************************/ /* practice 6 */ /************************************************************************/ void p4_6(void) { char first_name[40] = { 0 }; char last_name[40] = { 0 }; int first_name_length = 0; int last_name_length = 0; printf("What's your first name:"); scanf_s("%s", first_name); getchar(); printf("What's your last name:"); scanf_s("%s", last_name); getchar(); first_name_length = strlen(first_name); last_name_length = strlen(last_name); printf("%s %s\n", first_name, last_name); printf("%*d %*d\n", first_name_length, first_name_length, last_name_length, last_name_length); printf("%s %s\n", first_name, last_name); printf("%-*d %-*d\n", first_name_length, first_name_length, last_name_length, last_name_length); } /************************************************************************/ /* practice 7 */ /************************************************************************/ void p4_7(void) { double d_value = 1.0 / 3.0; float f_value = 1.0 / 3.0; printf("the value of FLT_DIG:%d, the value of DBL_DIG:%d\n", FLT_DIG, DBL_DIG); printf("the value of double:%.6lf, the value of float:%.6lf\n", d_value, f_value); printf("the value of double:%.12lf, the value of float:%.12lf\n", d_value, f_value); printf("the value of double:%.18lf, the value of float:%.18lf\n", d_value, f_value); } /************************************************************************/ /* practice 8 */ /************************************************************************/ #define KM_PER_MILE (1.609) #define PINT_PER_GALLON (3.785) void p4_8(void) { float driven_distance = 0.0; float gas_consumption = 0.0; float pint_per_hundred_km = 0.0; float mile_per_gallon = 0.0; printf("How much distance have you traveled in kilometer:"); scanf_s("%f", &driven_distance); getchar(); printf("How much gas have you used in pint:"); scanf_s("%f", &gas_consumption); getchar(); pint_per_hundred_km = gas_consumption / driven_distance * 100; mile_per_gallon = (driven_distance / KM_PER_MILE) / (gas_consumption / PINT_PER_GALLON); printf("Fuel consumptions:%f pint/100km or %f mile/gallon\n", pint_per_hundred_km, mile_per_gallon); } int main(int argc, char **argv) { p4_8(); getchar(); } |
此文为博主原创文章,转载请注明出处
答案一会导致内存溢出然后卡死。。去了*警告会消除。
嗯 运行环境Xcode
谢谢告知,*是打算拿来做占位符的,但是传的参数没传完整,因为书不在身边,所以先用10来代替了,改成
printf("%*s,%*s\n", 10, first_name, 10, last_name);
这样就可以了为什么我用C-free编写第一个程序显示scanf_s("%s", first_name);有错误啊?
没用过C-free,你可以试试将scanf_s改成sacnf,看是否可以
好的,我回去试试!像我这样的新手学完 Cprimer plus应该看什么书!
你这些程序都是用什么IDE写的啊?
为啥第二题调用strlen函数不用string.h程序头?*直接用strlen函数可以少定义两个变量。