是的,我正在添加这一部分。我知道input.txt与.c文件位于同一文件夹中,我打开了该文件并确保它已打开。因此,即使我输入了正确的文件名,它仍然会崩溃。不要将不完整的程序放在上面,因为它们会被无情地撕碎。你穿的应该是MCVE()。您必须证明您以某种方式避
是的,我正在添加这一部分。我知道input.txt与.c文件位于同一文件夹中,我打开了该文件并确保它已打开。因此,即使我输入了正确的文件名,它仍然会崩溃。不要将不完整的程序放在上面,因为它们会被无情地撕碎。你穿的应该是MCVE()。您必须证明您以某种方式避,c,C,是的,我正在添加这一部分。我知道input.txt与.c文件位于同一文件夹中,我打开了该文件并确保它已打开。因此,即使我输入了正确的文件名,它仍然会崩溃。不要将不完整的程序放在上面,因为它们会被无情地撕碎。你穿的应该是MCVE()。您必须证明您以某种方式避免了愚蠢的错误(并且不检查fopen()是否工作是非常愚蠢的-在没有检查的情况下不要提交代码!)。欢迎使用堆栈溢出。据称,文件已成功打开,修改后的代码处于循环中,直到文件成功打开(或者用户感到厌烦并终止程序-希望用户没有表示EOF!)。这个问题
是的,我正在添加这一部分。我知道input.txt与.c文件位于同一文件夹中,我打开了该文件并确保它已打开。因此,即使我输入了正确的文件名,它仍然会崩溃。不要将不完整的程序放在上面,因为它们会被无情地撕碎。你穿的应该是MCVE()。您必须证明您以某种方式避免了愚蠢的错误(并且不检查
fopen()
是否工作是非常愚蠢的-在没有检查的情况下不要提交代码!)。欢迎使用堆栈溢出。据称,文件已成功打开,修改后的代码处于循环中,直到文件成功打开(或者用户感到厌烦并终止程序-希望用户没有表示EOF!)。这个问题可能会在以后出现-使用未初始化的变量。很抱歉,我是C编程新手,我不太了解调试器是什么,我看到了代码块中的选项卡。我把作业指导放在编辑过的帖子里,以防万一。或者,如果你能为我指出如何使用调试器的正确方向?谢谢你迄今为止的帮助我做了一些progress@ThingyWotsit嘿,谢谢大家的帮助,我不知道到底发生了什么,在我重新启动电脑并清理了一些代码后,现在它工作了。我仍然对调试意味着什么感兴趣?
#include<stdio.h>
#include<stdlib.h>
int main() {
FILE * ifp = NULL;
char filename[20];
printf("What is the name of the input file?\n");
scanf(" %s", &filename);
while (ifp == NULL){
/*PROMPT USER FOR INPUT FILENAME*/
printf("What is the name of the input file?\n");
scanf(" %s", &filename);
/*OPEN INPUT FILE*/
ifp = fopen(filename, "r");
}
int totalSize = 0;
fscanf(ifp, "%d", &totalSize);
int id[totalSize];
char category[totalSize];
int handCombatPt[totalSize];
int distCombatPt[totalSize];
int observationPt[totalSize];
int concealPt[totalSize];
int agilityPt[totalSize];
float ranking[totalSize];
int row=0;
for (row=0; row<totalSize; row++) {
fscanf(ifp, "%d %c %d %d %d %d %d\n", id+row, category+row, handCombatPt+row, distCombatPt+row, observationPt+row, concealPt+row, agilityPt+row);
}
for (row=0; row<totalSize; row++) {
if (category[row] == 'A') {
ranking[row] = (handCombatPt[row] + distCombatPt[row]*2 + observationPt[row]*2 + concealPt[row] + agilityPt[row]*5)/10.0;
}
if (category[row] == 'C') {
ranking[row] = (handCombatPt[row]*5 + distCombatPt[row]*5 + observationPt[row] + concealPt[row] + agilityPt[row]*2)/10.0;
}
if (category[row] == 'S') {
ranking[row] = (handCombatPt[row] + distCombatPt[row] + observationPt[row]*5 + concealPt[row]*5 + agilityPt[row]*2)/10.0;
}
}
int firstA, firstS, secondS, firstC, secondC;
for (row=0; row<totalSize; row++) {
if (category[row]=='A' && ranking[firstA] < ranking[row]) {
firstA = row;
}
if (category[row]=='S' && ranking[firstS] < ranking[row]) {
secondS = firstS;
firstS = row;
}
else if (category[row]=='S' && ranking[secondS] < ranking[row]) {
secondS = row;
}
if (category[row]=='C' && ranking[firstC] < ranking[row]) {
secondC = firstC;
firstC = row;
}
else if (category[row]=='C' && ranking[secondC] < ranking[row]) {
secondC = row;
}
}
printf("A : %d %f \n", id[firstA], ranking[firstA]);
printf("C : %d %f \n", id[firstC], ranking[firstC]);
printf("C : %d %f \n", id[secondC], ranking[secondC]);
printf("S : %d %f \n", id[firstS], ranking[firstS]);
printf("S : %d %f \n", id[secondS], ranking[secondS]);
return 0;
}
10
14 A 447 252 68 34 978
2 C 230 299 597 180 9
27 A 318 220 97 28 1317
32 C 563 450 547 112 28
8 C 669 260 200 36 171
11 S 179 45 1342 732 174
19 S 74 249 861 1165 6
21 A 757 240 97 119 2032
15 S 275 177 588 577 52
6 C 886 401 327 109 48
A: 21 1171.00
C: 6 696.70
C: 32 578.00
S: 11 1094.20
S: 19 1046.50
#include<stdio.h>
#include<stdlib.h>
int main() {
FILE * ifp = NULL;
char filename[20];
while (ifp == NULL){
printf("What is the name of the input file?\n");
scanf(" %s", &filename);
ifp = fopen(filename, "r");
}
int totalSize = 0;
fscanf(ifp, "%d", &totalSize);
int id[totalSize];
char category[totalSize];
int handCombatPt[totalSize];
int distCombatPt[totalSize];
int observationPt[totalSize];
int concealPt[totalSize];
int agilityPt[totalSize];
float ranking[totalSize];
int row=0;
for (row=0; row<totalSize; row++) {
fscanf(ifp, "%d %c %d %d %d %d %d\n", id+row, category+row, handCombatPt+row, distCombatPt+row, observationPt+row, concealPt+row, agilityPt+row);
}
for (row=0; row<totalSize; row++) {
if (category[row] == 'A') {
ranking[row] = (handCombatPt[row] + distCombatPt[row]*2 + observationPt[row]*2 + concealPt[row] + agilityPt[row]*5)/10.0;
}
if (category[row] == 'C') {
ranking[row] = (handCombatPt[row]*5 + distCombatPt[row]*5 + observationPt[row] + concealPt[row] + agilityPt[row]*2)/10.0;
}
if (category[row] == 'S') {
ranking[row] = (handCombatPt[row] + distCombatPt[row] + observationPt[row]*5 + concealPt[row]*5 + agilityPt[row]*2)/10.0;
}
}
int firstA=0, firstS=0, secondS=0, firstC=0, secondC=0;
for (row=0; row<totalSize; row++) {
if (category[row]=='A' && ranking[firstA] < ranking[row]) {
firstA = row;
}
if (category[row]=='S' && ranking[firstS] < ranking[row]) {
secondS = firstS;
firstS = row;
}
else if (category[row]=='S' && ranking[secondS] < ranking[row]) {
secondS = row;
}
if (category[row]=='C' && ranking[firstC] < ranking[row]) {
secondC = firstC;
firstC = row;
}
else if (category[row]=='C' && ranking[secondC] < ranking[row]) {
secondC = row;
}
}
printf("A : %d %f \n", id[firstA], ranking[firstA]);
printf("C : %d %f \n", id[firstC], ranking[firstC]);
printf("C : %d %f \n", id[secondC], ranking[secondC]);
printf("S : %d %f \n", id[firstS], ranking[firstS]);
printf("S : %d %f \n", id[secondS], ranking[secondS]);
return 0;
}