0%

(补)抽奖用小代码

使用语言C++14.

这个小代码里有些功能是想拓展的但没有写.
现在的功能是配备一份names.txt的文件.里面是每行一个@name的格式.
在run这个程序的时候,输入任何不是@的字符就会开始抽奖.
很久之前写的,也比较乱.但问题是有段日子没有写C++了…重新捡起来需要点时间.





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  
#include <iostream>
#include <map>
#include <string>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <random>

using namespace std;


int main(int argc, const char * argv[]) {
cout << "Instruction in case I forgot!" << endl;
cout << endl;
cout << "if you go with ./a.out -f that means include a file call names.txt to include names to raffle" << endl;
cout << "add -reset at front will empty your names.txt" << endl;
cout << "otherwise, whatever you add to raffle will be automatically add to names.txt as well" << endl;
cout << endl;
cout << "add username with @!" << endl;
cout << "any input without @ will start the raffle!" << endl;

std::map<int,string> theMap;
int index = 0;

if( argc > 1 ){
for(int i = 1 ; i < argc ; i++){
if(argv[i][0] == '-'){

if(argv[i][1] == 'f'){//file
//find names.txt
ifstream file{"names.txt"};
string s;

while(file >> s){
string tb = s;
theMap.insert(pair<int,string>(index,tb));
index++;
}
}//end of file interface
if(argv[i][1] == 'r'){//reset
//find names.txt
//then clean it
ofstream newFile("names.txt", std::ofstream::app);
newFile << "something";
newFile.close();

}//end of reset
}
}
}//end of argc

//mutually
string w;
while(cin >> w){
if(w[0] == '@'){
theMap.insert(pair<int,string>(index,w));
index++;
//we add it into our names.txt as well
//....
//
}else break;
}
//raffle here
int rdmb = 0;
random_device rd;
mt19937 eng(rd());
uniform_int_distribution<> distr(0,index);
rdmb = distr(eng);
cout << "test:" << theMap[rdmb] << endl;

//for testing

//we print all the content in the map
cerr << "ramdom num: " << rdmb << endl;
for (auto it = theMap.begin(); it != theMap.end(); ++it) {
cerr << '\t' << it->first
<< '\t' << it->second << '\n';
}
cerr << endl;

//2>/dev/null

}






复录:Makefile.这次不需要使用.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  
CXX = g++
CXXFLAGS = -std=c++14 -Wall -Werror=vla -MMD -L/usr/X11/lib
EXEC = nameOfTheProject
OBJECTS = main.o sub.o
DEPENDS = ${OBJECTS:.o=.d}

${EXEC}: ${OBJECTS}
${CXX} ${CXXFLAGS} ${OBJECTS} -o ${EXEC} -lX11

-include ${DEPENDS}

clean:
rm ${OBJECTS} ${EXEC} ${DEPENDS}
.PHONY: clean




再复录:compile代码,试图唤醒沉睡的记忆.
1
g++ -std=c++14 -o a.out main.cpp