Я работаю с QT калькулятором, и вот настал тупик с QString
Что бы не пробовал не могу
QString expr(buffer);
cin.getline(buffer, sizeof buffer);
eatspaces(buffer); сделать так что бы expr обрабатывал QString а не buffer.
Понимаю что должно быть легко, но никак.
Что можно сделать?
вот полностью код:
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <QObject>
#include <QDebug>
#include <QString>
#include <QTextStream>
#include <QTranslator>
#include <QLibraryInfo>
#include <QCoreApplication>
#include <QTextCodec>
using std::cin;
using std::cout;
using std::endl;
void eatspaces(char* str);
double expr(char* str);
double term(char* str, qint32& index);
double number(char* str, qint32& index);
char* extract(char* str, qint32& index);
const qint32 MAX = 2000;
QTextStream outStream(stdout);
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
outStream.setCodec(QTextCodec::codecForName("cp866"));
outStream << QString("Добро пожаловать в работающий калькулятор") << endl;
outStream << QString("Введите выражение или пустую строку для выхода") << endl;
QString buffer[MAX] = {0};
for(;;)
{
cin.getline(buffer, sizeof buffer);
eatspaces(buffer);
if(!buffer[0])
return 0;
QString chisla(buffer);
outStream << QString ("\t= ") << expr(buffer)
<< endl << endl;
}
}
void eatspaces(char* str)
{
qint32 i = 0;
qint32 j = 0;
while((*(str + i) = *(str + j++)) != '\0')
if(*(str + i) != ' ')
i++;
return;
}
double expr(char* str)
{
double value = 0.0;
qint32 index = 0;
value = term(str, index);
for(;;)
{
switch(*(str + index++))
{
case '\0':
return value;
case '+':
value += term(str, index);
break;
case '-':
value -= term(str, index);
break;
default:
outStream << QString("Arrrgh!*#!! Там есть ошибка") << endl
<< endl;
exit(1);
}
}
}
double term(char* str, qint32& index)
{
double value = 0.0;
value = number(str, index);
while((*(str + index) == '*') || (*(str + index) == '/'))
{
if(*(str + index) == '*')
value *= number(str, ++index);
if(*(str + index) == '/')
value /= number(str, ++index);
}
return value;
}
double number(char* str, qint32& index)
{
double value = 0.0;
if(*(str + index) == '(')
{
char* psubstr = 0;
psubstr = extract(str, ++index);
value = expr(psubstr);
delete[]psubstr;
return value;
}
while(isdigit(*(str + index)))
value = 10*value + (*(str + index++) - '0');
if(*(str + index) != '.')
return value;
double factor = 1.0;
while(isdigit(*(str + (++index))))
{
factor *= 0.1;
value = value + (*(str + index) - '0')*factor;
}
return value;
}
char* extract(char* str, qint32& index)
{
char buffer[MAX];
char* pstr = 0;
qint32 numL = 0;
qint32 bufindex = index;
do
{
buffer[index - bufindex] = *(str + index);
switch(buffer[index - bufindex])
{
case ')':
if(numL == 0)
{
size_t size = index - bufindex;
buffer[index - bufindex] = '\0';
++index;
pstr = new char[index - bufindex];
if(!pstr)
{
outStream << QString("Не удалось выделить память,") << endl
<< endl;
exit(1);
}
strcpy_s(pstr, index-bufindex, buffer);
return pstr;
}
else
numL--;
break;
case '(':
numL++;
break;
}
} while(*(str + index++) != '\0');
outStream << QString ("Сбежал конец выражения, должен быть неверный ввод.")<< endl
<< endl;
exit(1);
return pstr;
}