11-3 | 4 | 7
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
ios_base::fmtflags original_flags=cout.flags();//保存现在的格式化参数的设置
cout<<812.000<<'|';
cout.setf(ios_base::left,ios_base::adjustfield);//对齐方式为左对齐
cout.width(10);//以长度为 10 的格式输出
cout<<813<<815.0<<'\n';
cout.unsetf(ios_base::adjustfield);//清除对齐方式
cout.precision(2);
cout.setf(ios_base::uppercase|ios_base::scientific);//以科学计数法的格式输出浮点数,显示 E
cout<<813.0;
cout.flags(original_flags);//恢复初始参数
cout<<endl;
ofstream output("test1.txt");
if(output)
{
output<<"Write data to file successfully !/已成功写入文件!";
}
output.close();
ifstream input("test1.txt");
if(input)
{
string s;
getline(input,s);
cout<<s;
}
return 0;
}
call the roll :
#include <iostream>
#include <cstring>
#include <time.h>
#include <fstream>
#include <sstream>
#include <string>
#include <windows.h>
#include <vector>
#include <conio.h>
using namespace std;
bool breorcon()
{
// string a;
// getline(cin,a);
if(_kbhit())//找了好久才找到,_kbhit()返回缓存区是否为空,空返回 0 ,非空返回 1
{
cin.sync();
cin.clear();
return false;
}
return true;
}
string generatetime()//利用WindowsAPI获取系统时间
{
SYSTEMTIME sys;
GetLocalTime(&sys);
int pl;
string m="";
pl=sys.wYear; m+=to_string(pl);
pl=sys.wMonth; if(pl<10)m+="0"; m+=to_string(pl);
pl=sys.wDay; if(pl<10)m+="0"; m+=to_string(pl);
pl=sys.wHour; if(pl<10)m+="0"; m+=to_string(pl);
pl=sys.wMinute; if(pl<10)m+="0"; m+=to_string(pl);
pl=sys.wSecond; if(pl<10)m+="0"; m+=to_string(pl);
return m;
}
void scanfileall(string n)//一次性读取文件并输出(这个程序没用到)
{
string s;
ifstream in(n);
ostringstream tmp;
tmp<<in.rdbuf();
s=tmp.str();
cout<<s;
}
int randomnum(int a,int b)//产生随机数
{
// srand((unsigned)time(nullptr));
// int d=rand()%3600+10;
// Sleep(d);
srand((unsigned)time(nullptr));
int c=rand()%2000+1000;
Sleep(c);
srand((unsigned)time(nullptr));
c=rand()%(b-a)+a;
return c;
}
void roll(string trollfile)//主要功能函数
{
trollfile+=".txt";
int rollnum=0,a=0;
string s;
vector<vector<string>> list,rolist;
string base="roll_";
base+=generatetime();
base+=".txt";
ifstream input(trollfile);
ofstream output(base);
if(output)
cout<<"Create data file successfully."<<endl;
else
cout<<"Error! Can not create/open the data file."<<endl;
if(!input)
cout<<"Fail to open file / file may be not exist or be moved ."<<endl;
else
{
cout<<"Scanning the orignal file..."<<endl;
while(getline(input,s))
{
istringstream scin(s);
list.push_back(vector<string>());
for(int k=0;k<4;k++)
{
list[a].push_back(string());
scin>>list[a][k];
}
a++;
}
}
input.close();
int rolljudge[a+1]={0};
cout<<"Press any key to stop."<<endl;
do
{
int k=randomnum(1,a+1);
if(rolljudge[k]==0)
{
rolist.push_back(vector<string>());
for(int j=0;j<4;j++)
{
rolist[rollnum].push_back(list[k][j]);
cout<<rolist[rollnum][j]<<" ";
}
cout<<endl;
rolljudge[k]=1;
rollnum++;
}
else continue;
}while(breorcon());
cout<<"Over the call."<<endl;
cout<<"Start write the roll data into the file..."<<endl;
for(int i=0;i<rollnum;i++)
{
for(int j=0;j<4;j++)
output<<rolist[i][j]<<" ";
output<<endl;
}
cout<<"Over write."<<endl;
}
int main()
{
string classname;
while(1)
{
cout<<"Input the name of file : ";
cin>>classname;
roll(classname);
}
return 0;
}
Collection :
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
void scanall(string n,int *p)
{
string k,s,a;
n+=".txt";
ifstream in(n);
ostringstream tmp;
tmp<<in.rdbuf();
istringstream fin(tmp.str());
while(getline(fin,s))
{
p[2]++;
istringstream scin(s);
while(scin>>a)
{
p[1]++;
p[0]+=a.size();
}
}
}
int main()
{
string n,j,pin;
while(1)
{
cout<<"Enter the name of the file: "<<endl;
cin>>n;
int num[3]={0};
scanall(n,num);
cout<<"Please input the name of the thing you want to know ,"<<endl
<<"like 'words' to get the number of words of the text."<<endl;
while(cin>>j)
{
if(j=="letters")
cout<<"Number of letters : "<<num[0]<<endl;
else if(j=="words")
cout<<"Number of words : "<<num[1]<<endl;
else if(j=="sentences")
cout<<"Number of sentences : "<<num[2]<<endl;
else
break;
}
}
return 0;
}