用python写计算绩点的代码(完美优化无bug版)与C语言代码做对比,以及pyinstaller的打包

最近刚查完成绩,于是乎用python写了个计算gpa的软件,顺便也学习python的打包,本人用pyinstaller模块进行打包,打包完后发现exe程序并不能独立的运行。。(而且文件大小起码还大C程序几百倍)还是得依赖dist文件夹里面的库。。于是乎又用C语言写了同样的程序(编译型语言还是好,不需要打包的)
.关于代码:
1.大家如果是想用这些代码来做可执行程序计算gpa的话建议用后面的C语言最终优化版,有了清屏函数使用更佳。。
.以下为python写该程序和C语言写该程序代码量的对比:
python:

import os
t = 0#用于累加 数值为单科学分*单科gpa
m = 0#用于累加 所有的学分
print("欢迎来到GPA计算系统".center(40//2,'-'))
while True:
    s = eval(input('请输入该科成绩:\n输入为0则表示输入结束\n'))
    if s == 0:
        print("单科成绩输入结束".center(40//2,'-'))
        break
    elif s>100 or s<0 :
        print('小子别乱输,小心宰了你 ……^……')
        continue
    elif s<60:
        g = 0
    else:
        g = s//10-5
    print('该科目绩点为:{:.2f}'.format(g))
    q = eval(input('请输入该科所占学分(用于统计平均GPA):\n'))
    if q>8 or q<0:
        print('Are you kidding me?有这么离谱的学分?')
        continue
    t = t + g*q
    m = m + q
if m!=0:
    print('您的平均绩点为:{:.2f}'.format(t/m))
os.system('pause')

C语言:(由于好像我这个编译器不支持中文编码,所以转用英文了。。)

#include <stdio.h>
#include <stdlib.h>
int main(){
    float t=0;//用于累加 得到单科gpa*单科学分
    float m=0;//用于计数 得到总学分
    printf("--------------------Welcome to GPA calculation system!--------------------\n");
    printf("WARN:Enter the number or the program will be abnormal!!!\n");
    while(1){
        int s,g;
        float q;
        printf("Please input your grade so that I can calculate your GPA.\nEnter 0 when you are finished and I will also calculate the average GPA for you\n");
        scanf("%d",&s);
        if (s==0){
            printf("--------------------Input is over!----------------------------------------\n");
            break;}
        else if (s<0||s>100){
            printf("Your input is incorrect, please re-enter:\n");
            continue;}
        else if(s<60){
        	g=0;}
        else
            g = s/10-5;
        printf("Your GPA for the subject is: %d\n",g);
        printf("Please enter the number of credits taken in the subject (for GPA average statistics):\n");
        scanf("%f",&q);
        m+=q;
        t=t+g*q;
    }
    if (m!=0)
    {
        printf("Your GPA for all subjects is:%.2f\n",t/m);
    }
    system("pause");
}

2.C语言最终优化版代码:便于多次输入的整洁,在第二次输入后使用了清屏函数。

#include <stdio.h>
#include <stdlib.h>
int main(){
    float t=0;//用于累加 得到单科gpa*单科学分
    float m=0;//用于计数 得到总学分
    while(1){
        int s,g;
        float q;
        printf("--------------------Welcome to GPA calculation system!--------------------\n");
        printf("WARN:Enter the number or the program will be abnormal!!!\n");
        printf("\nPlease input your grade so that I can calculate your GPA.\nEnter 0 when you are finished and I will also calculate the average GPA for you\n");
        scanf("%d",&s);
        if (s==0){
            printf("--------------------Input is over!----------------------------------------\n");
            break;}
        else if (s<0||s>100){
            printf("Your input is incorrect, please re-enter:\n");
            continue;}
        else if(s<60){
        	g=0;}
        else
            g = s/10-5;
        printf("Your GPA for the subject is: %d\n",g);
        printf("Please enter the number of credits taken in the subject (for GPA average statistics):\n");
        scanf("%f",&q);
        system("cls");
        m+=q;
        t=t+g*q;
    }
    if (m!=0)
    {
        printf("Your GPA for all subjects is:%.2f\n",t/m);
    }
    system("pause");
}

3.最后是程序大小对比,由于我这边python用的pyinstaller打包的所以以它的压缩包为准。。
这是python打包后的情况大概5m多

这是C语言编译后的情况大概42kb。。起码几千倍差距了。。。
二.关于pyinstaller模块的打包
1.给python安装轮子


pip install wheel

2.安装各种模块
<1>bs4模块用于爬虫

pip install bs4

<2>pyinstaller 用于打包

pip install pyinstaller

3.检查pyinstaller模块是否安装成功:

 pyinstaller --version

输出结果:(否则未安装)

4.1

4.将写好的python代码打包:

pyinstaller xxx.py

最后一行有以下字样即为打包成功:

INFO: Building COLLECT COLLECT-00.toc completed successfully.
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值