简介
B站视频讲解:【小工具】用 Dev-C++ 编写简单的平均数/中位数/众数/方差/一元线性回归方程计算器(附带控制台颜色设置,选择界面)
源代码
#include <cstdio>
#include <windows.h>
#include <iostream>
#include <algorithm>
#include <conio.h>
using namespace std;
double a[1000010];
double zhongshu[1000010];
void p1()
{
system("cls");
int n;
double pingjun;
double s2;
int zhongshucnt;
int zhongshutot;
int zhongshumax;
while(1)
{
pingjun=0;
s2=0;
printf("平均数/方差/中位数/众数\n\n");
printf("请输入数据个数:");
scanf("%d",&n);
printf("\n请输入数据(用 [Enter] 或 [Space] 隔开):\n");
for(int i=1;i<=n;i++)
{
scanf("%lf",&a[i]);
pingjun+=a[i];
s2+=a[i]*a[i];
}
s2/=n;
pingjun/=n;
s2-=pingjun*pingjun;
putchar('\n');
printf("平均数:");
cout<<pingjun;
putchar('\n');
putchar('\n');
printf("方差:");
cout<<s2;
putchar('\n');
sort(a+1,a+1+n);
putchar('\n');
printf("中位数:");
if(n&1)
cout<<a[(n+1)/2];
else
cout<<(a[n/2]+a[n/2+1])/2;
putchar('\n');
zhongshucnt=1;
zhongshutot=1;
zhongshumax=0;
for(int i=2;i<=n;i++)
{
if(a[i]==a[i-1])
{
zhongshucnt++;
}
else
{
if(zhongshucnt>zhongshumax)
{
zhongshumax=zhongshucnt;
zhongshu[1]=a[i-1];
zhongshutot=1;
}
else if(zhongshucnt==zhongshumax)
{
zhongshutot++;
zhongshu[zhongshutot]=a[i-1];
}
zhongshucnt=1;
}
}
if(zhongshucnt>zhongshumax)
{
zhongshumax=zhongshucnt;
zhongshu[1]=a[n];
zhongshutot=1;
}
else if(zhongshucnt==zhongshumax)
{
zhongshutot++;
zhongshu[zhongshutot]=a[n];
}
putchar('\n');
printf("众数共有 %d 个,%s出现了 %d 次:",zhongshutot,zhongshutot>1?"他们都":"它",zhongshumax);
for(int i=1;i<=zhongshutot;i++)
{
cout<<zhongshu[i];
putchar(' ');
}
putchar('\n');
putchar('\n');
printf("已经将输入数据排序:\n");
for(int i=1;i<=n;i++)
{
cout<<a[i];
putchar(' ');
}
putchar('\n');
putchar('\n');
Sleep(300);
while(_kbhit())
getch();
Sleep(200);
printf("按 [ESC] 键退出,按其他任意键重新开始...");
char ch=getch();
if(ch==27)
break;
else
system("cls");
}
}
double x[1000010];
double y[1000010];
void p2()
{
system("cls");
int n;
double totx,toty;
double xy,x2;
double xbar,ybar;
double a,b;
while(1)
{
totx=0;
toty=0;
xy=0;
x2=0;
printf("一元线性回归方程\n\n");
printf("请输入数据组数:");
scanf("%d",&n);
printf("\n输入格式:每组数据占一行,第 i 行的数据为 xi yi ,中间用空格隔开\n");
printf("\n请输入数据:\n");
for(int i=1;i<=n;i++)
{
scanf("%lf %lf",&x[i],&y[i]);
totx+=x[i];
toty+=y[i];
xy+=x[i]*y[i];
x2+=x[i]*x[i];
}
xbar=totx/n;
ybar=toty/n;
b=(xy-n*xbar*ybar)/(x2-n*xbar*xbar);
a=ybar-b*xbar;
putchar('\n');
printf("x平均值为:");
cout<<xbar;
putchar('\n');
putchar('\n');
printf("y平均值为:");
cout<<ybar;
putchar('\n');
putchar('\n');
printf("一元线性回归方程为:");
cout<<"y="<<a<<"+"<<b<<"x";
putchar('\n');
putchar('\n');
Sleep(300);
while(_kbhit())
getch();
Sleep(200);
printf("按 [ESC] 键退出,按其他任意键重新开始...");
char ch=getch();
if(ch==27)
break;
else
system("cls");
}
}
void Set(int x,int y)
{
HANDLE hOut;
COORD pos={x,y};
hOut=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut,pos);
}
int main()
{
printf("--计算器--\n\n");
HANDLE hConsoleOutput=GetStdHandle(STD_OUTPUT_HANDLE);
Set(0,2);
SetConsoleTextAttribute(hConsoleOutput,0|15*16);
printf("->平均数/中位数/众数/方差/数据排序 ");
Set(0,4);
SetConsoleTextAttribute(hConsoleOutput,15|0*16);
printf(" 一元线性回归方程 ");
char ch;
int mode=1;
while(1)
{
ch=getch();
if(ch==-32)
{
ch=getch();
if(ch==72 || ch==75)//左 上
{
mode=1;
Set(0,2);
SetConsoleTextAttribute(hConsoleOutput,0|15*16);
printf("->平均数/中位数/众数/方差/数据排序 ");
Set(0,4);
SetConsoleTextAttribute(hConsoleOutput,15|0*16);
printf(" 一元线性回归方程 ");
}
else if(ch==77 || ch==80)//右 下
{
mode=2;
Set(0,2);
SetConsoleTextAttribute(hConsoleOutput,15|0*16);
printf(" 平均数/中位数/众数/方差/数据排序 ");
Set(0,4);
SetConsoleTextAttribute(hConsoleOutput,0|15*16);
printf("-> 一元线性回归方程 ");
SetConsoleTextAttribute(hConsoleOutput,15|0*16);//记得要设置回黑底白字
}
}
else if(ch==13)
{
if(mode==1)
p1();
else
p2();
system("cls");
printf("--计算器--\n\n");
if(mode==1)
{
Set(0,2);
SetConsoleTextAttribute(hConsoleOutput,0|15*16);
printf("->平均数/中位数/众数/方差/数据排序 ");
Set(0,4);
SetConsoleTextAttribute(hConsoleOutput,15|0*16);
printf(" 一元线性回归方程 ");
}
else
{
Set(0,2);
SetConsoleTextAttribute(hConsoleOutput,15|0*16);
printf(" 平均数/中位数/众数/方差/数据排序 ");
Set(0,4);
SetConsoleTextAttribute(hConsoleOutput,0|15*16);
printf("-> 一元线性回归方程 ");
SetConsoleTextAttribute(hConsoleOutput,15|0*16);//记得要设置回黑底白字
}
}
}
return 0;
}