所有程序代码如下:(VS2022,Debug模式,*64)
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <algorithm>
using namespace std;
class gsdata
{
public:
string gstype;
double gsvalue;
string gsstring;
};
class strpart
{
public:
string strpartstring;
};
string gsencode(string s)
{
long i, gslength, result;
result = 0;
gslength = s.size();
for (i = 0 ; i < gslength; i++)
{
result = result * 93 + (s[i-1] - 33);
}
return to_string(result);
}
string gsdecode(string s)
{
long number,stri; string str;
str = "";
number = stol(s);
while (number > 0)
{
stri = number % 93;
number = number / 93;
str = str+char(33+stri);
}
reverse(str.begin(), str.end());
return str;
}
string gscutoutstring(string s,long a,long b)
{
long gslength; string result;
gslength = b - a + 1;
result = string(s, a, gslength);
return result;
}
int main()
{
fstream f;
gsdata gsfile[500];
long i, gsdatalength;
cin >> gsdatalength;
for (i = 0; i < gsdatalength; i++)
{
cin >> gsfile[i].gstype;
if (gsfile[i].gstype == "Val")
{
cin >> gsfile[i].gsvalue;
gsfile[i].gsstring = "";
}
else if(gsfile[i].gstype == "Str")
{
cin >> gsfile[i].gsstring;
gsfile[i].gsvalue = 0;
}
}
f.open("GSG.gsd", ios::out);
for (i = 0; i < gsdatalength; i++)
{
if (gsfile[i].gstype == "Val")
{
f << "VE-" << gsencode(to_string(gsfile[i].gsvalue));
cout << "VE-" <<gsencode(to_string(gsfile[i].gsvalue));
}
else if (gsfile[i].gstype == "Str")
{
f << "ST-" << gsencode(gsfile[i].gsstring);
cout << "ST-" << gsencode(gsfile[i].gsstring);
}
f << ",";
cout << ",";
}
f.close();
cout << endl;
f.open("GSG.gsd", ios::in);
long readlength,readpos,readi,readilength,readstrlength,d; string datastring,datastr1,datastr2;
strpart onedata[500];
f >> datastring;
cout << datastring;
readpos = 0; readi = 0;
readlength = datastring.size();
while(readpos<readlength)
{
onedata[readi].strpartstring = gscutoutstring(datastring,readpos,datastring.find(",",readpos)-1);
readi += 1;
readpos = datastring.find(",",readpos)+1;
cout << onedata[readi].strpartstring << endl;
}
readilength = readi+1;
for (readi = 0; readi < readilength; readi++)
{
readstrlength = onedata[readi].strpartstring.size();
d = onedata[readi].strpartstring.find("-",0);
datastr1 = gscutoutstring(onedata[readi].strpartstring, 0, d - 1);
datastr2 = gscutoutstring(onedata[readi].strpartstring, d+1, readstrlength-1);
if (datastr1 == "VE") cout << "Val ";
else if (datastr1 == "ST") cout << "Str ";
cout << gsdecode(datastr2) << endl;
}
f.close();
cout << endl;
return 0;
}
输入数据为:
问题如图所示,请问如何解决(输入完之后跑不起来)
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <algorithm>
using namespace std;
class gsdata
{
public:
string gstype;
double gsvalue;
string gsstring;
};
class strpart
{
public:
string strpartstring;
};
string gsencode(string s)
{
long i, gslength, result;
result = 0;
gslength = s.size();
for (i = 0 ; i < gslength; i++)
{
result = result * 93 + (s[i-1] - 33);
}
return to_string(result);
}
string gsdecode(string s)
{
long number,stri; string str;
str = "";
number = stol(s);
while (number > 0)
{
stri = number % 93;
number = number / 93;
str = str+char(33+stri);
}
reverse(str.begin(), str.end());
return str;
}
string gscutoutstring(string s,long a,long b)
{
long gslength; string result;
gslength = b - a + 1;
result = string(s, a, gslength);
return result;
}
int main()
{
fstream f;
gsdata gsfile[500];
long i, gsdatalength;
cin >> gsdatalength;
for (i = 0; i < gsdatalength; i++)
{
cin >> gsfile[i].gstype;
if (gsfile[i].gstype == "Val")
{
cin >> gsfile[i].gsvalue;
gsfile[i].gsstring = "";
}
else if(gsfile[i].gstype == "Str")
{
cin >> gsfile[i].gsstring;
gsfile[i].gsvalue = 0;
}
}
f.open("GSG.gsd", ios::out);
for (i = 0; i < gsdatalength; i++)
{
if (gsfile[i].gstype == "Val")
{
f << "VE-" << gsencode(to_string(gsfile[i].gsvalue));
cout << "VE-" <<gsencode(to_string(gsfile[i].gsvalue));
}
else if (gsfile[i].gstype == "Str")
{
f << "ST-" << gsencode(gsfile[i].gsstring);
cout << "ST-" << gsencode(gsfile[i].gsstring);
}
f << ",";
cout << ",";
}
f.close();
cout << endl;
f.open("GSG.gsd", ios::in);
long readlength,readpos,readi,readilength,readstrlength,d; string datastring,datastr1,datastr2;
strpart onedata[500];
f >> datastring;
cout << datastring;
readpos = 0; readi = 0;
readlength = datastring.size();
while(readpos<readlength)
{
onedata[readi].strpartstring = gscutoutstring(datastring,readpos,datastring.find(",",readpos)-1);
readi += 1;
readpos = datastring.find(",",readpos)+1;
cout << onedata[readi].strpartstring << endl;
}
readilength = readi+1;
for (readi = 0; readi < readilength; readi++)
{
readstrlength = onedata[readi].strpartstring.size();
d = onedata[readi].strpartstring.find("-",0);
datastr1 = gscutoutstring(onedata[readi].strpartstring, 0, d - 1);
datastr2 = gscutoutstring(onedata[readi].strpartstring, d+1, readstrlength-1);
if (datastr1 == "VE") cout << "Val ";
else if (datastr1 == "ST") cout << "Str ";
cout << gsdecode(datastr2) << endl;
}
f.close();
cout << endl;
return 0;
}
输入数据为:
问题如图所示,请问如何解决(输入完之后跑不起来)