上机练习19
p314.c
# include <stdio.h>
# include <stdlib.h>
# include <string.h> int main ( void )
{ FILE * fp; char name[ 21 ] ; char inputName[ 21 ] ; char size[ 11 ] ; float price; int number; int flag = 0 ; printf ( "Please input shang pin pin ming:" ) ; scanf ( "%s" , inputName) ; fp = fopen ( "sp.txt" , "r" ) ; if ( NULL == fp) { printf ( "file open error.\n" ) ; exit ( 1 ) ; } printf ( "\ncha zhao qing kuang:\n" ) ; while ( fscanf ( fp, "%s %s %f %d" , name, size, & price, & number) == 4 ) { if ( strcmp ( inputName, name) == 0 ) { flag = 1 ; printf ( "%s,%s,%d,%.2f\n" , name, size, number, price) ; } } if ( flag == 0 ) { printf ( "mei you shang pin :%s" , inputName) ; } fclose ( fp) ; fp = NULL ; return 0 ;
}
p320.c
# include <stdio.h>
# include <stdlib.h> int main ( void )
{ int num1, num2; int result; char ch; FILE * fp; fp = fopen ( "Comp.txt" , "r" ) ; if ( NULL == fp) { printf ( "file open error! \n" ) ; exit ( 1 ) ; } while ( 3 == fscanf ( fp, "%d %c %d" , & num1, & ch, & num2) ) { if ( '+' == ch) { result = num1 + num2; printf ( "%d + %d = %d\n" , num1, num2, result) ; } else if ( '-' == ch) { result = num1 - num2; printf ( "%d - %d = %d\n" , num1, num2, result) ; } else { printf ( "operator error!\n" ) ; fclose ( fp) ; exit ( 2 ) ; } } fclose ( fp) ; fp = NULL ; return 0 ;
}
p321.c
# include <stdio.h>
# include <stdlib.h> int main ( void )
{ int num1, num2; int result; char ch; FILE * fp; int i = 0 ; fp = fopen ( "Comp.txt" , "r" ) ; if ( NULL == fp) { printf ( "file open error! \n" ) ; exit ( 1 ) ; } while ( 3 == fscanf ( fp, "%d %c %d" , & num1, & ch, & num2) ) { if ( '+' == ch) { result = num1 + num2; printf ( "Line %03d: %5d + %-5d = %-6d\n" , i + 1 , num1, num2, result) ; } else if ( '-' == ch) { result = num1 - num2; printf ( "Line %03d: %5d - %-5d = %-6d\n" , i + 1 , num1, num2, result) ; } else { printf ( "operator error!\n" ) ; fclose ( fp) ; exit ( 2 ) ; } i++ ; } fclose ( fp) ; fp = NULL ; return 0 ;
}
p322.c
# include <stdio.h>
# include <stdlib.h> int main ( void )
{ FILE * fp; int num1, num2; int result; char op; int i = 0 ; char line[ 201 ] ; fp = fopen ( "Comp.txt" , "r" ) ; if ( NULL == fp) { printf ( "file open error!\n" ) ; exit ( 1 ) ; } while ( 1 ) { if ( fgets ( line, 200 , fp) == NULL ) { break ; } if ( EOF == sscanf ( line, "%d %c %d" , & num1, & op, & num2) ) { } else { if ( 3 == sscanf ( line, "%d %c %d" , & num1, & op, & num2) ) { if ( op == '+' ) { result = num1 + num2; printf ( "Line %03d: %5d + %-5d = %-6d\n" , i + 1 , num1, num2, result) ; } else if ( op == '-' ) { result = num1 - num2; printf ( "Line %03d: %5d - %-5d = %-6d\n" , i + 1 , num1, num2, result) ; } else { printf ( "Line %03d: Error!\n" , i + 1 ) ; } } else { printf ( "Line %03d: Error!\n" , i + 1 ) ; } } i++ ; } fclose ( fp) ; fp = NULL ; return 0 ;
}
p323.c
# include <stdio.h>
# include <stdlib.h>
int calculate ( int d1, char op, int d2) ; int main ( void )
{ int num1, num2, num3; char op1, op2; int result; FILE * fp; fp = fopen ( "Comp.txt" , "r" ) ; if ( NULL == fp) { printf ( "file open error!\n" ) ; exit ( 1 ) ; } while ( 5 == fscanf ( fp, "%d %c %d %c %d" , & num1, & op1, & num2, & op2, & num3) ) { if ( op2 == '*' ) { result = calculate ( num2, op2, num3) ; result = calculate ( num1, op1, result) ; } else { result = calculate ( num1, op1, num2) ; result = calculate ( result, op2, num3) ; } printf ( "%d %c %d %c %d = %d\n" , num1, op1, num2, op2, num3, result) ; } fclose ( fp) ; fp = NULL ; return 0 ;
} int calculate ( int d1, char op, int d2)
{ if ( op == '+' ) { return d1 + d2; } else if ( op == '-' ) { return d1 - d2; } else { return d1 * d2; }
}
p324.c
# include <stdio.h>
# include <stdlib.h> int main ( void )
{ int num1, num2; int result; char ch; FILE * fp1, * fp2, * fp3; int i = 0 ; fp1 = fopen ( "Comp.txt" , "r" ) ; if ( NULL == fp1) { printf ( "file1 open error! \n" ) ; exit ( 1 ) ; } fp2 = fopen ( "CompA.txt" , "r" ) ; if ( NULL == fp2) { printf ( "file2 open error! \n" ) ; fclose ( fp1) ; exit ( 1 ) ; } fp3 = fopen ( "CompB.txt" , "r" ) ; if ( NULL == fp3) { printf ( "file3 open error! \n" ) ; fclose ( fp1) ; fclose ( fp2) ; exit ( 1 ) ; } while ( 1 == fscanf ( fp1, "%d" , & num1) && 1 == fscanf ( fp2, " %c" , & ch) && 1 == fscanf ( fp3, "%d" , & num2) ) { if ( '+' == ch) { result = num1 + num2; } else if ( '-' == ch) { result = num1 - num2; } else { printf ( "operator error!\n" ) ; fclose ( fp1) ; fclose ( fp2) ; exit ( 2 ) ; } printf ( "Line %03d: %5d %c %-5d = %-6d\n" , i + 1 , num1, ch, num2, result) ; i++ ; } fclose ( fp1) ; fclose ( fp2) ; fclose ( fp3) ; fp1 = NULL ; fp2 = NULL ; fp3 = NULL ; return 0 ;
}
p325.c
# include <stdio.h>
# include <stdlib.h> int main ( void )
{ int num1, num2; int result; char ch; FILE * fp1, * fp2, * fp3, * fp4; int i = 0 ; fp1 = fopen ( "Comp3.txt" , "r" ) ; if ( NULL == fp1) { printf ( "file1 open error! \n" ) ; exit ( 1 ) ; } fp2 = fopen ( "CompA.txt" , "r" ) ; if ( NULL == fp2) { printf ( "file2 open error! \n" ) ; fclose ( fp1) ; exit ( 1 ) ; } fp3 = fopen ( "CompB.txt" , "r" ) ; if ( NULL == fp3) { printf ( "file3 open error! \n" ) ; fclose ( fp1) ; fclose ( fp2) ; exit ( 1 ) ; } fp4 = fopen ( "CompC.txt" , "w" ) ; if ( NULL == fp3) { printf ( "file4 open error! \n" ) ; fclose ( fp1) ; fclose ( fp2) ; fclose ( fp3) ; exit ( 1 ) ; } while ( 1 == fscanf ( fp1, "%d" , & num1) && 1 == fscanf ( fp2, " %c" , & ch) && 1 == fscanf ( fp3, "%d" , & num2) ) { if ( '+' == ch) { result = num1 + num2; } else if ( '-' == ch) { result = num1 - num2; } else { printf ( "operator error!\n" ) ; exit ( 2 ) ; } fprintf ( fp4, "Line %03d: %5d %c %-5d = %-6d\n" , i + 1 , num1, ch, num2, result) ; i++ ; } fclose ( fp1) ; fclose ( fp2) ; fclose ( fp3) ; fclose ( fp4) ; fp1 = NULL ; fp2 = NULL ; fp3 = NULL ; fp4 = NULL ; return 0 ;
}
p328.c
# include <stdio.h>
# include <stdlib.h> int main ( void )
{ char name[ 128 ] ; int chI; char ch; FILE * fp; printf ( "input the file's name: " ) ; gets ( name) ; fp = fopen ( name, "r" ) ; if ( NULL == fp) { printf ( "\nfile open error!" ) ; exit ( 1 ) ; } printf ( "------------------------File Begin:----------------------\n" ) ; while ( ( chI = fgetc ( fp) ) != EOF ) { ch = ( char ) chI; putchar ( ch) ; } printf ( "\n------------------------ File End. ----------------------\n" ) ; fclose ( fp) ; fp = NULL ; return 0 ;
}
p330.c
# include <stdio.h>
# include <stdlib.h> int main ( void )
{ char name[ 128 ] ; int chI; char ch; FILE * fp; printf ( "input the file's name: " ) ; gets ( name) ; fp = fopen ( name, "r" ) ; if ( NULL == fp) { printf ( "\nfile open error!" ) ; exit ( 1 ) ; } printf ( "------------------------File Begin:----------------------\n" ) ; while ( ( chI = fgetc ( fp) ) != EOF ) { ch = ( char ) chI; if ( ch == '*' ) { putchar ( '@' ) ; } else { putchar ( ch) ; } } printf ( "\n------------------------ File End. ----------------------\n" ) ; fclose ( fp) ; fp = NULL ; return 0 ;
}
p796.c
# include <stdio.h>
# include <stdlib.h> int main ( void )
{ FILE * fp; char ch; fp = fopen ( "Test.txt" , "w" ) ; if ( NULL == fp) { printf ( "\nCreate file error!\n" ) ; exit ( 1 ) ; } printf ( "Input chars: " ) ; while ( 1 ) { ch = getchar ( ) ; if ( '!' == ch) { break ; } if ( ch >= 'a' && ch <= 'z' ) { ch -= 'a' - 'A' ; } if ( ( fputc ( ch, fp) ) == EOF ) { printf ( "\nWriting file error!\n" ) ; fclose ( fp) ; exit ( 2 ) ; } } fclose ( fp) ; fp = NULL ; return 0 ;
}