debug_pos.h
/* ************************************************************************ * Filename: debug_pos.h * Description: Debugging error message positioning * Version: 1.0 * Created: 2014-11-14 22:55:26 * Revision: none * Compiler: gcc * Author: lsgx * Company: lsgx * ************************************************************************/ #ifndef _DEBUG_POS_H_ #define _DEBUG_POS_H_ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <time.h> #define MAX_TITLE_LEN 64 #define MAX_DEBUG_LEVEL 6 #define DEBUG_POS_LOG "debug_pos.log" #define DEBUG_UNKNOWN_TITLE "debug_unknown" #define DEBUG_TEST_TITLE "debug_test" #define DEBUG_INFO_TITLE "debug_info" #define DEBUG_WARN_TITLE "debug_warn" #define DEBUG_ERROR_TITLE "debug_error" #define DEBUG_QUITE_TITLE "debug_quite" #define DEBUG_UNKNOWN_LEVEL 5 #define DEBUG_TEST_LEVEL 4 #define DEBUG_INFO_LEVEL 3 #define DEBUG_WARN_LEVEL 2 #define DEBUG_ERROR_LEVEL 1 #define DEBUG_QUITE_LEVEL 0 #define DEBUG_LIMIT_TITLE DEBUG_WARN_TITLE #define DEBUG_LIMIT_LEVEL DEBUG_WARN_LEVEL struct DEBUG_STR { char title[MAX_TITLE_LEN]; int level; }; struct DEBUG_STR DEBUG_STR_ITEM[MAX_DEBUG_LEVEL] = { {DEBUG_QUITE_TITLE, DEBUG_QUITE_LEVEL}, {DEBUG_ERROR_TITLE, DEBUG_ERROR_LEVEL}, {DEBUG_WARN_TITLE, DEBUG_WARN_LEVEL}, {DEBUG_INFO_TITLE, DEBUG_INFO_LEVEL}, {DEBUG_TEST_TITLE, DEBUG_TEST_LEVEL}, {DEBUG_UNKNOWN_TITLE, DEBUG_UNKNOWN_LEVEL}, }; static void system_time(char * time_str) { time_t now = time((time_t *)NULL); struct tm * timenow = localtime(&now); sprintf(time_str, "%4d-%02d-%02d %02d:%02d:%02d", timenow->tm_year + 1900, timenow->tm_mon + 1, timenow->tm_mday, timenow->tm_hour, timenow->tm_min, timenow->tm_sec); } // #define DEBUG_POS_NO #ifndef DEBUG_POS_NO #define debug_pos(DEBUG_TITLE, ...) do { \ for (unsigned int i = 0; i < (sizeof(DEBUG_STR_ITEM)/sizeof(DEBUG_STR_ITEM[0])); i++) { \ if (strncmp(DEBUG_TITLE, DEBUG_STR_ITEM[i].title, MAX_TITLE_LEN) == 0) { \ FILE * debug_pos_fp = NULL; \ debug_pos_fp = fopen(DEBUG_POS_LOG, "a+"); \ char time_str[20] = {'\0'}; \ system_time(time_str); \ fprintf(debug_pos_fp, "[%s] [%s] [file_name: %s, line_no: %d, function_name: %s]\n\t>>>>", \ DEBUG_TITLE, time_str, __FILE__, __LINE__, __FUNCTION__); \ fprintf(debug_pos_fp, __VA_ARGS__); \ fputc('\n', debug_pos_fp); \ if (DEBUG_STR_ITEM[i].level < DEBUG_LIMIT_LEVEL) { \ fprintf(debug_pos_fp, "%s", "\t>>>>****** debug exit abort process ******\n"); \ fclose(debug_pos_fp); \ abort(); \ } \ fclose(debug_pos_fp); \ } \ } \ } while(0) #else #define debug_pos(DEBUG_TITLE, ...) NULL #endif // DEBUG_POS_NO #define debug_asert(condition, ...) do { \ if (condition) \ NULL; \ else \ debug_pos(DEBUG_ERROR_TITLE, __VA_ARGS__); \ } while(0) #endif // _DEBUG_POS_H_
profit.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <time.h> // #define DEBUG_POS_NO #include "debug_pos.h" int attendanees_calc(float ticket_price) { return (120 + 15 / 0.1 * (5 - ticket_price)); } float revenue_calc(float ticket_price) { int attendanees = 0; attendanees = attendanees_calc(ticket_price); debug_pos(DEBUG_INFO_TITLE, "attendanees is [%d]", attendanees); return (attendanees * ticket_price); } float cost_calc(float ticket_price) { int attendanees = 0; attendanees = attendanees_calc(ticket_price); debug_pos(DEBUG_INFO_TITLE, "attendanees is [%d]", attendanees); // return (180 + 0.4 * attendanees); return ((0.4 + 1.5) * attendanees); } float profit_calc(float ticket_price) { float revenue = 0.0; float cost = 0.0; revenue = revenue_calc(ticket_price); cost = cost_calc(ticket_price); debug_pos(DEBUG_INFO_TITLE, "revenue is [%f], cost is [%f]", revenue, cost); return (revenue - cost); } int main(int argc, char *argv[]) { char ch = '\0'; char status = '\0'; float ticket_price = 0.0; float profit = 0.0; while (status != 'N' && status != 'n') { printf("请输入票价 : "); fflush(stdout); scanf("%f", &ticket_price); while((ch = fgetc(stdin)) != EOF && ch != '\n') NULL; debug_pos(DEBUG_INFO_TITLE, "ticket_price is [%f]", ticket_price); profit = profit_calc(ticket_price); printf("profit is [%f]\n", profit); printf("是否继续[是:Y/y 否:N/n] : "); fflush(stdout); scanf("%c", &status); while((ch = fgetc(stdin)) != EOF && ch != '\n') NULL; } return 0; }
profit.scm
#lang racket/base ;; 合约 : profit : number -> number ;; 用途 : 对于给定 ticket-price, 利润是收入和成本之差 (define (profit ticket-price) (- (revenue ticket-price) (cost ticket-price))) ;; 合约 : revenue : number -> number ;; 用途 : 对于给定 ticket-price, 计算收入 (define (revenue ticket-price) (* (attendanees ticket-price) ticket-price)) ;; 合约 : cost : number -> number ;; 用途 : 对于给定 ticket-price, 计算成本 (define (cost ticket-price) ;;(+ 180 (* 0.4 (attendanees ticket-price)))) (* (+ 0.4 1.5) (attendanees ticket-price))) ;; 合约 : attendanees : number -> number ;; 用途 : 对于给定 ticket-price, 计算观众人数 (define (attendanees ticket-price) (+ 120 (* (/ 15 0.10) (- 5.00 ticket-price))))
当播放电影的成本是固定180时, 根据题意得出方程式为: y = (-150) * x^2 + 930 * x - 528
因为 y = a * x^2 + b * x + c 中 a = (-150) < 0, 开口朝下, 存在最大值。
根据对称轴公式直线 x = -b/2a = -(930 / (2 * (-150))) = 3.1, 此时最大值为 y = 913.5
当播放电影的成本是浮动时, 根据题意得出方程式为: y = (-150) * x^2 + 1155 * x - 1653
因为 y = a * x^2 + b * x + c 中 a = (-150) < 0, 开口朝下, 存在最大值。
根据对称轴公式直线 x = -b/2a = -(1155 / (2 * (-150))) = 3.85, 此时最大值为 y = 570.375
temconv.scm
#lang racket/base ;; 合约 : Celsius->Fahrenheit : number -> number ;; 用途 : 对于给定 Ctemp, 将摄氏温度转化为华氏温度 (define C2F-DIFF 32) (define C2F-SCALE (/ 9 5)) (define (C2F Ctemp) (+ (* Ctemp C2F-SCALE) C2F-DIFF)) ;; 合约 : Fahrenheit->Celsius : number -> number ;; 用途 : 对于给定 Ftemp, 将华氏温度转化为摄氏温度 (define F2C-DIFF 32) (define F2C-SCALE (/ 5 9)) (define (F2C Ftemp) (* (- Ftemp F2C-DIFF) F2C-SCALE)) ;; 合约 : FCF : number -> number ;; 用途 : 对于给定 Ftemp, 将华氏温度转化为摄氏温度再转化为华氏温度 (define (FCF Ftemp) (C2F (F2C Ftemp))) ;; 合约 : CFC : number -> number ;; 用途 : 对于给定 Ctemp, 将摄氏温度转化为华氏温度再转化为摄氏温度 (define (CFC Ctemp) (F2C (C2F Ctemp)))