- 浏览: 70470 次
- 性别:
- 来自: 大连
最近访客 更多访客>>
最新评论
-
newlangwen1:
你好 我想请教个问题,我运行了一下上面的程序,结果是输入什么就 ...
C程序 对文本行进行排序
#include<stdlib.h> #include<stdio.h> #include<ctype.h> #include<math.h> #include<string.h> #define MAXOP 100 #define TRUE 1 #define FALSE 0 #define MAX_ID_LEN 32 #define MAXVARS 30 #define NUMBER 0 /* 4-6 these are new for this exercise*/ #define IDENTIFIER 1 #define ENDSTRING 2 /* 4-6 end of new stuff */ /* 4-6 this is new for this program */ struct varType{ char name[MAX_ID_LEN]; double val; }; /* 4-6 End of new stuff */ int Getop(char s[]); void push(double val); double pop(void); void showTop(void); void duplicate(void); void swapItems(void); //void clearStack(); //void dealWithName(char s[]); /* 4-6 this is new for this program */ /* Changed clearStack(void) to clearStacks(struct varType var[])*/ void clearStacks(struct varType var[]); void dealWithName(char s[], struct varType var[]); void dealWithVar(char s[], struct varType var[]); int pos = 0; struct varType last; /* 4-6 End of new stuff */ int main(void) { int type; double op2; char s[MAXOP]; int flag = TRUE; struct varType var[MAXVARS]; clearStacks(var); while((type = Getop(s)) != EOF) { switch(type) { case NUMBER: push(atof(s)); break; case IDENTIFIER: dealWithName(s, var); break; case '+': push(pop() + pop()); break; case '*': push(pop() * pop()); break; case '-': op2 = pop(); push(pop()- op2); break; case '/': op2 = pop(); if(op2) push(pop() / op2); else printf("\nError: division by zero!"); break; case '%': op2 = pop(); if(op2) push(fmod(pop(), op2)); else printf("\nError: division by zero!"); break; case '?': showTop(); break; case '#': duplicate(); break; case '~': swapItems(); break; case '!': clearStacks(var); case '\n': printf("\n\t%.8g\n", pop()); break; /* 4-6 this is new for this program */ case ENDSTRING: break; case '=': pop(); var[pos].val = pop(); last.val = var[pos].val; push(last.val); break; case '<': printf("The last variable used was: %s (value == %g)\n", last.name, last.val); break; /* 4-6 End of new stuff */ default: printf("\nError: unknown command %s.\n", s); break; } } return EXIT_SUCCESS; } #define MAXVAL 100 int sp = 0; /* Next free stack position. */ double val[MAXVAL]; /* value stack. */ /* push: push f onto stack. */ void push(double f) { if(sp < MAXVAL) val[sp++] = f; else printf("\nError: stack full can't push %g\n", f); } /*pop: pop and return top value from stack.*/ double pop(void) { if(sp > 0) return val[--sp]; else { printf("\nError: stack empty\n"); return 0.0; } } void showTop(void) //? { double top=val[sp]; printf("%u\n",top); } void duplicate(void) //# { int top=sp-1; double topVal; topVal=val[top]; val[top+1]=topVal; sp++; } void swapItems(void) //~ { double temp; int top= sp; int secTop = sp-1; temp = val[top]; val[top]=val[secTop]; val[secTop]=temp; } /* 4-6 this is new for this program */ /* Altered to clear both the main stack and that of the variable structure */ void clearStacks(struct varType var[]) { int i; /* Clear the main stack by setting the pointer to the bottom. */ sp = 0; /* Clear the variables by setting the initial element of each name to the terminating character. */ for( i = 0; i < MAXVARS; ++i) { var[i].name[0] = '\0'; var[i].val = 0.0; } } /* a string/name may be either a maths function or a variable */ void dealWithName(char s[], struct varType var[]) { double op2; if(!strcmp(s, "sin")) push(sin(pop())); else if(!strcmp(s, "cos")) push(cos(pop())); else if (!strcmp(s, "exp")) push(exp(pop())); else if(!strcmp(s, "pow")) { op2 = pop(); push(pow(pop(), op2)); } /* Finally if it isn't one of the supported maths functions we have a variable to deal with. */ else { dealWithVar(s, var); } } /* Our identifier is not one of the supported maths function so we have to regard it as an identifier. */ void dealWithVar(char s[], struct varType var[]) { int i = 0; while(var[i].name[0] != '\0' && i < MAXVARS-1) { if(!strcmp(s, var[i].name)) { strcpy(last.name, s); last.val = var[i].val; push(var[i].val); pos = i; return; } i++; } /* variable name not found so add it */ strcpy(var[i].name, s); /* And save it to the last variable */ strcpy(last.name, s); push(var[i].val); pos = i; } /* 4-6 End of new stuff */ int getch(void); void unGetch(int); /* Getop: get next operator or numeric operand. */ int Getop(char s[]) { int i = 0; int c; int next; /* Skip whitespace */ while((s[0] = c = getch()) == ' ' || c == '\t') ; s[1] = '\0'; //如果读入的是标识符...字母 if(isalpha(c)) { i = 0; while(isalpha(s[i++] = c )) c = getch(); s[i - 1] = '\0'; if(c != EOF) unGetch(c); return IDENTIFIER; } /* Not a number but may contain a unary minus. */ if(!isdigit(c) && c != '.' && c != '-') return c; if(c == '-') { next = getch(); if(!isdigit(next) && next != '.') { return c; } c = next; } else c = getch(); while(isdigit(s[++i] = c)) c = getch(); if(c == '.') /* Collect fraction part. */ while(isdigit(s[++i] = c = getch())) ; s[i] = '\0'; if(c != EOF) unGetch(c); return NUMBER; } #define BUFSIZE 100 char buf[BUFSIZE]; int bufp = 0; /* Getch: get a ( possibly pushed back) character. */ int getch(void) { return (bufp > 0) ? buf[--bufp]: getchar(); } /* unGetch: push character back on input. */ void unGetch(int c) { if(bufp >= BUFSIZE) printf("\nUnGetch: too many characters\n"); else buf[bufp++] = c; }
发表评论
-
C程序 声明与定义的区别
2010-07-30 15:34 1303http://zhidao.baidu.com/questio ... -
C语言 函数指针 与 指针函数
2010-07-30 10:27 910指针函数是返回指针的函数,即本质是一个函数。函数返回类型是某一 ... -
C程序 命令行参数
2010-07-30 09:34 672echo.cpp #include <stdi ... -
C程序 整型数按位取反 输出结果
2010-07-29 15:16 1317#include <stdio.h> int m ... -
C程序 对文本行进行排序
2010-07-26 09:30 995#include <stdio.h> #incl ... -
C语言:malloc()函数与alloc()函数
2010-07-24 10:02 908C语言跟内存分配方式 ... -
C程序设计语言(第二版) 5-4 strend(s,t)
2010-07-23 16:29 1464strend(s,t) 如果字符串t出现在字符串s的尾部,该函 ... -
C程序设计语言(第二版) 5-3 编写strcat()
2010-07-23 16:02 799#include<stdio.h> void ... -
C程序 关于字符串的问题
2010-07-23 15:47 673#include<stdio.h> void ... -
C程序设计语言(第二版)5-2
2010-07-22 17:13 675#include <ctype.h> #incl ... -
C程序设计语言(第二版) 4-14
2010-07-22 11:47 852定义宏swap(t, x, y)以交换t类型的两个参数 ... -
C程序设计语言(第二版) 4-13 递归版本reverse()将字符串倒置输出
2010-07-22 11:18 997编写一个递归版本的reverse()函数,将字符串倒置 ... -
C程序设计语言(第二版) 4-12
2010-07-22 10:29 1022运用printd函数的设计思想编写一个递归版本的itoa函数, ... -
C程序 递归小例子 快速排序
2010-07-22 09:26 918从执行速度来讲,下列版本的快速排序可能不是最快的,但是它是最简 ... -
C程序 递归实现将数字作为字符串输出
2010-07-21 15:47 1268将一个数字作为字符串打印的情况,数字是反序生成的:低位数字先于 ... -
C语言学习之变量存储
2010-07-21 15:14 868http://zhidao.baidu.com/questio ... -
C语言外部变量的使用以及extern的用法
2010-07-21 15:11 2195http://hi.baidu.com/%C3%E2%B7%D ... -
C语言 全局变量和局部变量
2010-07-21 15:07 1427http://hi.baidu.com/%C3%E2%B7%D ... -
C语言 extern
2010-07-21 11:20 1098在头文件中声明函数时,前面的extern可有可无,只要这些函数 ... -
C程序设计语言(第二版) 4-8 最多只压回 一个字符
2010-07-20 18:47 757#include <stdio.h> i ...
相关推荐
C程序设计语言(第二版-英文版)
学习C语言的书籍 这本书就是发明C的作者写的 肯定差不了
《计算机科学丛书:C程序设计语言(第2版·新版)》讲述深入浅出,配合典型例证,通俗易懂,实用性强,适合作为大专院校计算机专业或非计算机专业的C语言教材,也可以作为从事计算机相关软硬件开发的技术人员的参考...
谭浩强--C语言程序设计(第二版)习题答案
最经典的C语言书,没有之一
《C程序设计语言<第2版新版>习题解答(原书第2版)》是对Brian W.Kernighan和Dennis M.Ritchie所著的《C程序设计语言<第2版新版>习题解答(原书第2版)》所有练习题的解答,是极佳的编程实战辅导书。K&R的著作是C语言...
C语言设计,c程序设计语言第二版,官方题解
经典的国内C语言教程,高校通用指定教程,C语言学习者可以看看
计算机C程序设计语言_第二版新版是计算机类必看的C语言的一本经典你书籍~和谭浩强的《C程序设计》属于同系列
[C语言程序设计(第二版)] (1).rar
C 程序设计语言(第2版)电子版
《C程序设计语言》是由C语言的设计者Brian W. Kernighan和Dennis M. Ritchie编写的一部介绍标准C语言及其程序设计方法的权威性经典著作。全面、系统地讲述了C语言的各个特性及程序设计的基本方法,包括基本概念、...
C程序设计--适合新手入门--算法入门--内容精简!
c语言程序设计,谭浩强编写的第二版,感觉比第三版要好些(个人认为),有书和答案,都是pdf格式的,经典中的经典,就不多说了
C语言入门圣经,两位作者参与了C语言和Unix系统的设计,权威的计算机科学家
C程序设计语言第二版《英文版》,每个学习C语言人必读的经典著作。
C程序设计语言第二版 最经典的教材 分两段 全部下载后解压
C程序设计语言第二版课后习题答案 英文版的
C程序设计语言第二版中文版,写的很详细,方便自学者使用。
C程序设计语言(第2版·新版),C程序设计语言(第2版·新版)习题解答4份电子档,其中C程序设计语言(第2版·新版)习题解答是对Brian W.Kernighan和Dennis M.Ritchie所著的《c程序设计语言第二版》所有课后练习题...