c语言读取顺序文件并处理
我们今天学习如何在创建读取文件之后,对其进行处理!不妨看看c语言如何读取顺序文件并处理,以下仅供参考!
以下是代码:
# include
# include
# include
# include
# include
using namespace std;
enum requesttype{ZERO_BALANCE=1,CREDIT_BALANCE, DEBIT_BANLANCE,END};//这里定义的是枚举类型,也就是赋值1,2,3
int getrequest();
bool shoulddisplay(int, double);//这个函数的`作用,就是从读取的数据中选择不同的条件进行输出!
void outputline(int, const string, double);//输出数据
int main() {
ifstream inclientfile("", ios::in);//我们假定已经定义好了相关的数据在文件中!
if (!inclientfile) {
cerr << "file could not be opened" << endl;
exit(1);
}
int request;
int account;
char name[30];
double balance;
request = getrequest();
while (request != END) {//选择数据类型
switch (request) {
case ZERO_BALANCE:
cout << "accounts with zero balances:";
break;
case CREDIT_BALANCE:
cout << "accounts with creadit balances:";
break;
case DEBIT_BANLANCE:
cout << "accounts with debit balances:";
break;
}
inclientfile >> account >> name >> balance;//读入数据
while (!()) {//设置循环条件
if (shoulddisplay(request, balance)) {
outputline(account, name, balance);
}
inclientfile >> account >> name >> balance;
}
r();
g(0);//回到文件的起始位置
request = getrequest();
}
cout << "end of run." << endl;
system("pause");
return 0;
}
int getrequest() {
int request;
cout << "enter request" << endl
<< "1-list accounts with zero balances" << endl
<< "2-list accounts with credit balances" << endl
<< "3-list accounts with debit balances" << endl
<< "4-end of run" << fixed << showpoint;
do {
cout << "?";
cin >> request;
} while (requestEND);
return request;
}
bool shoulddisplay(int type, double balance) {
if (type == ZERO_BALANCE&&balance == 0) {
return true;
}
if (type == CREDIT_BALANCE&&balance < 0) {
return true;
}
if (type == DEBIT_BANLANCE&&balance > 0) {
return true;
}
return false;
}
void outputline(int account, const string name, double balance) {
cout << left << setw(10) << account << setw(13) << name
<< setw(7) << setprecision(2) << right << balance << endl;
}
以下是执行后结果:
-
C语言中递归函数的教学方法
导语:函数递归基于分治法思想,将复杂的大规模问题转化为小规模问题进行求解,在算法设计中具有重要的理论意义和实用价值,是C语言教学的难点。下面就由小编为大家介绍一下C语言中递归函数的教学方法,欢迎大家阅读!1.引言C语言是一种语法简洁紧凑、运算符丰富、可移植...
-
2017年计算机C语言考试编程练习题及答案
江山代有才人出,各领风骚数百年。以下是小编为大家搜索整理的`2017年计算机C语言考试编程练习题及答案,希望能给大家带来帮助!更多精彩内容请及时关注我们应届毕业生考试网!1.从键盘上输入8个浮点数,编程求出其和以及平均值。要求写出求和以及平均值的函数。答:编...
-
c#实现sunday算法实例
Sunday算法思想跟BM算法很相似,在匹配失败时关注的是文本串中参加匹配的最末位字符的下一位字符,下面小编为大家整理了c#实现sunday算法实例,希望能帮到大家!因正则表达式搜索总是出现死循环,开始考虑改为其他搜索方式,因为自带的IndexOf默认只能找到第一个或最后一...
-
C语言入门知识:常量
导语:在程序运行中,其值不能被改变的量称为常量。下面是常量相关知识,欢迎阅读:常量有5种类型:整型常量、实型常量、字符常量、字符串常量和符号常量。(一)数值转换数字的四种表现形式:①:二进制:所有数字由0,1构成,逢二进一,二进制数中不会出现2.。例:110101②:八进制:以数...