查看: 1620|回复: 17
|
C programming coding 问题
[复制链接]
|
|
我run不到,有谁可以帮我看哪里出错?
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#include<ctype.h>
#include<string.h>
struct stackNode {
int data;
struct stackNode *nextPtr;
};
typedef struct stackNode StackNode;
typedef StackNode *StackNodePtr;
int evaluatePostfixExpression( char *expr );
int calculate( int op1, int op2, char Operator );
void push( StackNodePtr *topPtr, int value );
int pop( StackNodePtr *topPtr );
int isEmpty( StackNodePtr topPtr );
void printStack( StackNodePtr topPtr );
char postfix[50];
int answer;
void main()
{
printf("Print an postfix expression\n");
scanf("%s", postfix);
evaluatePostfixExpression(postfix);
printf("The value of the expression is: %i",answer);
}
int evaluatePostfixExpression( char *expr )
//Evaluate the postfix expression.
{
StackNode node;
StackNodePtr ptrnode;
ptrnode = &node;
int x;
int y;
int z;
strcat( expr , '\0' );/*Append the null character ('\0') to the end of the postfix expression.
When the null character is encountered, no further processing is necessary.*/
int i=0;
for(i; postfix[i]!='\0'; i++){ //While '\0' has not been encountered, read the expression from left to right.
if(isdigit(postfix[i])){ //If the current character is a digit,Push its integer value onto the stack
push(&ptrnode, postfix[i]); //(the integer value of a digit character is its value in the computer's character
printStack(ptrnode); //set minus the value of '0' in the computer's character set).
}
else if(postfix[i]=='+'||postfix[i]=='-'||postfix[i]=='*'||postfix[i]=='/'||postfix[i]=='^'){ //Otherwise, if the current character is an operator, Pop the two top elements of the stack into
x=pop(&ptrnode); //variables x and y. Calculate y operator x.
printStack(ptrnode);
y=pop(&ptrnode);
printStack(ptrnode);
z=calculate(x,y,postfix[i]);
push(&ptrnode, z); /*Push the result of the calculation onto the stack.*/
printStack(ptrnode);
}
if (postfix[i]=='\0'){ //When the null character is encountered in the expression, pop the top value of the
answer = pop(&ptrnode);//stack. This is the result of the postfix expression.
printStack(ptrnode);
}
}
}
int calculate( int op1, int op2, char Operator )
//Evaluate the expression op1 operator op2.
{
if (Operator=='+')
return op1+op2;
else if (Operator=='-')
return op1-op2;
else if (Operator=='*')
return op1*op2;
else if (Operator=='/')
return op1/op2;
else if (Operator=='^')
return op1^op2;
else{
return printf("calculation error");
}
}
void push( StackNodePtr *topPtr, int value )
//Push a value on the stack.
{
StackNodePtr temp; /* to create a new node */
temp = (StackNodePtr) malloc(sizeof(value));//need Malloc because it will not remember it
temp->data = value;
temp->nextPtr = NULL; /* the new node points to NULL */
if(isEmpty(*topPtr)==0) {
temp->nextPtr = *topPtr;
}
}
int pop( StackNodePtr *topPtr )
//Pop a value off the stack.
{
char Data ; /* to be used to store the data */
StackNodePtr tmp; /* to be used for handling the node*/
/* that is going to be deleted */
tmp = *topPtr; /* tmp has the address of the node */
/* that is going to be deleted */
Data = tmp->data;
*topPtr = tmp->nextPtr;
free(tmp);
return Data;
}
int isEmpty( StackNodePtr topPtr )
//Determine if the stack is empty.
{
if(topPtr->nextPtr==NULL)
return 1;
else
return 0;
}
void printStack( StackNodePtr topPtr )
//Print the stack.
{
while ((topPtr->nextPtr)!=NULL){
printf("%C",topPtr->data);
}
if ((topPtr->nextPtr)==NULL)
printf("NULL");
printStack(topPtr->nextPtr);
}
|
|
|
|
|
|
|
|
发表于 22-5-2013 02:31 PM
|
显示全部楼层
你至少应该描述一下出现什么症状,有什么Error Message吧?你这样子永远得不到答案的,没人有义务给你当人肉电脑的。
还有,请善用代码模式。 |
|
|
|
|
|
|
|

楼主 |
发表于 22-5-2013 06:56 PM
|
显示全部楼层
geekman 发表于 22-5-2013 02:31 PM 
你至少应该描述一下出现什么症状,有什么Error Message吧?你这样子永远得不到答案的,没人有义务给你当人肉 ...
这个 function 有问题 int evaluatePostfixExpression( char *expr );
代码模式去哪里弄?
|
|
|
|
|
|
|
|
发表于 22-5-2013 07:05 PM
|
显示全部楼层
代码模式:在发帖时注意看上面的图标,<>的就是代码模式。
有问题?我还有答案咧!将Error Message写出来啊。啊不然你想别人怎么帮你哦?没有Error Message的话就描述症状,为什么你认为那个Function有错?没有任何线索之下,照我说,每一行都有嫌疑,干脆整个Fuction拖出去砍头,删了算了。.gif)
|
|
|
|
|
|
|
|

楼主 |
发表于 22-5-2013 08:37 PM
|
显示全部楼层
geekman 发表于 22-5-2013 07:05 PM 
代码模式:在发帖时注意看上面的图标,的就是代码模式。
有问题?我还有答案咧!将Error Message写出来啊 ...
Postfix.cpp(46) : error C2664: 'strcat' : cannot convert parameter 1 from 'struct stackNode *' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
谢谢指导,我看到那个《》了
|
|
|
|
|
|
|
|
发表于 22-5-2013 09:01 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 22-5-2013 09:14 PM
|
显示全部楼层
莲花 发表于 22-5-2013 08:37 PM 
Postfix.cpp(46) : error C2664: 'strcat' : cannot convert parameter 1 from 'struct stackNode *' to ...
问题出在这行:- strcat( expr , '\0' );/*Append the null character ('\0') to the end of the postfix expression.
复制代码 Postfix.cpp(46) : error C2664: 'strcat' : cannot convert parameter 1 from 'struct stackNode *' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
你确定你打出来的代码是你send给Compiler的?这里说你的strcat的第一个Argument错误,strcat要的是两个char *,而你提供给它的第一个Argument 是 struct stackNode * 类型的。可是你post出来的代码的确是char *。你确定你的代码是原装货???.gif)
|
|
|
|
|
|
|
|

楼主 |
发表于 22-5-2013 09:20 PM
|
显示全部楼层
geekman 发表于 22-5-2013 09:01 PM 
哎呀呀一看到我青筋暴涨啊。第一件事:请慎用Global Variable!能够不用Global Variable就尽量别用, ...
不止你晕,我也晕。。。
这个是功课来的,我抄朋友的,但是就是run不到,所以才厚着脸皮来问~~
|
|
|
|
|
|
|
|

楼主 |
发表于 22-5-2013 09:30 PM
|
显示全部楼层
给我一天的时间思考,明晚才回你,我自己也搞不清楚状态~~ |
|
|
|
|
|
|
|
发表于 22-5-2013 10:38 PM
|
显示全部楼层
这是我写的,base on Single Directional Linked List 的 Stack (使用Visual C++ 2010 Express):Stack并不是那么简单的。 本帖最后由 geekman 于 22-5-2013 10:40 PM 编辑
|
|
|
|
|
|
|
|

楼主 |
发表于 24-5-2013 12:07 AM
|
显示全部楼层
geekman 发表于 22-5-2013 10:38 PM 
这是我写的,base on Single Directional Linked List 的 Stack (使用Visual C++ 2010 Express):Stack并 ...
谢了~不过这功课有指定用的function~就是这些指定的~它只给这些function name~剩下的要自己加进去~- struct stackNode {
- int data;
- struct stackNode *nextPtr;
- };
- typedef struct stackNode StackNode;
- typedef StackNode *StackNodePtr;
- int evaluatePostfixExpression( char *expr );
- int calculate( int op1, int op2, char Operator );
- void push( StackNodePtr *topPtr, int value );
- int pop( StackNodePtr *topPtr );
- int isEmpty( StackNodePtr topPtr );
- void printStack( StackNodePtr topPtr );
复制代码 |
|
|
|
|
|
|
|
发表于 24-5-2013 07:28 AM
|
显示全部楼层
莲花 发表于 24-5-2013 12:07 AM 
谢了~不过这功课有指定用的function~就是这些指定的~它只给这些function name~剩下的要自己加进去~
我只是告诉你stack该怎么做,功课还是得你自己做的。这次做文抄公,抄出个大头佛来,应该得到教训了吧?要抄也该找个懂的人来抄啊。 |
|
|
|
|
|
|
|
发表于 24-5-2013 08:39 AM
|
显示全部楼层
其实,仔细想想,Stack应该反过来做才有效率,Node里面的Pointer应该Point to Previous Node 才是更有效率的做法。固有观念害死人啊。 |
|
|
|
|
|
|
|

楼主 |
发表于 31-5-2013 02:12 AM
|
显示全部楼层
- #include <stdlib.h> //malloc()
- #include <stdio.h> //for printf(), scanf()
- #include <string.h> //for strlen()
- #include <ctype.h> //for toupper()
- #include <io.h> //for fflush()
- #include <math.h> //for mathematical function
- #define MAX 30//set a global variable for maximum
- ////////////Prototypes////////////
- int evaluatePostfixExpression (char expr[], struct stackNode **topPtr); //////////////////////////////////
- int calculate (struct stackNode **topPtr, char Operator); //////////////////////////////////
- void push (struct stackNode **topPtr, int); //////////////////////////////////
- int pop (struct stackNode **topPtr);//////////////////////////////////
- int is_empty (struct stackNode *topPtr); //////////////////////////////////
- struct stackNode { //structure use
- int data;
- struct stackNode *nextPtr;
- };
- int status = 1;
- void main (void)
- {
- struct stackNode *stack = NULL; //pointer to node for stacking
- char postfix[MAX]; //array for input
- int answer; //answer to be displayed
- printf("\n\n\nEnter a less than %d character postfix expression without spacing>>\n", MAX); //display message to ask user key in postfix expression
- gets(postfix); //input string
- answer = evaluatePostfixExpression(postfix, &stack);//evaluate expression & return answer
- if(status==1)
- {
- printf("\nThe answer to the postfix expression is:\n"); //display message for user
- printf("%s = %.1d\n", postfix, answer); //print output
- }
- }
- /////////// end main() ////////////////
- int evaluatePostfixExpression (char expr[], struct stackNode **topPtr) //function
- {
- int val, answer; //variables to be pushed onto stack
- int length, i; //misc variables
- length = strlen(expr); //find size of string
- for (i = 0; i < length && status > 0; ++i)
- {
- if (isdigit(expr[i])) //if operand push onto stack
- {
- val = (int)(expr[i] - '0'); //cast character as float
- push(topPtr, val); //push onto stack
- }
- else if(expr[i] =='+'||expr[i] =='-'||expr[i] =='*'||expr[i] =='/'||expr[i] =='%'||expr[i] =='^') //if math crunch out answer
- {
- answer = calculate(topPtr, expr[i]); //call func to evaluate math expr
- push(topPtr, answer); //push answer back on stack
- }
- else //if bad set ok_so_far = 0
- {
- printf("\nWrong value or operator entered in postfix expression.");
- status = 0; //if bad value clear status
- }
- }
- if(status == 0)
- printf("\nPlease restart program\n");
- return (answer);
- }
- ////////////end evaluatePostfixExpression/////////////
- void push(struct stackNode **topPtr, int pushval)
- {
- struct stackNode *ptr; //pointer for new struct
- ptr = (struct stackNode *) malloc(sizeof(struct stackNode)); //ptr is pointer to memory designated for a struct node
- if(ptr == (struct stackNode *) NULL) //if malloc returns NULL
- {
- printf("ERROR! Unable to allocate memory\n");
- free(ptr); //return 0 so calling function knows error occurred
- } //end if
- else
- {
- ptr->nextPtr = *topPtr; //point new item's link to previous node
- ptr->data = pushval; //assign new value into new node
- *topPtr = ptr; //point nodeptr to new item in list
- } //end else
- }
- ////////////end push/////////////////
- int pop (struct stackNode **topPtr)
- {
- int ans;
- struct stackNode *temp;
- if (is_empty(*topPtr)== 1) //if stack is empty display error message
- {
- printf("\nERROR! Attempt to pop an empty stack quiting program");
- exit(1);
- } //end if
- else
- {
- ans = (*topPtr)->data;
- temp = *topPtr;
- *topPtr = (*topPtr)->nextPtr;
- free(temp); //release popped node's memory back to OS
- } //end else
- return(ans); //return the answer
- }
- ////////////end pop/////////////
- int is_empty (struct stackNode *topPtr )
- {
- if (topPtr == NULL) //if stack points to NULL there are no nodes
- return(1);
- return(0);
- }
- /////////////end is_empty//////////
- int calculate (struct stackNode **topPtr, char Operator)
- {
- int op1, op2; //operands 1 and 2
- int answer; //answer
- op2 = pop(topPtr); //pop operand 2 from stack
- if(op2 == -1)
- exit(1);
- op1 = pop(topPtr); //pop operand 1 from stack
- switch(Operator) //begin switch section
- {
- case '+': //if operator is PLUS
- answer = (op1 + op2);
- break;
- case '-': //if operator is MINUS
- answer = (op1 - op2);
- break;
- case '*': //if operator is MUTLIPLE
- answer = (op1 * op2);
- break;
- case '/': //if operator is DIVIDE
- if (op2 == 0) //if op2 is 0 display the error message
- {
- printf("ERROR! Attempt to divide by zero");
- exit(1);
- }
- else answer = (op1 / op2);
- break;
- case '%': //if the operator is MODULUS
- answer = (op1 % op2);
- break;
- case '^': //if the operator is POWER
- answer = (pow(op1,op2));
- break;
- } //end switch section
- return (answer); //return answer to calling function
- }
- ////////////end calculate///////////
复制代码 本帖最后由 莲花 于 1-6-2013 11:13 AM 编辑
|
|
|
|
|
|
|
|
发表于 31-5-2013 10:17 AM
|
显示全部楼层
莲花 发表于 31-5-2013 02:12 AM 
How may I help you?
|
|
|
|
|
|
|
|

楼主 |
发表于 1-6-2013 11:04 AM
|
显示全部楼层
geekman 发表于 31-5-2013 10:17 AM 
How may I help you?
只是想要跟你讲我解决了,谢谢你一直回复我~~
|
|
|
|
|
|
|
|

楼主 |
发表于 1-6-2013 11:05 AM
|
显示全部楼层
SotongJiang 发表于 31-5-2013 10:58 PM 
geekman的耐心真好,循循善诱,希望莲花能明白你的好意。
我学校的老师都没有他酱好心咯~~
|
|
|
|
|
|
|
|
发表于 1-6-2013 12:00 PM
|
显示全部楼层
我以前求学时也曾被不负责任的Programming老师深深地伤害过单纯幼嫩的心灵,所以并不想让现在的幼苗们受到我曾遭遇的无助的困境。.gif)
只是我的言论有时会尖酸刻薄了点,希望大家别介意啊哈,毕竟本性难移啊。.gif) |
|
|
|
|
|
|
| |
本周最热论坛帖子
|