mardi 25 juillet 2017

inheritance in c++: swap the base class and derived class

I just want to swap my derived class and base class and i find it so hard to do it. can you please help me? My project is already correct but my teacher wants me to swap my derived class to my base class so I think I have to change almost everything. please I only have 4 more days for the deadline.

base.h

#ifndef BASE_H_INCLUDED
#define BASE_H_INCLUDED

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


using namespace std;

class input_parsing{
public:
    char myeq[30],mygx[30],syms[10],inp[10];
    input_parsing(){}
    ~input_parsing(){}
    long a,b,c,totpoly,par1=0;
    char *mychar[15];
    int turn=1;



//-----------------------------------------------
void enter_equation(){
        char *tokenptr;

        switch(turn){
                        case 1:
                        cout<<"   Enter " << turn <<"st Equation : ";
                        break;
                        case 2:
                        cout<<"   Enter " << turn <<"nd Equation : ";
                        break;
                        case 3:
                        cout<<"   Enter " << turn <<"rd Equation : ";
                        break;
                        default:
                        cout<<"   Enter " << turn <<"th Equation : ";
                        break;
        }


        gets(myeq);

        turn++;

        b=0;
        if(myeq[0]!='-'){ syms[b]='+'; b++; }
        c=b;
        do{
            if(myeq[c]=='-'){
                syms[b]='-';
                b++;
            }
            else if(myeq[c]=='+'){
                syms[b]='+';
                b++;
            }
            else{}
            c++;
        }while(c<strlen(myeq));

        a=0;
        tokenptr=strtok(myeq,"+|-|=");


        while(tokenptr!=NULL){
            mychar[a]=tokenptr;
            tokenptr=strtok(NULL,"+|-|=");

            a++;
        }
        totpoly=a;
    }





bool check_x(char myarr[]){
    for(long a=0; a<strlen(myarr); a++){
        if(myarr[a]=='x') return true;
        }
        return false;
    }

};


#endif

derived.h

#ifndef DERIVED_H_INCLUDED
#define DERIVED_H_INCLUDED


#include "base.h"

    class base_solution:public input_parsing{
    public:
    base_solution(){}
    ~base_solution(){}

    typedef double **arrptr;
    arrptr myconst;
    long numeq;


    typedef double *arrptr2;
    arrptr2 x;

    void algo(){
        int i,j,k,b,mcount;
        double divisor;
        char z[2];
        cout<<"   Enter how many equation/s: ";
        cin>>numeq;

        init_2darr();

        gets(z);
        i=0;
        while(i<numeq){
            enter_equation();
            getconstant(i);
            i++;
        }


    // solution starts here

    for (i=0;i<numeq;i++)                    //Pivotisation
            for (k=i+1;k<numeq;k++)
                if (myconst[i][i]<myconst[k][i])
                    for (j=0;j<=numeq;j++)
                    {
                        double temp=myconst[i][j];
                        myconst[i][j]=myconst[k][j];
                        myconst[k][j]=temp;
                    }


        for (i=0;i<numeq-1;i++)            //loop to perform the gauss elimination
            for (k=i+1;k<numeq;k++)
                {
                    double t=myconst[k][i]/myconst[i][i];
                    for (j=0;j<=numeq;j++)
                        myconst[k][j]=myconst[k][j]-t*myconst[i][j];    //make the elements below the pivot elements equal to zero or elimnate the variables
                }


        for (i=numeq-1;i>=0;i--)                //back-substitution
        {                        //x is an array whose values correspond to the values of x,y,z..
            x[i]=myconst[i][numeq];                //make the variable to be calculated equal to the rhs of the last equation
            for (j=i+1;j<numeq;j++)
                if (j!=i)            //then subtract all the lhs values except the coefficient of the variable whose value                                   is being calculated
                    x[i]=x[i]-myconst[i][j]*x[j];
            x[i]=x[i]/myconst[i][i];            //now finally divide the rhs by the coefficient of the variable to be calculated
        }
        cout<<"\n   values:\n";
        for (i=0;i<numeq;i++){
            printf("   x");
            printf("%d", i);
            printf("= ");
            printf("%.10lf\n",x[i]);
        }
                                        // Print the values of x, y,z,....

    delete[] x;
    delete[] myconst;

    }
    //===================================
    void getconstant(long c){
    char expr[10],expr1[10],expr2[10];
    char *tokenptr;
    double ans;
    long a,b,d;
    char mnumb[5];

    for(b=0; b<totpoly; b++){
        for(a=0;a<5; a++) mnumb[a]=NULL;
        for(a=0; a<10; a++) expr1[a]=expr[a]=expr2[a]=NULL;

        tokenptr=NULL;
        strcpy(expr,mychar[b]);

        ans=0;
        if(expr[0]!='x'&&check_x(expr)==true){
            tokenptr=strtok(expr,"x");
            if(syms[b]=='+')  ans+=atof(tokenptr);
            else              ans-=atof(tokenptr);

            if(mychar[b][strlen(mychar[b])-1]!='x'){
                tokenptr=strtok(NULL,"");
          //      strcat(expr2,strcpy(expr1,tokenptr));
                }
            strcpy(expr1,tokenptr);

            d=0;
            for(a=0; a<strlen(expr1); a++){
                    if(expr1[a]!='x'){
                            mnumb[d]=expr1[a];
                            d++;
                    }
            }
            myconst[c][atol(mnumb)]=ans;

        }
        else if(expr[0]=='x'){
            if(syms[b]=='+')   ans=1;
            else ans=-1;
            if( expr[1]!='\0'){
                tokenptr=strtok(expr,"");
        //        strcat(expr2,strcpy(expr1,tokenptr));
            }
            strcpy(expr1,tokenptr);
            d=0;
        for(a=0; a<strlen(expr1); a++){
                    if(expr1[a]!='x'){
                            mnumb[d]=expr1[a];
                            d++;
                    }
                }

            myconst[c][atol(mnumb)]=ans;


        }
        else if(isdigit(expr[strlen(expr)-1])){
            if(syms[b]=='-')  ans=-atof(expr);
            else ans=atof(expr);
            expr1[0]=NULL;
            myconst[c][numeq]=ans;
        }
        else{
        }
      }
    }
    //=====================================


    void init_2darr(){
    int a,b;
        myconst=new double*[numeq];
        for(a=0; a<=numeq; a++){
            myconst[a]=new double[numeq];
        }

        x=new double[numeq];

        for(b=0; b<numeq; b++){
            for(a=0; a<=numeq; a++){
                myconst[b][a]=0.0;
            }
        }
    }
    };




    #endif

main.cpp

#include "base.h"
#include "derived.h"
using namespace std;


int main()
{
    base_solution nle;
    nle.algo();
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire