polulation.c

来自「You will write a program for calculating」· C语言 代码 · 共 35 行

C
35
字号
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846 

int main(void){
	FILE *fp;
	char *filename = "population.txt";
	int month;
	float temperature[48], food[48];
	unsigned int population[48];
	population[0] = 0;
	for (month =0; month <48; month++){
		temperature[month] = 15.0 * sin(2.0 * PI * (month - 3) / 12.0) + 5.0;
		if (temperature[month]<0){
			food[month] =0;
			population[month+1]= population[month]* power(0.99, (temperature[month]+10)); 
		}
		else {
			food[month] = 1000.0 * sqrt(temperature[month]);
			population[month+1] = (population[month]*2)*power(0.99, (temperature[month]+10)); 
		}

	fp = fopen(filename, "w");
	if(fp == NULL){
		fprintf(stderr, "Error: Cannot open file!\n"); 
		return EXIT_FAILURE;
	}else{
		for (month =0; month <48; month++){
			fprintf (fp , "%d\t%.2lf\t%.0lf\t%u\n" , month , temperature[month] , food[month] , population[month]) ;
			fclose(fp) ;
	}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?