C Primer Plus (7.12) 編程練習

来源:https://www.cnblogs.com/NoldorFromMiddleEarth/archive/2023/02/03/17087833.html
-Advertisement-
Play Games

/*C Primer Plus (7.11) 3*/ 1 #include<stdio.h> 2 int main() 3 { 4 double weight,height; 5 printf("Please enter your weight and height.\n"); 6 printf(" ...


/*C Primer Plus (7.11) 3*/

 1 #include<stdio.h>
 2 int main()
 3 {
 4     double weight,height;
 5     printf("Please enter your weight and height.\n");
 6     printf("Weight (pound):");
 7     scanf("%lf",&weight);
 8     printf("Height (inch):");
 9     scanf("%lf",&height);
10 //加入建立比較友好的人機交互
11     if (weight < 100 && height > 64)
12         if (height >= 72)
13         printf("You are very tall for your weight.\n");
14     else
15         printf("You are tall for your weight.\n");
16     else if (weight > 300 && height < 48)
17         printf("You are quite short for your weight.\n");
18     else
19         printf("Your weight is ideal.\n");
20 //減少沒有用的if判斷條件
21     return 0;
22 }
23 /*
24 輸出樣例
25 
26 Please enter your weight and height.
27 Weight (pound):99
28 Height (inch):65
29 You are tall for your weight.
30 
31 Please enter your weight and height.
32 Weight (pound):98
33 Height (inch):72
34 You are very tall for your weight.
35 
36 Please enter your weight and height.
37 Weight (pound):301
38 Height (inch):46
39 You are quite short for your weight.
40 
41 Please enter your weight and height.
42 Weight (pound):200
43 Height (inch):50
44 Your weight is ideal.
45 
46 */

/*C Primer Plus (7.11) 10*/

 1 #include<stdio.h>
 2 int main()
 3 {
 4     char ch;
 5 
 6     while ((ch=getchar()) != '#')
 7     {
 8         if (ch != '\n')
 9         {
10             printf("Step 1\n");
11             if (ch == 'b')
12                 break;
13             else if (ch !='c')
14             {
15                 if (ch != 'h')
16                     printf("Step 2\n");
17                     printf("Step 3\n");
18             }
19         }
20     }
21     printf("Done.\n");
22     return 0;
23 }
24 /*
25 輸出樣例
26 
27 q
28 Step 1
29 Step 2
30 Step 3
31 c
32 Step 1
33 h
34 Step 1
35 Step 3
36 b
37 Step 1
38 Done.
39 
40 */

/*C Primer Plus (7.12) 1*/

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int space, linebreak, others;
 5     int realothers = 0;
 6     char ch;
 7     space = linebreak = others = 0;
 8 
 9     printf("Please enter some characters (# to quit).\n");
10     while ((ch = getchar()) != '#')
11     {
12         if (ch == ' ' ? space++ : others++ && ch == '\n' ? linebreak++ : others++);
13     }
14     realothers = others / 2;
15     printf("These are the number of characters required for statistics.\n");
16     printf("Space : %d" ,space);
17     printf("\nLinebreak : %d" ,linebreak);
18     printf("\nOthers: %d" ,realothers);
19 
20     return 0;
21 }
22 /*
23 輸出樣例
24 
25 Please enter some characters (# to quit).
26 Hello,My name is Coco.
27 Hello. My name is Mike !#
28 These are the number of characters required for statistics.
29 Space : 8
30 Linebreak : 1
31 Others: 38
32 
33 */

/*C Primer Plus (7.12) 2*/

 1 #include<stdio.h>
 2 int main(void)
 3 {
 4     int i = 0;
 5     char ch;
 6 
 7     printf("Please enter some characters (# to quit):");
 8     while ((ch = getchar()) != '#')
 9     {
10         if (i++ % 8 == 0)
11         {
12             putchar('\n');                     //每輸出8個字元的信息就進行一次換行操作
13         }
14         if (ch == '\n')
15         {
16             printf("\'\\n\' -> %2d ",ch);
17         }
18         else if (ch == '\t')
19         {
20             printf("\'\\t\' -> %2d ",ch);
21         }
22         else
23         {
24             printf("\'%c\' -> %2d ",ch,ch);
25         }
26     }
27     printf("\nDone.");
28 
29     return 0;
30 }
31 /*
32 輸出樣例
33 
34 Please enter some characters (# to quit):KurokiTomoko#
35 
36 'K' -> 75 'u' -> 117 'r' -> 114 'o' -> 111 'k' -> 107 'i' -> 105 'T' -> 84 'o' -> 111
37 'm' -> 109 'o' -> 111 'k' -> 107 'o' -> 111
38 Done.
39 
40 */

/*C Primer Plus (7.12) 3*/

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int num;
 5     int even,odd;                    //偶數的個數,奇數的個數
 6     int e_sum,o_sum;
 7     double e_value,o_value;          //偶數和的平均值,奇數和的平均值
 8     even = odd = num = e_sum = o_sum = 0;
 9     e_value = o_value =0.0;
10     printf("Please enter some integer numbers.\n");
11     printf("The result your entered (0 to quit) : ");
12     while (scanf("%d",&num) == 1 && num)
13     {
14         (num % 2 == 0 ? (even++, e_sum += num) : (odd++, o_sum += num));
15         printf("Now you can enter again (0 to quit) : ");
16     }
17     printf("There are %d even numbers.\n",even);
18     if (even > 0)
19     {
20         e_value = e_sum / (double)even;
21         printf("The average of even numbers is : %.3lf\n",e_value);
22     }
23     printf("There are %d odd numbers.\n",odd);
24     if (odd > 0)
25     {
26         o_value = o_sum / (double)odd;
27         printf("The average of odd numbers is : %.3lf",o_value);
28     }
29     printf("\nDone.");
30     return 0;
31 }
32 /*
33 輸出樣例
34 
35 Please enter some integer numbers.
36 The result your entered (0 to quit) : 1
37 Now you can enter again (0 to quit) : 2
38 Now you can enter again (0 to quit) : 3
39 Now you can enter again (0 to quit) : 4
40 Now you can enter again (0 to quit) : 5
41 Now you can enter again (0 to quit) : 6
42 Now you can enter again (0 to quit) : 7
43 Now you can enter again (0 to quit) : 8
44 Now you can enter again (0 to quit) : 9
45 Now you can enter again (0 to quit) : 0
46 There are 4 even numbers.
47 The average of even numbers is : 5.000
48 There are 5 odd numbers.
49 The average of odd numbers is : 5.000
50 Done.
51 
52 */

/*C Primer Plus (7.12) 4*/

 1 #include<stdio.h>
 2 int main()
 3 {
 4     char ch;
 5     int count1 = 0;
 6     int count2 = 0;
 7     printf("Please enter the text you want (enter '#' to quit).");
 8     printf("\nNow please enter : ");
 9     while ((ch = getchar()) != '#')
10     {
11         if (ch == '.')
12         {
13             putchar('!');
14             count1++;
15         }
16         else if (ch == '!')
17         {
18             printf("!!");
19             count2++;
20         }
21         else
22         {
23             putchar(ch);
24         }
25     }
26     printf("The number of times an exclamation mark "
27            "has been replaced with a period is : %d",count1);
28     printf("\nThe number of times an exclamation mark "
29            "is replaced by two exclamations is : %d",count2);
30     printf("\nDone.");
31 
32     return 0;
33 }
34 /*
35 輸出樣例
36 
37 Please enter the text you want (enter '#' to quit).
38 Now please enter : !!!!!.....
39 !!!!!!!!!!!!!!!
40 #
41 The number of times an exclamation mark has been replaced with a period is : 5
42 The number of times an exclamation mark is replaced by two exclamations is : 5
43 Done.
44 
45 */

/*C Primer Plus (7.12) 5*/

#include<stdio.h>
int main()
{
    char ch;
    int count1 = 0;
    int count2 = 0;
    printf("Please enter the text you want (enter '#' to quit).");
    printf("\nNow please enter : ");
    while ((ch = getchar()) != '#')
    {
        switch(ch)
        {
        case '.':
            {
                putchar('!');
                count1++;
                break;
            }
        case '!':
            {
                printf("!!");
                count2++;
                break;
            }
        default:
            {
                putchar(ch);
            }
        }
    }
    printf("The number of times an exclamation mark "
           "has been replaced with a period is : %d",count1);
    printf("\nThe number of times an exclamation mark "
           "is replaced by two exclamations is : %d",count2);
    printf("\nDone.");

    return 0;
}
/*
輸出樣例

Please enter the text you want (enter '#' to quit).
Now please enter : Hello, This is Coconut !
Hello, This is Coconut !!
My name is Coconut.
My name is Coconut!
#
The number of times an exclamation mark has been replaced with a period is : 1
The number of times an exclamation mark is replaced by two exclamations is : 1
Done.

*/

/*C Primer Plus (7.12) 6*/

#include<stdio.h>
int main()
{
    int count = 0;
    char ch;
    char prev;            //讀取的前一個字元
    printf("Please enter some characters ('#' to quit):");
    prev = '#';          //前一個字元為“#”的時候會停止(用於識別結束符號)
    while ((ch = getchar()) != '#')
    {
        if(prev == 'e' && ch == 'i')
            count++;
        prev = ch;
    }
    printf("There %d ei in this sentence.",count);

    return 0;
}
/*
輸出樣例

Please enter some characters ('#' to quit):Receive your eieio award.#
There 3 ei in this sentence.

*/

/*C Primer Plus (7.12) 7*/

#include<stdio.h>
#define BASIC_SALARY 10.00
#define EXTRA_WORK 1.5
#define NORMAL_TAX 0.15
#define EXTRA_TAX 0.20
#define OTHER_TAX 0.25
int main()
{
    double worktime = 0.0;
    double salary,tax,netincome;
    salary = tax = netincome = 0.0;
    printf("Please enter your "
           "working hours in a week : ");
    while (scanf("%lf",&worktime) != 1 || worktime <= 0)
    {
        while (getchar() != '\n') continue;
        printf("Please enter a right number( >= 0 ).");
    }
    salary = worktime > 40 ? (40.00 * BASIC_SALARY) + (1.5 * (worktime - 40)) * BASIC_SALARY : worktime * BASIC_SALARY;
    if (salary <= 300)
    {
        tax = 300.00 * NORMAL_TAX;
        netincome = salary - tax;
    }
    else if (salary <= 450)
    {
        tax = 300.00 * NORMAL_TAX + (salary - 300.00) * EXTRA_TAX;
        netincome = salary - tax;
    }
    else
    {
        tax = 300.00 * NORMAL_TAX + 150.00 * EXTRA_TAX + (salary - 450.00) * OTHER_TAX;
        netincome = salary - tax;
    }
    printf("There is your salary, tax and net income information.\n");
    printf("Salary : %.3lf",salary);
    printf("\nTax : %.3lf",tax);
    printf("\nNet income : %.3lf",netincome);

    return 0;
}
/*
輸出樣例

Please enter your working hours in a week : 300
There is your salary, tax and net income information.
Salary : 4300.000
Tax : 1037.500
Net income : 3262.500

Please enter your working hours in a week : 450
There is your salary, tax and net income information.
Salary : 6550.000
Tax : 1600.000
Net income : 4950.000

Please enter your working hours in a week : 521.73
There is your salary, tax and net income information.
Salary : 7625.950
Tax : 1868.988
Net income : 5756.963
*/

/*C Primer Plus (7.12) 8*/

#include<stdio.h>
#include<stdbool.h>
#define EXTRA_WORK 1.5
#define NORMAL_TAX 0.15
#define EXTRA_TAX 0.20
#define OTHER_TAX 0.25
void quit ();
void menu ();
void Salary (double Bsalary , double worktime);

int choice = 0;
double worktime = 0.0;

int main()
{
    while (true)
    {
         menu ();

         switch(choice)
     {
        case 1 :
           {
               Salary(8.75,worktime);
               break;
           }
        case 2 :
           {
               Salary(9.33,worktime);
               break;
           }
        case 3 :
           {
               Salary(10.00,worktime);
               break;
           }
        case 4 :
           {
               Salary(11.20,worktime);
               break;
           }
        case 5 :
           {
               quit();
               printf("Done.");
               return 0;
           }
     }
    }
}

void quit()
{
    printf("\t\t\n************************************************\t\t\n");
    printf("||                                            ||");
    printf("\n||                                            ||");
    printf("\n||      Thank you to use this programme!      ||");
    printf("\n||                                            ||");
    printf("\n||                                            ||");
    printf("\t\t\n************************************************\t\t\n");
}

void menu()
{
    printf("\t\t\n*****************************************************************\t\t\n");
    printf("Enter the number corresponding to the desired pay rate or action:\n");
    printf("1) $8.75/hr                          2) $9.33/hr");
    printf("\n3) $10.00/hr                         4) $11.20/hr\n");
    printf("5) quit");
    printf("\t\t\n*****************************************************************\t\t\n");
    printf("Please enter your options: ");
    scanf("%d",&choice);
    while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5)
    {
        printf("Please enter the right choice:");
        scanf("%d",&choice);
    }
}

void Salary(double Bsalary , double worktime)
{
  double tax,netincome,salary;
  salary = tax = netincome = 0.0;
  printf("Please enter your working hours in a week : ");

    while (scanf("%lf",&worktime) != 1 || worktime <= 0)
    {
        while (getchar() != '\n') continue;
        printf("Please enter a right number( >= 0 ).	   

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1:apk文件結構 如圖所示: assets: 存放應用程式的靜態資源文件,如圖片資源,json配置文件,html離線資源等。註意,assets目錄下是支持任意深度的子目錄。 res: 規定的指定文件,圖標,圖片資源等,且res下文件都會生成對應的資源id, 但是assets下是不會的。 lib: ...
  • 在元素設置浮動(float)後,該元素就會脫離文檔流,並且向左或向右浮動,直至它的外邊緣遇到包含框或者另一個浮動框的邊緣。 一、浮動元素對佈局的影響 1.1、浮動元素造成父元素的高度塌陷: 原來的父元素高度是內部元素撐開的,但是當內部元素浮動後,脫離文檔流浮動起來,那父元素的高度就坍塌,變為高度 0 ...
  • 寫代碼的時候遇到這個問題了,在這裡複習一下 非箭頭函數 非箭頭函數的this指向比較好理解,就是調用這個函數的對象,舉個慄子: var obj = { foo: { bar: 3, foo:{ bar: 4, foo: function a() { console.log(this.bar) }, ...
  • 這裡給大家分享我在網上總結出來的一些知識,希望對大家有所幫助 什麼是跨域? 跨域不是問題,是一種安全機制。瀏覽器有一種策略名為同源策略,同源策略規定了部分請求不能被瀏覽器所接受。 值得一提的是:同源策略導致的跨域是瀏覽器單方面拒絕響應數據,伺服器端是處理完畢並做出了響應的。 什麼是同源策略 一個ur ...
  • 1.CSS、SCSS、Sass CSS是開發人員熟知的一種用於頁面樣式開發的語言,可以通過內容的分離控制減少代碼的重覆性,降低代碼的複雜程度。 Sass與 SCSS 都是 CSS 預處理器,可包含在基於 CSS 的 UI(用戶界面)或前端框架中以簡化開發。Sass 與 SCSS 框架在高級別的 CS ...
  • 通常,不同的公司里有著不同的編碼規範,主要是從代碼得準確性、穩定性、可讀性等地方著手制定,以提高團隊成員之間的協作效率,這裡主要是列出一些常見的編碼規範。 ...
  • 隨著移動互聯網發展,手機端購物已成為人們生活的常態。人們在搜索商品時採用的手段也越來越豐富,當前的主要搜索方式是文本搜索與拍照搜索。 ...
  • 1 簡介 之前在文章《dapr入門與本地托管模式嘗試》中介紹了dapr和本地托管,本文我們來介紹如果在代碼中使用dapr的服務調用功能,並把它整合到Spring Boot中。 Dapr服務調用的邏輯如下: 本次實驗會創建兩個服務: pkslow-data,提供數據服務,用於返回數據; pkslow- ...
一周排行
    -Advertisement-
    Play Games
  • 1、預覽地址:http://139.155.137.144:9012 2、qq群:801913255 一、前言 隨著網路的發展,企業對於信息系統數據的保密工作愈發重視,不同身份、角色對於數據的訪問許可權都應該大相徑庭。 列如 1、不同登錄人員對一個數據列表的可見度是不一樣的,如數據列、數據行、數據按鈕 ...
  • 前言 上一篇文章寫瞭如何使用RabbitMQ做個簡單的發送郵件項目,然後評論也是比較多,也是準備去學習一下如何確保RabbitMQ的消息可靠性,但是由於時間原因,先來說說設計模式中的簡單工廠模式吧! 在瞭解簡單工廠模式之前,我們要知道C#是一款面向對象的高級程式語言。它有3大特性,封裝、繼承、多態。 ...
  • Nodify學習 一:介紹與使用 - 可樂_加冰 - 博客園 (cnblogs.com) Nodify學習 二:添加節點 - 可樂_加冰 - 博客園 (cnblogs.com) 介紹 Nodify是一個WPF基於節點的編輯器控制項,其中包含一系列節點、連接和連接器組件,旨在簡化構建基於節點的工具的過程 ...
  • 創建一個webapi項目做測試使用。 創建新控制器,搭建一個基礎框架,包括獲取當天日期、wiki的請求地址等 創建一個Http請求幫助類以及方法,用於獲取指定URL的信息 使用http請求訪問指定url,先運行一下,看看返回的內容。內容如圖右邊所示,實際上是一個Json數據。我們主要解析 大事記 部 ...
  • 最近在不少自媒體上看到有關.NET與C#的資訊與評價,感覺大家對.NET與C#還是不太瞭解,尤其是對2016年6月發佈的跨平臺.NET Core 1.0,更是知之甚少。在考慮一番之後,還是決定寫點東西總結一下,也回顧一下.NET的發展歷史。 首先,你沒看錯,.NET是跨平臺的,可以在Windows、 ...
  • Nodify學習 一:介紹與使用 - 可樂_加冰 - 博客園 (cnblogs.com) Nodify學習 二:添加節點 - 可樂_加冰 - 博客園 (cnblogs.com) 添加節點(nodes) 通過上一篇我們已經創建好了編輯器實例現在我們為編輯器添加一個節點 添加model和viewmode ...
  • 前言 資料庫併發,數據審計和軟刪除一直是數據持久化方面的經典問題。早些時候,這些工作需要手寫複雜的SQL或者通過存儲過程和觸發器實現。手寫複雜SQL對軟體可維護性構成了相當大的挑戰,隨著SQL字數的變多,用到的嵌套和複雜語法增加,可讀性和可維護性的難度是幾何級暴漲。因此如何在實現功能的同時控制這些S ...
  • 類型檢查和轉換:當你需要檢查對象是否為特定類型,並且希望在同一時間內將其轉換為那個類型時,模式匹配提供了一種更簡潔的方式來完成這一任務,避免了使用傳統的as和is操作符後還需要進行額外的null檢查。 複雜條件邏輯:在處理複雜的條件邏輯時,特別是涉及到多個條件和類型的情況下,使用模式匹配可以使代碼更 ...
  • 在日常開發中,我們經常需要和文件打交道,特別是桌面開發,有時候就會需要載入大批量的文件,而且可能還會存在部分文件缺失的情況,那麼如何才能快速的判斷文件是否存在呢?如果處理不當的,且文件數量比較多的時候,可能會造成卡頓等情況,進而影響程式的使用體驗。今天就以一個簡單的小例子,簡述兩種不同的判斷文件是否... ...
  • 前言 資料庫併發,數據審計和軟刪除一直是數據持久化方面的經典問題。早些時候,這些工作需要手寫複雜的SQL或者通過存儲過程和觸發器實現。手寫複雜SQL對軟體可維護性構成了相當大的挑戰,隨著SQL字數的變多,用到的嵌套和複雜語法增加,可讀性和可維護性的難度是幾何級暴漲。因此如何在實現功能的同時控制這些S ...