我是C ++编程的新手,并试图解决我们教授给我们的UVa Online Judge上的一些问题。 现在我在706液晶显示器上(https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=16&page=show_problem&problem=647)。 在我的IDE(VS2015社区)中,一切正常并且符合预期,但是当我尝试提交代码时,我收到运行时错误。不幸的是,他们没有显示更多细节。 我尝试在几个在线c ++编译器(例如http://cpp.sh/)中运行我的代码,并且永远不会出错。
这是我的代码
#include <iostream>
#include <cmath>
// set the "borders" for each number
#define ZERO 1+2+4+16+32+64;
#define ONE 4+32;
#define TWO 1+4+8+16+64
#define THREE 1+4+8+32+64
#define FOR 2+4+8+32
#define FIVE 1+2+8+32+64
#define SIX 1+2+8+16+32+64
#define SEVEN 1+4+32
#define EIGHT 1+2+4+8+16+32+64
#define NINE 1+2+4+8+32+64
using namespace std;
// draw the digits line by line. some lines (first, middle, last) only represents horizontal lines, where all other lines represent vertical lines
// in every line for every digit is checked wheter the lines on the current position must be drawn or not.
void drawDigits(int* digits, int cDigits, int height, int width) {
int middle = (height >> 1);
for (int i = 0; i < height; ++i) {
for (int j = 0; j < cDigits; ++j) {
if (i == 0) {
if (digits[j] & 1) {
cout << " ";
for (int y = 0; y < width - 2; ++y) cout << "-";
cout << " ";
}
else {
for (int y = 0; y < width; ++y) cout << " ";
}
}
else if (i < middle) {
if (digits[j] & 2) {
cout << "|";
for (int y = 0; y < width - 2; ++y) cout << " ";
}
else {
for (int y = 0; y < width - 1; ++y) cout << " ";
}
if (digits[j] & 4) {
cout << "|";
}
else {
cout << " ";
}
}
else if (i == middle) {
if (digits[j] & 8) {
cout << " ";
for (int y = 0; y < width - 2; ++y) cout << "-";
cout << " ";
}
else {
for (int y = 0; y < width; ++y) cout << " ";
}
}
else if (i < height - 1) {
if (digits[j] & 16) {
cout << "|";
for (int y = 0; y < width - 2; ++y) cout << " ";
}
else {
for (int y = 0; y < width - 1; ++y) cout << " ";
}
if (digits[j] & 32) {
cout << "|";
}
else {
cout << " ";
}
}
else {
if (digits[j] & 64) {
cout << " ";
for (int y = 0; y < width - 2; ++y) cout << "-";
cout << " ";
}
else {
for (int y = 0; y < width; ++y) cout << " ";
}
}
if (j != cDigits - 1) cout << " ";
}
cout << endl;
if (i == height - 1) cout << endl;
}
}
// a given number (c-string) is prepared for drawing
// find out the length of the 'number' and assign for each digit the defined value in the ditigs-array
void drawLCD(int size, char* number) {
if (size < 1 || size > 10) return;
int length = 0;
while (*(number + length) != '\0') {
++length;
}
int* digits = new int[length];
for (int i = 0; i < length; ++i) {
int t = 0;
switch (*(number++) - '0') {
case 0:
t = ZERO;
break;
case 1:
t = ONE;
break;
case 2:
t = TWO;
break;
case 3:
t = THREE;
break;
case 4:
t = FOR;
break;
case 5:
t = FIVE;
break;
case 6:
t = SIX;
break;
case 7:
t = SEVEN;
break;
case 8:
t = EIGHT;
break;
case 9:
t = NINE;
break;
default:
t = ZERO;
}
digits[i] = t;
}
drawDigits(digits, length, 2 * size + 3, size + 2);
}
void callDraws(int cNums, int* size, char** numbers) {
for (int i = 0; i < cNums; ++i) {
drawLCD(size[i], numbers[i]);
}
}
// gets the integer value from a number in a c-string
int getInt(char* str) {
int length = 0;
int res = 0;
while (*(str + length) != '\0') {
++length;
}
for (int i = 0; i < length; ++i) {
res += (int)pow(10, length - (i + 1)) * (*(str++)-'0');
}
return res;
}
int main() {
char* n = new char[10];
int counter = 0;
int* size = new int[1000];
char** numbers = new char*[1000];
for (int i = 0; i < 1000; ++i) {
size[i] = 0;
numbers[i] = "";
}
while (cin >> n) {
if (size[counter] <= 0) {
int nn = getInt(n);
if (nn <= 0) break;
size[counter] = nn;
}
else {
numbers[counter++] = n;
n = new char[10];
}
}
callDraws(counter, size, numbers);
return 0;
}
当我在调用callDraws()之前输入'return 0'时,代码终止。所以错误必须在打印数字的某个地方。
此运行时错误在哪里或如何找到它? 提前谢谢。