0

このプログラムは、ユーザーから数式の文字列 (char 配列) を取得して解析し、各部分 (数値と演算子を個別に) を exp という配列の要素に保存します。以前に C# でこのプログラムをデバッグしましたが、かなり機能しました。適切ですが、今から大学で C++ を勉強しているので、この言語でプログラミングする必要があるため、このプログラムをビジュアル スタジオでデバッグすると、次のようなエラーが表示されます。

「LineCalculator.exe でバッファ オーバーランが発生し、プログラムの内部状態が破壊されました。Break を押してプログラムをデバッグするか、Continue を押してプログラムを終了してください。」

助けてください、どうすればいいですか?

#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <math.h>
using namespace std;

struct str{
    char text[10];
};
void input();
void CurrentObs(char source[],char current , int i,str exp[],int c,int ExpNum);
void ExpAssign(int i ,int c , int ExpNum,char current ,char source[],str exp[]);
int _tmain(int argc, _TCHAR* argv[])
{
    input();
    return 0;
}
void input()*//for getting the string from the user*
{
    char source[50];
    gets(source);
    struct str exp[20];
    int i=-1;
    int c=0;
    int ExpNum=-1;
    do{
        i++;
        char current=source[i];
        CurrentObs(source,current,i,exp,c,ExpNum);
    }while(i<50);
    //the next loop will print the parsed expressions in separate lines
    for(int b=0;b<20;b++)
        cout<<exp[b].text<<endl;
}
//the next function is to check if the current character is one of the signs mentioned in the signs array or not
void CurrentObs(char source[],char current , int i,str exp[],int c,int ExpNum)
{
    struct str signs[7]={"+","-","/","*","=","(",")"};
    for (int x=0;x<7;x++)
        if (current==*signs[i].text)
        {
            ExpNum++;
            ExpAssign(i,c,ExpNum,current,source,exp);
            break;
        }
}
void ExpAssign(int i ,int c , int ExpNum,char current ,char source[],str exp[])
{
    char parsed[10];
    for (int j = c; j < i; j++)
        parsed[j-c]=source[j];
    strcpy(exp[ExpNum].text,parsed);
    c=i+1;
4

0 に答える 0