数字表达式求值程序 (c/c++)
一个控制台下的数字表达式求值程序 (c/c++)
源代码见下:
#include <stdio.h>
#include <string>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <stack>
using namespace std;
//设置运算符优先级的算法
int Priority(const string opera) // 运算符优先级
{
if(opera=="+"||opera=="-")
{
return (1);
}
else if(opera=="*"||opera=="/")
{
return (2);
}
else
{
return (0);
}
}
void MiddlefixToPostfix(vector<string> &ivec1,vector<string> &ivec2)
//中缀表达式到后缀表达式的转换算法,ivec2中存放的是中缀表达式,将其转换为后缀表达式形式存放到ivec2中。
{
//定义一个存放操作符的栈对象。
stack<string> operatorstk;
for(vector<string>::size_type i=0;i!=ivec2.size();++i)
{
if(ivec2[i]=="(")
{
operatorstk.push(ivec2[i]);
}
else if(ivec2[i]=="+"||ivec2[i]=="-"||ivec2[i]=="*"||ivec2[i]=="/")
{
if(operatorstk.empty())
{
operatorstk.push(ivec2[i]);
}
else
{
if(Priority(ivec2[i])<=Priority(operatorstk.top()))
{
while( operatorstk.empty()==false && operatorstk.top()!="(" )
{
ivec1.push_back(operatorstk.top());
operatorstk.pop();
}
operatorstk.push(ivec2[i]);
}
else
{
相关文档:
http://man.lupaworld.com/content/develop/c&c++/c/c.htm
1. 如果参数是指针,且仅作输入用,则应在类型前加const,以防止该指针在函数体内被意外修改
2. 在函数体的“入口处”,对参数的有效性进行检查
在函数体的“出口处”,对return语句的正确性和效率进行检 ......
一、c++ 调C:
/* c语言头文件:cExample.h */
#ifndef C_EXAMPLE_H
#define C_EXAMPLE_H
#ifdef __cplusplus
extern "C"
{
#endif
int add(int x,int y);
#ifdef __cplusplus
}
#endif
#endif
/* c语言实现文件:cExample.c */
#include "cExample.h"
int add( int x, int y )
{
return ......
c++库文件中的符号的含义:
所有的符号都是以下划线加上大写字母也就是"_Z"开头,对于在 类里或者命名空间中的符号,后面紧跟"N",然后是各个命名空间和类的名字,每个名字前是名字字符串的长度,随后是大写字母"E",对于一个函数,他的参数列表都在E后面, ......
问题源于论坛的一道题目:
http://topic.csdn.net/u/20100216/21/ec98464e-a47e-4263-bb1c-a001e130ba87.html
下面是问题:
设int arr[]={6,7,8,9,10};
int *ptr=arr;
*(ptr++)+=123;
printf("%d,%d",*ptr,*(++ptr));
答案为什么是:8,8
问题的焦点也落在printf执行的问题上,到底先执行谁,后执行谁, 还有部分 ......