// 課題14 単方向リスト ループ /* #include #include #include #define LIMIT_LOW 0 #define LIMIT_HIGH 25 // 構造体の定義 typedef struct tag { int temp; // 温度 struct tag *next; // 自分自身の型へのポインタ変数 } tempData; // 温度データ // 新データ作成関数 tempData* makeNewNode(int t) { tempData* pNewNode; //** person 型のメモリ領域確保 ** pNewNode = (tempData*)malloc(sizeof(tempData)); if (pNewNode != NULL) { //** データ設定 ** pNewNode->temp= t; pNewNode->next = NULL; } return pNewNode ; } int main(void) { int temp; // 温度入力用変数 tempData *pTop; // 温度データリストのトップ tempData *pNow; // 温度データリスト内の現在位置 tempData *pNew; // 温度データの新規データ // 必要であれば,ここに変数を追加 int c, ave; // 最初のデータは,必ず範囲内のデータであるとする scanf("%d", &temp); ave = temp; pTop = makeNewNode(temp); pNow = pTop; // 次のデータを入力 scanf("%d", &temp); c = 1; while ( (LIMIT_LOW <= temp) && (temp <= LIMIT_HIGH)) { // ここにリスト作成処理を記述 pNew = makeNewNode(temp); pNow->next = pNew; pNow = pNew; ave = ave + temp; c = c + 1; // 次のデータを入力 scanf("%d", &temp); } ave = ave / c ; // ここに平均より大きいデータだけ表示する処理 pNow = pTop; while (pNow != NULL) { if (pNow->temp > ave) { printf("%d\n", pNow->temp); } pNow = pNow->next; } return 0 ; } */ import java.util.Scanner; public class Exer14OOP { public static final int LIMIT_LOW = 0; public static final int LIMIT_HIGH = 25; public static void main(String[] args) { int temp; // 温度入力用変数 TempData14OOP pTop; // 温度データリストのトップ TempData14OOP pNew; // 温度データの新規データ // 最初のデータは,必ず範囲内のデータであるとする Scanner scanner = new Scanner(System.in); temp = scanner.nextInt(); pTop = new TempData14OOP(temp); // 次のデータを入力 temp = scanner.nextInt(); while ((LIMIT_LOW <= temp) && (temp <= LIMIT_HIGH)) { // ここにリスト作成処理を記述 pNew = new TempData14OOP(temp); pTop.insert(pNew); temp = scanner.nextInt(); } scanner.close(); int avg = pTop.getAverage(); pTop.print(avg); } } class TempData14OOP { public int temp; // 温度 public TempData14OOP next; // 次のデータ public TempData14OOP(int t) { temp = t; next = null; } public void insert(TempData14OOP data) { if (next == null) { next = data; } else { next.insert(data); } } public int getCount() { if (next == null) { return 1; } return next.getCount() + 1; } public int getSum() { if (next == null) { return temp; } return next.getSum() + temp; } public int getAverage() { return getSum() / getCount(); } public void print(int average) { if (temp > average) { System.out.printf("%d\n", temp); } if (next != null) { next.print(average); } } }