写了个读取和写入的函数(放在最后)但是不知道在哪里使用
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#define MaxSize 20
int flap[10] = { 0 }; /*票数 */
/*为方便测试先给 用户 和 参选对象 各赋三个初始值 */
struct guest_info { /*声明结构体类型 -- 用户*/
char name[8]; /*姓名*/
int phone; /*号码 */
int votenum; /*投票次数*/
int sign; /*投票标记 ,0:未投票--1:已投票*/
}GuestList[MaxSize] = { {"吴一",1234,3,0},{"大二",1567,3,0},{"张三",5678,3,0} };
struct Can_info { /*声明结构体类型 -- 参选对象*/
int ranking; /*排名*/
int percent; /*百分比*/
int sum; /*票数 */
char name[8]; /*姓名*/
int phone; /*号码 */
int time; /*选举时间*/
}CanList[MaxSize] = { {0,"李四",0,9871,20210617},{0,"王五",0,9762,20210617},{0,"赵六",0,9763,20210617} };
void Admin(int, int); /*管理员*/
void Candidate(int); /*参选对象*/
void Voter(int*, int); /*用户*/
void InsertC(int*); /*参选对象插入*/
void InsertV(int*); /*用户插入*/
void Show(int, int); /*显示*/
void Update(int, int); /*修改*/
void inrecord(); /*写入磁盘文件*/
void outrecord(); /*读取磁盘文件*/
int main()
{
int i; /*因两个结构体各已有三个初始值,所以count从3开始记录*/
int count = 3; /*count为用户计数器,记录已经登记记录个数 */
int cnt = 3; /*cnt为备选对象计数器,记录已经登记记录个数 */
do { /*显示一个简易菜单 */
printf("*****************************\n");
printf("* 欢迎进入 *\n");
printf("* 投票小程序 *\n");
printf("*****************************\n");
printf("\n");
printf(" 1---管理员\n");
printf(" 2---参选对象\n");
printf(" 3---用户\n");
printf(" 4---注册参选对象\n");
printf(" 5---注册用户\n");
printf(" 6---退出\n");
scanf_s("%d", &i); /*接收用户的选择*/
switch (i)
{
case 1:Admin(count, cnt); /*调用管理员运算 */
break;
case 2:Candidate(cnt); /*调用参选对象运算 */
break;
case 3:Voter(&count, cnt); /*调用用户运算 */
break;
case 4:InsertC(&cnt); /*调用注册参选对象运算 */
break;
case 5:InsertV(&count); /*调用注册用户运算 */
break;
case 6:break; /*退出系统 */
default:printf("错误选择!请重新选择\n");
break;
}
} while (i != 6);
return 0;
}
void Admin(int count, int cnt) /*管理员*/
{
int adminname, password, i;
printf("请输入管理员账号:");
scanf_s("%d", &adminname);
if (adminname == 123) { /*设置管理员账号*/
printf("请输入管理员密码:");
scanf_s("%d", &password);
if (password == 123) { /*设置管理员密码*/
do { /*显示一个简易菜单 */
printf("*****************************\n");
printf("* 欢迎进入 *\n");
printf("* 管理员系统 *\n");
printf("*****************************\n");
printf("\n");
printf(" 1---修改投票次数的限定\n");
printf(" 2---修改选举时间的限定\n");
printf(" 3---显示参选对象信息\n");
printf(" 4---显示用户信息\n");
printf(" 5---发布,统计投票结果\n");
printf(" 6---退出\n");
scanf_s("%d", &i); /*接收用户的选择*/
switch (i)
{
case 1:Update(count, 1); /*调用修改投票次数运算 */
break;
case 2:Update(cnt, 2); /*调用修改选举时间运算 */
break;
case 3:Show(cnt, 11); /*调用参选对象信息运算 */
break;
case 4:Show(count, 12); /*调用用户信息运算 */
break;
case 6:break; /*退出系统 */
default:printf("错误选择!请重选\n");
break;
}
} while (i != 6);
return;
}
else {
printf("密码错误!\n");
return;
}
}
else {
printf("用户名错误!\n");
return;
}
}
void Candidate(int cnt) /*参选对象*/
{
int i, phone, n;
printf("*****************************\n");
printf("* 欢迎进入 *\n");
printf("* 参选对象信息系统 *\n");
printf("*****************************\n");
printf("\n");
printf("请输入手机号:");
scanf_s("%d", &phone);
for (i = 0; i < cnt; i++) /*查找是否存在记录 */
if (CanList[i].phone == phone)
{
printf(" 用户名 手机号 投票次数 \n");
printf("%8s", CanList[i].name);
printf("%7d", CanList[i].phone);
printf("%5d", CanList[i].sum);
do {
printf("查看其他参选对象,请输入1:\n退出请输入2:");
scanf_s("%d", &n);
switch (n)
{
case 1:Show(cnt, 11);
break;
case 2:break;
default:printf("错误选择!请重选\n");
break;
}
} while (n != 2);
return;
}
printf("手机号不存在!\n");
return;
}
void Voter(int* count, int cnt) /*选民*/
{
int i, phone, n, num, j, x = 1;
int flag = 1; /*通过sign标记位来判断每个手机号今日是否已投票 */
printf("*****************************\n");
printf("* 欢迎进入 *\n");
printf("* 用户投票系统 *\n");
printf("*****************************\n");
printf("\n");
printf("请输入手机号:");
scanf_s("%d", &phone);
for (i = 0; i < *count; i++) /*查找是否存在记录 */
if (GuestList[i].phone == phone)
{
j = i;
do {
printf("查阅参选对象,请输入1:\n投票请输入2:\n退出请输入3:");
scanf_s("%d", &n);
if (n == 1) {
printf("\n");
printf("参选对象 票数 排名 百分比\n");
for (i = 0; i < cnt; i++)
{
printf("%8s", CanList[i].name);
printf("%5d", CanList[i].sum);
printf("%6d", CanList[i].ranking);
printf("%13d\n", CanList[i].percent);
}
}
else if (n == 3) {
return;
}
else if (n == 2) {
if (GuestList[j].sign == 1) {
printf("一个手机号码一天只能投一次,\n您已投票,不可再投票!\n");
}
else {
printf("请输入参选对象姓名:");
scanf_s("%d", CanList[i].name);
for (i = 0; i < cnt; i++) {
if (CanList[i].name == CanList[i].name) {
CanList[i].sum = CanList[i].sum + 1;
GuestList[j].votenum = GuestList[j].votenum - 1;
GuestList[j].sign = 1;
flag = 0;
printf("投票成功!\n");
}
}
if (flag)
printf("不存在该参选对象!!!\n");
}
}
} while (x != 0);
}
printf("手机号不存在!\n");
return;
}
void InsertC(int* cnt) /*参选对象插入 */
{
int i, in_phone, n;
if (*cnt == MaxSize)
{
printf("空间已满!");
return;
}
printf("*****************************\n");
printf("* 欢迎进入 *\n");
printf("* 参选对象注册系统 *\n");
printf("*****************************\n");
printf("\n");
printf("请输入手机号:");
scanf_s("%d", &in_phone);
for (i = 0; i < *cnt; i++) /*查找是否存在记录 */
if (CanList[i].phone == in_phone)
{
printf("手机号已存在!");
return;
}
CanList[i].phone = in_phone; /*接收插入数据 */
printf("请输入姓名:");
scanf_s("%s", CanList[i].name);
CanList[i].sum = 0;
CanList[i].ranking = 0;
(*cnt)++;
printf("参选对象注册成功!\n");
}
void InsertV(int* count) /*用户插入 */
{
int i, in_phone;
if (*count == MaxSize)
{
printf("空间已满!");
return;
}
printf("*****************************\n");
printf("* 欢迎进入 *\n");
printf("* 用户注册系统 *\n");
printf("*****************************\n");
printf("\n");
printf("请输入手机号:");
scanf_s("%d", &in_phone);
for (i = 0; i < *count; i++) /*查找是否存在记录 */
if (GuestList[i].phone == in_phone)
{
printf("手机号已存在!");
return;
}
GuestList[i].phone = in_phone; /*接收插入数据 */
printf("请输入姓名:");
scanf_s("%s", GuestList[i].name);
GuestList[i].votenum = 3;
GuestList[i].sign = 0;
(*count)++;
printf("用户注册成功!\n");
return;
}
void Show(int n, int lab) /*显示*/
{
int i;
if (lab == 11) {
printf("\n");
printf(" 排名 姓名 手机号 票数\n");
for (i = 0; i < n; i++)
{
printf("%6d", CanList[i].ranking);
printf("%8s", CanList[i].name);
printf("%7d", CanList[i].phone);
printf("%5d", CanList[i].sum);
}
}
else if (lab == 12) {
printf("\n");
printf(" 用户名 手机号 投票次数\n");
for (i = 0; i < n; i++)
{
printf("%8s", GuestList[i].name);
printf("%8d", GuestList[i].phone);
printf("%8d\n", GuestList[i].votenum);
}
}
}
void Update(int count, int lab) /*修改*/
{
int i, number;
int time;
if (lab == 1) { /*调用修改投票次数运算 */
printf("请输入要修改的投票次数:");
scanf_s("%d", &number);
for (i = 0; i < count; i++)
GuestList[i].votenum = number;
}
else if (lab == 2) { /*调用修改选举时间运算 */
printf("请输入要修改的选举时间:");
scanf_s("%d", &time);
for (i = 0; i < count; i++)
CanList[i].time = time;
}
}
void inrecord() { //写入投票函数
int i;
//将投票数据保存到file1.txt磁盘文件中
FILE* fpWrite = fopen("file1.txt", "w");
if (fpWrite == NULL)
{
printf("无法打开文件\n");
exit(0);
}
for (i = 0; i < 10; i++) {
fprintf(fpWrite, "%d ", flap[i]);
}
fclose(fpWrite);
return 0;
};
void outrecord() { //读取投票函数
//从file1.txt磁盘文件中读出投票数据
int i;
FILE* fpRead = fopen("file1.txt", "r");
if (fpRead == NULL)
{
printf("无法打开文件\n");
exit(0);
}
for (i = 0; i < 10; i++) fscanf(fpRead, "%d ", &flap[i]);
fclose(fpRead);
};