Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C+中的顺序访问文件问题+; 程序应从CPP文件中读取C++程序,并将所有非关键字标识符打印到文本文件中。_C++_Visual C++_Readfile_Sequential - Fatal编程技术网

C+中的顺序访问文件问题+; 程序应从CPP文件中读取C++程序,并将所有非关键字标识符打印到文本文件中。

C+中的顺序访问文件问题+; 程序应从CPP文件中读取C++程序,并将所有非关键字标识符打印到文本文件中。,c++,visual-c++,readfile,sequential,C++,Visual C++,Readfile,Sequential,首先在load函数中,我尝试编写3种不同的读取方式 cpp文件。但行数始终为201行。真实的 答案是103行 deleteCharacterConstants函数中的第二个,为什么 当我使用时它不起作用 && line[b] != '\'' or ''' 第三,我不知道如何编码GetIdentifier、legal、isNumber、, isKeywords和isDuplicate 功能。有人能帮我吗??如果可以,告诉我逻辑和逻辑 样品 最后,非常感谢 这是我的全部计划 #i

首先在load函数中,我尝试编写3种不同的读取方式 cpp文件。但行数始终为201行。真实的 答案是103行

deleteCharacterConstants函数中的第二个,为什么 当我使用时它不起作用

&& line[b] != '\'' or ''' 
第三,我不知道如何编码GetIdentifier、legal、isNumber、, isKeywords和isDuplicate 功能。有人能帮我吗??如果可以,告诉我逻辑和逻辑 样品

最后,非常感谢

这是我的全部计划

#include <iostream>
#include <fstream>
using namespace::std;

// read in a C++ program from a cpp file, and put it to the array "program".
void load(char program[][200], int &numLines);

void deleteComments(char line[]); // if there is a single-line comment in "line", delete it.

void deleteStringConstants(char line[]); // if there are string constants in "line", delete them.

void deleteCharacterConstants(char line[]); // if there are character constants in "line", delete them.

// put all identifiers in "line" into identifiers[ numIdentifiers ].
void getIdentifiers(char line[], char identifiers[][32], int &numIdentifiers);

// if character is a letter, digit or underscore, then return true, otherwise return false.
bool legal(char character);

// print all non-number, non-keyword strings in "identifiers" into a text file.
void store(char identifiers[][32], int numIdentifiers);

bool isNumber(char string[]); // if "string" consists of digits only, then return true, otherwise return false.

bool isKeywords(char string[]); // if "string" is a keyword of C++, then return true, otherwise return false.

// if there is a nonnegtive integer i < pos such that identifiers[ pos ] is equal to identifiers[i],
// then return true; otherwise return false.
bool isDuplicate(char identifiers[][32], int pos);

const char keywords[][20] = { "auto", "break", "case", "char", "const", "continue", "default",
"define", "do", "double", "else", "enum", "extern", "float", "for",
"goto", "if", "int", "long", "register", "return", "short",
"signed", "sizeof", "static", "struct", "switch", "typedef",
"union", "unsigned", "void", "volatile", "while", "bool",
"catch", "class", "const_cast", "delete", "dynamic_cast",
"explicit", "false", "friend", "inline", "mutable", "namespace",
"new", "operator", "private", "protected", "public",
"reinterpret_cast", "static_cast", "template", "this", "throw",
"true", "try", "typeid", "typename", "using", "virtual", "include" };

int main()
{
    char program[1000][200];
    int numLines = 0;

    load(program, numLines); // reads in a C++ program from a cpp file, and put it to the array program.

    char identifiers[2000][32];
    int numIdentifiers = 0;

    for (int i = 0; i < numLines; i++)
    {
        deleteComments(program[i]); // if there is a single-line comment in program[ i ], delete it.
        deleteStringConstants(program[i]); // if there are string constants in program[ i ], delete them.
        deleteCharacterConstants(program[i]); // if there are character constants in program[ i ], delete them.
        if (strcmp(program[i], "") != 0)
            // put all identifiers in program[ i ] into identifiers[ numIdentifiers ].
            getIdentifiers(program[i], identifiers, numIdentifiers);
    }

    // print all non-number, non-keyword strings in "identifiers" into a text file.
    store(identifiers, numIdentifiers);

    system("pause");
}
    void load(char program[][200], int &numLines){
    ifstream inFile("test.cpp", ios::in);

    if (!inFile)
    {
        cout << "File could not be opened" << endl;
        exit(1);
    }

    for (int i = 0; !inFile.eof(); i++){
        inFile.getline(program[i], 200);
        numLines++;
    }
    inFile.close();
}

void deleteComments(char line[])
{
    int i = 0;

    while (line[i] != '\0' && (line[i] != '/' || line[i + 1] != '/'))
        i++;

    if (line[i] == '/' && line[i + 1] == '/')
    {
        line[i] = '\0';
        return;
    }
}


void deleteStringConstants(char line[])
{
    int b = 0;
    int e;
    while (line[b] != '\0')
    {
        while (line[b] != '\0' && line[b] != '\'')
            b++;
        if (line[b] == '\0')
            break;
        e = b + 1;
        if (line[e] == '\0')
            break;
        while (line[e] != '\0' && (line[e - 1] == '\\' || line[e] != '\''))
            e++;
        if (line[e] == '\0')
            break;
        for (int i = b; i <= e; i++)
            line[i] = ' ';
        b = e + 1;
    }
}


void deleteCharacterConstants(char line[])
{
    int b = 0;
    int e;
    while (line[b] != '\0')
    {
        while (line[b] != '\0' && line[b] != '"')
            b++;
        if (line[b] == '\0')
            break;
        e = b + 1;
        if (line[e] == '\0')
            break;
        while (line[e] != '\0' && (line[e - 1] == '\\' || line[e] != '"'))
            e++;
        if (line[e] == '\0')
            break;
        for (int i = b; i <= e; i++)
            line[i] = ' ';
        b = e + 1;
    }
}

void getIdentifiers(char line[], char identifiers[][32], int &numIdentifiers){

}

bool legal(char character){
    return 0;
}

void store(char identifiers[][32], int numIdentifiers)
{
    ofstream Result("Words.txt", ios::out);


    for (int i = 0; i < numIdentifiers; i++) {
        for (int j = 0; j <= 32; j++){
            Result << identifiers[i][j] << endl;
        }
    }

    cout << endl << endl;

    Result.close();
}

bool isNumber(char string[]){
    return 0;
}

bool isKeywords(char string[]){
    return 0;
}
bool isDuplicate(char identifiers[][32], int pos){
    return 0;
}

这里的问题太多了。请缩小范围。脱离主题:
!eof()
作为循环条件几乎总是错误的,在这里也是错误的。C++:考虑使用STD::SET,而不是2D数组的char,用于<代码>关键字>代码>主题:不使用<代码> STD::String s确实使你变得更难。节目就这样结束了。堆栈不会展开,析构函数不会被调用,资源也不会被清理。这是一个糟糕的场景。
// Fig. 6.20: fig06_20.cpp
// Demonstrating C++ Standard Library class template vector.
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

void outputVector( const vector< int > & ); // display the vector
void inputVector( vector< int > &, int start ); // input values into the vector

int main()
{
   vector< int > integers1( 7 ); // 7-element vector< int >
   vector< int > integers2( 10 ); // 10-element vector< int >

   // print integers1 size and contents
   cout << "Size of vector integers1 is " << integers1.size()
      << "\nvector after initialization:" << endl;
   outputVector( integers1 );

   // print integers2 size and contents
   cout << "\nSize of vector integers2 is " << integers2.size()
      << "\nvector after initialization:" << endl;
   outputVector( integers2 );

   // input and print integers1 and integers2
   cout << "\nEnter 17 integers:" << endl;
   inputVector( integers1, 1 );
   inputVector( integers2, integers1.size() + 1 );

   cout << "\nAfter input, the vectors contain:\n"
      << "integers1:" << endl;
   outputVector( integers1 );
   cout << "integers2:" << endl;
   outputVector( integers2 );
   ......
iostream
iomanip
vector
std
outputVector
inputVector
start
main
......