Classic C program Example ==81--90
"Program 81"
Title: 809*?? =800*?? +9*?? +1 where?? The two-digit number represented, 8*?? The result is a two-digit number, 9*?? The result is a 3-digit number. Please?? The two-digit number represented, and 809*?? After the result.
1. Program Analysis:
2. Program source code: output (Long b,long i)
{printf ("\n%ld/%ld=809*%ld+%ld", b,i,i,b%i);
}
Main ()
{long int a,b,i;
a=809;
for (i=10;i<100;i++)
{b=i*a+1;
if (b>=1000&&b<=10000&&8*i<100&&9*i>=100)
Output (b,i); }
}
==============================================================
"Program 82"
Title: Octal Conversion to decimal
1. Program Analysis:
2. Program Source code:
Main ()
{char *p,s[6];int n;
P=s;
Gets (p);
n=0;
while (* (p)! = ' + ')
{n=n*8+*p-' 0 ';
p++;}
printf ("%d", n);
}
==============================================================
"Program 83"
Title: An odd number of 0-7 can be composed.
1. Program Analysis:
2. Program Source code:
Main ()
{
Long sum=4,s=4;
Int J;
for (j=2;j<=8;j++)/*j was place of number*/
{printf ("\n%ld", sum);
if (j<=2)
s*=7;
Else
s*=8;
Sum+=s;}
printf ("\nsum=%ld", sum);
}
==============================================================
"Program 84"
Title: An even number is always expressed as the sum of two primes.
1. Program Analysis:
2. Program Source code:
#include "stdio.h"
#include "math.h"
Main ()
{int a,b,c,d;
scanf ("%d", &a);
for (b=3;b<=a/2;b+=2)
{for (c=2;c<=sqrt (b); C + +)
if (b%c==0) break;
if (c>sqrt (b))
D=a-b;
Else
Break
For (C=2;c<=sqrt (d); C + +)
if (d%c==0) break;
if (c>sqrt (d))
printf ("%d=%d+%d\n", a,b,d);
}
}
==============================================================
"Program 85"
Title: Judging a prime number can be divisible by several 9
1. Program Analysis:
2. Program Source code:
Main ()
{long int m9=9,sum=9;
int zi,n1=1,c9=1;
scanf ("%d", &zi);
while (n1!=0)
{if (!) ( Sum%zi))
n1=0;
Else
{m9=m9*10;
SUM=SUM+M9;
c9++;
}
}
printf ("%ld,can is divided by%d \" 9\ "", SUM,C9);
} ==============================================================
"Program 86"
Title: Two string connection programs
1. Program Analysis:
2. Program Source code:
#include "stdio.h"
Main ()
{char a[]= "acegikm";
Char b[]= "BDFHJLNPQ";
Char c[80],*p;
int i=0,j=0,k=0;
while (a[i]!= ' &&b[j]!= ')
{if (A[i]<b[j])
{c[k]=a[i];i++;}
Else
C[k]=b[j++];
k++;
}
c[k]= ' + ';
if (a[i]== ')
P=b+j;
Else
P=a+i;
strcat (C,P);
Puts (c);
}
==============================================================
"Program 87"
Title: Answer result (struct variable pass)
1. Program Analysis:
2. Program Source code:
#include "stdio.h"
struct student
{int x;
char c;
A
Main ()
{a.x=3;
A.c= ' a ';
f (a);
printf ("%d,%c", A.X,A.C);
}
F (struct student B)
{
b.x=20;
b.c= ' Y ';
}
==============================================================
"Program 88"
Title: Reads an integer value of 7 numbers (1-50), each reading a value, the program prints out the number of *.
1. Program Analysis:
2. Program Source code:
Main ()
{int i,a,n=1;
while (n<=7)
{do {
scanf ("%d", &a);
}while (a<1| | A>50);
for (i=1;i<=a;i++)
printf ("*");
printf ("\ n");
n++;}
Getch ();
}
==============================================================
"Program 89"
Title: A company uses a public telephone to pass data, the data is a four-bit integer, is encrypted during delivery, the encryption rules are as follows:
Each digit is added with 5, then replaced by the remainder of the 10, and the first and fourth are exchanged, and the second and third bits are exchanged.
1. Program Analysis:
2. Program Source code:
Main ()
{int a,i,aa[4],t;
scanf ("%d", &a);
aa[0]=a%10;
AA[1]=A%100/10;
aa[2]=a%1000/100;
aa[3]=a/1000;
for (i=0;i<=3;i++)
{aa[i]+=5;
aa[i]%=10;
}
for (i=0;i<=3/2;i++)
{T=aa[i];
Aa[i]=aa[3-i];
aa[3-i]=t;
}
for (i=3;i>=0;i--)
printf ("%d", aa[i]);
}
==============================================================
"Program 90"
Title: The first question, read the results.
1. Program Analysis:
2. Program Source code:
#include "stdio.h"
#define M 5
Main ()
{int a[m]={1,2,3,4,5};
int i,j,t;
I=0;j=m-1;
while (I<J)
{t=* (a+i);
* (a+i) =* (A+J);
* (A+J) =t;
i++;j--;
}
for (i=0;i<m;i++)
printf ("%d", * (A+i));
}
Classic C program Example ==91--100
"Program 91"
Title: Example of a time function 1
1. Program Analysis:
2. Program Source code:
#include "stdio.h"
#include "time.h"
void Main ()
{time_t lt;/*define a longint time varible*/
Lt=time (NULL);/*system Time and date*/
printf (CTime (<)); /*english format output*/
printf (Asctime (LocalTime (<)));/*tranfer to tm*/
printf (Asctime (Gmtime (<))); /*tranfer to Greenwich time*/
}
==============================================================
"Program 92"
Title: Example of a time function 2
1. Program Analysis:
2. Program Source code:
/*calculate time*/
#include "time.h"
#include "stdio.h"
Main ()
{time_t start,end;
int i;
Start=time (NULL);
for (i=0;i<3000;i++)
{printf ("\1\1\1\1\1\1\1\1\1\1\n");}
End=time (NULL);
printf ("\1:the different is%6.3f\n", Difftime (End,start));
}
==============================================================
"Program 93"
Title: Example of a time function 3
1. Program Analysis:
2. Program Source code:
/*calculate time*/
#include "time.h"
#include "stdio.h"
Main ()
{clock_t start,end;
int i;
Double var;
Start=clock ();
for (i=0;i<10000;i++)
{printf ("\1\1\1\1\1\1\1\1\1\1\n");}
End=clock ();
printf ("\1:the different is%6.3f\n", (double) (End-start));
}
==============================================================
"Program 94"
Title: Time Function Example 4, a guessing game, judge the speed of a person's reaction. (Compiled by moderators when they are beginners)
1. Program Analysis:
2. Program Source code:
#include "time.h"
#include "Stdlib.h"
#include "stdio.h"
Main ()
{char C;
clock_t Start,end;
time_t A, B;
Double var;
int i,guess;
Srand (Time (NULL));
printf ("Do-want to play it." Y ' or ' n ') \ nthe ");
Loop
while ((C=getchar ()) = = ' Y ')
{
I=rand ()%100;
printf ("\nplease input number you guess:\n");
Start=clock ();
A=time (NULL);
scanf ("%d", &guess);
while (guess!=i)
{if (guess>i)
{printf ("Please input a little smaller.\n");
scanf ("%d", &guess);}
Else
{printf ("Please input a little bigger.\n");
scanf ("%d", &guess);}
}
End=clock ();
B=time (NULL);
printf ("\1:it took you%6.3f seconds\n", var= (Double) (End-start)/18.2);
printf ("\1:it took you%6.3f seconds\n\n", Difftime (B,a));
if (var<15)
printf ("\1\1 You is very clever! \1\1\n\n ");
else if (var<25)
printf ("\1\1 You is normal! \1\1\n\n ");
Else
printf ("\1\1 You is stupid! \1\1\n\n ");
printf ("\1\1 congradulations \1\1\n\n");
printf ("The number you are guess is%d", i);
}
printf ("\ndo want to try it again?" ( \ "Yy\". or.\ "n\") \ n ");
if ((C=getch ()) = = = ' Y ')
Goto Loop;
}
==============================================================
"Program 95"
Title: Small Family Financial Management program
1. Program Analysis:
2. Program Source code:
/*money Management system*/
#include "stdio.h"
#include "Dos.h"
Main ()
{
FILE *FP;
struct Date D;
float sum,chm=0.0;
int len,i,j=0;
int C;
Char ch[4]= "", ch1[16]= "", chtime[12]= "", chshop[16],chmoney[8];
PP:CLRSCR ();
sum=0.0;
Gotoxy;p rintf ("|---------------------------------------------------------------------------|");
Gotoxy (;p rintf) ("| Money Management System (C1.0) 2000.03 | ");
Gotoxy (1,3);p rintf ("|---------------------------------------------------------------------------|");
Gotoxy (1,4);p rintf ("| --Money Records--| --Today Cost List--| ");
Gotoxy (1,5);p rintf ("| ------------------------ |-------------------------------------|");
Gotoxy (1,6);p rintf ("| Date:--------------| | ");
Gotoxy (1,7);p rintf ("| | | | |)";
Gotoxy (1,8);p rintf ("| -------------- | |");
Gotoxy (1,9);p rintf ("| Thgs:------------------| | ");
Gotoxy (1,10);p rintf ("| | | | |)";
Gotoxy (1,11);p rintf ("| ------------------ | |");
Gotoxy (1,12);p rintf ("| Cost:----------| | ");
Gotoxy (1,13);p rintf ("| | | | |)";
Gotoxy (1,14);p rintf ("| ---------- | |");
Gotoxy (1,15);p rintf ("| | |)";
Gotoxy (1,16);p rintf ("| | |)";
Gotoxy (1,17);p rintf ("| | |)";
Gotoxy (1,18);p rintf ("| | |)";
Gotoxy (1,19);p rintf ("| | |)";
Gotoxy (1,20);p rintf ("| | |)";
Gotoxy (1,21);p rintf ("| | |)";
Gotoxy (1,22);p rintf ("| | |)"; Gotoxy (1,23);p rintf ("|---------------------------------------------------------------------------|");
i=0;
GETDATE (&D);
sprintf (Chtime, "%4d.%0 2d.%0 2d ", d.da_year,d.da_mon,d.da_day);
for (;;)
{
Gotoxy (3,24);p rintf ("tab __browse cost list ESC __quit");
Gotoxy (13,10);p rintf ("");
Gotoxy (13,13);p rintf ("");
Gotoxy (13,7);p rintf ("%s", chtime);
j=18;
Ch[0]=getch ();
if (ch[0]==27)
Break
strcpy (Chshop, "");
strcpy (Chmoney, "");
if (ch[0]==9)
{
mm:i=0;
Fp=fopen ("Home.dat", "r+");
Gotoxy (3,24);p rintf ("");
Gotoxy (6,4);p rintf ("list Records");
Gotoxy (1,5);p rintf ("|-------------------------------------|");
Gotoxy (41,4);p rintf ("");
Gotoxy (41,5);p rintf ("|");
while (FSCANF (FP, "%10s%14s%f\n", Chtime,chshop,&chm)!=eof)
{if (i==36)
{getch ();
i=0;}
if ((i%36) <17)
{Gotoxy (4,6+i);
printf ("");
Gotoxy (4,6+i);}
Else
if ((i%36) >16)
{Gotoxy (41,4+I-17);
printf ("");
Gotoxy (42,4+I-17);}
i++;
Sum=sum+chm;
printf ("%10s%-14s%6.1f\n", Chtime,chshop,chm);}
Gotoxy (1,23);p rintf ("|---------------------------------------------------------------------------|");
Gotoxy (1,24);p rintf ("| |");
Gotoxy (1,25);p rintf ("|---------------------------------------------------------------------------|");
Gotoxy (10,24);p rintf ("Total is%8.1f$", sum);
Fclose (FP);
Gotoxy (49,24);p rintf ("Press any key to ..."); Getch (); Goto pp;
}
Else
{
while (ch[0]!= ' \ R ')
{if (j<10)
{strncat (chtime,ch,1);
j + +;}
if (ch[0]==8)
{
Len=strlen (Chtime)-1;
if (j>15)
{len=len+1; j=11;}
strcpy (CH1, "");
J=j-2;
Strncat (Ch1,chtime,len);
strcpy (Chtime, "");
Strncat (chtime,ch1,len-1);
Gotoxy (13,7);p rintf ("");}
Gotoxy (13,7);p rintf ("%s", chtime); Ch[0]=getch ();
if (ch[0]==9)
goto mm;
if (ch[0]==27)
Exit (1);
}
Gotoxy (3,24);p rintf ("");
Gotoxy (13,10);
j=0;
Ch[0]=getch ();
while (ch[0]!= ' \ R ')
{if (j<14)
{strncat (chshop,ch,1);
j + +;}
if (ch[0]==8)
{Len=strlen (Chshop)-1;
strcpy (CH1, "");
J=j-2;
Strncat (Ch1,chshop,len);
strcpy (Chshop, "");
Strncat (chshop,ch1,len-1);
Gotoxy (13,10);p rintf ("");}
Gotoxy (13,10);p rintf ("%s", chshop); Ch[0]=getch ();}
Gotoxy (13,13);
j=0;
Ch[0]=getch ();
while (ch[0]!= ' \ R ')
{if (j<6)
{strncat (chmoney,ch,1);
j + +;}
if (ch[0]==8)
{Len=strlen (Chmoney)-1;
strcpy (CH1, "");
J=j-2;
Strncat (Ch1,chmoney,len);
strcpy (Chmoney, "");
Strncat (chmoney,ch1,len-1);
Gotoxy (13,13);p rintf ("");}
Gotoxy (13,13);p rintf ("%s", Chmoney); Ch[0]=getch ();}
if ((strlen (chshop) ==0) | | (Strlen (Chmoney) ==0))
Continue
if ((Fp=fopen ("Home.dat", "A +"))!=null);
fprintf (FP, "%10s%14s%6s", Chtime,chshop,chmoney);
FPUTC (' \ n ', FP);
Fclose (FP);
i++;
Gotoxy (41,5+i);
printf ("%10s%-14s%-6s", Chtime,chshop,chmoney);
}}}
==============================================================
"Program 96"
Title: Count the number of occurrences of a string's substrings
1. Program Analysis:
2. Program Source code:
#include "string.h"
#include "stdio.h"
Main ()
{char str1[20],str2[20],*p1,*p2;
int sum=0;
printf ("Please input the strings\n");
scanf ("%s%s", STR1,STR2);
P1=STR1;P2=STR2;
while (*p1!= ')
{
if (*P1==*P2)
{while (*p1==*p2&&*p2!= ')
{p1++;
p2++;}
}
Else
p1++;
if (*p2== ')
sum++;
P2=STR2;
}
printf ("%d", sum);
Getch ();}
==============================================================
"Program 97"
Title: Enter some characters from the keyboard and send them to disk one after the other until you enter a #.
1. Program Analysis:
2. Program Source code:
#include "stdio.h"
Main ()
{FILE *FP;
Char ch,filename[10];
scanf ("%s", filename);
if ((Fp=fopen (filename, "w")) ==null)
{printf ("Cannot open file\n");
Exit (0);}
Ch=getchar ();
Ch=getchar ();
while (ch!= ' # ')
{FPUTC (CH,FP);p Utchar (CH);
Ch=getchar ();
}
Fclose (FP);
}
==============================================================
"Program 98"
Title: Enter a string from the keyboard, convert all lowercase letters to uppercase letters, and then output to a disk file "test" to save.
Enter the string to! End.
1. Program Analysis:
2. Program Source code:
#include "stdio.h"
Main ()
{FILE *FP;
Char str[100],filename[10];
int i=0;
if ((Fp=fopen ("Test", "W")) ==null)
{printf ("Cannot open the file\n");
Exit (0);}
printf ("Please input a string:\n");
Gets (str);
while (str[i]!= '! ')
{if (str[i]>= ' a ' &&str[i]<= ' Z ')
str[i]=str[i]-32;
FPUTC (STR[I],FP);
i++;}
Fclose (FP);
Fp=fopen ("Test", "R");
Fgets (Str,strlen (str) +1,FP);
printf ("%s\n", str);
Fclose (FP);
}
==============================================================
"Program 99"
Title: There are two disk files A and B, each holding a line of letters, requiring the information in these two files are merged (alphabetically),
Output to a new file, C.
1. Program Analysis:
2. Program Source code:
#include "stdio.h"
Main ()
{FILE *FP;
int I,j,n,ni;
Char c[160],t,ch;
if ((Fp=fopen ("A", "R")) ==null)
{printf ("file A cannot be opened\n");
Exit (0);}
printf ("\ n A contents is: \ n");
for (i=0; (CH=FGETC (FP))!=eof;i++)
{c[i]=ch;
Putchar (C[i]);
}
Fclose (FP);
Ni=i;
if ((Fp=fopen ("B", "R")) ==null)
{printf ("file B cannot be opened\n");
Exit (0);}
printf ("\ n B contents is: \ n");
for (i=0; (CH=FGETC (FP))!=eof;i++)
{c[i]=ch;
Putchar (C[i]);
}
Fclose (FP);
N=i;
for (i=0;i<n;i++)
for (j=i+1;j<n;j++)
if (C[i]>c[j])
{t=c[i];c[i]=c[j];c[j]=t;}
printf ("\ n C file is:\n");
Fp=fopen ("C", "w");
for (i=0;i<n;i++)
{PUTC (C[I],FP);
Putchar (C[i]);
}
Fclose (FP);
}
==============================================================
"Program 100"
Title: There are five students, each student has 3 courses of the results, from the keyboard input above data (including student number, name, three class results), calculated
Average scores, the original data and the calculated average score are stored in the disk File "stud".
1. Program Analysis:
2. Program Source code:
#include "stdio.h"
struct student
{char num[6];
Char Name[8];
int score[3];
float AVR;
} Stu[5];
Main ()
{int i,j,sum;
FILE *FP;
/*input*/
for (i=0;i<5;i++)
{printf ("\ n \ score:\n".%d ", i);
printf ("Stuno:");
scanf ("%s", stu[i].num);
printf ("Name:");
scanf ("%s", stu[i].name);
sum=0;
for (j=0;j<3;j++)
{printf ("score%d.", j+1);
scanf ("%d", &stu[i].score[j]);
SUM+=STU[I].SCORE[J];
}
stu[i].avr=sum/3.0;
}
Fp=fopen ("Stud", "w");
for (i=0;i<5;i++)
if (fwrite (&stu[i],sizeof (struct student), 1,FP)!=1)
printf ("File Write error\n");
Fclose (FP);
}
100 examples of interesting C language programming (4)