namereq

温度を構造体で管理

Jun 5th, 2018
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define LIMIT_LOW 0
  6. #define LIMIT_HIGH 25
  7.  
  8. /* 構造体の定義 */
  9. typedef struct tag {
  10.     int temp; /* 温度 */
  11.     struct tag *next; /* 1つ後のデータへのポインタ変数 */
  12. } tempData; /* 温度データ */
  13.  
  14.  
  15. /* 新データ作成関数 */
  16. tempData* makeNewNode(int t) {
  17.     tempData* pNewNode;
  18.     /*** person 型のメモリ領域確保 ***/
  19.     pNewNode = (tempData*)malloc(sizeof(tempData));
  20.     if (pNewNode != NULL) {
  21.         /*** データ設定 ***/
  22.         pNewNode->temp= t;
  23.         pNewNode->next = NULL;
  24.     }
  25.     return pNewNode ;
  26. }
  27.  
  28. /* リストから,引数で指定された温度より低いデータを削除する関数 */
  29. void removeFromList(tempData **ppTop, int argTemp) {
  30.     /* 必要な変数を追加する */
  31.     tempData *pNow; /* 温度データリスト内の現在位置 */
  32.     tempData *pPrev = NULL; /* 温度データリスト内の現在位置の1つ手前 */
  33.     /* リストから,引数で指定された温度より低いデータを削除する */
  34.     pNow = *ppTop;
  35.     while (pNow != NULL) {
  36.         if(pNow->temp < argTemp) {
  37.             pPrev->next = pNow->next;
  38.         } else {
  39.             pPrev = pNow;
  40.         }
  41.         pNow = pNow->next;
  42.     }
  43. }
  44.  
  45. int main(void) {
  46.     int temp; /* 温度入力用変数 */
  47.     tempData *pTop; /* 温度データリストの先頭 */
  48.     tempData *pNow; /* 温度データリスト内の現在位置 */
  49.     tempData *pNew; /* 温度データの新規データ */
  50.     int c, ave; /* 平均計算用 */
  51.  
  52.     /* 最初のデータは,必ず範囲内のデータであるとする */
  53.     scanf("%d", &temp);
  54.     pTop = makeNewNode(temp);
  55.     pNow = pTop;
  56.     /* 次のデータを入力 */
  57.     scanf("%d", &temp);
  58.     ave = temp;
  59.  
  60.     c = 1;
  61.     while ((LIMIT_LOW <= temp) && (temp <= LIMIT_HIGH)) {
  62.         /* リスト末尾に追加 */
  63.         pNew = makeNewNode(temp);
  64.         pNow->next = pNew;
  65.         pNow = pNew;
  66.         /* 集計 */
  67.         ave = ave + temp;
  68.         c = c + 1;
  69.         /* 次のデータを入力 */
  70.         scanf("%d", &temp);
  71.     }
  72.     /* 平均値を計算 */
  73.     ave = ave / c;
  74.  
  75.     /* 平均より小さいデータを削除 */
  76.     removeFromList(&pTop, ave);
  77.  
  78.     /* データを表示する処理 */
  79.     /* 出力 */
  80.     pNow = pTop ;
  81.     while (pNow != NULL) {
  82.         printf("%d\n", pNow->temp);
  83.         pNow = pNow->next;
  84.     }
  85.  
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment