利用txt文件用C++来计算

#include
#include

#include
#include

using namespace std;

struct ft
{int psi,r;

};

int len;//文件行数 全局变量
int CountLines(const char *filename)
{
ifstream ReadFile;
int n = 0;
string tmp;
ReadFile.open(filename, ios::in);//ios::in 表示以只读的方式读取文件
if (ReadFile.fail())//文件打开失败:返回0
{
return 0;
}
else//文件存在
{
while (getline(ReadFile, tmp, ‘\n’))
{
n++;
}
ReadFile.close();
return n;
}
}

ft* CreateftFromFile(const char *fname)
{
ifstream in(fname);
if (!in){ cout << “No such a file” << endl; return NULL; }
ft s;
ft *stu = new ft[len];

int i = 0;
while (in >> s.r >> s.psi)
{

    stu[i] = s;
    ++i;

}
in.close();

return stu;

}

void DestroyftStruct(ft *&s)
{
if (s==NULL){ cout << “无信息” << endl; return; }
delete[] s;
s = NULL;
}

void test0()
{
cout << “最后结果” << endl;
ft s1 = CreateftFromFile(“C://Users//15595//Desktop//chengji1.txt”);
ft s2 = CreateftFromFile(“C://Users//15595//Desktop//chengji2.txt”);
len=CountLines(“C://Users//15595//Desktop//chengji1.txt”);
long result=0;
for(int i=0;i<len;i++)
{result+=s1->r
s1->psi
s2->psi;
s1++;
s2++;
}
cout<<result<<endl;
}

int main()
{
test0();
cout<<len<<endl;

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值