更好的方式使用指针? [英] Better way to use a pointer?

查看:130
本文介绍了更好的方式使用指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个程序,将显示条形图*最大数量*可以是40.我有一切工作,但有一个问题的代码。有更好的方法,你可以看到,我必须回到原来的地址两次使用:

  p_bar_length = p_bar_length  -  size ;有没有更好的方法?


  #include< iostream> 

using namespace std;

const int MAX_SPLATS = 40;

void bar_chart(double values [],int size)
{
double greater = values [0]; //分配第一个元素为最大

//找到最大值
for(int i = 1; i {
if(最大值< values [i])//检查是否有更大的值
{
highest = values [i];
}
}

//查找要使用的spalts数量
//以precent为基础的最大值
int * p_bar_length = new (nothrow)int [size];
for(int i = 0; i {
* p_bar_length =(values [i] / highest)* MAX_SPLATS;
p_bar_length ++; //转到下一个内存地址
}

//返回到内存地址
p_bar_length = p_bar_length - size;

// pritnt正确的splats数量
for(int i = 0; i {
for(int j = 0; j <* p_bar_length; j ++)
{
cout< *;
}
p_bar_length ++;
cout<< endl;
}

//返回到内存地址
p_bar_length = p_bar_length - size;

delete [] p_bar_length;
}

int main()
{
double values [6] = {22,40,28,26,14,46}
int val_size = 6;

bar_chart(values,val_size);

系统(pause);

return 0;
}


解决方案

使用数组索引:

  p_bar_length [i] =(values [i] / highest)* MAX_SPLATS; 

或使用指针运算:

  *(p_bar_length + i)=(values [i] / highest)* MAX_SPLATS; 


I'm trying to create a program that will display bar graphs with * the maximum number of * can be 40. I have everything working but had a question with the code. Is there a better way as you can see I have to go back to the original address twice using:

    p_bar_length = p_bar_length - size;

Is there a better way to do this?

#include <iostream>

using namespace std;

const int MAX_SPLATS = 40;

void bar_chart(double values[], int size)
{
    double largest = values[0]; //assign first element to be the largest

    //Find the largest value
    for (int i = 1; i < size; i++)
    {
        if (largest < values[i]) // check to see if there is something larger
        {
            largest = values[i];
        }
    }

    // Find the number of spalts to use
    // with the precent based on the largest value
    int* p_bar_length = new (nothrow) int[size];
    for (int i = 0; i < size; i++)
    {
        *p_bar_length = (values[i] / largest) * MAX_SPLATS;
        p_bar_length++; // Go to next memory address
    }

    // Go back to the orignal memory address
    p_bar_length = p_bar_length - size;

    // Pritnt the correct number of splats
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < *p_bar_length; j++)
        {
            cout << "*";
        }
        p_bar_length++;
        cout << endl;
    }

    // Go back to the orignal memory address
    p_bar_length = p_bar_length - size;

    delete[] p_bar_length;
}

int main()
{
    double values[6] = { 22, 40, 28, 26, 14, 46};
    int val_size = 6;

    bar_chart(values, val_size);

    system("pause");

    return 0;
}

解决方案

Rather than incrementing the pointer, use the array index:

p_bar_length[i] = (values[i] / largest) * MAX_SPLATS;

or use pointer arithmetic:

*(p_bar_length + i) = (values[i] / largest) * MAX_SPLATS;

这篇关于更好的方式使用指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆