我正在研究一个基本的计算器(我给自己的个人项目),此时,我正在努力验证给定的字符串。
我的问题是,如何检查重复的连续字符?
我的理由是,只需抓取一个字符串而不是一次解析一个字符就更简单了;本质上创造更多的代码和更多的工作
由于算术显然是二元运算,所以应该就是这样。
实例,例如,
在这项检查中都应被视为无效。
应忽略空格。
本质上,我需要停止重复运算符,但是知道一种方法可以帮助其他编程目标。
这个问题有类似的问题,但它们都是其他语言的,并且似乎是为了实现这项任务的逆转。
坦率地说,我只是不理解基本的基础逻辑,因为我不知道我正在阅读的语言(即python,java,c#)。到目前为止我只知道C.
到目前为止,我的代码看起来像这样:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifndef _BASIC_CALC_
# define _BASIC_CALC_
# define STRLEN 81 //max string length
# define MAX 41 //max strings to be allocated into memory
# define SWIDTH 40 //limit asterick width
typedef _Bool bool;
const bool true = 1;
const bool false = 0;
typedef struct Count {
int _of_store;
int _of_tree;
int _of_ops;
} count_t;
typedef struct Expression {
char store[STRLEN];
char ** tree;
char ** ops;
int total;
count_t size;
} exp_t;
#endif
const char operator[] = { " */+-" };
const char digit[] = { " 0123456789" };
int isop(int operator)
{
switch (operator)
{
case '*':
case '/':
case '+':
case '-':
puts("IS an operator.");
break;
default:
puts("is NOT an operator.");
return false;
}
return operator;
}
bool dblcheck(exp_t * expression, int position)
{
//this function should search for consecutive operators
//and should return false if is there are consecutive operators
//or should return true if there are NO consecutive operators
//the logic need not be inverted...
}
bool validated(exp_t * expression)
{
for (int index = 0; expression->store[index]; index++)
{
if (isalpha(expression->store[index]))
{
puts("invalidated: found alpha character!");
return false;
}
if (ispunct(expression->store[index]))
{
if (!isop(expression->store[index]))
{
puts("invalidated: found bad operator!");
return false;
}
if (!dblcheck(expression, index))
{
puts("invalidated: found consecutive operators!");
return false;
}
}
}
return true;
}
bool getexp(exp_t * expression)
{
printf("(expression)?> ");
if (NULL == fgets(expression->store, STRLEN, stdin))
{
return false;
}
if (!validated(expression) && expression->store[0] != '\n')
{
puts("oops! you, the crazy person, provided an invalid expression.\n");
return false;
}
if (expression->store[0] != '\n')
{
printf("your expression: ");
puts(expression->store);
return true;
}
return false;
}
void stars(int count)
{
for (int index = 0; index <= count; index++)
{
putchar('*');
}
putchar('\n');
}
void prompt(void)
{
putchar('\n');
stars(SWIDTH);
puts("Type in a basic arithmetic expression.\n");
puts("For example, 2 + 3");
puts("or, 2 * 10 / 5");
puts("are examples of basic expressions.\n");
puts("Valid operators are +, -, *, and /.\n");
puts("To quit, press [enter] on a new expression line.");
puts("(expression)?> [enter]");
stars(SWIDTH);
putchar('\n');
}
int main(void)
{
exp_t expression;
prompt();
do {
if (!getexp(&expression))
{
continue;
}
} while (expression.store != NULL && expression.store[0] != '\n');
puts("Done!");
return 0;
}
我的目的是乐于做到这一点,所以它尽可能非正式。因此,某些侮辱/讽刺言论。
就此帖Tags
而言
我不确定要使用什么,因此我选择使用C
和String
标记。
提前感谢任何试图帮助我的人。