gpt4 book ai didi

c - 二维数组正在截断一些字符

转载 作者:行者123 更新时间:2023-11-30 20:41:26 25 4
gpt4 key购买 nike

我只需要知道如何解决这个问题。

#include<stdio.h>
#include<stdlib.h>

#define PRICE 1985.95
#define MAX_LENGTH 20
#define MAX_EMPLOYEES 25
#define MAX_WEEKS 7

void getNames(int* employeeNumber, char names[][MAX_LENGTH]);
void getSales(int* employeeNumber,int* weeksNumber, int weeks[][MAX_WEEKS+1]);

void calcEachTotal(int empNumber, int weekNumber, int weeks[][MAX_WEEKS+1]);
void calcTotal(int,int,int*,int [][MAX_WEEKS+1],int [][3]);
void calcCoTot(int ,int*, int [][3]);

void outputDataScreen(int,int,int,char [][MAX_LENGTH],int [][MAX_WEEKS+1], int [][3]);
void outputDataFile(int,int,int,char [][MAX_LENGTH],int [][MAX_WEEKS+1], int [][3]);

int main (void)
{
char employees[MAX_EMPLOYEES][MAX_LENGTH] = {{0}};
//holds personal sales info and each employees total sales
int sales[MAX_EMPLOYEES][MAX_WEEKS+1]={{0}};
//holds the weekly lowest,highest and total
int weeklyStats[MAX_WEEKS][3] = {{0}};
int employeeNumber;
int weeksNumber;
int companyTotal = 0;



getNames(&employeeNumber,employees);
getSales(&employeeNumber, &weeksNumber,sales);
calcEachTotal(employeeNumber, weeksNumber, sales);
//call function to calculate personal,weekly and company total sales
calcTotal(employeeNumber,weeksNumber,&companyTotal,sales,weeklyStats);
calcCoTot(weeksNumber,&companyTotal, weeklyStats);
//call two functions to output the data to the screen and the output text file
outputDataScreen(employeeNumber,weeksNumber,companyTotal,employees,sales,weeklyStats);
outputDataFile(employeeNumber,weeksNumber,companyTotal,employees,sales,weeklyStats);




return 1;


}


/*===============getNames======================
Pre : NAMES.txt is an open file.
Post : Number of employees
This function will read the number of emplyees
and close if there is more than 25 employees
*/


void getNames(int* employeeNumber, char names[][MAX_LENGTH])
{

FILE* spNames;

int i;
int j;

// statements
spNames = fopen("/Users/r3spectak/Desktop/NAMES.TXT", "r");

if (!spNames)
{
printf("Could not open NAMES.txt\n\n");
exit(100);
}

fscanf(spNames,"%d",employeeNumber);

if (*employeeNumber > MAX_EMPLOYEES )
{
printf("Unable to read more than 25 employees\n\n");
exit(100);
}

for(i = 0; i< *employeeNumber;i++)
for(j = 0; j<= MAX_LENGTH; j++)
fscanf(spNames,"%c", &names[i][j]);




fclose(spNames);


}

/*=====getSales==============
Pre : SALES.txt is an open file.
number of week and employee number with the array of weeks.
Post : number of weeks
This function reads the number of weaks and close
*/

void getSales(int* employeeNumber,int* weeksNumber, int weeks[][MAX_WEEKS+1])
{
// Local variables
FILE* spSales;

int i;
int j;

// Statements
spSales = fopen("/Users/r3spectak/Desktop/SALES.TXT", "r");

if (!spSales)
{
printf("Could not open Sales.txt\n\n");
exit(102);
}

fscanf(spSales,"%d",weeksNumber);

if (*weeksNumber > MAX_WEEKS)
{
printf("Unable to read more than 6 weeks\n\n");
exit(102);
}

for(i = 0 ; i < *employeeNumber;i++)
for(j = 0 ; j < *weeksNumber ; j++)
fscanf(spSales,"%d", &weeks[i][j]);

fclose(spSales);
}


/*===========calcEachTotal====================
Pre : Number of employees, Number of weeks
Post : Employee total
This function calculates the weekly total for each employee and store in an array
*/
void calcEachTotal(int employeeNumber, int weekNumber, int weeks[][MAX_WEEKS+1])
{

// Local Variables
int i;
int j;

// Statements
for(i = 0; i< employeeNumber; i++)
for(j=0, weeks[i][MAX_WEEKS] = 0; j< weekNumber;j++)
weeks[i][MAX_WEEKS] += weeks[i][j];

}
/*===========calcTotal====================
Pre : the number of the employees and the weeks , two arrays that hold the names and sales info and Stats array which is for weekly low,high and total
Post : calculates the weekly total for each employee. the highest, lowest and the total of each week and the company total and stores them in the arrays
This function
*/

void calcTotal(int employeeNumber, int weekNumber,int *companyTotal, int weeks[][MAX_WEEKS+1],int stats[][3])
{
// Local Variables
int i;
int j;

// Statements
//calculate the weekly highest, lowest and total and store it in the stats array
for (i = 0 ; i<weekNumber ; i++)
{
stats[i][0]=weeks[0][i];
stats[i][1]=weeks[0][i];
stats[i][2]=0;
for (j=0; j <employeeNumber; j++)
{
if (weeks[j][i] > stats[i][0])
stats[i][0] = weeks[j][i];

if (weeks[j][i] < stats[i][1])
stats[i][1] = weeks[j][i];

stats[i][2] += weeks[i][j];

}
}

}

/*===========calcCoTot====================
Pre : the number of the employees and the weeks , two arrays that hold the names and sales info and Stats array which is for weekly low,high and total
Post : calculates the weekly total for each employee. the highest, lowest and the total of each week and the company total and stores them in the arrays
This function
*/

void calcCoTot(int weekNumber,int *companyTotal, int stats[][3])
{
// Local Variables
int i;
int j;


//calculate the company total
for( i = 0; i < weekNumber ; i++)
*companyTotal += stats[i][2];

}

//===============outputDataScreen========
/*
pre : the number of the workers and weeks, a char array that holds the names , an int array for personal sales and
another int array stats which holds weekly info , also another int for company total
post : outputs all the information in a table to the screen
*/

void outputDataScreen(int empNum,int weeks,int companyTotal,char names[][MAX_LENGTH],int sales[][MAX_WEEKS+1], int stats[][3])

{
// Local Variables
int i;
int j;
int k;

// Statements
printf("\t\tHomework 2: Two Dimensional Arrays\n");
printf(" *** Sales Table *** \n\n");

printf("=============\t\t");
for ( i = 0; i < weeks; i++)
printf("======\t");

printf("\nSalesperson\t\t");

for ( i = 0; i<weeks; i++)
printf("Week%d\t",i+1);

printf("\n=============\t\t");

for ( i = 0; i < weeks; i++)
printf("======\t");

for ( i = 0 ; i < empNum ; i++)
{
for ( j = 0 ; j<= MAX_LENGTH; j++)
printf("%c",names[i][j]);
//printf("");

for(k = 0; k < weeks;k++)
printf("\t%d",(int)sales[i][k]);
}

printf("\n*** The output is in OUTPUT.TXT ***\n\t\t\tEnd of the program!\n\t\t\tHave a great day!\n");


}


//===============outputDataFile=======
/*
pre : the number of the workers and weeks, a char array that holds the names , an int array for personal sales and
another int array stats which holds weekly info , also another int for company total
post : outputs all the information in a table to the File
*/

void outputDataFile(int empNum,int weeks,int companyTotal,char names[][MAX_LENGTH],int sales[][MAX_WEEKS+1], int stats[][3])

{
// Local Variables
FILE* fpOut;
int i;
int j;
int k;



//open the txt file to output the information
// Statements
fpOut = fopen("/Users/r3spectak/Desktop/OUTPUT.TXT","w");

fprintf(fpOut,"\t\tHomework 2: Two Dimensional Arrays\n");
fprintf(fpOut," *** Sales Table *** \n\n");
fprintf(fpOut,"Number of Employees: %d\n",empNum);
fprintf(fpOut,"Number of Weeks: %d\n\n",weeks);

fprintf(fpOut,"Personal sales info\n");


fprintf(fpOut,"=============\t\t");
for ( i = 0; i < weeks; i++)
fprintf(fpOut,"======\t");

fprintf(fpOut,"\nSalesperson\t\t");

for ( i = 0; i<weeks; i++)
fprintf(fpOut,"Week%d\t",i+1);

fprintf(fpOut,"\n=============\t\t");

for ( i = 0; i < weeks; i++)
fprintf(fpOut,"======\t");



for ( i = 0 ; i < empNum ; i++)
{

for ( j = 0 ; j < MAX_LENGTH ; j++)
fprintf(fpOut,"%c",names[i][j]);
fprintf(fpOut," ");

for(k = 0 ; k < weeks;k++)
fprintf(fpOut,"%-.2f ",(double)sales[i][k]*PRICE);

fprintf(fpOut,"%-.2f ",(double)sales[i][MAX_WEEKS]*PRICE);

fprintf(fpOut,"*\n");


}

fprintf(fpOut,"\n\nWEEKLY INFO :\n\n");

fprintf(fpOut,"HIGHEST SALE ");

for (i=0 ; i < weeks ; i++ )
fprintf(fpOut,"%-.2f ",(double)stats[i][0]*PRICE);

fprintf(fpOut,"\n");

fprintf(fpOut,"LOWEST SALE ");

for (i=0 ; i < weeks ; i++ )
fprintf(fpOut,"%-.2f ",(double)stats[i][1]*PRICE);

fprintf(fpOut,"\n");


fprintf(fpOut,"WEEKLY TOTAL ");

for (i=0 ; i < weeks ; i++ )
fprintf(fpOut,"%-.2f ",(double)stats[i][2]*PRICE);

fprintf(fpOut,"\nCOMPANY TOTAL SALES : %-.2lf\n\n",(double)companyTotal*PRICE);

fclose(fpOut);


}

NAMES.txt

    16
Kelly, Victor
Lam, Gary
Nagasake, David
Nguyen, Jeff
Nguyen, Michael
Sinn, Scott
Smith, Jacob
Son, Thai
Tavares, Maribel
Tran, Diane
Tsukamoto, Andrew
Wang, Mary
Young, Daniel
Wells, Steve
Wong, Justin
Johnson, Mary

销售.txt

    4
25 20 25 25
20 22 23 22
25 26 25 22
30 28 25 26
25 30 45 20
30 25 20 21
27 25 24 26
20 23 24 20
28 26 24 25
30 10 35 32
28 29 30 35
15 16 15 14
12 15 12 19
20 24 20 18
10 15 12 16
32 30 33 29

该程序可以正常工作,但我遇到了数组问题。它打印不正确。字符顺序乱了,有些名字被截断了。数组的声明有问题吗?或者我必须打印空白区域吗?有人请帮助我!

输出.txt

 *** Sales Table *** 

Number of Employees: 16
Number of Weeks: 4

Personal sales info
============= ====== ====== ====== ======
Salesperson Week1 Week2 Week3 Week4
============= ====== ====== ====== ======
Kelly, Victor 49648.75 39719.00 49648.75 49648.75 188665.25 *

Lam, Gary 39719.00 43690.90 45676.85 43690.90 172777.65 *

Nagasake, David 49648.75 51634.70 49648.75 43690.90 194623.10 *

Nguyen, Jeff 59578.50 55606.60 49648.75 51634.70 216468.55 *

Nguyen, Michae 49648.75 59578.50 89367.75 39719.00 238314.00 *

Sinn, Scott 59578.50 49648.75 39719.00 41704.95 190651.20 *

Smith, Jacob 53620.65 49648.75 47662.80 51634.70 202566.90 *

Son, Thai 39719.00 45676.85 47662.80 39719.00 172777.65 *

Tavares, M 55606.60 51634.70 47662.80 49648.75 204552.85 *
ribel
Tran, Dia 59578.50 19859.50 69508.25 63550.40 212496.65 *
e
Tsukamot 55606.60 57592.55 59578.50 69508.25 242285.90 *
, Andrew
Wang, M 29789.25 31775.20 29789.25 27803.30 119157.00 *
ry
Young, 23831.40 29789.25 23831.40 37733.05 115185.10 *
Daniel
Wells 39719.00 47662.80 39719.00 35747.10 162847.90 *
Steve
Wong 19859.50 29789.25 23831.40 31775.20 105255.35 *
Justin
Joh 63550.40 59578.50 65536.35 57592.55 246257.80 *


WEEKLY INFO :

HIGHEST SALE 63550.40 59578.50 89367.75 69508.25
LOWEST SALE 19859.50 19859.50 23831.40 27803.30
WEEKLY TOTAL 722885.80 734801.50 822183.30 909565.10
COMPANY TOTAL SALES : 3189435.70

最佳答案

当你读到名字时,你不只读到名字那么长,您总是读取 MAX_LENGTH 个字符。所以第一个员工的名字不会结束当您认为确实如此时...它始终是 MAX_LENGTH 字符。另外,你永远不会以 null 终止你的字符串...names[i][j] = '\0'。

for(i = 0; i< *employeeNumber;i++)
for(j = 0; j<= MAX_LENGTH; j++)
fscanf(spNames,"%c", &names[i][j]);

为什么使用 scanf?这一直是我痛苦的根源。尝试使用 fgets().. 读取该行。基于线路的 IO 更好。如果您使用 scanf("%c") 我不知道它对换行符有何作用。

char *fgets(char *s, int size, FILE *stream);

for(i = 0; i< *employeeNumber;i++) {
fgets(names[i], MAX_LENGTH, spNames);
// trim the newline from the name (there is probably a better way)
int len = strlen(names[i]) - 1; // -1 because len=1 has only index 0
if (names[i][len] == '\n') names[i][len] = '\0';
}

关于c - 二维数组正在截断一些字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14742836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com