该程序将从用户获取数学表达式的字符串(char数组)并解析它然后将每个部分(数字和运算符分别)保存在名为exp的数组的元素中。我在c#之前调试此程序它工作得很好但是从现在起我在大学学习C ++时我必须用这种语言编程,所以当我在visual studio中调试这个程序时,我得到一个错误,上面写着:
“LineCalculator.exe中发生了缓冲区溢出,该程序已破坏程序的内部状态。按Break键调试程序或继续终止程序。”
请帮忙,我该怎么办?
#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;