#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int valid(char s[]){
int valid = 0;
int cnt = 0;
int i;
for(i = 0;i < strlen(s);i++){
if(s[i] >= '0' && s[i] <= '9'){
valid = 1;
}
else if(s[i] == '.')
cnt++;
else{
valid = 0;
break;
}
}
if(cnt > 1)
valid = 0;
if(valid == 0){
printf("Plz enter valid numbers!\n");
exit(1);
}
return valid;
}
int main(int argc,char * argv[]){
double num1;
double num2;
if(argc!=3){
printf("Plz enter two numbers!!\n");
exit(0);
}
if((valid(argv[1])) && (valid(argv[2]))){
sscanf(argv[1],"%lf",&num1);
sscanf(argv[2],"%lf",&num2);
}
printf("answer: %lf \n",num1+num2);
/* num1-num2
num1*num2
To get num1/num2 need to validate that the num2 cant not be zero.
*/
return 2;
}
No comments:
Post a Comment