| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1334 人关注过本帖
标题:请教如何评价一个程序好坏?
只看楼主 加入收藏
moox
Rank: 2
来 自:福建
等 级:论坛游民
帖 子:92
专家分:93
注 册:2017-1-21
结帖率:82.35%
收藏
已结贴  问题点数:20 回复次数:2 
请教如何评价一个程序好坏?
程序代码:
#include<iostream>
#include<vector>
#include<string>
using namespace std;
vector<int> add(vector<int> &a,vector<int> &b);
void main()
{
    int str1[]={1,1,1,1,0,0},str2[]={1};
    vector<int> a(str1,str1+sizeof(str1)/sizeof(str1[0])),aa(str2,str2+sizeof(str2)/sizeof(str2[0]));
    vector<int> b(add(a,aa));
    for(vector<int>::iterator it=b.begin();it!=b.end();it++)
        cout<<*it<<' ';
    cout<<endl;
}
vector<int> add(vector<int> &a,vector<int> &aa)
{
    int top1=-1,top2=-1;
    int stack1[10],stack2[10];
    vector<int> result;
    for(int i=a.size()-1;i>=0;i--) stack1[++top1]=a[i];
    for(i=aa.size()-1;i>=0;i--) stack2[++top2]=aa[i];//放到栈中
    int last1=0,last2=0;
    while(last1<=top1 && last2<=top2)
    {
        if(stack1[last1]+stack2[last2]==2)//进位
        {
             int tmp=last1;
             stack1[tmp++]=0;
             while(stack1[tmp]==1 && tmp<=top1) { stack1[tmp]=0;tmp++;}
             if(tmp>top1) stack1[++top1]=1;
             else stack1[tmp]=1;
        }
        else //不进位
        {
            stack1[last1]=stack1[last1]+stack2[last2];
        }
        last1++;last2++;
    }
    if(last2<=top2) stack1[top1++]=stack2[last2++];//stack1为最终计算结果
    while(top1>=0) result.push_back(stack1[top1--]);//出栈
    return result;

}

如何评价这个??
我想知道标准。
搜索更多相关主题的帖子: include vector int sizeof tmp 
2017-12-31 11:21
moox
Rank: 2
来 自:福建
等 级:论坛游民
帖 子:92
专家分:93
注 册:2017-1-21
收藏
得分:0 
string addbinary(string a,string b)
{
    int carry=0;
    string result;
    for(int i=a.size()-1,int j=b.size()-1;i>=0||j>=0;i--,j--)
    {
        int ai= i>=0?a[i]-'0':0;
        int bj= j>=0?b[j]-'0':0;
         int val=(ai+bj+carry)%2;
        carry=(ai+bj+carry)/2;
        result.insert(result.begin(),val+'0');
    }
    if(carry==1)
        result.insert(result.begin(),'1');
    return result;
}

。。。
2017-12-31 18:04
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:20 
#include<iostream> --- 虽然没有语法错误,但……
void main() --- 不要用不符合标准的烂编译器
vector<int> add(vector<int> &a,vector<int> &b) --- 你的老师肯定是从C来的,而且它一定还没学会C。记住是 const vector<int>& a
int stack1[10] --- 既然你确定10个了,何必还用vector
for(int i=a.size()-1;i>=0;i--) --- 告诉你的老师,其它语言我不懂,但在C/C++中,下标类型都用size_t
for(i=aa.size()-1;i>=0;i--) --- 不要用过时的烂编译器
string addbinary(string a,string b) --- const string& a

程序代码:
#include <string>
std::string addbinary( const std::string& a, const std::string& b );

#include <iostream>
#include <algorithm>
using namespace std;

int main( void )
{
    cout << addbinary("111100","1") << endl;
    cout << addbinary("111111","1") << endl;
}

std::string addbinary( const std::string& a, const std::string& b )
{
    string result;
    result.reserve( std::max(a.size(),b.size())+1 );

    unsigned carry = 0;
    for( size_t i=0,j=0; i<a.size() || j<b.size(); ++i,++j )
    {
        unsigned ai = i<a.size() ? a[a.size()-i-1]-'0' : 0;
        unsigned bj = j<b.size() ? b[b.size()-j-1]-'0' : 0;
        result.push_back( '0' + (carry+ai+bj)%2 );
        carry = (carry+ai+bj)/2;
    }
    if( carry != 0 )
        result.push_back( '0'+carry );

    reverse( result.begin(), result.end() );
    return result;
}

2018-01-02 09:07
快速回复:请教如何评价一个程序好坏?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016925 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved