Добавил:
korayakov
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабы / Калькулятор / Calc / func2
.cpp#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
//-----------Сложение и вычитание----------------------------------------
string func2(string str)
{
char sres[10];
char *tmp1,*tmp2;
int p0,p2,i,flag=1,count=0,k=0;
//Если приходит число без операндов, то выводим его на экран
for(i=1;i<str.length();i++)
if((str[i]=='+'||str[i]=='-')&&str[i-1]!='e')
k++;
if(k==0) flag=0;
//----------------------------------------------------------
while(flag==1)
{
i=str.find("-",1);
if(i!=-1&&str[i+1]=='-')
str.replace(i,2,"+");
if(i!=-1&&str[i+1]=='+')
str.replace(i,2,"-");
i=str.find("+",0);
if(i!=-1&&str[i+1]=='-')
str.replace(i,2,"-");
if(i!=-1&&str[i+1]=='+')
str.replace(i,2,"+");
p0=str.find_first_of("+-",1);
if(str[p0-1]=='e')
p0=str.find_first_of("+-",p0+1);
tmp1=new char[p0];
str.copy(tmp1,p0,0);
p2=str.find_first_of("+-",p0+1);
if(str[p2-1]=='e')
p2=str.find_first_of("+-",p2+1);
if(p2==-1)
{
p2=str.length();
flag=0;
}
tmp2=new char[p2-p0-1];
str.copy(tmp2,p2-p0-1,p0+1);
if(str[p0]=='+') _gcvt(atof(tmp1)+atof(tmp2),15,sres);
else _gcvt(atof(tmp1)-atof(tmp2),15,sres);
str.replace(0,p2,sres);
delete [] tmp1;
delete [] tmp2;
}
return str;
}
Соседние файлы в папке Calc