代码没有错误,但是运行结果很荒谬
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h> //控制台头文件 包含getch
struct student
{
char no[10];
char name[12];
int score[4];
char sex[5];
char year[5];
char month[3];
int day[5];
char tel[15];
};
char filename[100] = "studd.txt"; //设置文件名
FILE* fp;
void create(); //创建函数声明
void append(); //添加函数声明
void search(); //查找函数声明
void del(); //删除函数声明
void modify(); //修改函数声明
void output(); //显示函数声明
int main(void) //无类型
{
int num;
while (1)
{
printf(" ***学生成绩系统***\n ");
printf(" 1.创建记录\n");
printf(" 2.添加记录\n");
printf(" 3.查找记录\n");
printf(" 4.修改记录\n");
printf(" 5.删除记录\n");
printf(" 6.显示记录\n");
printf(" 0.退出系统\n");
printf(" 选择序号0-6:");
scanf("%d", &num);
if (num >= 0 && num <= 6)
{
switch (num)
{
case 1:create(); break;
case 2:append(); break;
case 3:search(); break;
case 4:modify(); break;
case 5:del(); break;
case 6:output(); break;
case 0:exit(1);
}
printf(" 操作完毕,请再次选择!\n");
}
else
printf(" 选择错误,请再次选择!\n");
}
getch(); //数据自动读取
return 0; //函数的返回值
}
//创建记录
void create()
{
struct student stu; //结构体
if ((fp = fopen(filename, "w")) == NULL)
{
printf("Cannot Open File! ");
exit(0); //终止函数
}
printf(" 请输入学号(以0结束) ");
scanf("%s", stu.no);
while (1)
{
printf("请输入姓名\n");
scanf("%s\n",stu.name);
printf("请输入成绩\n");
scanf("%d\n",stu.score);
printf("请输入性别\n");
scanf("%s\n",stu.sex);
printf("请输入年\n");
scanf("%s\n",stu.year);
printf("请输入月\n");
scanf("%s\n",stu.month);
printf("请输入日\n");
scanf("%d\n",stu.day);
printf("请输入电话\n");
scanf("%s\n",stu.tel);
break;
fprintf(fp, "%s%s%d%s%s%d%s ", stu.no, stu.name, stu.score,stu.sex,stu.year,stu.month,stu.day,stu.tel);
scanf("%s", stu.no); //字符串型格式符
}
fclose(fp);
}
//添加记录
void append()
{
struct student stu;
if ((fp = fopen(filename, "a")) == NULL) //追加
{
printf(" Cannot Open File!");
exit(0);
}
printf(" 请输入要添加的学号、姓名、成绩、性别、出生年、月、日,电话 ");
scanf("%s%s%d%s%s%s%d%s\n", stu.no, stu.name, &stu.score,stu.sex,stu.year,stu.month,stu.day,stu.tel);
fprintf(fp, "%s%s%d%s%s%s%d%s\n", stu.no, stu.name, stu.score,stu.sex,stu.year,stu.year,stu.month,stu.day,stu.tel);
fclose(fp);
}
//查找记录
void search()
{
int k = 0;
char nokey[10];
struct student stu;
printf(" 请输入学号:");
scanf("%s", nokey);
if ((fp = fopen(filename, "r")) == NULL) //读
{
printf(" Cannot Open File!\n");
exit(0);
}
fseek(fp, 1L * sizeof(struct student), 0); //将文件指针移动
while (!feof(fp)) //检查文件是否结束
{
fscanf(fp, "%s%s%d%s%s%s%d%s", stu.no, stu.name, stu.score,stu.sex,stu.year,stu.month,stu.day,stu.day);
if (strcmp(nokey, stu.no) == 0) //字符串比较
{
printf(" 已查找到,该记录为: ");
printf("%s%s%d%s%s%s%d%s\n", stu.no, stu.name, stu.score,stu.sex,stu.year,stu.month,stu.day,stu.tel);
k = 1;
break;
}
}
if (!k)
printf(" 文件中无此人的记录。\n");
fclose(fp);
}
//修改记录
void modify()
{
int k = 0;
long position;
char nokey[10];
struct student stu;
printf(" 请输入学号:");
scanf("%s", nokey);
if ((fp = fopen(filename, "r+")) == NULL)
{
printf(" Cannot Open File!\n");
exit(0);
}
fseek(fp, 1L * sizeof(struct student), 0);
while (!feof(fp))
{
fscanf(fp, "%s%s%d%s%s%s%d%s", stu.no, stu.name, &stu.score,stu.sex,stu.year,stu.month,stu.day,stu.tel);
if (strcmp(nokey, stu.no) == 0)
{
position = ftell(fp);
k = 1;
break;
}
}
if (k)
{
printf(" 已查找到,该记录为: ");
printf("%s%s%d%s%s%s%d%s\n", stu.no, stu.name, stu.score,stu.sex,stu.year,stu.month,stu.day,stu.tel);
printf(" 请输入新的学号、姓名及成绩:");
scanf("%s%s%d%s%s%s%d%s", stu.no, stu.name, &stu.score,stu.sex,stu.year,stu.month,stu.day,stu.tel);
fseek(fp, position - 1L * sizeof(struct student), SEEK_SET);
fprintf(fp, " %s%s%d%s%s%s%d%s", stu.no, stu.name, stu.score,stu.sex,stu.year,stu.month,stu.day,stu.tel);
}
else
printf(" 文件中无此人的记录。\n");
fclose(fp);
}
//删除记录
void del()
{
int m, k = 0;
long position;
char nokey[10];
struct student stu;
printf(" 请输入学号:");
scanf("%s", nokey);
if ((fp = fopen(filename, "r+")) == NULL)
{
printf(" Cannot Open File!\n");
exit(0);
}
fseek(fp, 1L * sizeof(struct student), 0);
while (!feof(fp))
{
fscanf(fp, "%s%s%d%s%s%s%d%s", stu.no, stu.name, &stu.score,stu.sex,stu.year,stu.month,stu.day,stu.tel);
if (strcmp(nokey, stu.no) == 0)
{
position = ftell(fp); //fp文件当前读写位置
k = 1;
break;
}
}
if (k)
{
printf(" 已查找到,该记录为: \n");
printf("%s%s%d%s%s%s%d%s\n", stu.no, stu.name, stu.score,stu.sex,stu.year,stu.month,stu.day,stu.tel);
printf(" 确实要删除记录,请按1;不删除记录,请按0:");
scanf("%d", &m);
if (m)
{
fseek(fp, position - 1L * sizeof(struct student), SEEK_SET);
fprintf(fp, "%s%s%d%s%s%s%d%s", "", "", "");
}
}
else
printf(" 文件中无此人的记录。\n");
fclose(fp);
}
//显示记录
void output()
{
struct student stu;
if ((fp = fopen(filename, "r")) == NULL)
{
printf(" Cannot Open File!");
exit(0);
}
printf(" 文件内容为: \n");
fseek(fp, 1L * sizeof(struct student), 0);
while (!feof(fp))
{
fscanf(fp, "%s%s%d%s%s%s%d%s ", stu.no, stu.name, &stu.score,stu.sex,stu.year,stu.month,stu.day,stu.tel);
printf("%s%s%d%s%s%s%d%s\n", stu.no, stu.name, stu.score,stu.sex,stu.year,stu.month,stu.day,stu.tel);
}
fclose(fp);
}
什么结果
为啥定义int score[4]呢?有四个成绩?
scanf("%s%s%d%s%s%s%d%s\n", stu.no, stu.name, &stu.score,
从输入来看,显然只有一个成绩啊?
问题描述
据说著名犹太历史学家Josephus有过以下的故事:在罗马人占领乔塔帕特后,39 个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人抓到,于是决定了一个自杀方式,41个人排成一个圆圈,由第1个人开始报数,每报数到第3人该人就必须自杀,然后再由下一个重新报数,直到所有人都自杀身亡为止。然而Josephus 和他的朋友并不想遵从。首先从一个人开始,越过k-2个人(因为第一个人已经被越过),并杀掉第k个人。接着,再越过k-1个人,并杀掉第k个人。这个过程沿着圆圈一直进行,直到最终只剩下一个人留下,这个人就可以继续活着。问题是,给定了和,一开始要站在什么地方才能避免被处决。Josephus要他的朋友先假装遵从,他将朋友与自己安排在第16个与第31个位置,于是逃过了这场死亡游戏。
解题思路
约瑟夫问题是个有名的问题:N个人围成一圈,从第一个开始报数,第M个将被杀掉,最后剩下一个,其余人都将被杀掉。例如N=6,M=5,被杀掉的顺序是:5,4,6,2,3。
(1) 每个人只有死和活着,因此可以用布尔标记每个人的状态,可用true表示死,false表示活。
(2) 开始时每个人都是活的,所以数组初值全部赋为false。
(3) 模拟杀人过程,直到所有人都被杀死为止。
示例代码(C语言)约瑟夫环 Visual Studio 2019
/**
* 数据结构 C语言 顺序表_约瑟夫问题
* @FileName Seq_Josephus_Problem.c
* @author W.Lionel.Esaka
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
//预处理
#define MAXSIZE 200 //数据数组大小 MAXSIZE 方便更改
#define bool int //C语言中没有bool值,用int替换
#define true 1 //用true替换1,用false替换0
#define false 0 //也可以用Correct/Error
typedef int ElemType; //ElemType/Status 元素类型/状态
typedef int Status; //用来指代特定的数据类型
/*定义线性表结构体*/
typedef struct
{
ElemType Data[MAXSIZE]; //Data数组,用来存储线性表数据的“容器”
int Count; //数据个数计数器
} Sequential_linear_table; //Sequential_linear_table(顺序线性表)
//创建线性表(线性表指针,数据数组,数组长度)
void CreateList(Sequential_linear_table* list,int n)
{
int i;
for (i = 0; i < n; i++) //将所给数据数组数据赋值给线性表中的数组
list->Data[i] = i + 1;
list->Count = n; //线性表长度/计数器置为n
}
//判定是否为空表(线性表指针)
bool ListEmpty(Sequential_linear_table* list)
{
return (list->Count == 0); //返回此表达式的值,假为true,真为ture
}
//返回线性表长度值(线性表指针)
int ListLength(Sequential_linear_table* list)
{
return(list->Count); //返回线性表计数器中的值
}
//输出线性表(线性表指针)
void ListDisplay(Sequential_linear_table* list)
{
int i;
if (ListEmpty(list))
{
printf("空表\n");
return; //for循环输出线性表数据数组中的数据
}
for (i = 0; i < list->Count; i++)
{
printf("%4d\t", list->Data[i]);
}
printf("\n");
}
//求某个数据元素值(线性表指针,所查找的数据位置,将所的数据返回给此变量)
bool GetElem(Sequential_linear_table* list, int i, ElemType* e)
{
if (i<1 || i>list->Count) //判断所给位置是否在线性表范围内
return false;
*e = list->Data[i - 1]; //将查找位置的数据返回给所给变量
return true;
}
//按元素值查找元素位置(线性表指针,所查找的数据)
bool LocateElem(Sequential_linear_table* list, ElemType e)
{
int i = 0;
while (i < list->Count&& list->Data[i] != e)
i++; //将所给数据与线性表数组中的数据一一对比查找
if (i >= list->Count) //超出范围返回为false
return false;
else
return i + 1;
}
//插入数据元素(线性表指针,数据插入位置,将要插入的数据)
bool InsertList(Sequential_linear_table* list, int i, ElemType e)
{
int j;
if (i<1 || i>list->Count + 1)
return false; //不在范围内时返回false
i--; //将顺序表逻辑序号转化为数据数组序号
for (j = list->Count; j > i; j--) //将Data数组元素整体后移一位
list->Data[j] = list->Data[j - 1];
list->Data[i] = e; //插入元素e
list->Count++; //顺序表计数器加1
return true; //成功插入返回true
}
//删除数据元素(线性表指针,所要删除数据位置,被删除的数据值)
bool DeleteList(Sequential_linear_table* list, int i, ElemType e)
{
int j;
if (i<1 || i>list->Count) //不在范围内时返回false
return false;
i--; //将顺序表逻辑序号转化为物理序号
e = list->Data[i]; //将被删除数据值赋值给e
for (j = i; j < list->Count - 1; j++) //将Data数组元素整体前移一位
list->Data[j] = list->Data[j + 1];
list->Count--; //顺序表计数器减1
return true; //成功删除返回true
}
//入口函数
int main()
{
//游戏初始数值定义
printf("\n |约瑟夫问题测试| \n=-=-=-=-=-=-=-=-=-=-=\n");
ElemType value = 0;
int n,m,i = 1,Number_Off = 1;
printf("输入参与游戏的总人数n:");
scanf("%d", &n);
printf("输入报数上限m:");
scanf("%d", &m);
if (n > 200)
{
printf("超过游戏人数上限,程序退出\n");
exit(0);
}
//线性表初始化
Sequential_linear_table seqlist;
Sequential_linear_table* list = &seqlist;
//创建本局游戏
CreateList(list, n);
printf("\n本局游戏创建成功\n本局游戏人数为%d\n报名上限为%d\n", ListLength(list),m);
//游戏开始
while ( list->Count > 1 )
{
if (Number_Off == m)
{
DeleteList(list,i,value);
Number_Off = 1;
}
else
{
i++;
Number_Off++;
}
if ( i > list->Count )
{
i = 1;
}
}
printf("\n最后获胜的人的编号为");
ListDisplay(list);
return 0;
}
问题标题: 在线提问关于代码问题 问题内容: 我在编写代码时遇到了一个问题。尽管代码没有错误,但是运行结果却很荒谬。希望能通过你的帮助来解决这个问题。
问题详情: 我最近写了一段代码,但是当我运行它时,得到的结果非常奇怪,不符合我的预期。尽管我检查了代码多次,但仍未找到错误。我想知道是否有什么明显的问题,或者有什么调试技巧可以帮助我解决这个问题。
以下是我当前的代码,用于实现某个功能(描述功能):
# 在这里贴上你的代码
此外,我还尝试了以下方法来解决问题,但没有成功: - 我检查了变量和函数的命名,确保没有拼写错误。 - 我确保代码逻辑正确,并没有明显的错误。 - 我尝试使用不同的输入数据,但结果始终不对。
我想请你帮助我检查代码,看是否能发现我可能忽略的问题。如果可能的话,请提供一些调试技巧,以帮助我解决这个问题。谢谢!
解决方案: 首先,很高兴能帮助你解决这个问题。对于代码的问题,我们可以采用以下调试技巧来找到问题所在:
首先,检查代码的语法和拼写错误。确保代码没有任何语法错误或者变量和函数的拼写错误。
分析代码逻辑。仔细阅读代码,确保每一步的逻辑都是正确的,没有遗漏或错误的地方。
使用调试工具。如果你在使用集成开发环境(IDE)编写代码,可以使用其内置的调试功能进行逐行调试。设置断点并观察代码的执行流程,查看每个变量的值是否符合预期。通过逐步执行代码,可以找到问题所在。
输出调试信息。在代码中添加打印语句,输出变量的值和程序执行到的位置,以便更好地理解代码的执行情况。
缩小问题范围。如果整个代码的运行结果都不符合预期,可以逐步注释掉部分代码,只保留关键部分进行测试。这样可以找到是哪一部分代码引起了问题。
使用单元测试。编写一些测试用例,检查代码在各种输入情况下的输出结果是否正确。这有助于验证代码的正确性,并找到问题所在。
如果你能提供你的代码,我可以帮助你进行更具体的调试和分析。请将你的代码贴在回答中,并提供你期望的输出和实际得到的输出,这样我就能更好地帮助你解决问题了。
希望以上解决方案能对你解决问题有所帮助!如果还有其他问题,请随时向我提问。