一种程序,用于纠正运算符中有错误的C++程序

A program that will correct a C++ program that has errors in the operator

本文关键字:程序 运算符 有错误 C++ 用于 一种      更新时间:2023-10-16

编写一个程序,纠正C++程序中的运算符<<>>cincout一起使用的错误。该程序替换的每次(不正确的)出现

cin <<带有更正版本cin >>

以及cout >>带有更正版本cout <<

考虑到在cin<<之间以及在cout>>之间可能存在任意数量的空白字符(一个或多个)的可能性。替换校正版本在cincout与以下运算符之间只有一个空白。

您的程序应该从用户那里获得源文件名作为输入。修正后的版本应输出到名为corrected.txt的文件中,并应在终端上显示。这意味着程序的输出(在获得文件名之后)应该与文件corrected.txt的内容完全相同。您的程序应该定义一个以输入和输出文件流为参数调用的函数。

该代码没有编译错误。但是,代码输出什么都没有(文件存在)。此外,在我编译程序后,文件中的单词将消失。有人能给我一些建议吗?谢谢

#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <cctype>
#include <fstream>
#include <string>
using namespace std;
void print(ifstream& input,ofstream& output, char next)
{
    input.get(next);
    cout << next;
    output.put(next);
    while(input >> next)
    {
        if(next=='c')
        {
            output.put(next);
            cout << next;
            input.get(next);
            if(next=='i')
            {   output.put(next);
                cout << next;
                input.get(next);

                if(next=='n')
                {
                    output.put(next);
                    cout << next;
                    while(next==' ')
                    {
                        input.get(next);
                    }
                    output.put(' ');
                    if(next=='<')
                    {
                        input.get(next);
                        if(next == '<')
                        {
                            cout << ">>";
                            input.get(next);
                        }
                        else
                        {
                            output.put(next);
                            cout << next;
                        }
                    }
                    else
                    {
                        output.put(next);
                        cout << next;
                    }
                }
                else
                {
                    output.put(next);
                    cout << next;
                }
            }
            else if (next=='o')
            {
                output.put(next);
                cout << next;
                input.get(next);
                if(next=='u')
                {
                    output.put(next);
                    cout << next;
                    input.get(next);

                    if(next=='t')
                    {
                        output.put(next);
                        cout << next;
                        while(next==' ')
                        {
                            input.get(next);
                        }
                        output.put(' ');
                        if(next=='>')
                        {
                            input.get(next);
                            if(next == '>')
                            {
                                cout << "<<";
                                input.get(next);
                            }
                            else
                            {
                                output.put(next);
                                cout << next;
                            }
                        }
                        else
                        {
                            output.put(next);
                            cout << next;
                        }
                    }
                    else
                    {
                        output.put(next);
                        cout << next;
                    }
                }
                else
                {
                    output.put(next);
                    cout << next;
                }
                input.get(next);
            }
        }
    }
}
int main()
{
    ifstream input;
    ofstream output;
    char next;
    char filename[16];
    cout << "Enter filename:" << endl;
    cin >> filename;
    input.open(filename);
    if(input.fail()) {
        cout << "Could not open the file " << endl;
        exit(0);
    }
    output.open("original.txt");
    if (output.fail()) {
        cout << "Could not open the file " << filename << endl;
        exit(1);
    }
    print(input, output, next);
    input.close();
    output.close();
    return 0;
}

我无法在评论中解释这一点。请不要过于苛刻地评判:

void print(ifstream& input,ofstream& output)
{
    bool first = true;
    std::string command;
    while(std::getline(input, command, ';'))
    { // loop until no more input to read or input fails to be read
        if (command.find("cin")!= std::string::npos)
        { // found cin somewhere in command. This is too crude to work. See below
            size_t pos = command.find("<<"); // look for the first <<
            while (pos != std::string::npos)
            { // keep replacing and looking until end of string
                command.replace(pos, 2, ">>"); // replace with >>
                pos = command.find("<<", pos); // look for another 
            }
        }
        else if (command.find("cout")!= std::string::npos)
        { // same as above, but other way around
            size_t pos = command.find(">>"); 
            while (pos != std::string::npos)
            {
                command.replace(pos, 2, "<<");
                pos = command.find(">>", pos);
            }
        }
        if (! first)
        {
            output << ';' << command; // write string to output
        }
        else
        {
            first = false;
            output << command; // write string to output
        }
    }
}

这将捕获并正确处理MOST情况。它不能处理的是:

之类的东西

cout << Cincinnati; // find is just too dumb. Regex is not
           ^ finds a cin right here and starts reversing <<

评论

/* cout >> hah ha ha Sucker!!!; */

字符串文字

std::string fail = "cout >> Got you again!!!";

[咒骂删除]宏

#define evil cout >
> "Gotcha!!!";

还有一些非常奇怪的东西,比如

cout << vector<vector<int>>(42)[0];
                         ^ Muhuhahahahahaha!!!

要做到这一点,您需要一个能够检测并消除上述故障情况的非平凡状态机。