C语言编程学习day4

1、股票价格涨跌趋势,常用蜡烛图技术中的K线图来表示,分为按日的日K线、按周的周K线、按月的月K线等。以日K线为例,每天股票价格从开盘到收盘走完一天,对应一根蜡烛小图,要表示四个价格:开盘价格Open(早上刚刚开始开盘买卖成交的第1笔价格)、收盘价格Close(下午收盘时最后一笔成交的价格)、中间的最高价High和最低价Low。

如果Close<Open,表示为“BW-Solid”(即“实心蓝白蜡烛”);如果Close>Open,表示为“R-Hollow”(即“空心红蜡烛”);如果Open等于Close,则为“R-Cross”(即“十字红蜡烛”)。如果Low比Open和Close低,称为“Lower Shadow”(即“有下影线”),如果High比Open和Close高,称为“Upper Shadow”(即“有上影线”)。请编程序,根据给定的四个价格组合,判断当日的蜡烛是一根什么样的蜡烛。

#include <stdio.h>

int main(){
    
    double open, high, low, close;
    scanf("%lf%lf%lf%lf", &open, &high, &low, &close);
    if(close < open){
        if(low < close && low < open && high > close && high > open){
            printf("BW-Solid with Lower Shadow and Upper Shadow");
        }else if(low < close && low < open){
            printf("BW-Solid with Lower Shadow");
        }else if(high > close && high > open){
            printf("BW-Solid with Upper Shadow");
        }else{
            printf("BW-Solid");
        }
    }else if(close == open){
        if(low < close && low < open && high > close && high > open){
            printf("R-Cross with Lower Shadow and Upper Shadow");
        }else if(low < close && low < open){
            printf("R-Cross with Lower Shadow");
        }else if(high > close && high > open){
            printf("R-Cross with Upper Shadow");
        }else{
            printf("R-Cross");
        }
    }else if(close > open){
        if(low < close && low < open && high > close && high > open){
            printf("R-Hollow with Lower Shadow and Upper Shadow");
        }else if(low < close && low < open){
            printf("R-Hollow with Lower Shadow");
            //return 0;
        }else if(high > close && high > open){
            printf("R-Hollow with Upper Shadow");
        }else{
            printf("R-Hollow");
        }
    }
        
    return 0;
}

**改进版:**内层条件判断有相同的公共部分,改进后封装成函数,会更好。需要注意的是,封装的函数需要传一个字符串,而C语言没有String类型,所以只能用字符数组(char*string),并且需要引入头文件#include <string.h>

#include <stdio.h>
#include <string.h>

//相同部分的封装
void Shadow(double open, double high, double low, double close, char* state){
     if(low < close && low < open && high > close && high > open){
            printf("%s with Lower Shadow and Upper Shadow", state);
        }else if(low < close && low < open){
            printf("%s with Lower Shadow", state);
        }else if(high > close && high > open){
            printf("%s with Upper Shadow", state);
        }else{
            printf("%s", state);
        }
}

int main(){
    
    double open, high, low, close;
    scanf("%lf%lf%lf%lf", &open, &high, &low, &close);
    if(close < open){
        Shadow(open, high, low, close, "BW-Solid");
    }else if(close == open){
        Shadow(open, high, low, close, "R-Cross");
    }else if(close > open){
        Shadow(open, high, low, close, "R-Hollow");
    }
        
    return 0;
}

2、给定两个整数A和B,输出从A到B的所有整数以及这些数的和。

#include <stdio.h>

int main(){
    
    int start, end;
    scanf("%d%d", &start, &end);
    int count = 0;
    int sum = 0;
    
    for(int i= start; i<=end; i++){
        if(count == 5){
            //printf("    ", i);
            printf("\n"); //每5个数换行
            count = 0;
        }
        printf("%5d", i); //右对齐
        count++;
        sum +=i;
    }
    printf("\n");
    printf("Sum = %d\n", sum);
}

运行结果:
在这里插入图片描述
3、根据下面关系式,求圆周率的值,直到最后一项的值小于给定阈值。
在这里插入图片描述
在这里插入图片描述
我的代码:在dev c++上执行,没问题,能得到3.132157,但在PTA上不能通过。

#include <stdio.h>

int main(){
    
    double n;
    double result = 0;
    double molecular;
    double denominator;
    scanf("%lf", &n);
    
    for(int i=0; ; i++){
    	molecular = 1;
    	denominator = 1;
        for(int j=0; j<=i; j++){
            if(j>0){
                molecular *=(j*1.0); 
                denominator *=(2.0*j +1);
            }
        }
        result +=molecular / denominator;
        if(molecular / denominator < 0.01){
        	break;
        }
    }
    printf("%f", 2*result);
    return 0;
}

运行结果:
在这里插入图片描述
网上找了一篇能通过的代码,参考他的写法,并不需要2层for循环,只需一个while循环即可,代码简介。每一步计算的结果通过调试与我写的都一样,结果也一样,可就是他的能通过,我的就显示是计算出错,不能通过。就难以理解
能过PTA的代码

#include <stdio.h>

int main(){
	
	int i =1;
	int j = 3;
	double accuracy, result = 1;
	scanf("%lf", &accuracy);
	double molecular = 1;
	double denominator = 1;
	double item = 1;
	
	while(item > accuracy){
		molecular *= i; //做分子的阶乘
		denominator *= j;//分母做(2*n+1)
		item = molecular / denominator;
		result += item;
		i++;
		j += 2; 
	}
	printf("%f\n", 2*result);
	
	return 0;
}

4、程序每次读入一个正3位数,然后输出按位逆序的数字。注意:当输入的数字含有结尾的0时,输出不应带有前导的0。比如输入700,输出应该是7。
**思路:**该题要注意两种特殊情况,700 逆序后是7,也就是前两个0要舍去,102逆序后201,里面的0处理应该与700里面的第二个0处理不一样,需注意。

#include <stdio.h>

int main(){
    
    int N;
    int integer = 0;
    scanf("%d", &N);
    
    while(N > 0){
        if(N % 10 != 0 || integer >0){//N % 10 != 0 || integer >0两者之间用“||”,可以有效处理700与102这两个数里的0
            integer = integer*10 + N %10;
        }
        N /= 10;
    }
    
    printf("%d\n", integer);
    return 0;
}

注意事项:C语言中,当输入多个相同数据类型的变量,例如scanf("%d%d%d", &a, &b,&c), 在3个"%d%d%d"之间可以不需要空格,而当输入多个不同类型的变量,例如scanf("%d %c %f", &a, &b,&c), 在"%d %c %f"之间是需要空格的,不然输入会出错。

**总结:**前几天一直写数学题,忽视了一个严重的问题,就是变量命名的规范,应该尽量用单词(见名知意),而不是用a,b,c来替代。此外,C语言对字符串的处理,没有String类,所以要习惯用字符数组。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

别偷我的猪_09

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值