【问题描述】
1011
1111
1110
1110
2
3
1
给出一个年份year,请你设计一个方案,使得镇政府从该年起的n年内总支出最小,注意考虑闰年情况。
注意,村民只能进入停靠点而不能直接进入车站,但允许在车站处设置停靠点。
【输入】
第一行四个数: xa ya xb yb
第二行四个数: xc yc xd yd
第三行四个数: m n t p (0<=3000,0<=10)
以上数字,m,year,n为正整数,p,t为正实数,其余均为实数。
第二行设置的停靠点数N(N为正整数)
以下N行,每行两个实数,代表停靠点的坐标
如有多解,任意输出一解即可。
所有实数保留四位小数。
比如:
要求:做一个带括号的整数的四则混合算法。
思路:用堆栈实现。(我说的不详细,说详细的说不出来,太菜)
代码:下一页
---------------------------------------------------------------
主要确立括号优先运算的问题
输入 a*((b+c)*(d+e)) 确定三个括号的优先运算
定义优先及 高则 递增
主要在于解析 给内嵌更高的运算级别
出现一个括号记录括号数目及优先及 如上面的 括号数 1 优先及为1 括号数2,3 优先及2
定义括号计算结果变量 解吸完毕后 再根据优先及运算的出最终结果
这是俺以前写的,用的是c++
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
double GetVal(string s)
{
do
{
string::size_type idxM = s.find_first_of("*/");
if (idxM != s.npos)
{
string::size_type idxL;
string::size_type idxR;
if (idxM != 0 && idxM != s.size() - 1)
{
idxL = s.find_last_of("+-/*", idxM - 1);
idxR = s.find_first_of("+-/*", idxM + 1);
}
else if (idxM == 0)
{
idxL = s.npos;
}
else
{
idxR = s.npos;
}
string::size_type L;
string::size_type R;
if (idxL == s.npos) {L = 0;}
else {L = idxL + 1;}
if (idxR == s.npos) {R = s.size();}
else {R = idxR;}
double d1 = atof(string(s, L, idxM - L).c_str());
double d2 = atof(string(s, idxM + 1, R - idxM - 1).c_str());
double d3;
switch(s.at(idxM))
{
case '*':
d3 = d1 * d2;
break;
case '/':
d3 = d1 / d2;
break;
default:
cout << "error";
}
char buffer[25];
s.replace(L, R - L, _gcvt(d3, 12, buffer));
GetVal(s);
}
else if ((idxM = s.find_first_of("+-")) != s.npos)
{
string::size_type idxL;
string::size_type idxR;
if (idxM != 0 && idxM != s.size() - 1)
{
idxL = s.find_last_of("+-", idxM - 1);
idxR = s.find_first_of("+-", idxM + 1);
}
else if (idxM == 0)
{
idxL = s.npos;
}
else
{
idxR = s.npos;
}
string::size_type L;
string::size_type R;
if (idxL == s.npos) {L = 0;}
else {L = idxL + 1;}
if (idxR == s.npos) {R = s.size();}
else {R = idxR;}
double d1 = atof(string(s, L, idxM - L).c_str());
double d2 = atof(string(s, idxM + 1, R - idxM - 1).c_str());
double d3;
switch(s.at(idxM))
{
case '+':
d3 = d1 + d2;
break;
case '-':
d3 = d1 - d2;
break;
default:
cout << "error";
}
char buffer[25];
s.replace(L, R - L, _gcvt(d3, 12, buffer));
}
else
{
break;
}
} while(1);
return atof(s.c_str());
}
double DealWithBracketExpr(string str)
{
do
{
string::size_type idxR = str.find_first_of(')');
if (idxR == str.npos){break;}
string::size_type idxL = str.find_last_of('(', idxR);
string strTemp(str, idxL, idxR - idxL + 1);
strTemp.erase(remove(strTemp.begin(), strTemp.end(),
'('),
strTemp.end());
strTemp.erase(remove(strTemp.begin(), strTemp.end(),
')'),
strTemp.end());
double d = GetVal(strTemp);
char buffer[25];
str.replace(idxL, idxR - idxL + 1, _gcvt(d, 12, buffer));
} while(1);
return atof(str.c_str());
}
int main()
{
//string str("(1.23+23)*45+34/34-56.9");
cout << "Enter your expression:" << endl;
string str;
cin >> str;
str.insert(str.begin(), '(');
str.insert(str.end(), ')');
cout << DealWithBracketExpr(str) << endl;
return 0;
}
---------------------------------------------------------------
算术表达式存放在expression.dat中。每个算术表达式用“;”号结束。如:
-1+3+5/5*2;
5+(-36);
1+(-8-3);
1+8/2;
6-5;
(-1+2)*3+(8-7)-1*(2+5);
(1+6*8+9-2)*2+8-4;
6/(3-3);
((1-2)/3+4;
该程序具有四则运算功能,并且能够判别简单的错误。如:括号不匹配、多余运算符、除数不能等于0等。
代码:
==============================
“oper.h”
struct StackNode;
typedef struct StackNode *PtrToNode;
typedef PtrToNode Stack;
Stack CreatStack(void);
void Push(int x,Stack S);
int Top(Stack S);
int IsEmpty(Stack S);
void Pop(Stack S);
void Scaner(void);
void getch(void);
void getbc(void);
void retract(void);
int digit(char ch);
void concat(void);
void Operate(void);
void MakeEmpty(Stack S);
void CheckErr(void);
---------------------------------------------------------------
代码 :
==========================
"main.c"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "oper.h"
FILE *fp;
Stack NumStack,OpStack;
char ch;
char token[20];
int flag=2; /*当flag=2时,表示输入的'-'号当作负号*/
struct StackNode
{ int Element;
PtrToNode next;
};
int main(int argc, char *argv[])
{
if((fp=fopen("expression.dat","r"))==NULL)
{
printf("Can't open this file/n");
system("pause");
exit(0);
}
NumStack=CreatStack();
OpStack=CreatStack();
while(!feof(fp))
Scaner();
fclose(fp);
system("PAUSE");
return 0;
}
Stack CreatStack(void)
{
Stack S;
S=(Stack)malloc(sizeof(struct StackNode));
if(S==NULL)
{printf("Out of space !!");exit(1);}
S->next=NULL;
return S;
}
void Push(int x,Stack S)
{
PtrToNode TmpCell;
TmpCell=(PtrToNode)malloc(sizeof(struct StackNode));
if (TmpCell==NULL)
{printf("Out of space !!");exit(1);}
else
{
TmpCell->Element=x;
TmpCell->next=S->next;
S->next=TmpCell;
}
}
int Top(Stack S)
{
if(IsEmpty(S))
{
printf("表达式有错!! ");
CheckErr();
exit(1);
}
else
return S->next->Element;
}
int IsEmpty(Stack S)
{
return S->next==NULL;
}
void Pop(Stack S)
{
PtrToNode FirstCell;
if(IsEmpty(S))
{printf("The stack is empty !!/n");exit(1);}
else
{
FirstCell=S->next;
S->next=S->next->next;
free(FirstCell);
}
}
void MakeEmpty(Stack S)
{
if(!IsEmpty(S))
S->next=NULL;
}
void getch() /*从缓冲区读入一字符*/
{
ch=fgetc(fp);
}
void getbc() /*若ch中是空白字符,则不停调用getch()直到读入的不是空白字符为止*/
{
while (ch==' ')
getch();
}
void retract() /*指针回退一个字符*/
{
fseek(fp,ftell(fp)-1L,0);
}
int digit(char ch) /*判断ch中的是否是数字*/
{
return isdigit((int)ch);
}
void concat() /*将ch中的字符连接到token的后面*/
{
unsigned int i;
i=strlen(token);
token[i]=ch;
token[i+1]='/0';
}
void Operate(void)
{
int a,b;
a=Top(NumStack);
Pop(NumStack);
b=Top(NumStack);
Pop(NumStack);
switch(Top(OpStack))
{
case'*':Push(b*a,NumStack);break;
case'/':if(a==0) {printf("除数不能为零!!");system("pause");exit(1);}
Push(b/a,NumStack);break;
case'-':Push(b-a,NumStack);break;
case'+':Push(b+a,NumStack);break;
default:printf("the operation is error ! /n");
}
Pop(OpStack);
}
void CheckErr()
{
if (!IsEmpty(OpStack))
{
if(Top(OpStack)=='(') printf("括号不匹配!/n");
else printf("有多余运算符!/n");
}
else
printf("未知错误!/n");
}