简介
主要采用str族函数实现字符检测,只能用DOS传参进行计算,详见运行结果。
代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>/*
int main(int argc, char** argv)
{
int i=0;
printf("总共有%d条有效字符串\n",argc);
while(argv[i])
puts(argv[i++]);
return 0;
}*/void Calc(char* str)
{char* pRes = strpbrk(str, "+-*/%");if (!pRes){puts("本软件支持+-*/,请按格式输入,例如:输入xx 1+2 回车"); //xx表示程序名return ;}char c = *pRes;double result = 0;double left = atof(str);double right = atof(pRes + 1);switch (c){case '+':printf("=%lf\n", left + right);break;case '-':printf("=%lf\n", left - right);break;case '*':printf("=%lf\n", left * right);break;case '/':printf("=%lf\n", left / right);break;case '%':printf("=%d\n", (int)left % (int)right);break;}
}int main(int count, char** ps)
{if (count<2){puts("本软件支持+-*/,请按格式输入,例如:输入xx 1+2 回车");return -1;}//假设输入:DOS 33+52char s[64] = { 0 };int i = 0;while (ps[++i])strcat(s, ps[i]);Calc(s);//字符串查找函数 strchr strpbrk strtok strstrreturn 0;
}