#microsoft internship problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
1) Given a string, you have to check if it is a valid number or not.(The number can be signed, floating point/integer). | |
If it is a number return true else return false. | |
The constraints were :- | |
i) No decision statements allowed (No if-else,no switch-case). | |
ii) No ternary conditional operators allowed (? : not allowed). | |
iii) No looping statements allowed (No for/while/do-while). | |
*/ | |
#include<iostream> | |
using namespace std; | |
bool dot=false; | |
bool endt=false; | |
bool finalr=true; | |
bool isdot(char c) | |
{ | |
return(c=='.'&&(!dot)&&(dot=true)); | |
} | |
bool validno(char c) | |
{ | |
return(c=='0'||c=='1'||c=='2'||c=='3'||c=='4'||c=='5'||c=='6'||c=='7'||c=='8'||c=='9'); | |
} | |
bool rec(char *s) | |
{ | |
endt=*s=='\0'; | |
bool thisis=endt||validno(*s)||isdot(*s); | |
finalr=finalr&&thisis; | |
bool ttm=(*s=='.')&&(*(s+1)=='\0')&&(finalr=false); | |
s++; | |
bool tm=(!endt)&&rec(s); | |
return(finalr); | |
} | |
bool check(char s[]) | |
{ | |
dot=(s[0]=='.'); | |
bool frst=s[0]=='-'||s[0]=='+'||dot||validno(s[0]); | |
bool tm=(*(s+1)=='\0')&&(*s=='.'||*s=='+'||*s=='-')&&(finalr=false); | |
return(frst&&rec(&s[1])); | |
} | |
int main() | |
{ | |
dot=false; | |
endt=false; | |
finalr=true; | |
char s[1000]; | |
cin>>s; | |
cout<<check(s); | |
main(); | |
} |
Comments
Post a Comment