C primer Plus 程式設計練習(一)
阿新 • • 發佈:2019-01-04
C Primer Plus 程式設計練習(一)
第三章,問題2、5、7、8
直接上程式碼了,有一部分是從Jimmy Chen 的blog上學的。
要是有什麼問題,特別希望能賜教哦,
程式碼見下面:
#include <stdio.h> #define InchPerCen 2.54 //一英寸相當於2.54釐米 #define SecondPerYear 3.156e7 //學習命名規範:秒和年的換算 #define PintPerCup 2 #define CupPerOunce 8 #define OuncePerBigSpoon 2 #define BigSpoonPerTeaSpoon 3 //Every BigSpoon equals to three TeaSpoon void p3_2(void) { int ASCII = 0; printf("Please enter an ASCII Number:"); scanf("%d",&ASCII); //最近有的時候會忘了改變變數地址上的值從而一直使用變數初值 printf("%d\n",ASCII); //printf("%d\n",(char)ASCII); //第一遍寫的bug,下意識的一直用%d printf("%c\n",(char)ASCII); //Convert to character directly return; } void p3_5(void) //學習模組化的程式設計方法,利用在主函式外宣告函式,在主函式中呼叫 { int age = 0,SecondsNumAge = 0; printf("Please Enter your age:"); scanf("%d",&age); SecondsNumAge = age*SecondPerYear; printf("%d\n",SecondsNumAge); return; } void p3_7(void) { float HeightCen = 0,HeightInch = 0; printf("Please Enter your height in inch:"); scanf("%f",&HeightInch); HeightCen = HeightInch*InchPerCen; printf("%f\n",HeightCen); return; } void p3_8(void) { int CupNum = 0; float Pint = 0,Ounce = 0, BigSpoon = 0, TeaSpoon = 0; printf("Please Enter the Number of Cups:"); scanf("%d",&CupNum); Pint = CupNum/PintPerCup; Ounce = CupNum*CupPerOunce; BigSpoon = Ounce*OuncePerBigSpoon; TeaSpoon = BigSpoon*BigSpoonPerTeaSpoon; printf("%f %f %f %f\n",Pint,Ounce,BigSpoon,TeaSpoon ); return; } int main() { p3_2(); getchar(); return 0; }
第四章(部分)
#include <stdio.h> #include <string.h> #include <float.h> #define Gallon 3.785 #define Mile 1.609 void p4_1(void) { char first_name[40]; char last_name[40]; printf("What's your first name:"); scanf("%s", first_name); printf("What's your last name:"); scanf("%s", last_name); getchar(); printf("%*s,%*s\n", 10, first_name, 10, last_name); //修飾了*的抑制用法 return; } 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", first_name); printf("What's your last name:"); scanf("%s", last_name); getchar(); first_name_length = strlen(first_name); last_name_lenght = strlen(last_name); printf("\"%s,%s\"\n", first_name, last_name); printf("\"%20s,%20s\"\n", first_name, last_name); printf("\"%-20s,%-20s\"\n", first_name, last_name); printf("%*s,%*s\n", first_name_length+3, first_name, last_name_lenght+3, last_name); return; } 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", first_name); getchar(); printf("What's your last name:"); scanf("%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); } 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:%.6f, the value of float:%.6f\n", d_value, f_value); printf("the value of double:%.12Ld, the value of float:%.12Lf\n", d_value, f_value); //注意double要用Ld printf("the value of double:%.16Ld, the value of float:%.16Lf\n", d_value, f_value); } int main() { p4_7(); return 0; }
第五章(部分)
#include <stdio.h> const int M = 60; void c5_1(void) { float value; printf("Enter value:"); scanf("%f",&value); while (value > 0) //第一遍寫錯了,寫成了<=0 { printf("Enter value:"); scanf("%f",&value); } return; } void c5_2(void) { int n,Value_print; printf("Please enter an integer:"); scanf("%d",&n); Value_print = n; while (Value_print <= n+10) { printf("%d\n",Value_print); Value_print += 1; } return; } void c5_3(void) { int day; printf("Please enter the number of day:"); scanf("%d",&day); while (day >= 0) { int a,b; a = day/7; b = day%7; printf("%d days are %d weeks, %d days\n",day, a, b); printf("Please enter the number of day:"); //對於問題三這種的迴圈問題,再迴圈體內,將初始條件再寫一遍 scanf("%d",&day); } return; } void c5_5(void) { return; } int main() { c5_3(); return 0; }