一种程序,用于纠正运算符中有错误的C++程序
A program that will correct a C++ program that has errors in the operator
编写一个程序,纠正C++程序中的运算符<<
或>>
与cin
和cout
一起使用的错误。该程序替换的每次(不正确的)出现
cin <<
带有更正版本cin >>
以及cout >>
带有更正版本cout <<
考虑到在cin
和<<
之间以及在cout
和>>
之间可能存在任意数量的空白字符(一个或多个)的可能性。替换校正版本在cin
或cout
与以下运算符之间只有一个空白。
您的程序应该从用户那里获得源文件名作为输入。修正后的版本应输出到名为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!!!
要做到这一点,您需要一个能够检测并消除上述故障情况的非平凡状态机。
相关文章:
- 没有运算符"="与这些操作数匹配,JUCE 窗口演示应用程序
- 为什么程序会错过"if"运算符?
- 为什么带有逻辑和运算符以及增量运算符的 c++ 程序给出的输出与预期不同?
- 重载 <= 运算符,但程序以退出代码 -1073741571 (0xC00000FD) 结尾
- 运算符重载 new 时 void * nullptr 使程序崩溃
- 条件运算符不允许程序终止
- 单独的处理程序左值和右值 [] 运算符
- 为什么 gcc 和 clang 各自为这个程序产生不同的输出?(转换运算符与构造函数)
- C++ 带有 While 运算符的控制台应用程序
- CGI程序的自定义输出缓冲区,运算符重载
- 如何将可编辑的预定义字符串写入 C++ 控制台应用程序中的 cin 输入运算符
- 矩阵运算C++运算符重载程序错误
- C++:程序的执行(构造函数、析构函数、赋值运算符等)
- ::(范围解析运算符)在此程序中做什么
- 提升程序选项:自定义验证器是否需要重载运算符>>?
- 运算符重载程序中的混淆
- isSet() 或运算符 void*() 或显式操作程序 bool() 或其他东西
- 在 c++ 计算器程序中重载 '+' 运算符时无法获得正确的输出
- 程序总是返回二进制'>>':找不到运算符,它采用错误类型的左操作数
- 重载运算符==具有采用模板化迭代程序的自由函数