求小甲鱼C++视频第十一讲的FishOil源代码
请各位鱼油把小甲鱼源代码给我,或者给自己写的,谢谢了!:big:big:big:big #include <iostream>#include <fstream>
#include <windows.h> // 为了使用Sleep()函数
struct FishOil
{
std::string name;
std::string uid;
char sex;
};
bool InitFishC();
bool ReadFishC();
void RecordFishC();
bool WriteFishC(FishOil *OilData);
int main()
{
int i;
InitFishC();// 初始化数据。
while( 1 )
{
std::cout << "请选择需要进行的操作: \n";
std::cout << "1. 打印数据到屏幕\n";
std::cout << "2. 录入数据\n";
std::cout << "3. 退出程序\n";
std::cin >> i;
switch( i )
{
case 1:
if( ReadFishC() )
std::cout << "成功读取文件^_^\n\n";
else
std::cout << "读取文件失败T_T\n\n";
break;
case 2:
RecordFishC();
break;
case 3:
return 0;
}
}
std::cout << "初始化失败T_T......\n\n";
return 0;
}
bool InitFishC()
{
FishOil OilInit = {"小甲鱼", "fishc00001", 'M'};
if( WriteFishC(&OilInit) == false )
std::cout << "初始化失败T_T\n";
}
bool ReadFishC()
{
std::string temp;
std::ifstream fileInput("FishC.txt");
if( fileInput.is_open() )
{
std::cout << "\n正在输出记录数据...... ";
for( int i=0; i <= 100; i++ ) // 打印百分比
{
std::cout.width(3);
std::cout << i << "%";
Sleep(50);
std::cout << "\b\b\b\b";
}
std::cout << "\n\n";
std::cout << " 姓名 " << "身份证" << " 性别 " << "\n\n";
while( std::getline( fileInput, temp ) )
{
std::cout << temp << " ";
std::cout << "\n";
}
std::cout << "\n\n";
return true;
}
else
return false;
}
void RecordFishC()
{
char goon, Save;
FishOil OilData;
FishOil *pOilData;
goon = 'Y';
while( 'Y' == goon )
{
std::cout << "请输入鱼C账号: ";
std::cin >> OilData.name;
std::cout << "请输入鱼C身份证:";
std::cin >> OilData.uid;
std::cout << "请输入性别:";
std::cin >> OilData.sex;
std::cout << "录入成功, 请问需要保存吗?(Y/N)";
std::cin >> Save;
if( 'Y' == Save )
{
pOilData = &OilData;
if( WriteFishC( pOilData ) )
std::cout << "成功写入文件^_^\n";
else
std::cout << "写入文件失败T_T\n";
}
else
{
return;
}
std::cout << "/n请问需要再次录入吗?(Y/N)";
std::cin >> goon;
}
}
bool WriteFishC( FishOil *pOilData )
{
std::ofstream fileOutput("FishC.txt", std::ios::app);
// std::ios::app用来说明在老数据追加新数据
if( fileOutput.is_open() )
{
fileOutput << pOilData->name << " ";
fileOutput << pOilData->uid << " ";
fileOutput << pOilData->sex << "\n";
fileOutput.close();
std::cout << "数据成功保存到FishC.txt\n\n";
}
else
std::cout << "保存失败T_T\n";
}
#include <iostream>
#include <fstream>
#include <windows.h> // 为了使用Sleep()函数
struct FishOil
{
std::string name;
std::string uid;
char sex;
};
bool InitFishC();
bool ReadFishC();
void RecordFishC();
bool WriteFishC(FishOil *OilData);
int main()
{
int i;
InitFishC();// 初始化数据。
while( 1 )
{
std::cout << "请选择需要进行的操作: \n";
std::cout << "1. 打印数据到屏幕\n";
std::cout << "2. 录入数据\n";
std::cout << "3. 退出程序\n";
std::cin >> i;
switch( i )
{
case 1:
if( ReadFishC() )
std::cout << "成功读取文件^_^\n\n";
else
std::cout << "读取文件失败T_T\n\n";
break;
case 2:
RecordFishC();
break;
case 3:
return 0;
}
}
std::cout << "初始化失败T_T......\n\n";
return 0;
}
bool InitFishC()
{
FishOil OilInit = {"小甲鱼", "fishc00001", 'M'};
if( WriteFishC(&OilInit) == false )
std::cout << "初始化失败T_T\n";
}
bool ReadFishC()
{
std::string temp;
std::ifstream fileInput("FishC.txt");
if( fileInput.is_open() )
{
std::cout << "\n正在输出记录数据...... ";
for( int i=0; i <= 100; i++ ) // 打印百分比
{
std::cout.width(3);
std::cout << i << "%";
Sleep(50);
std::cout << "\b\b\b\b";
}
std::cout << "\n\n";
std::cout << " 姓名 " << "身份证" << " 性别 " << "\n\n";
while( std::getline( fileInput, temp ) )
{
std::cout << temp << " ";
std::cout << "\n";
}
std::cout << "\n\n";
return true;
}
else
return false;
}
void RecordFishC()
{
char goon, Save;
FishOil OilData;
FishOil *pOilData;
goon = 'Y';
while( 'Y' == goon )
{
std::cout << "请输入鱼C账号: ";
std::cin >> OilData.name;
std::cout << "请输入鱼C身份证:";
std::cin >> OilData.uid;
std::cout << "请输入性别:";
std::cin >> OilData.sex;
std::cout << "录入成功, 请问需要保存吗?(Y/N)";
std::cin >> Save;
if( 'Y' == Save )
{
pOilData = &OilData;
if( WriteFishC( pOilData ) )
std::cout << "成功写入文件^_^\n";
else
std::cout << "写入文件失败T_T\n";
}
else
{
return;
}
std::cout << "/n请问需要再次录入吗?(Y/N)";
std::cin >> goon;
}
}
bool WriteFishC( FishOil *pOilData )
{
std::ofstream fileOutput("FishC.txt", std::ios::app);
// std::ios::app用来说明在老数据追加新数据
if( fileOutput.is_open() )
{
fileOutput << pOilData->name << " ";
fileOutput << pOilData->uid << " ";
fileOutput << pOilData->sex << "\n";
fileOutput.close();
std::cout << "数据成功保存到FishC.txt\n\n";
}
else
std::cout << "保存失败T_T\n";
}
很需要,谢谢 好多错的,怎么改 多谢啊 lzy5299487 发表于 2014-6-25 18:17
#include
#include
#include // 为了使用Sleep()函数
{:1_1:} 非常感谢……这里有一点没看懂,一定要加上后面的std::ios::app嘛?以及我写的时候,是在程序开头写了using namespace std 是不是可以直接写ios::app?谢谢 楼主你会不?我也发现好多错啊上面的代码。怎么改,求教 解释一下程序可好 #include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
struct FishOil //定义一个结构
{
std::string name; //结构里面有三个变量,两种类型
std::string uid;
char sex;
};
bool InitFishC(); //初始化,抽象出来,把每个功能抽象成一个个函数
bool ReadFishC(); //读取数据
void RecordFishC(); //记录数据
bool WriteFishC(FishOil *Oildata);//写入数据
//主函数
int main()
{
int i;
InitFishC();
while( 1 )
{
std::cout << "请选择需要进行的操作:\n";
std::cout << "1.打印数据到屏幕\n";
std::cout << "2.录入数据\n";
std::cout << "3.退出程序\n";
std::cin >> i ;//用户界面输入的选择数字i传递给cin
switch(i) //switch事件,包含用户输入的三个选择
{
case 1:
if( ReadFishC() ) //如果ReadFishC()函数的返回值为真,则打印成功读取文件,否则打印读取文件失败;
std::cout << "成功读取文件\n\n" ;
else
std::cout << "读取文件失败\n\n" ;
break;
case 2:
RecordFishC();
break;
case 3:
return 0;
}
}
//std:: cout << "初始化失败\n\n";
return 0;
}
//初始化
bool InitFishC()
{
FishOil OilInit = {"小甲鱼", "fishc00001", 'M'};//在文件中写入内容
if( WriteFishC(&OilInit) == false )
std::cout << "初始化失败\n";
return 0;
}
bool ReadFishC()
{
std::string temp;
std::ifstream fileInput("FishC.txt");
if( fileInput.is_open() )
{
std::cout << "\n正在输出记录数据...... ";
for( int i=0; i <= 100; i++ ) // 打印百分比
{
std::cout.width(3);
std::cout << i << "%";
Sleep(50);
std::cout << "\b\b\b\b";
}
std::cout << "\n\n";
std::cout << " 姓名 " << "身份证" << " 性别 " << "\n\n";
while( std::getline( fileInput, temp ) )
{
std::cout << temp << " ";
std::cout << "\n";
}
std::cout << "\n\n";
return true;
}
else
return false;
}
void RecordFishC()
{
char goon, Save;
FishOil OilData;
FishOil *pOilData;
goon = 'Y';
while( 'Y' == goon )
{
std::cout << "请输入鱼C账号: ";
std::cin >> OilData.name;
std::cout << "请输入鱼C身份证:";
std::cin >> OilData.uid;
std::cout << "请输入性别:";
std::cin >> OilData.sex;
std::cout << "录入成功, 请问需要保存吗?(Y/N)";
std::cin >> Save;
if( 'Y' == Save )
{
pOilData = &OilData;
if( WriteFishC( pOilData ) )
std::cout << "成功写入文件^_^\n";
else
std::cout << "写入文件失败T_T\n";
}
else
{
return;
}
std::cout << "/n请问需要再次录入吗?(Y/N)";
std::cin >> goon;
}
}
bool WriteFishC(FishOil *pOilData)
{
std::ofstream fileOutput("FishC.txt", std::ios::app);
// std::ios::app用来说明在老数据追加新数据
if( fileOutput.is_open() )
{
fileOutput << pOilData->name << " ";
fileOutput << pOilData->uid << " ";
fileOutput << pOilData->sex << "\n";
fileOutput.close();
std::cout << "数据成功保存到FishC.txt\n\n";
}
else
std::cout << "保存失败T_T\n";
return 0;
}
//int Array = {1,2,3,4,5};
//int *ptr = Array;
//int a = *ptr+1;
//int b = *(ptr+1);
//std::cout << a << '\n';
//std::cout << b << '\n';
本帖最后由 15871070017 于 2017-3-29 19:09 编辑
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
struct FishOil //定义一个结构
{
std::string name; //结构里面有三个变量,两种类型
std::string uid;
char sex;
};
bool InitFishC(); //初始化,抽象出来,把每个功能抽象成一个个函数
bool ReadFishC(); //读取数据
void RecordFishC(); //记录数据
bool WriteFishC(FishOil *Oildata);//写入数据//主函数
int main()
{
int i;
InitFishC();//初始化,
while( 1 )
{
std::cout << "请选择需要进行的操作:\n";
std::cout << "1.打印数据到屏幕\n";
std::cout << "2.录入数据\n";
std::cout << "3.退出程序\n";
std::cin >> i ;//用户界面输入的选择数字i传递给cin
switch(i) //switch事件,包含用户输入的三个选择
{
case 1:
if( ReadFishC() ) //如果ReadFishC()函数的返回值为真,则打印成功读取文件,否则打印读取文件失败;
std::cout << "成功读取文件\n\n" ;
else
std::cout << "读取文件失败\n\n" ;
break;
case 2:
RecordFishC();
break;
case 3:
return 0;
}
} //std:: cout << "初始化失败\n\n";
return 0;
}
//初始化
bool InitFishC()
{
FishOil OilInit = {"小甲鱼", "fishc00001", 'M'};//在文件中写入内容
//char* path="FishC.txt";
//这种也是一种创建文本的方式;
//std::fstream
Fish("FishC.txt",std::ios::in|std::ios::out);
if(WriteFishC(&OilInit) ==true)
{
std::cout << "初始化成功\n";
}
else
{
std::cout<<"初始化失败\n";
}
return 0;
}
bool ReadFishC()
{
std::string temp;
std::ifstream fileInput("FishC.txt");
if( fileInput.is_open() )
{
std::cout << "\n正在输出记录数据...... ";
for( int i=0; i <= 100; i++ ) // 打印百分比
{
std::cout.width(3);
std::cout << i << "%";
Sleep(50);
std::cout << "\b\b\b\b";
}
std::cout << "\n\n";
std::cout << " 姓名 " << "身份证" << " 性别 " << "\n\n";
while( std::getline( fileInput, temp ) )
{
std::cout << temp << " ";
std::cout << "\n";
}
std::cout << "\n\n";
return true;
}
else
return false;
}
void RecordFishC()
{
char goon, Save;
FishOil OilData;
FishOil *pOilData; goon = 'Y';
while( 'Y' == goon||'y'==goon )
{
std::cout << "请输入鱼C账号: ";
std::cin >> OilData.name;
std::cout << "请输入鱼C身份证:";
std::cin >> OilData.uid;
std::cout << "请输入性别:";
std::cin >> OilData.sex;
std::cout << "录入成功, 请问需要保存吗?(Y/N)";
std::cin >> Save;
if( 'Y' == Save||'y'==Save )
{
pOilData = &OilData;
if( WriteFishC( pOilData ) )
std::cout << "成功写入文件^_^\n";
else
std::cout << "写入文件失败T_T\n";
}
else
{
return;
}
std::cout << "/n请问需要再次录入吗?(Y/N)";
std::cin >> goon;
}
}
bool WriteFishC(FishOil *pOilData)
{
std::ofstream fileOutput("FishC.txt", std::ios::app); // std::ios::app用来说明在老数据追加新数据
if( fileOutput.is_open() )
{
fileOutput << pOilData->name << " ";
fileOutput << pOilData->uid << " ";
fileOutput << pOilData->sex << "\n";
fileOutput.close();
std::cout << "数据成功保存到FishC.txt\n\n";
return true;
}
else
{
std::cout << "保存失败T_T\n";
return false;
}
}
POPMOMG 发表于 2017-3-23 16:49
#include
#include
#include
你的答案有些编译器编译出错在getline语法上,看我的代码,还有一个问题是如果输入超过1到3这些数字时会出现死循环 lzy5299487 发表于 2014-6-25 18:17
#include
#include
#include // 为了使用Sleep()函数
3Q
好绕啊
鱼币怎么购买/
页:
[1]