需要一个 linux 下c语言 的程序 300行源代码左右?谢谢

如题所述

300行有点多,一般超过100行就要分模块了。你看这个行不?
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define EMAX 101

char operator[]="()+-*/^";
char expr[EMAX];
struct stack_head {
struct stack_head *pre;
};
struct opt_stack_node {
struct stack_head stack;
char c;
} *opt_topp = NULL;/*The top of the operator stack*/
struct val_stack_node {
struct stack_head stack;
long long val;
} *val_topp = NULL;/*The top of the value stack*/

void stack_push(void *toppp, void *newp);
void *stack_pop(void *toppp);
void opt_stack_push(char c);
char opt_stack_pop();
void val_stack_push(long long val);
long long val_stack_pop();
int isopt(char c);/*Check if character is a operator*/
/*The set of actions in each state with a given character*/
void action_0(int i);
void action_1(int i);
void action_2(int i);
void action_3(int i);
void action_4(int i);
void action_5(int i);
void action_6(int i);
void action_7(int i);
void action_8();
void input_expr();/*To input an expression*/

void stack_push(void *toppp, void *newp)
{
struct stack_head *temp = (struct stack_head *)newp;
temp->pre = *(struct stack_head **)toppp;
*(struct stack_head **)toppp = (struct stack_head *)newp;
}

void * stack_pop(void *toppp)
{
struct stack_head *temp;
if (*(struct stack_head **)toppp == NULL) {
printf("Linked stack is empty!\n");
exit(1);
}
temp = *(struct stack_head **)toppp;
*(struct stack_head **)toppp = temp->pre;
return (void *)temp;
}

void stack_clear(void *toppp)
{
struct stack_head *temp;
while (*(struct stack_head **)toppp!=NULL) {
temp = (*(struct stack_head **)toppp)->pre;
free(*(struct stack_head **)toppp);
*(struct stack_head **)toppp = temp;
}
}

void opt_stack_push(char c)
{
struct opt_stack_node *opt_nodep
= (struct opt_stack_node *)malloc(sizeof(struct opt_stack_node));
opt_nodep->c = c;
stack_push(&opt_topp, opt_nodep);
}

char opt_stack_pop()
{
struct opt_stack_node *temp
= (struct opt_stack_node *)stack_pop(&opt_topp);
char c = temp->c;
free(temp);
return c;
}

void val_stack_push(long long val)
{
struct val_stack_node *val_nodep
= (struct val_stack_node *)malloc(sizeof(struct val_stack_node));
val_nodep->val = val;
stack_push(&val_topp, val_nodep);
}

long long val_stack_pop()
{
struct val_stack_node *temp
= (struct val_stack_node *)stack_pop(&val_topp);
long long val = temp->val;
free(temp);
return val;
}

int isopt(char c)
{
if (strchr(operator,c) == NULL || c == '\0') {
return 0;
} else {
return 1;
}
}

int compute(long long i, char opt, long long j, long long *result)
{
long long prod = 1;
switch (opt) {
case '+':
*result = i + j;
return 0;
case '-':
*result = i - j;
return 0;
case '*':
*result = i * j;
return 0;
case '/':
if (j == 0) {
printf(": Semantic Error: A divisor cannot be 0\n");
return 101;
} else {
*result = i / j;
return 0;
}
case '^':
if (j >= 0) {
while (j > 0) {
prod = prod * i;
j--;
}
*result = prod;
return 0;
} else if (i == 0) {
printf(": Semantic Error: "
"The power of 0 cannot be negative\n");
return 102;
} else {
while (j > 0) {
prod = prod / i;
j--;
}
*result = prod;
return 0;
}
default:
return 201;
}
}

void action_0(int i)
{
char c = expr[i];
if (isspace(c)) {
action_0(++i);
} else if (isdigit(c)) {
action_1(i);
} else if (isopt(c) || c == '\0') {
switch (c) {
case '(':
opt_stack_push(c);
action_0(++i);
break;
case '-':
opt_stack_push('_');
action_1(++i);
break;
default:
printf(": Syntax Error:201\n");
}
} else {
printf(": Lexical Error:101 Unexpected character in input\n");
}
}

void action_1(int i)
{
char c = expr[i];
if (isdigit(c)) {
val_stack_push((long long)c - 48);
action_2(++i);
} else if (isopt(c) || c == '\0' || isspace(c)) {
switch (c) {
case '(':
opt_stack_push(c);
action_0(++i);
break;
case '-':
opt_stack_push('_');
action_1(++i);
break;
default:
printf(": Syntax Error:211\n");
}
} else {
printf(": Lexical Error:111 Unexpected character in input\n");
}
}

void action_2(int i)
{
char c = expr[i];
if (isdigit(c)) {
val_stack_push((long long)c - 48 + val_stack_pop() * 10);
action_2(++i);
} else if (isopt(c) || c == '\0' || isspace(c)) {
action_3(i);
} else {
printf(": Lexical Error:121 Unexpected character in input\n");
}
}

void action_3(int i)
{
char c = expr[i];
if (isspace(c)) {
action_3(++i);
} else if (isopt(c) || c == '\0' || isdigit(c)) {
switch (c) {
case '^':
action_4(i);
break;
case '*':
case '/':
action_5(i);
break;
case '+':
case '-':
action_6(i);
break;
case ')':
action_7(i);
break;
case '\0':
action_8();
break;
default:
printf(": Syntax Error:231\n");
}
} else {
printf(": Lexical Error:131 Unexpected character in input\n");
}
}

void action_4(int i)
{
char c = expr[i];
char top_opt = opt_topp->c;
switch (top_opt) {
case '+':
case '-':
case '(':
case '$':
case '*':
case '/':
case '^':
opt_stack_push(c);
action_0(++i);
break;
case '_':
val_topp->val = -val_topp->val;
opt_stack_pop();
action_4(i);
break;
default:
printf(": Unexpected Error:341\n");
}
}

void action_5(int i)
{
char c = expr[i];
char top_opt = opt_topp->c;
long long rs;
long long left;
long long right;
switch (top_opt) {
case '+':
case '-':
case '(':
case '$':
opt_stack_push(c);
action_0(++i);
break;
case '*':
case '/':
case '^':
right = val_stack_pop();
left = val_stack_pop();
if(compute(left, opt_stack_pop(), right, &rs)==0) {
val_stack_push(rs);
action_5(i);
}
break;
case '_':
val_topp->val = -val_topp->val;
opt_stack_pop();
action_5(i);
break;
default:
printf(": Unexpected Error:351\n");
}
}

void action_6(int i)
{
char c = expr[i];
char top_opt = opt_topp->c;
long long rs;
long long left;
long long right;
switch (top_opt) {
case '(':
case '$':
opt_stack_push(c);
action_0(++i);
break;
case '+':
case '-':
case '*':
case '/':
case '^':
right = val_stack_pop();
left = val_stack_pop();
if(compute(left, opt_stack_pop(), right, &rs)==0) {
val_stack_push(rs);
action_6(i);
}
break;
case '_':
val_topp->val = -val_topp->val;
opt_stack_pop();
action_6(i);
break;
default:
printf(": Unexpected Error:361\n");
}
}

void action_7(int i)
{
char c = expr[i];
char top_opt = opt_topp->c;
long long rs;
long long left;
long long right;
switch (top_opt) {
case '(':
opt_stack_pop();
action_3(++i);
break;
case '$':
printf(": Syntax Error:271 Unclosed parenthesis,"
" expect another ')'\n");
break;
case '+':
case '-':
case '*':
case '/':
case '^':
right = val_stack_pop();
left = val_stack_pop();
if(compute(left, opt_stack_pop(), right, &rs)==0) {
val_stack_push(rs);
action_7(i);
}
break;
case '_':
val_topp->val = -val_topp->val;
opt_stack_pop();
action_7(i);
break;
default:
printf(": Unexpected Error:371\n");
}
}

void action_8()
{
char top_opt = opt_topp->c;
long long rs;
long long left;
long long right;
switch (top_opt) {
case '(':
printf(": Syntax Error:281 Unclosed parenthesis,"
" expect another '('\n");
break;
case '$':
printf("%lld\n",val_stack_pop());
break;
case '+':
case '-':
case '*':
case '/':
case '^':
right = val_stack_pop();
left = val_stack_pop();
if(compute(left, opt_stack_pop(), right, &rs) == 0) {
val_stack_push(rs);
action_8();
}
break;
case '_':
val_topp->val = -val_topp->val;
opt_stack_pop();
action_8();
break;
default:
printf(": Unexpected Error:381\n");
}
}

void input_expr()
{
scanf("%100[^\n]", expr);
while (getchar() != '\n') {}
}

int main()
{
printf("\nErGeCalculator version 1.0.2 (2012-10-15)\n"
"The maximum length of expression is 100 characters\n"
"Quit with 'q()'\n\n"
"> ");
input_expr();
while (strcmp(expr,"q()") != 0) {
stack_clear(&opt_topp);
stack_clear(&val_topp);
opt_stack_push('$');
action_0(0);
printf("> ");
input_expr();
}
return 0;
}
温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网