Home > Software engineering >  C how to make if statements more efficient
C how to make if statements more efficient

Time:02-13

So i wrote a program to manipulate a file with questions and answers to a specific order so a program by the name of active presenter can read it and turn it into a quiz. I am pretty new to coding and my code is garbage. I mostly wanna reduce the size of my if statements because i wanna include every word from the alphabet in it. How would i got about doing this?

my code:

#include<iostream>
#include<fstream>
#include<string.h>
#include<stdio.h>
using namespace std;

main(){
    string mystring, input, output, anwsers, delO, pravi;
    cout<<"Vpisi ime datoteke iz katere bomo vzeli text"<<endl;
    cin>>input;
    if(input.find(".txt")!= std::string::npos){}else{input  = ".txt";}
    cout<<"Vpisi ime datoteke v katero bomo dali text"<<endl;
    cin>>output;
    if(output.find(".txt")!= std::string::npos){}else{output  = ".txt";}
    cout<<"Vpisi ime datoteke z odgovori"<<endl;
    cin>>anwsers;
    if(anwsers.find(".txt")!= std::string::npos){
    }else{
    anwsers  = ".txt";}
    ifstream ifile (input);
    char znakn='~', znakp='=';
    ofstream ofile("delovanje.txt", ios::trunc);
    if(ifile.is_open()){
            while(ifile){
                getline(ifile,mystring);
                if(mystring.find("A-0")!= std::string::npos||
                    mystring.find("K-0")!= std::string::npos||
                    mystring.find("P-0")!= std::string::npos||
                    mystring.find("O-0")!= std::string::npos||
                    mystring.find("N-0")!= std::string::npos||
                    mystring.find("M-0")!= std::string::npos||
                    mystring.find("L-0")!= std::string::npos||
                    mystring.find("R-0")!= std::string::npos||
                    mystring.find("Z-0")!= std::string::npos){
                    string del;
                    del=mystring.substr(0,6);
                    ifstream odgovor(anwsers);
                    if(odgovor.is_open()){
                        while(odgovor){
                            getline(odgovor,delO);
                            if(delO.find(del)!=std::string::npos){
                                int anus=delO.find(del);
                                int daj=anus 9;
                                string pravilno=delO.substr(daj,1);
                                pravi="(" pravilno ")";
                            }
                        }
                    }
                }
                if(mystring.find("(1)")!= std::string::npos||
                   mystring.find("(2)")!= std::string::npos||
                   mystring.find("(3)")!= std::string::npos||
                   mystring.find("(4)")!= std::string::npos||
                   mystring.find("(5)")!= std::string::npos){
                    ofstream ofile("delovanje.txt", ios::app);
                    if(ofile.is_open()){
                        if(mystring.find("(1)")!=std::string::npos){
                            ofile<<" {";
                        }
                        if(mystring.find(pravi)!=std::string::npos){
                            ofile<<znakp<<mystring<<" ";
                        }else{
                            ofile<<znakn<<mystring<<" ";
                        }
                        }
                    }else if(mystring.find("A-0")!= std::string::npos||
                            mystring.find("K-0")!= std::string::npos||
                            mystring.find("P-0")!= std::string::npos||
                            mystring.find("O-0")!= std::string::npos||
                            mystring.find("N-0")!= std::string::npos||
                            mystring.find("M-0")!= std::string::npos||
                            mystring.find("L-0")!= std::string::npos||
                            mystring.find("R-0")!= std::string::npos||
                            mystring.find("Z-0")!= std::string::npos){
                        ofstream ofile("delovanje.txt", ios::app);
                        if(ofile.is_open()){
                            ofile<<"}"<<endl<<endl<<mystring;
                }
            cout<<mystring<<endl;
            }else{
                ofstream ofile("delovanje.txt", ios::app);
                if(ofile.is_open()){
                    ofile<<mystring<<" ";
                    cout<<mystring;
                }else{
                    ofile.open("delovanje.txt", ios::app);
                    if(ofile.is_open()){
                        ofile<<" "<<mystring<<" ";
                        cout<<mystring;
                    }
                }
            }
        }
    }
    ofile.close();
    ofstream koncnica("delovanje.txt", ios::app);
            if(koncnica.is_open()){
                cout<<"}";
            }else{
                koncnica.open("delovanje.txt", ios::app);
                if(koncnica.is_open()){
                cout<<"}";
            }
            }
    koncnica.close();
    ifstream popravljanje("delovanje.txt");
    if(popravljanje.is_open()){
        ofstream popravek(output,ios::trunc);
        while(popravljanje){
            string celota, a, b,fixed;
            int prva,druga,tretja,dolprva,doldruga,potem,ena,dva;
            getline(popravljanje,celota);
            if(celota.find("=(2)")!=std::string::npos){
                prva=celota.find("~(1)");
                druga=celota.find("=(2)");
                tretja=celota.find("~(3)");
                dolprva=druga-prva;
                doldruga=tretja-druga;
                a=celota.substr(prva,dolprva);
                b=celota.substr(druga,doldruga);
                potem=dolprva doldruga;
                fixed=b " " a " ";
                celota.replace(prva,potem,fixed);
                ena=celota.find("(1)") 1;
                dva=celota.find("(2)") 1;
                string l="2",m="1";
                int bruh=1;
                celota.replace(ena,bruh,l);
                celota.replace(dva,bruh,m);
                if(popravek.is_open()){
                    popravek<<celota<<endl;
                }else{
                    popravek.open(output,ios::app);
                    if(popravek.is_open()){
                        popravek<<celota<<endl;
                    }
                }
            }else{
                if(popravek.is_open()){
                    popravek<<celota<<endl;
                }else{
                    popravek.open(output,ios::app);
                    if(popravek.is_open()){
                        popravek<<celota<<endl;
                    }
            }
        }
    }
    }
    popravljanje.close();
    int odstrani=remove("delovanje.txt");
    if(odstrani==0){
        cout<<endl<<"delovanje.txt deleted"<<endl;
    }else{
        cout<<endl<<"delovanje.txt not deleted"<<endl;
    }
    return 0;
}

CodePudding user response:

Note: I just took considered how to reduce the size of the if conditions without considering the possibility of other optimizations.

You could use the regex library to match the strings against patterns. If needed you could also add capturing groups to extract info about the position of substrings.

The following code simply checks prints output to the console depending on the patterns matched:

class StringHandler
{
public:
    void operator() (std::string const& s)
    {
        if (std::regex_search(s, m_regex1))
        {
            std::cout << "letter and number\n";
        }
        if (std::regex_search(s, m_regex2))
        {
            std::cout << "number in brackets\n";
        }
    }
private:
    std::regex m_regex1{"[AK-PRZ]-0"};
    std::regex m_regex2{"\\(\\d \\)"};
};

int main()
{
    StringHandler h;

    std::cout << "input1\n";
    h("(200)");
    std::cout << "input2\n";
    h("foo A-0, baz (200) 42");
    std::cout << "input3\n";
    h("( 3 ) X-0");
}

Note that matching all uppercase letters is even simpler (regex [A-Z]-0), but I restricted myself to the letters your if statement is matching right now.

  •  Tags:  
  • c
  • Related