计算器的Golang实现， 支持 +-*/，并会判断表达式是否合法package code import ( "strconv" "unicode" ) type Calculator struct { origin string s string left int signMap map[byte]bool } func NewCalculator(s string) Calculator { signMap := map[byte]bool{ '+': true, '-': true, '*': true, '/': true, '(': true, ')': true, ' ': true, } return Calculator{ origin: s, s: s, left: 0, signMap: signMap, } } func (cal *Calculator) Execuate() int { stack := NewStack() num := 0 numFlag := false emptyFlag := false sign := '+' //往前遍历字符串 for len(cal.s) > 0 { c := cal.s[0] cal.s = cal.s[1:] if c == ' ' { emptyFlag = true continue } //判断是不是数字，考虑到连续累加 if unicode.IsDigit(rune(c)) { if numFlag { //数字之间不能有空格 if emptyFlag { panic(cal.origin + ": express error, empty can not between in numbers") } } else { numFlag = true } num = num*10 + (int(c) - int('0')) } else { numFlag = false if _, ok := cal.signMap[c]; !ok { panic(cal.origin + ": express error, invaild sign:" + string(c)) } } emptyFlag = false //如果是(，递归处理，直到 碰到）弹出，结果返回 if c == '(' { cal.left++ num = cal.Execuate() } //是操作符，或者到了字符串最后，进行sush操作 if len(cal.s) == 0 || !unicode.IsDigit(rune(c)) { switch sign { case '+': //直接入stack stack.Push(num) case '-': //变成负数，再入stack num = 0 - num stack.Push(num) case '*': //取出statck的top元素并弹出，相乘，再入stack v, _ := stack.Peek() stack.Pop() stack.Push(num * v) case '/': //取出statck的top元素并弹出，相乘，再入stack v, _ := stack.Peek() stack.Pop() stack.Push(v / num) } sign = rune(c) num = 0 numFlag = false } if c == ')' { cal.left-- if cal.left < 0 { panic(cal.origin + ": express error, more )") } break } } if len(cal.s) == 0 && cal.left > 0 { panic(cal.origin + ":" + cal.s + ": express error, more (: " + strconv.Itoa(cal.left)) } //statck求和，返回 return stack.Sum() }
While John Gilbert was in hospital, he asked his doctor to tell him about his operation has been successful, but the doctor refused to do so.The following day, the patient asked for a bedside telephone.When he was alone, he telephoned the hospital exchange and asked for Doctor Millington.When the doctor answered the phone, Mr. Gilbert said he was inquiring about a certain patient, a Mr. John Gilbert.He asked if Mr. Gilbert's operation had been successful and the doctor told him that it had been.He then asked when Mr. Gilbert would be allowed to go home and the doctor told him that he would have to stay in hospital for another two weeks.Then Dr. Millington asked the caller if he was a relative of the patient."No", the patient answered, "I am Mr. John Gilbert."
My old friend, Harrison, had lived in the Mediterranean for many years before he returned to England.He had often dreamed of retiring in England and had planned to settle down in the country.He had no sooner returned than he bought a house and went to live there.Almost immediately, he began to complain about the weather.For even though, it was still summer, it rained continually and it was often bitterly cold.After so many years of sunshine, he got a shock.He acted as if he had never lived in England before.In the end, it was more than he could bear.He had hardly had time to settle down when he sold the house and left the country.The dream had had for so many years ended there.Harrison had thought of everything except the weather.
The Olympic Games will be held in our country in four years' time.As a great many people will be visiting the country, the government will be building new hotels, an immense stadium, and a new "Olympic-Standard" swimming pool.They will also be building new roads and a special railway line.The Games will be held just outside the capital and the whole area will be called "Olympic City".Workers will have completed the new roads by the end of this year.By the end of next year, they will have finished work on the new stadium.The fantastic modern buildings have been designed by Kurt Gunter.Everybody will be watching anxiously as the buildings go up.we are all excited and are looking forward to the Olympic Games because they had never been held before in this country.
Debbie Hart is going to swim the English Channel tomorrow.She is going to set out from the French coast at five o'clock in the morning.Debbie is only eleven years old and she hopes to set up a new world record.She is a strong swimmer and many people feel that she is sure to succeed.Debbie's father will set out with her in a small boat.Mr.Hart has trained his daughter for years.Tomorrow he will be watching her anxiously as she swims the long distance to England.Debbie intends to take short rests every two hours.She will have something to drink but she will not eat any solid food.Most of Debbie's school friends will be waiting for her on the English coast.Among them will be Debbie's mother, who swam the Channel herself when she was a girl.