zoukankan      html  css  js  c++  java
  • 程序设计第三次作业--C++计算器初始部分

    面向对象程序设计作业3--C++计算器初始部分

    Github 链接:https://github.com/luojingzhao/object-oriented/tree/master/calculate

    本次作业要求

    因为是第一次遇到关于C++的题目,当第一次看到这个作业的时候,整个人就是蒙了。自己学习C++刚刚学到Scan,本来对Scan就没什么深刻的概念。而作业却更深的要求我们要使用队列的模板函数。在班群上有同学问慕课网的哪些课程要学完才能完成作业,于是我跟着学长的提示,完成了未看完的相关视频。于是,我尝试着进行了第一次尝试。


    尝试

    首先在使用DEV-C++建立project的时候就遇到了困难,这让我费了一下午的时间。

    一开始不懂的,一直选择Windows Application,然后将慕课网上的代码示例敲进去根本运行不了。在请教了同样学计算机的表哥后,才发现自己愚蠢的错误。

    克服了第一个难关后,我花了几天的时间写好了代码:

    代码

    main.cpp

    #include <iostream>
    #include<stdlib.h>
    #include<string>
    #include<queue>
    #include"calculate.h"
    using namespace std;
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    
    int main(int argc, char** argv) 
    {
        Dispose *p=new Dispose();  //申请内存 
        Print *t=new Print();     //申请内存 
        string _input, strGetInputRet;
        cin>>_input;
        p->setinput(_input);
        strGetInputRet = p->getinput();
        p->ToStringQueue(strGetInputRet);
        t->output();
        delete p;  //释放内存 ,并使其指向空指针 
        p=NULL;
        delete t;
        t=NULL;
        return 0;
    }
    

    calculate.cpp

    #include<iostream>
    #include<stdlib.h>
    #include<string>
    #include<queue>
    #include"calculate.h"
    using namespace std;
    
    queue<string>data; //定义队列的类型并实例化 
    string str;  //实例化string类型 
    int count;   //用来数输入的数的位数 
    
    void Dispose::setinput(string &_input)
    {
        input=_input;
    }
    
    string Dispose::getinput()
    {
        return input;
    }
    
    void Dispose::ToStringQueue(string &input)
    {
        int n=input.length();
        int i;
        for(i=0;i<n;i++)
        {
    	    if(count>10)
    	    {
    		cout<<"Error"<<endl;
    		break;
    	    }
    	    else if(input[i]=='+'||input[i]=='-'||input[i]=='*'
    	                         ||input[i]=='/'||input[i]=='='||input[i]=='('||input[i]==')')
    	    {
    		    count=0;  //将位数归零 
    		    data.push(str);  //将之前的数存入 
    		    str.clear();
    		    str=input[i];   //将符号再次存入 
    		    data.push(str);
    		    str.clear();
    	    }
    	    else if(count<=10)
    	    {
    		    count++;  //计数		
    		    str+=input[i];
    	    }
        }
    
        if(count<=10)
        {
        	    data.push(str);  //处理若末尾是未超过的数的输出 
    	    str.clear();
        }
    }
    
    void Print::output()
    {
        if(count<=10)
        {
    	    while(data.empty()==0)
           {
    	       cout<<data.front()<<endl;
    	       data.pop();
          }
        }
    }
    

    calculate.h

    #include<iostream>
    #include<stdlib.h>
    #include<string>
    #include<queue>
    using namespace std;
    
    class Dispose
    {
        public:
    	    void setinput(string &_input); //函数的封装 
    	    string getinput();
    	    void ToStringQueue(string &input); //主要的处理函数 
        private:    
    	    string input;
    };
    
    class Print
    {
        public:
    	    void output();
    };
    

    遇到的困难

    <1>首先对queue的模板函数并不是很了解,于是我查找了网上相关的资料:C++ STL--stack/queue 的使用方法百度百科--queue,大概掌握了queue的基本的用法。
    <2>其次在对string的使用上只局限于慕课网上的string字符的相加啊,并不了解string中函数的用法。于是在我开始的void Dispose::ToStringQueue(string &input)的代码书写上想不到用什么来进行对作业要求“当输入的数字超过10位(包括小数位)时,报错”进条件限制。询问了同学和表哥后,发现string函数可以向数组那样挨个地访问。我也查找了网上的相关的资料
    <3> if(count<=10) { data.push(str); //处理若末尾是未超过的数的输出 str.clear(); }
    这个是处理判断尾数若不是运算符并且未超过10位的数的代码。在我第一次写代码的时候,并没有注意到要考虑这样的情况。但在输入数据100*2=200的时候,运行的时候没有打印出200

    于是我查找了代码,发现在最后一次的count位数的时候,就忘记了输出最后一位数。

    体会和感受

    从一个什么也不懂的c++小白,到现在的略懂一点的小白。(哈哈,我也只能这样形容自己,毕竟自己的基础和功底都是比较差的)从视频中学习到现在的实践,三次的作业练习让我收获了很多。也是这样也才让我强迫着自己去学习点东西,而不是无所事事的过完这个假期。一次次的挑战和一次次完成作业的欣喜,也让我喜欢上了计算机这个专业。我想,当一个人能够持之以恒地去干一些事情,并从中收获写什么,这才是学习或者工作的乐趣吧。

  • 相关阅读:
    Python成长笔记
    Python成长笔记
    Python成长笔记
    Python成长笔记
    Python成长笔记
    Python成长笔记
    Python成长笔记
    Python成长笔记
    Python成长笔记
    解决Jenkins生成测试报告的问题
  • 原文地址:https://www.cnblogs.com/fzuljz/p/5215341.html
Copyright © 2011-2022 走看看