/* order.txt */
NC271X notebook computer / 1 @ 1729.00
LP552 Laser printer / 1 @ 1499.00
TC552V Toner cartridge / 2 @ 99.00
DS420 3.5" diskttes / 10 @ 2.99
CC542 Carrying case / 1 @ 24.99
/* Result */
NC271X notebook computer 1 @ 1729.00 = 1729.00
LP552 Laser printer 1 @ 1499.00 = 1499.00
TC552V Toner cartridge 2 @ 99.00 = 198.00
DS420 3.5" diskttes 10 @ 2.99 = 29.90
CC542 Carrying case 1 @ 24.99 = 24.99
-----------------------------------------------------------------
Total 3379.97
Element (symbol) Atomic Weight
===================================================
1. Hydorgen (H) 1.001
2. Helium (He) 4.003
3. Lithium (Li) 5.939
4. Beryllium (Be) 9.012
5. Boron (B) 10.811
6. Carbon (C) 12.011
7. Nitrogen (N) 14.007
8. Oxygen (O) 15.999
9. Fluorine (F) 18.998
10. Neon (Ne) 20.183
请按任意键继续. . .
/* main.c */
#include
#include
#define MaxElementName 15
#define MinSymbolName 2
int main()
{
FILE *infile;
char elementName[MaxElementName+1];
char elementSymbol[MinSymbolName+1];
char namebuf[20];
int atomicNumber;
double atomicWeight;
char termch;
int nscan;
char a[10];
char b[15];
char c[15];
char buffer[50];
double total = 0.0;
int num = 0;
double val = 0.0;
char *ElementFile = "order.txt";
infile = fopen(ElementFile, "r");
if (infile == NULL) {
perror("Can't open");
system("pause");
exit(1);
}
while (1)
{
nscan = fscanf(infile, "%10[^ ] %15[^ ] %15[^/]/ %d @ %lf%c",
a, b, c, &num, &val, &termch);
total += val;
if (nscan == EOF)
break;
if (nscan != 6 || termch != '\n')
perror("Improper file format");
sprintf(buffer, "%-15s%s %s", a,b,c);
printf("%-40s %-2d @ %8.2f = %8.2f \n", buffer, num, val, num*val);
}
printf("-----------------------------------------------------------------\n");
printf("Total %8.2f\n\n\n", total);
fclose(infile);
ElementFile = "elements.txt";
infile = fopen(ElementFile, "r");
printf("Element (symbol) Atomic Weight \n");
printf("===================================================\n");
while (1)
{
nscan = fscanf(infile, "%15[^,], %2[^,], %d, %lf%c",
elementName, elementSymbol,
&atomicNumber, &atomicWeight,
&termch);
if (nscan == EOF)
break;
if (nscan != 5 || termch != '\n')
perror("Improper file format");
sprintf(namebuf, "%s (%s)", elementName, elementSymbol);
printf("%3d. %-20s %8.3f\n", atomicNumber, namebuf, atomicWeight);
}
fclose(infile);
system("pause");
return 0;
}
阅读(941) | 评论(1) | 转发(0) |