- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我只需要知道如何解决这个问题。
#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/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及预
为什么在 C# 中添加两个 char 结果是 int 类型? 例如,当我这样做时: var pr = 'R' + 'G' + 'B' + 'Y' + 'P'; pr 变量变为 int 类型。我希望它是
下面的代码可以编译,但 char 类型的行为与 int 类型的行为不同。 特别是 cout ::ikIsX >() ::ikIsX >() ::ikIsX >() using names
我正在寻找一个正则表达式,它可以匹配长度为 1 个或多个字符但不匹配 500 的内容。这将在 Rails 路由文件中使用,特别是用于处理异常。 路线.rb match '/500', to: 'err
对于 C 编程作业,我正在尝试编写几个头文件来检查所谓的“X 编程语言”的语法。我最近才开始,正在编写第一个头文件。这是我编写的代码: #ifndef _DeclarationsChecker_h_
为什么扩展的 ascii 字符(â、é 等)被替换为 字符? 我附上了一张图片...但我正在使用 PHP 从 MySQL 中提取数据,其中一些位置有扩展字符...我使用的是 Arial 字体。 您可以
我有一个与 R 中的断线相关的简单问题。 我正在尝试粘贴,但在获取(字符/数字)之间的断线时遇到问题。请注意,这些值包含在向量中(V1=81,V2=55,V3=25)我已经尝试过这段代码: cat(p
如何将 ANSI 字符 (char) 转换为 Unicode 字符 (wchar_t),反之亦然? 是否有用于此目的的任何跨平台源代码? 最佳答案 是的,在 中你有mbstowcs()和 wcsto
函数 fromCharCode 不适用于国际 ANSI 字符。例如,对于 ID 为 192 到 223 的俄语 ANSI (cp-1251) 字符,它返回特殊字符。如何解决这个问题? 我认为,需要将A
如果不喜欢,我想隐藏 id,但不起作用 SELECT * FROM character, character_actor WHERE character.id NOT LIKE character_a
现在这个程序成功地反转了键盘输入的单词。但是我想在我反转它之前“保存”指针中的单词,所以我可以比较两者,反转的和“原始的”,并检查它们是否是回文。我还没有太多经验,可能会出现比我知道的更多的错误,但我
Memcpy 和 memcmp 函数可以接受指针变量吗? char *p; char* q; memcpy(p,q,10); //will this work? memcmp(p,q,10); //w
恐怕我对一个相当过饱和的主题的细节有疑问,我搜索了很多,但找不到一个明确的答案来解决这个特定的明显-imho-重要的问题: 使用UTF-8将byte[]转换为String时,每个字节(8bit)都变成
我有一个奇怪的问题。我需要从 stat 命令打印输出字符串。 我已经编写了获取一些信息的代码。 import glob import os for file in glob.glob('system1
我正在使用 Java 并具有其值如下所示的字符串, String data = "vale-cx"; data = data.replaceAll("\\-", "\\-\\"); 我正在替换其中的“
String urlParameters = "login=test&password=te&ff"; 我有一个String urlParams,& - 是密码的一部分,如何使其转义,从而不被识别为分
大家好,我只想从此字符串中提取第一个字母: String str = "使 徒 行 傳 16:31 ERV-ZH"; 我只想获取这些字符: 使 徒 行 傳 并且不包括 ERV-ZH 仅数
这个问题已经有答案了: Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized point
所以, 我有一个字符**;它本质上是一个句子,带有指向该句子中每个单词的指针;即 'h''i''\0''w''o''r''l''d''\0''y''a''y''!''\0' 在这种情况下,我希望使用可
这个问题在这里已经有了答案: Using quotation marks inside quotation marks (12 个答案) 关闭 7 年前。 如何打印 " 字符? 我知道打印 % 符号
我是一名优秀的程序员,十分优秀!