I am solving now an algorithmic problem using recursion. So what you need to find out in this problem is that if the string you inputed is an expression. So for example you have string "256+300-500" - this is an expression. So an expression is a string that contains numbers and the signes "+" and "-". The signs + and - cant stay one near eachother and also there cant be to + or 2 - near eachother. So "256++300" and "256+-600-500" is not an expression. Also if the number contains a letter in it then it is not an expression "54e2". So please help me make this program. I have done the part which sees if the number string is a number.
#include <iostream>
#include <cmath>
#include<string>
using namespace std;
int cif(char c)
{
if(isdigit(c))return 1;
else return 0;
}
int num (string s)
{
int z=s.length();
if(z==1) return cif(s[0]);
else
{
char c =s[0];
s=s.substr(1);
if(cif(c) && num(s))return 1; else return 0;
}
}
int main()
{
cout << num("2353Y3554");
return 0;
}
The output of this program is 0 becase it is not a number, if it was the output would have been 1. Please help me make the program that I need continueing with this one, please.
Aucun commentaire:
Enregistrer un commentaire