Simple mathematical operation program written in C++ (simple operation program source code for common factors, average values, linear functions, quadratic functions, etc.)

After we learn the simple loop structure of cpp, we can create operations on common factors, common multiples, and average values.

#include <bits/stdc + + .h>
using namespace std;
int main(){
cout<<"First enter 1 or 2 to represent the number of numbers, then press Enter and then enter the number (positive integer), all its common factors will be given"<<endl;
int q;
cin>>q;
if (q==1){
int a;
cin>>a;
for(int i=a;i>0;i--){
if(a%i==0){
cout<<i<<endl;
}
}
}
else if (q==2){
int a,b,c;
cin>>a>>b;
if(a>=b){
c=b;
}
else{
c=a;
}
for(int i=c;i>0;i--){
if(a%i==0 & amp; & amp;b%i==0){
cout<<i<<endl;
}
}
}
int end;
cin>>end;
\t
\t
\t
\t
\t
\t
return 0;
} 

The above code can find all common factors of a number.

The final end variable is to prevent the program from ending at the moment of output (the same below).

We continue to improve it.

#include <bits/stdc + + .h>
using namespace std;
int main(){
cout<<"Please enter two positive integers, and their greatest common factor and least common multiple will be output"<<endl;
int a,b,i,i2;
int i3=0;
cin>>a>>b;
bool found=false;
bool found2=false;
int sm=(a>=b?b:a);
i=sm + 1;
int bi=(a>=b?a:b);
while(!found & amp; & amp;i>0){
i--;
if((a%i==0) & amp; & amp;(b%i==0)){
found=true;
}
}
cout<<"The greatest common factor is"<<i;
\t
while(!found2 & amp; & amp;i>0){
i3++;
i2=i3*bi;
if(i2%sm==0){
found2=true;
    }
}
cout<<"The greatest common factor is "<<i<<", and the least common multiple is "<<i2;
int end;
cin>>end;
return 0;
}
#include <bits/stdc + + .h>
using namespace std;
int main(){
cout<<"Please enter the number of numbers and each number (greater than -100000, less than 100000, a new line must be entered after each number)"<<endl;
int n;
cin>>n;
double e=0;
int t=-100000;
int b=100000;
for (int i=1;i<=n;i + + ){
double a;
cin>>a;
e + =a;
if(t<a){
t=a;
}
if(b>a){
b=a;
}
}
e=e/n;
cout<<"The average value is"<<e<<endl<<"The maximum value is"<<t<<endl<<"The minimum value is"<<b;
int end;
cin>>end;
return 0;
}

Following the idea of finding common factors, we then get the operations of finding the greatest common factor, the least common multiple, and the average, maximum, and minimum values of multiple numbers.

In ordinary calculators, there are usually only decimal operations, but fractions are often used in math problems. Now that we have learned the loop structure, we can make our own program for fraction operation, retaining the numerator and denominator. Following the idea of the above code, we can use the loop structure Keep it simple.

#include <bits/stdc + + .h>
using namespace std;
int main(){
cout<<"Input the numerator and denominator of the first and second fractions respectively. Before each calculation, if y is input, the numerator and denominator of the simplest fraction that is the sum of the two fractions will be output. If n is input, the sum of the two fractions will be output. The numerator and denominator of the simplest fraction to multiply. If e is entered, the program will be terminated"<<endl;
int a,b,c,d,e,f,g;
char q;
do{
cin>>q;
    if(q=='n'){
cin>>a>>b>>c>>d;
e=a*c;
f=b*d;
if (e>=f){
g=f;
}
else {
g=e;
}
if (g>0){
for (int i=g;i>=1;i--){
if (e%i==0 & amp; & amp;f%i==0){
e=e/i;
f=f/i;
}
}
    }
    else if (g<=0){
for (int i=g;i<0;i + + ){
if (e%i==0 & amp; & amp;f%i==0){
e=e/i;
f=f/i;
}
}
    }
cout<<"The numerator is "<<e<<" and the denominator is "<<f<<endl;}
if(q=='y'){
cin>>a>>b>>c>>d;
e=a*d + b*c;
f=b*d;
if (e>=f){
g=f;
}
else {
g=e;
}
if (g>0){
for (int i=g;i>=1;i--){
if (e%i==0 & amp; & amp;f%i==0){
e=e/i;
f=f/i;
}
}
    }
    else if (g<=0){
for (int i=g;i<0;i + + ){
if (e%i==0 & amp; & amp;f%i==0){
e=e/i;
f=f/i;
}
}
    }
cout<<"The numerator is "<<e<<" and the denominator is "<<f<<endl;
}
    }while (q!='e');
\t
\t
return 0;
}

So, we have the operations of multiplication and division of fractions.

Now that we have basic fraction operations, why not apply them to other mathematical operations?

We have been able to do function-related calculation programs before, but the preserved decimal output is often not the answer we want. Therefore, we can add fractional operations to the function calculation program.

#include <iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main(){
int a,b,c,d,e,f,g,h;
cout<<"Please enter two reference coordinates (each number should be converted into a fractional form (numerator first). Enter a total of eight numbers, separated by spaces, and the simplest fraction of k and b will be output"<<endl;
cin>>a>>b>>c>>d>>e>>f>>g>>h;
if(a*1.0/b!=e*1.0/f & amp; & amp;c*1.0/d!=g*1.0/h){
int ms=(c*h-d*g)*b*f;
int mm=(a*f-b*e)*d*h;
int msm=(ms>=mm?mm:ms);
    if (msm>0){
for (int i=msm;i>=1;i--){
if (ms%i==0 & amp; & amp;mm%i==0){
ms=ms/i;
mm=mm/i;
}
}
    }
    else if (msm<=0){
for (int i=msm;i<0;i + + ){
if (ms%i==0 & amp; & amp;mm%i==0){
ms=ms/i;
mm=mm/i;
}
}
    }
int ns=c*b*mm-a*d*ms;
int nm=d*b*mm;
\t
\t
\t
int nsm=(ns>=nm?nm:ns);
    if (nsm>0){
for (int i=nsm;i>=1;i--){
if (ns%i==0 & amp; & amp;nm%i==0){
ns=ns/i;
nm=nm/i;
}
}
    }
    else if (nsm<=0){
for (int i=nsm;i<0;i + + ){
if (ns%i==0 & amp; & amp;nm%i==0){
ns=ns/i;
nm=nm/i;
}
}
    }
cout<<"k="<<ms<<"/"<<mm<<",b="<<ns<<"/"<<nm;

}
else{
cout<<"Error data";
}
int o;
cin>>o;
return 0;
}

This code can find the analytical formula of a linear function that is compatible with fractions

#include <iostream>
using namespace std;
int main(){
cout<<"Please enter three reference coordinates (each number should be converted into a fraction (numerator first)). Enter a total of twelve numbers, separated by spaces. The simplest fraction of a, b, c will be output."<< endl;
int aa,ab,ba,bb,ca,cb,da,db,ea,eb,fa,fb;
cin>>aa>>ab>>ba>>bb>>ca>>cb>>da>>db>>ea>>eb>>fa>>fb;
if(aa*1.0/ab!=ca*1.0/cb & amp; & amp;aa*1.0/ab!=ea*1.0/eb & amp; & amp;ca*1.0/cb!=ea*1.0/eb ){
int as=ab*ab*fb*cb*eb*(ba*db-bb*da)*(ca*eb-cb*ea)-ab*cb*eb*eb*bb*(aa*cb-ab* ca)*(da*fb-db*fa);
        int am=db*fb*bb*(aa*cb-ab*ca)*(ca*eb-cb*ea)*(aa*eb-ab*ea);
        int asma=(as>=am?am:as);
        if(asma>0){
            for(int i=asma;i>0;i--){
                if(as%i==0 & amp; & amp;am%i==0){
                    as=as/i;
                    am=am/i;
                }
            }
        }
        if(asma<=0){
            for(int i=asma;i<0;i + + ){
                if(as%i==0 & amp; & amp;am%i==0){
                    as=as/i;
                    am=am/i;
                }
            }
        }
        int bs=am*cb*cb*ab*ab*(ba*db-bb*da)-bb*db*as*(aa*cb-ab*ca)*(aa*cb + ab*ca);
        int bm=am*ab*cb*bb*db*(aa*cb-ab*ca);
        int bsm=(bs>=bm?bm:bs);
        if(bsm>0){
            for(int i=bsm;i>0;i--){
                if(bs%i==0 & amp; & amp;bm%i==0){
                    bs=bs/i;
                    bm=bm/i;
                }
            }
        }
        if(bsm<=0){
            for(int i=bsm;i<0;i + + ){
                if(bs%i==0 & amp; & amp;bm%i==0){
                    bs=bs/i;
                    bm=bm/i;
                }
            }
        }
        int cs=am*bm*ab*ab*ba-as*aa*aa*bm*bb-am*ab*bs*aa*bb;
        int cm=am*bm*ab*ab*bb;
        int csm=(cs>=cm?cm:cs);
        if(csm>0){
            for(int i=csm;i>0;i--){
                if(cs%i==0 & amp; & amp;cm%i==0){
                    cs=cs/i;
                    cm=cm/i;
                }
            }
        }
        if(csm<=0){
            for(int i=csm;i<0;i + + ){
                if(cs%i==0 & amp; & amp;cm%i==0){
                    cs=cs/i;
                    cm=cm/i;
                }
            }
        }
        cout<<"a="<<as<<"/"<<am<<",b="<<bs<<"/"<<bm<<",c="<<cs<<"/ "<<cm;
    }
else{
cout<<"Error data";
}
int o;
cin>>o;
return 0;
}

This code can find the analytical expression of a quadratic function that is compatible with fractions

#include <bits/stdc + + .h>
using namespace std;
int main(){
int aa,ab,ba,bb,ca,cb;
cout<<"Please enter the numerator and denominator of the three coefficients of the general form of the quadratic function (six integers in total)"<<endl;
cin>>aa>>ab>>ba>>bb>>ca>>cb;
int xla,xlb,xra,xrb;
cout<<"Please enter the domain (enter the numerator and denominator of the left and right bounds respectively, a total of four integers)"<<endl;
cin>>xla>>xlb>>xra>>xrb;
char answer;
cout<<"Please enter l or s, l means to find the maximum value, s means to find the minimum value"<<endl;
cin>>answer;
//Enter the required settings
\t
int ta=-ab*ba;
int tb=2*aa*bb;
int la,lb,sa,sb,ya,yb;
//Define the variables needed next
\t
if(aa*ab<0){
if(xla*1.0/xlb<=ta*1.0/tb){
if(xra*1.0/xrb>=ta*1.0/tb){
la=ta;
lb=tb;
sa=((ta*1.0/tb-xla*1.0/xlb)>=(xra*1.0/xrb-ta*1.0/tb)?xla:xra);
sb=((ta*1.0/tb-xla*1.0/xlb)>=(xra*1.0/xrb-ta*1.0/tb)?xlb:xrb);
}
else if(xra*1.0/xrb<ta*1.0/tb){
la=xra;
lb=xrb;
sa=xla;
sb=xlb;
}
}
\t\t
else if(xla*1.0/xlb>ta*1.0/tb){
la=xla;
lb=xlb;
sa=xra;
sb=xrb;
}
}
\t
\t
else if(aa*ab>0){
if(xla*1.0/xlb<=ta*1.0/tb){
if(xra*1.0/xrb>=ta*1.0/tb){
sa=ta;
sb=tb;
la=((ta*1.0/tb-xla*1.0/xlb)>=(xra*1.0/xrb-ta*1.0/tb)?xla:xra);
lb=((ta*1.0/tb-xla*1.0/xlb)>=(xra*1.0/xrb-ta*1.0/tb)?xlb:xrb);
}
else if(xra*1.0/xrb<ta*1.0/tb){
la=xla;
lb=xlb;
sa=xra;
sb=xrb;
}
}
\t\t
else if(xla*1.0/xlb>ta*1.0/tb){
la=xra;
lb=xrb;
sa=xla;
sb=xlb;
}
}
//Judge various situations and get the numerator and denominator of x
\t
\t
\t
\t
if(answer=='l'){
ya=aa*bb*la*la*lb*cb + ab*lb*lb*ba*la*cb + ab*lb*lb*lb*bb*ca;
yb=ab*lb*lb*lb*bb*cb;
\t\t
int ym=(ya>=yb?yb:ya);
    if (ym>0){
for (int i=ym;i>=1;i--){
if (ya%i==0 & amp; & amp;yb%i==0){
ya/=i;
yb/=i;
}
}
    }
    else if (ym<=0){
for (int i=ym;i<0;i + + ){
if (ya%i==0 & amp; & amp;yb%i==0){
ya/=i;
yb/=i;
}
}
    }
The simplest numerator and denominator of the maximum value of cout<<" are "<<ya<<" and "<<yb;
}
\t
else if(answer=='s'){
ya=aa*bb*sa*sa*sb*cb + ab*sb*sb*ba*sa*cb + ab*sb*sb*sb*bb*ca;
yb=ab*sb*sb*sb*bb*cb;
\t\t
int ym=(ya>=yb?yb:ya);
    if (ym>0){
for (int i=ym;i>=1;i--){
if (ya%i==0 & amp; & amp;yb%i==0){
ya/=i;
yb/=i;
}
}
    }
    else if (ym<=0){
for (int i=ym;i<0;i + + ){
if (ya%i==0 & amp; & amp;yb%i==0){
ya/=i;
yb/=i;
}
}
    }
The simplest numerator and denominator of the minimum value of cout<<" are "<<ya<<" and "<<yb;
}
//Put the numerator and denominator of x into the function, find the numerator and denominator of y, simplify them, and output
int o;
cin>>o;
return 0;
}

This code can find the maximum value of a quadratic function

In this case, we can continue to extend the loop structure to radical simplification. Here we use the code to solve the quadratic equation of one variable.

#include <bits/stdc + + .h>
using namespace std;
int main(){
int aa,ab,ba,bb,ca,cb;
cout<<"Please enter the numerator and denominator of each coefficient of the general form of a quadratic equation (a total of six integers), and the solution will be output"<<endl;
cin>>aa>>ab>>ba>>bb>>ca>>cb;
int deltaa=(ba*ba*ab*cb)-(4*aa*ca*bb*bb);
int deltab=ab*cb*bb*bb;
if(deltaa*deltab>0){
int dmi=(deltaa>deltab?deltab:deltaa);
if (dmi>0){
for (int i=dmi;i>=1;i--){
if (deltaa%i==0 & amp; & amp;deltab%i==0){
deltaa/=i;
deltab/=i;
}
}
    }
    else if (dmi<=0){
for (int i=dmi;i<0;i + + ){
if (deltaa%i==0 & amp; & amp;deltab%i==0){
deltaa/=i;
deltab/=i;
}
}
    }
int k=1;
int de=deltaa*deltab;
for(int i=ceil(sqrt(deltaa*deltab));i>=2;i--){
if((deltaa*deltab)%(i*i)==0){
k*=i;
de/=(i*i);
\t\t\t
}
}
int xa=-ab*ba*deltab;
int xb=k*ab*bb;
int xc=2*bb*deltab*aa;
\t
\t
\t
int xmi=(xa>xb?xb:xa);
xmi=(xmi>xc?xc:xmi);
if (xmi>0){
for (int i=xmi;i>=1;i--){
if (xa%i==0 & amp; & amp;xb%i==0 & amp; & amp;xc%i==0){
xa/=i;
xb/=i;
xc/=i;
}
}
    }
    else if (xmi<=0){
for (int i=xmi;i<0;i + + ){
if (xa%i==0 & amp; & amp;xb%i==0 & amp; & amp;xc%i==0){
xa/=i;
xb/=i;
xc/=i;
}
}
    }
    cout<<"The denominator is "<<xc<<", and the numerator is "<<xa<<" plus or minus "<<xb<<" under the multiple root sign "<<de;
}



else if(deltaa*deltab==0){
int xa=ab*ba;
int xb=2*aa*bb;
int xmi=(xa>xb?xb:xa);
if (xmi>0){
for (int i=xmi;i>=1;i--){
if (xa%i==0 & amp; & amp;xb%i==0){
xa/=i;
xb/=i;
}
}
    }
    else if (xmi<=0){
for (int i=xmi;i<0;i + + ){
if (xa%i==0 & amp; & amp;xb%i==0){
xa/=i;
xb/=i;
}
}
    }
    cout<<"The denominator is "<<xb<<", and the numerator is -"<<xa;
}

else if(deltaa*deltab<0){
    cout<<"No solution";
}
int o;
cin>>o;
return 0;
}

The above is the source code of some simple mathematical operations mainly expanded from loop structures. If you have any questions, please ask!