The first personal homework update

Source: Internet
Author: User

The goal of this assignment is to write a arithmetic applet.

1. Requires the ability to read the TXT format of the test, and calculate the standard answer, after the user input answers to their answers according to the standard enigmatic grading.

2. With respect to operations, support for subtraction and parentheses is required, but decimals cannot occur, and all results can be represented by integers, true fractions, and false fractions.

3. Second, on the expansion of the function, it is required to be able to generate the program itself rather than read from the TXT, and the resulting problem can not be repeated, the distribution of the problem of uniform.

In the data structure class before two semesters, we have implemented this simple synthetic calculator. So I intend to use the same idea, using the structure of the stack to store the operation symbols and values, and design the order of the algorithm organization operations, the implementation of brackets → multiplication → add and subtract priority.

In order to realize the customization of the function, I did not adopt the stack which is packaged in the library, but implemented the class of a stack by myself. The header file code is as follows:

1Template<typename t>2 classStack3 {4 Private:5S Mhead;6     intmaxSize;7     intcurrentsize;8  Public:9 Stack ();TenStack (intmsize); One~Stack (); A T Top (); -     BOOLpop (); -     BOOLpush (T N); the     BOOLClear (); -     BOOLisEmpty (); - };

Second, the main body of the program-the calculator class. The header file code is as follows:

1#include"Stack.h"2 #defineMax_length 1283 classCalculator4 {5 Private:6     DoubleNumber ;7     CharMark;8     CharQuestion[max_length];9stack<Char> cstack = stack<Char> ( the);Tenstack<Double> dstack = stack<Double> ( the); One      A     voidStoremark (Char Constmark); -     BOOLPriorto (CharInputChartop); -     voidCalculateChar Constmark); the     voidShowans (); -     DoubleGetans (); -     voidreply (); -  Public: + Calculator (); -~Calculator (); +     voidrun (); A     voidparse (); at};

As long as the above functions are clear, it is better to write the precedence of the operators. Here is the most profound aspect of this experience-file IO.

Due to the usual opportunities for C + + (do not know why Unity Support C # but not support C + +), especially the file IO this piece with less, so in this area took a lot of detours. Because our C + + is a self-taught lesson, and the file IO is not the focus of the exam, so my file IO approach is still in the C language stage. But when I think about all the problems that were caused by the pointer in the file and the feof to scan for this unpleasant experience, I decided to try a new approach. On the Internet to see, think ifstream this goods good, can be read to simplify the file as iostream in the CIN and cout, such high-forcing lattice method cannot learn.

In fact, it is relatively simple to use, the main advantage is the putback () the same as the bug function, who also need a character a character of their own back Ah, a function to do. Here's My Code:

1 voidCalculator::p arse ()2 {3Ifstream myfile = Ifstream ("MyFile.txt");4Ifstream myfile_temp = Ifstream ("MyFile.txt");5     if(!myfile) {6cout <<"Unable to open myfile";7System"Pause");8     }9     if(!myfile_temp) {Tencout <<"Unable to open myfile"; OneSystem"Pause"); A     } -      while(!myfile.eof ())//EOF loops at the end of the file more than once -     { theMyFile >>Mark; -         Switch(Mark) -         { -          Case '+': +          Case '-': -          Case '*': +          Case '/': A          Case '(': at          Case ')': - Storemark (Mark); -              Break; -          Case '=': -Storemark ('#'); - Myfile_temp.getline (question, max_length); incout << Question <<Endl; -             //////////////////////////////add input and output here////////////////////////////// to reply (); +             //////////////////////////////////////////////////////////////////////////// -             //Showans (); the              Break; *          Case '0': $          Case '1':Panax Notoginseng          Case '2': -          Case '3': the          Case '4': +          Case '5': A          Case '6': the          Case '7': +          Case '8': -          Case '9': $ Myfile.putback (Mark); $MyFile >>Number ; - Dstack.push (number); -              Break; the         default: -              Break;Wuyi         } theMark ='!';//change to characters that do not work, preventing repeated parsing of characters -     } Wu myfile.close (); - myfile_temp.close (); About}

Finally attached to my program the current effect, not to make scores and automatic question, but also will be made out soon ~

The first personal homework update

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.