#include <cstdlib>
#include <iostream>
#include <Math.h>
#include <algorithm>
#include <string>
#include <iterator>
#include <iostream>
#include <vector> // std::vector
using namespace std;
int stepCount, i, x, y, z, j, k, array1Size, array2Size, tester, checker;
int numstring[10] = { 0,1,2,3,4,5,6,7,8,9 };
int numstringTest[10] = { 0,1,2,3,4,5,6,7,7,9 };
int* numbers;
int* differentNumbers;
int* p;
int* otherNumbers;
void stepCounter(int a) {
// determines the step number of the number
if (a/10 == 0)
stepCount = 1;
else if (a/100 == 0)
stepCount = 2;
else if (a/1000 == 0)
stepCount = 3;
else if (a/10000 == 0)
stepCount = 4;
else if (a/100000 == 0)
stepCount = 5;
else if (a/1000000 == 0)
stepCount = 6;
else if (a/10000000 == 0)
stepCount = 7;
else if (a/100000000 == 0)
stepCount = 8;
else if (a/1000000000 == 0)
stepCount = 9;
}
void stepIndicator(int b) {
// indicates each step of the number and pass them into array 'number'
stepCounter(b);
numbers = new int[stepCount];
for (i = stepCount; i>0; i--) {
//
/*
x = (round(pow(10,stepCount+1-i)));
y = (round(pow(10,stepCount-i)));
z = (round(pow(10,stepCount-i)));
*/
x = (int)(pow(10, stepCount + 1 - i) + 0.5);
y = (int)(pow(10, stepCount - i) + 0.5);
numbers[i - 1] = (b%x - b%y)/y;
}
}
int sameNumberCheck(int *array, int arraySize) {
//checks if the array has two or more of same integer inside return 1 if same numbers exist, 0 if not
for (i = 0; i<arraySize - 1; i++) {
//
for (j = i + 1; j<arraySize; j++) {
//
if (array[i] == array[j]) {
//
return 1;
}
}
}
return 0;
}
void getDifferentNumbers(int* array, int arraySize) {
//
k = 0;
j = 0;
checker = 0;
otherNumbers = new int[10 - arraySize]; //exact number of other numbers is 10 - numbers we have
for (i = 0; i<10; i++) {
if ((i>0)&(checker = 0)) {
k++;
otherNumbers[k - 1] = i - 1;
}
//
checker = 0;
for (j = 0; j<arraySize; j++) {
//
p = array + j;
cout << *p << endl; //ilkinde doğru sonra yanlış yapıyor?!
if (*p = i) {
checker++;
}
}
}
}
int main(int argc, char *argv[])
{
stepCounter(999999);
cout << stepCount << endl;
stepIndicator(826424563);
for (j = 0; j<9; j++) {
//
cout << numbers[j] << endl;
}
cout << sameNumberCheck(numstringTest, 10) << " must be 1" << endl;
cout << sameNumberCheck(numstring, 10) << " must be 0" << endl;
cout << endl;
getDifferentNumbers(numstringTest, 10);
cout << endl;
cout << endl << otherNumbers[0] << " is the diff number" << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Привет, моя проблема с указателями на самом деле. Вы увидите выше, function getDifferentNumbers. Это просто сравнение, если в любом заданном массиве повторяются числа (0-9). Для этого я передал указатель на функцию. Я просто делаю сравнение с помощью указателя. Однако здесь есть странная вещь. Когда я выполняю, в первый раз он исправляет, но секундомер, он идет совершенно безумным! Это функция:Странное поведение указателей в C++
void getDifferentNumbers(int* array, int arraySize) {
//
k = 0;
j = 0;
checker = 0;
otherNumbers = new int[10 - arraySize]; //exact number of other numbers is 10 - numbers we have
for (i = 0; i<10; i++) {
if ((i>0)&(checker = 0)) {
k++;
otherNumbers[k - 1] = i - 1;
}
//
checker = 0;
for (j = 0; j<arraySize; j++) {
//
p = array + j;
cout << *p << endl; //ilkinde doğru sonra yanlış yapıyor?!
if (*p = i) {
checker++;
}
}
}
}
и это массив, который я прошел в функцию:
int numstringTest[10] = {0,1,2,3,4,5,6,7,7,9};
он должен дать номер 7 в otherNumbers [0], однако это не делает. И я не знаю почему. Я действительно не вижу никакого неправильного заявления или операции здесь. Когда я исполняю, это первое выдает правильные значения
numstringTest: 1,2,3,4,5,6,7,7,9
но на следующей итерации 9 цикла выводит:
0000000000111111111122222222223333333333
Рассмотрите возможность использования отступов менее 16000 столбцов на уровень, по крайней мере, при размещении здесь (стиль веб-страницы ограничивает ширину, а также не все мониторы одинакового размера) –
Можете ли вы что-то сделать с форматированием кода - Легче читать на этой веб-странице –
Приношу свои извинения, это не повторится. Благодарю. – Alper91