重复输入
#include <stdio.h>
void main()
{
char ch ;
while ((ch = getchar()) != '#') {
putchar(ch);
}
}
打开一个文件并显示其中的内容,要写上路径。
#include <stdio.h>
#include <stdlib.h>
void main()
{
int ch ;
FILE *fp;
char fname[50];
printf(" enter the name of the file ");
scanf("%s",fname);
fp =fopen(fname, "r");
if (fp== NULL) {
printf(" filed to open file , bye \n");
exit(1);
}
while ((ch = getc(fp))!= EOF) {
putchar(ch);
}
fclose(fp);
}
垃圾菜数
#include <stdio.h>
int main()
{
int guess = 1;
printf(" pick an integer from 1 to 100 i will try to guess ");
printf("it .\n respond with a y if my guess is right and with ");
printf("\n an n if it is wrong .\n");
char ch;
printf("Uhhhh. is your number %d \n",guess);
while ((ch = getchar()) != 'y') {
printf(" well then is it %d \n",++guess);
}
printf(" i knew i could do it \n");
}
带有较大的I/O 问题的程序,其实就是矩阵
#include <stdio.h>
void display ( char cr ,int lines , int width )
{
int row ,col;
for (row = 1 ; row <= lines ; row++)
{
for ( col = 1 ; col <= width ; col++)
{
putchar(cr);
}
putchar ('\n');
}
}
void main()
{
int ch ;
int rows ,cols ;
while ((ch = getchar())!= '\n') {
scanf("%d %d", &rows , &cols);
display(ch,rows,cols);
}
}
#include <stdio.h>
void display ( char cr ,int lines , int width )
{
int row ,col;
for (row = 1 ; row <= lines ; row++)
{
for ( col = 1 ; col <= width ; col++)
{
putchar(cr);
}
putchar ('\n');
}
}
void main()
{
int ch ;
int rows ,cols ;
while ((ch = getchar())!= '\n') {
if ((scanf("%d %d" , &rows , & cols ) )!= 2) {
break;
}display(ch,rows,cols);
while (getchar() != '\n') {
continue;
} }
}
确认输入
#include <stdio.h>
#include <stdbool.h>
int get_int (void )
{
int input ;
char ch ;
while (scanf("%d",&input) != 1)
{
while ((ch = getchar () )!= '\n')
{
putchar(ch);
}
}
return input;
}
double sum_squares ( int a, int b)
{
double total = 0;
int i ;
for ( i = a ; i <= b ; i ++ ) {
total += i *i ;
}
return total;
}
bool bad_limits ( int begin ,int end , int low ,int high )
{
bool not_good = false;
if (begin > end) {
printf(" %d isn't smaller than %d \n ",begin,end);
not_good =true;
}
if (begin < low || end < low ) {
printf(" values nust be %d or greater \n ",low);
not_good =true;
}
if (begin > high || end>high) {
printf("values nust be %d or greater \n ",high );
not_good =true;
}
return not_good;
}
void main()
{
const int MIN = -1000;
const int MAX = +1000;
int start ;
int stop ;
double answer ;
start =get_int();
printf(" upper ilmit ");
stop =get_int();
while (start != 0 || stop != 0) {
if (bad_limits(start, stop,MIN, MAX)) {
printf("plese");
}
else
{
answer =sum_squares(start, stop);
printf("the ");
printf("for %d to %d is %g \n ",start, stop , answer);
}
printf("En");
printf("low ");
start =get_int();
printf("upp");
stop =get_int();
}
菜单技术
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
char get_choice(void );
char get_first(void);
void count(void);
int get_int(void );
void count(void)
{
int n ,i ;
printf(" count how far \n");
n = get_int();
for (int i = 1; i <= n ; i++)
{
printf("%d \n ",i);
}
while (getchar()!= '\n')
{
continue;
}
}
char get_first(void)
{
int ch ;
ch = getchar() ;
while (getchar() != '\n') {
continue;
}
return ch;
}
char get_choice(void )
{
int ch;
printf(" enter the letter of your choice \n");
printf("a.advice b.bell \n");
printf("c.count q.quit \n");
ch = get_first();
while ((ch < 'a' || ch > 'c' ) && ch != 'q' ) {
printf(" plese respond with a, b, c, or q .\n");
ch = get_first();
}
return ch ;
}
int get_int(void )
{
int input ;
char ch ;
while (scanf(" %d" , &input) != 1 ) {
while ((ch = getchar()) != '\n') {
putchar(ch);
}
printf("is not an integer ");
}
return input;
}
void main()
{
int choice;
void count (void);
while ((choice = get_choice()) != 'q') {
switch (choice) {
case 'a':
printf(" buy low , sell high \n");
break;
case 'b':
putchar('\a');
break;
case 'c':
count();
break;
case 'd':
printf(" buy low , sell high \n");
break;
default:
break;
}
}
}
网友评论