高分请C++高手帮忙解下面两道题,不胜感激....(要调试成功的)
1、编写一个可以删除文本文件中的所有以”//”开头字符串的C++程序。要求必须使用C++的I/O流成员函数来完成。2、已知类Test含有整型私有数据成员num,编写主程序...
1、编写一个可以删除文本文件中的所有以”//”开头字符串的C++程序。要求必须使用C++的I/O流成员函数来完成。
2、已知类Test含有整型私有数据成员num,编写主程序,要求:
一、写出Test的完整定义,并含有必要的函数成员;
二、建立长度为9、元素型为Test的动态数组且初值为0;
三、将各元素的值均设置为0x7FF;
四、显示各元素的值;
五、删除动态数组; 展开
2、已知类Test含有整型私有数据成员num,编写主程序,要求:
一、写出Test的完整定义,并含有必要的函数成员;
二、建立长度为9、元素型为Test的动态数组且初值为0;
三、将各元素的值均设置为0x7FF;
四、显示各元素的值;
五、删除动态数组; 展开
展开全部
//第一个
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
using std::string;
int main()
{
ifstream infile("resource.txt");
ofstream outfile("destination.txt");
if (!infile)
{
cout << "Can't find file resource.txt." << endl;
exit(1);
}
if (!outfile)
{
cout << "Can't open file destination.txt." << endl;
exit(1);
}
string temp;
while (getline(infile, temp))
{
if (temp.substr(0, 2) != "//")
{
cout << temp << endl;
outfile << temp << endl;
}
}
infile.close();
outfile.close();
system("copy /y destination.txt resource.txt");
remove("destination.txt");
return 0;
}
//第二个
#include <iomanip.h>
#include <iostream.h>
#define SIZE 9
class Test {
private:
int num;
public:
Test(int n = 0): num(n) { }
void set(int n)
{
num = n;
}
void show(void) const
{
cout<<hex<<num<<endl;
}
};
int main()
{
Test *temp = new Test[SIZE];
int id = 0;
for (id = 0; id < SIZE; ++id)
{
temp[id].set(0x7FFF);
}
for (id = 0; id < SIZE; ++id)
{
temp[id].show();
}
delete []temp;
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
using std::string;
int main()
{
ifstream infile("resource.txt");
ofstream outfile("destination.txt");
if (!infile)
{
cout << "Can't find file resource.txt." << endl;
exit(1);
}
if (!outfile)
{
cout << "Can't open file destination.txt." << endl;
exit(1);
}
string temp;
while (getline(infile, temp))
{
if (temp.substr(0, 2) != "//")
{
cout << temp << endl;
outfile << temp << endl;
}
}
infile.close();
outfile.close();
system("copy /y destination.txt resource.txt");
remove("destination.txt");
return 0;
}
//第二个
#include <iomanip.h>
#include <iostream.h>
#define SIZE 9
class Test {
private:
int num;
public:
Test(int n = 0): num(n) { }
void set(int n)
{
num = n;
}
void show(void) const
{
cout<<hex<<num<<endl;
}
};
int main()
{
Test *temp = new Test[SIZE];
int id = 0;
for (id = 0; id < SIZE; ++id)
{
temp[id].set(0x7FFF);
}
for (id = 0; id < SIZE; ++id)
{
temp[id].show();
}
delete []temp;
return 0;
}
展开全部
第二个的给你,好像有点内存泄露,懒得查了。。。。
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;
class Test
{
public:
void SetNum(int n)
{
num = n;
}
int GetNum()
{
return num;
}
private:
int num;
};
int main(int argc, char* argv[])
{
Test* testArray = new Test[9];
for(int i = 0; i != 9; ++i)
{
testArray[i].SetNum(0);
}
for(i = 0; i != 9; ++i)
{
testArray[i].SetNum(0x7FF);
}
for(i = 0; i != 9; ++i)
{
cout << setiosflags(ios::uppercase) << hex << testArray[i].GetNum() << endl;
}
delete[] testArray;
getch();
return 0;
}
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;
class Test
{
public:
void SetNum(int n)
{
num = n;
}
int GetNum()
{
return num;
}
private:
int num;
};
int main(int argc, char* argv[])
{
Test* testArray = new Test[9];
for(int i = 0; i != 9; ++i)
{
testArray[i].SetNum(0);
}
for(i = 0; i != 9; ++i)
{
testArray[i].SetNum(0x7FF);
}
for(i = 0; i != 9; ++i)
{
cout << setiosflags(ios::uppercase) << hex << testArray[i].GetNum() << endl;
}
delete[] testArray;
getch();
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
/*希望能被采纳*/
/**
你先在该目录下建一个resource.txt的文本文件,在里面输入一些测试字符串。
编写一个可以删除文本文件中的所有以”//”开头字符串的C++程序。
要求必须使用C++的I/O流成员函数来完成。
*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main() {
std::ifstream infile("resource.txt", std::ifstream::in);
std::ofstream outfile("destination.txt", std::ofstream::out);
if (!infile) {
cout << "Can't find file resource.txt." << endl;
exit(1);
}
string temp;
while (getline(infile, temp)) {
if (temp.substr(0, 2) != "//") {
outfile << temp << endl;
}
}
infile.close();
outfile.close();
infile.open("destination.txt");
outfile.open("resource.txt");
while (getline(infile, temp)) {
outfile << temp << endl;
}
infile.close();
outfile.close();
remove("destination.txt");
return 0;
}
/**
已知类Test含有整型私有数据成员num,编写主程序,要求:
一、写出Test的完整定义,并含有必要的函数成员;
二、建立长度为9、元素型为Test的动态数组且初值为0;
三、将各元素的值均设置为0x7FF;
四、显示各元素的值;
五、删除动态数组;
*/
#include <iostream>
#define SIZE 9
using std::cout;
using std::endl;
class Test {
public:
Test(int n = 0): num(n) { }
void set(int n) {
num = n;
}
void show(void) const {
cout << num << endl;
}
private:
int num;
};
int main() {
Test *test = new Test[SIZE](0);
int index = 0;
for (index = 0; index < SIZE; ++index) {
test[index].set(0x7FFF);
}
for (index = 0; index < SIZE; ++index) {
test[index].show();
}
delete [] test;
return 0;
}
/**
你先在该目录下建一个resource.txt的文本文件,在里面输入一些测试字符串。
编写一个可以删除文本文件中的所有以”//”开头字符串的C++程序。
要求必须使用C++的I/O流成员函数来完成。
*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main() {
std::ifstream infile("resource.txt", std::ifstream::in);
std::ofstream outfile("destination.txt", std::ofstream::out);
if (!infile) {
cout << "Can't find file resource.txt." << endl;
exit(1);
}
string temp;
while (getline(infile, temp)) {
if (temp.substr(0, 2) != "//") {
outfile << temp << endl;
}
}
infile.close();
outfile.close();
infile.open("destination.txt");
outfile.open("resource.txt");
while (getline(infile, temp)) {
outfile << temp << endl;
}
infile.close();
outfile.close();
remove("destination.txt");
return 0;
}
/**
已知类Test含有整型私有数据成员num,编写主程序,要求:
一、写出Test的完整定义,并含有必要的函数成员;
二、建立长度为9、元素型为Test的动态数组且初值为0;
三、将各元素的值均设置为0x7FF;
四、显示各元素的值;
五、删除动态数组;
*/
#include <iostream>
#define SIZE 9
using std::cout;
using std::endl;
class Test {
public:
Test(int n = 0): num(n) { }
void set(int n) {
num = n;
}
void show(void) const {
cout << num << endl;
}
private:
int num;
};
int main() {
Test *test = new Test[SIZE](0);
int index = 0;
for (index = 0; index < SIZE; ++index) {
test[index].set(0x7FFF);
}
for (index = 0; index < SIZE; ++index) {
test[index].show();
}
delete [] test;
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询