【发布时间】:2014-01-18 08:59:54
【问题描述】:
首先我真的很抱歉,但我不知道我的程序中的错误在哪里,我已经经历了一百次但我找不到它。
当我在 VB2010 中调试时,我遇到了访问验证错误,因此必须有一个函数从某个不允许的地方获取内存。编写程序从数据库中获取一些数据,对其进行排序并返回到 output.txt。
它是拉丁语-克罗地亚语词典。无论如何请帮忙。
这是我的代码:
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <string>
#include<stdlib.h>
using namespace std;
void ascii_convert(int);
void string_compare(string);
void word_sort(int a,string inp);
string word1=0;
string word2=0;
string word3=0;
string upis;
string word[30] = {};
int match=0;
int input=0;
int broj_rijeci = 0;
int main()
{
string line;
ifstream myfile ("database.txt"); //loading database.txt
int count = 0;
if (myfile.is_open()){
while ( getline (myfile,line) ){
if(line.at(0) == '['){ //get's the number of words in database..
int i = line.length()-1; //...that is the first line of databse
int brojac = 0;
while(line.at(i) != line.at(0)){
input = line.at(i);
ascii_convert(input);
broj_rijeci = broj_rijeci + input*static_cast<int>(pow(static_cast<double>(10),brojac));
i--;
brojac++;
}
}
if(line.at(0) != '['){
for(int i = 0; line.at(i)!=' ' ;i++){
word[count] = word[count] + line.at(i);
}
count++;
}
}
myfile.close();
}
else cout << "Unable to open file";
for(int i = 0; i<broj_rijeci ;i++) cout<<word[i]<<endl;
cin>>upis;
string_compare(upis); //after this line I will put the output as output.txt
system("pause");
return 0;
}
void string_compare(string){
int len1 = upis.length();
int len;
for(int i = 0; i < broj_rijeci; i++){
int len2 = word[i].length();
if (len1<len2) len = len1;
else len = len2;
for(int y = 0; y < len; y++){
if(upis.at(y) == word[i].at(y)) match++;
}
word_sort(match,word[i]);
}
}
void word_sort(int a,string inp){
int match1 = 0;
int len1 = upis.length();
int len;
for(int i = 0; i < broj_rijeci; i++){
int len2 = word1.length();
if (len1<len2) len = len1;
else len = len2;
for(int y = 0; y < len; y++){
if(upis.at(y) == word1.at(y)) match1++;
}
}
if(a>match1){
word3 = word2;
word2 = word1;
word1 = inp;
}else{
int match1 = 0;
int len1 = upis.length();
int len;
for(int i = 0; i < broj_rijeci; i++){
int len2 = word2.length();
if (len1<len2) len = len1;
else len = len2;
for(int y = 0; y < len; y++){
if(upis.at(y) == word2.at(y)) match1++;
}
}
if(a>match1){
word3 = word2;
word2 = inp;
}else{
int match1 = 0;
int len1 = upis.length();
int len;
for(int i = 0; i < broj_rijeci; i++){
int len2 = word3.length();
if (len1<len2) len = len1;
else len = len2;
for(int y = 0; y < len; y++){
if(upis.at(y) == word3.at(y)) match1++;
}
}
if(a>match1) word3 = inp;
}
}
}
void ascii_convert(int){ //function for converting variable "input"
if(input == 48){ //from ascii to decimal numbers
input = 0;
}
if(input == 49){
input = 1;
}
if(input == 50){
input = 2;
}
if(input == 51){
input = 3;
}
if(input == 52){
input = 4;
}
if(input == 53){
input = 5;
}
if(input == 54){
input = 6;
}
if(input == 55){
input = 7;
}
if(input == 56){
input = 8;
}
if(input == 57){
input = 9;
}
}
这是我的“database.txt”:
[4]
terra ae f zemlja
amica ae f prijateljica
puela ae f djevojcica
nauta ae m mornar
感谢您的支持
【问题讨论】:
-
你的
ascii_converter函数是没有作用的(如果你弄清楚输入和输出值之间的关系也可以写成一行)。 -
@sftrabbit:很遗憾,它会正常工作。
input是全球性的。 -
@CHAO 哦。有趣的是他们将
input传递给未命名的参数... -
@sftrabbit:是的,这个函数似乎很矛盾,它是要接受参数还是要弄乱全局变量。 :P
-
您是否尝试过使用
valgrind?它是调试内存管理问题的最佳工具之一。