📄 chap8.lst
字号:
listing 1
/* Case Switcher */
#include <conio.h>
#include <stdio.h>
#include <ctype.h>
int main(void) {
char ch;
do {
ch = getche();
if(islower(ch)) putchar(toupper(ch));
else putchar(tolower(ch));
} while (ch!='.'); /* use a period to stop*/
return 0;
}
listing 2
/* This program appears to act as a command-prompt gone wild. It
displays the command prompt but displays every character
the user types as the next letter in the alphabet.
*/
#include <stdio.h>
#include <conio.h>
int main(void)
{
char ch;
do {
printf("C>");
for(;;) {
ch = getch(); /* read chars without echo */
if(ch=='\r' || ch==1) {
printf("\n");
break;
}
putchar(ch+1);
}
} while(ch!=1) ; /* exit on control-A */
return 0;
}
listing 3
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[80];
gets(str);
printf("Length is %d", strlen(str));
return 0;
}
listing 4
puts("hello");
listing 5
printf("Hi %c %d %s", 'c', 10, "there!");
listing 6
#include <stdio.h>
int main(void)
{
double f;
for(f=1.0; f<1.0e+10; f=f*10)
printf("%g ", f);
return 0;
}
listing 7
#include <stdio.h>
int main(void)
{
unsigned num;
for(num=0; num<=16; num++) {
printf("%d ", num); /* Integer */
printf("%o ", num); /* Octal */
printf("%x ", num); /* Hexidecimal - lowercase */
printf("%X\n", num); /* Hexidecimal - uppercase */
}
return 0;
}
listing 8
#include <stdio.h>
int sample;
int main(void)
{
printf("%p", &sample);
return 0;
}
listing 9
#include <stdio.h>
int main(void)
{
int count;
printf("this%n is a test\n", &count);
printf("%d", count);
return 0;
}
listing 10
#include <stdio.h>
int main(void)
{
double item;
item = 10.12304;
printf("%f\n", item);
printf("%10f\n", item);
printf("%012f\n", item);
return 0;
}
listing 11
#include <stdio.h>
int main(void)
{
int i;
/* display a table of squares and cubes */
for(i=1; i<20; i++)
printf("%8d %8d %8d\n", i, i*i, i*i*i);
return 0;
}
listing 12
#include <stdio.h>
int main(void)
{
printf("%.4f\n", 123.1234567);
printf("%3.8d\n", 1000);
printf("%10.15s\n", "This is a simple test.");
return 0;
}
listing 13
#include <stdio.h>
int main(void)
{
printf("........................\n");
printf("right-justified:%8d\n", 100);
printf("left-justified:%-8d\n", 100);
return 0;
}
listing 14
#include <stdio.h>
int main(void)
{
printf("%x %#x\n", 10, 10);
printf("%*.*f", 10, 4, 123.3);
return 0;
}
listing 15
#include <stdio.h>
int main(void)
{
int i, j;
scanf("%o%x", &i, &j);
printf("%o %x", i, j);
return 0;
}
listing 16
unsigned num;
scanf("%u", &num);
listing 17
scanf("%c%c%c", &a, &b, &c);
listing 18
#include <stdio.h>
int main(void)
{
char str[80];
printf("Enter a string: ");
scanf("%s", str);
printf("Here's your string: %s", str);
return 0;
}
listing 19
#include <stdio.h>
int main(void)
{
char *p;
printf("Enter an address: ");
scanf("%p", &p);
printf("Value at location %p is %c\n", p, *p);
return 0;
}
listing 20
%[XYZ]
listing 21
#include <stdio.h>
int main(void)
{
int i;
char str[80], str2[80];
scanf("%d%[abcdefg]%s", &i, str, str2);
printf("%d %s %s", i, str, str2);
return 0;
}
listing 22
%[A-Z]
listing 23
/* A scanset example using ranges. */
#include <stdio.h>
int main(void)
{
char s1[80], s2[80];
printf("Enter numbers, then some letters");
scanf("%[0-9]%[a-zA-Z]", s1, s2);
printf("%s %s", s1, s2);
return 0;
}
listing 24
/* A scanset example using inverted ranges. */
#include <stdio.h>
int main(void)
{
char s1[80], s2[80];
printf("Enter non-numbers, then some non-letters");
scanf("%[^0-9]%[^a-zA-Z]", s1, s2);
printf("%s %s", s1, s2);
return 0;
}
listing 25
scanf("%d", &count);
listing 26
scanf("%s", str);
listing 27
scanf("%20s", str);
listing 28
scanf("%s", str2);
listing 29
scanf("%d%*c%d", &x, &y);
listing 30
FILE *fp;
listing 31
FILE *fp;
fp = fopen("test", "w");
listing 32
FILE *fp;
if((fp = fopen("test", "w"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
listing 33
do {
ch = getc(fp);
} while(ch!=EOF);
listing 34
/* KTOD: A key to disk program. */
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp;
char ch;
if(argc!=2) {
printf("You forgot to enter the filename.\n");
exit(1);
}
if((fp=fopen(argv[1], "w")) == NULL) {
printf("Cannot open file.\n");
exit(1);
}
do {
ch = getchar();
putc(ch, fp);
} while (ch != '$');
fclose(fp);
return 0;
}
listing 35
/* DTOS: A program that reads text files
and displays them on the screen. */
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp;
char ch;
if(argc!=2) {
printf("You forgot to enter the filename.\n");
exit(1);
}
if((fp=fopen(argv[1], "r")) == NULL) {
printf("Cannot open file.\n");
exit(1);
}
ch = getc(fp); /* read one character */
while (ch!=EOF) {
putchar(ch); /* print on screen */
ch = getc(fp);
}
fclose(fp);
return 0;
}
listing 36
while(!feof(fp)) ch = getc(fp);
listing 37
/* This program will copy a file to another. */
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *in, *out;
char ch;
if(argc!=3) {
printf("You forgot to enter a filename.\n");
exit(1);
}
if((in=fopen(argv[1], "rb")) == NULL) {
printf("Cannot open source file.\n");
exit(1);
}
if((out=fopen(argv[2], "wb")) == NULL) {
printf("Cannot open destination file.\n");
exit(1);
}
/* This code actually copies the file. */
while(!feof(in)) {
ch = getc(in);
if(!feof(in)) putc(ch, out);
}
fclose(in);
fclose(out);
return 0;
}
listing 38
#include <stdio.h>
int main(void)
{
char s[80];
printf("Enter a string: ");
fgets(s, 80, stdin);
printf("Here is your string: %s", s);
return 0;
}
listing 39
/* Write a floating point number to a disk file. */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
float f = 12.23;
if((fp=fopen("test", "wb"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
fwrite(&f, sizeof(float), 1, fp);
fclose(fp);
return 0;
}
listing 40
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
register int i;
FILE *fp;
float balance[100];
/* open for write */
if((fp=fopen("balance", "wb"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
for(i=0; i<100; i++) balance[i] = (float) i;
/* this saves the entire balance array in one step */
fwrite(balance, sizeof balance, 1, fp);
fclose(fp);
/* zero array */
for(i=0; i<100; i++) balance[i] = 0.0;
/* open for read */
if((fp=fopen("balance","rb"))==NULL) {
printf("cannot open file\n");
exit(1);
}
/* this reads the entire balance array in one step */
fread(balance, sizeof balance, 1, fp);
/* display contents of array */
for(i=0; i<100; i++) printf("%f ", balance[i]);
fclose(fp);
return 0;
}
listing 41
int func1(void)
{
FILE *fp;
if((fp=fopen("test", "rb")) == NULL) {
printf("Cannot open file.\n");
exit(1);
}
fseek(fp, 234L, 0);
return getc(fp); /* read one character */
/* at 234th position */
}
}
listing 42
/* DUMP: A simple disk look utility using fseek. */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define SIZE 128
void display(int numread);
char buf[SIZE];
void display();
int main(int argc, char *argv[])
{
FILE *fp;
int sector, numread;
if(argc!=2) {
printf("Usage: dump filename\n");
exit(1);
}
if((fp=fopen(argv[1], "rb"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
do {
printf("Enter sector: ");
scanf("%d", §or);
if(sector >= 0) {
if(fseek(fp, sector*SIZE, SEEK_SET)) {
printf("seek error");
}
if((numread=fread(buf, 1, SIZE, fp)) != SIZE)
printf("EOF reached.");
display(numread);
}
} while(sector>=0);
return 0;
}
/* Display the contents of a file. */
void display(int numread)
{
int i, j;
for(i=0; i<numread/16; i++) {
for(j=0; j<16; j++) printf("%3X", buf[i*16+j]);
printf(" ");
for(j=0; j<16; j++) {
if(isprint(buf[i*16+j])) printf("%c", buf[i*16+j]);
else printf(".");
}
printf("\n");
}
}
listing 43
/* A remove() example. */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char fname[80];
printf("Name of file to remove: ");
gets(fname);
if(remove(fname)) {
printf("Error removing file\n");
exit(1);
}
return 0;
}
listing 44
int putchar(int c)
{
return putc(c, stdout);
}
listing 45
#include <stdio.h>
int main(void)
{
char str[80];
printf("Enter a string: ");
gets(str);
printf(str);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -