C++学习记录(留坑)

#include <iostream>
#include <ctime>
#include <fstream>    ///文件打开有o.i权限 
#include <stdio.h>
#include <sstream>   ////bool转string 
#include <stdlib.h>
#include <string>
using namespace std;
char filename[]="game.log";
string times(){
    time_t now =time(0);    ///时间戳 
    char* dd=ctime(&now);    ///格式化 
    return dd;
}
void gamefile(string log){
    ofstream outfile;
    outfile.open(filename);
    outfile<<log<<endl;
    outfile.close();
}
bool game(){
    int list[6]={1,2,3,4,5,6};
    srand(time(0));
    int nub=rand()%6+1;
    cout<<"input a nubmer:";
    int guess;
    cin>>guess;
    int real=list[nub];
    int gu=list[guess];
    if(gu==real){
        cout<<"!!!!!TTTTTTTTT";
        return true; 
    }
    else{
        cout<<"F,try again!!!";
        return false;
    }
}
char* strandchar(string strings){
    char o[]="";
    int i;
    for (i=0;i<strings.length();i++){
        o[i]=strings[i];
    }
    return o;
    
} 
int main(){
//    string dd="fafdsdfsd";
//    char* aa=strandchar(dd);
    int i=1; 
//    char* logg;
    while(1){
        bool jg=game();
        ostringstream os1;      ///类型转换 
        os1<<jg;
        string logtime=times();
        logtime=strandchar(logtime); 
//        cout<<logtime<<endl; 
//        logg.append(strandchar(logtime));
        char cc=static_cast<char>(i);
//        string logg=("%d\n第%di轮结果:%d",logtime,i,string(os1.str()));
        char* tp=("轮数:");
//        cout<<tp<<endl; 
//        string logg;
//        logg.append(logtime);
//        logg.append(tp);
//        logg.append(cc);
//        char* logg=logtime+logg;
//        sprintf(logg,"%s,%s",logtime,tp);
        stringstream logg;
        logg<<logtime<<tp<<cc;
//        string loggg=logg.str();
        cout<<logg<<endl;
        gamefile(logg);
        if (true==jg){
//            gamefile(loggg);
//            cout<<loggg<<endl;
            break;
        }
        i++;    
    }
    
}

/////时间and文件 
//int main()
//{
//    time_t now =time(0);
//    char* dd=ctime(&now); 
//    ofstream outfile;
//    outfile.open("cxxtest.txt");
//    outfile<<dd<<endl;
//    cout<<dd<<endl;
//}

//extern int a , b,c;     #全局定义 
///////9*9乘法 
//int main()
//{
//    cout << "Hello, world!" << endl;
//
////    time_t now = time(0);
////    char* c=ctime(&now);
//
//    for (int i=1;i<=9;i++){
//        for (int j=1;j<=i;j++){
//            printf("%d*%d=%d  ",i,j,i*j);
//        }
//        printf("\n");
//    }
//
////    cout << c ;
//    return 0;
//}
/////数组操作 
//int main(){
//    int list[10]={0,1,2,3,4,5,6,7,8,9};
//    
//    int a=list[i];
////    printf(a);
//    cout<<a;
//    return 0;
//}
//    

 

posted @ 2018-06-01 14:15  Mr,Read  阅读(270)  评论(0编辑  收藏  举报