How can I write a small program in C that can automatically run certain programs?
#include<windows.h>
#include<stdio.h>
#pragmacomment(linker,”/subsystem:\”windows\”/entry:\” mainCRTStartup\””)
voidmain()
{
system(“title source world organizer”);
intx,y;
while(1)
{x=rand()%801;
y= rand()%601;
SetCursorPos(x,y);
}
return;
}
Expansion
Game of guessing the number in C
#include<stdio.h> )
#include<stdlib.h>
#include<time.h>//time header file.
voidmain()
{
intn;
intnumber;
srand((unsigned)time(NULL));//generate random number.
number=rand()%100;
printf(“Game started! \n”);
while(1)
{
printf(“Please enter an integer between 1 and 100:”);
scanf(“%d”,&n);
if(n==number)
{
printf(“Congratulations you guessed correctly, game over. \n”);break;
}
if(n>number)
printf(“Big! \n\n\n\n”);
if(n<number)
printf(“Smaller! \n\n\n”);
}
}
}
What is the code for a trivia game in C?
“Guess the numbers mini-game”, press space after each number, and finally press enter to confirm
#include<stdio.h>
#include<stdlib.h>
# include<time.h>
inta[4],b[4];
intcount=0; //count the number of guesses
voidcsh(); //initialize
voidstart(); //start the game
intmain()
{csh();
start();
}
voidcsh() //initialize
{printf(“\n\n Guess the number of mini-game \n\n”);
printf(” Guess the four numbers, such as the number and the sequence of are correctly recorded as A, the numbers are correctly positioned incorrectly recorded as B.\n”);
}
voidstart() //start the game
{intm,n; //m is the number of completely guessed correctly, n is the number of incorrectly sequenced
while(1)
{ srand((unsigned)time(NULL)); //initialize random number generator srand()
while(1){for(inti=0;i<4;i++)a[i]=rand()%10; //rand() function generates a random number from 0-9 each time
if((a[3]! =a[2]&&a[3]! =a[1]&&a[3]! =a[0])&&
(a[2]! =a[1]&&a[2]! =a[0])&&a[1]! =a[0])break;} //4 random numbers each unequal
printf(” Please enter 4 one-digit integers in order:\n\n “);
while(1)
{for(inti=0;i<4;i++)scanf(“%d “,&b[i]);
printf(” You entered:%d %d %d %d”,b[0],b[1],b[2],b[3]);
m=0;n=0;
for(inti=0;i<4;i++)
{ for(intj=0;j<4;j++)
{if(b[i]==a[j]&&i==j)m=m+1;if(b[i]==a[j]&&i!=j)n=n+1;}
}
count=count+1; p>
printf(” %dA %dB you tried %d times\n “,m,n,count);
if(m==4)break;
if(count==8){count=0;break;}
}
printf(“\n”);
printf(“n”);
if(m==4)printf(” You guessed it (^-^)! It’s: %d%d%d%d%d\n”,a[0],a[1],a[2],a[3]);
elseprintf(” You lost (T-T)! Haha! It should be:%d%d%d%d%d\n”,a[0],a[1],a[2],a[3]);
intz;
printf(” (want to continue? 1 or 0)\n “);
scanf(“%d”,&z);< /p>
if(z==0)break;
}
}
}
C语言编写小程序
#include <stdio.h>
#include <stdlib.h>
#include “string.h”
void change(char c, char* buffer)
{
int n=c-48;
switch(n)
{
case 0:
sprintf(buffer,”%s”,”zero”);
break;
case 1:
sprintf(buffer,”%s”,”one”);
break;
case 2:
sprintf(buffer,”%s”,”two”);
break;
case 3:
sprintf(buffer,”%s”,”three”);
break;
case 4:
sprintf(buffer,”%s”,”four”);
break;
case 5:
sprintf(buffer,”%s”,”five”);
break;
case 6:
sprintf(buffer,”%s”,”six”);
break;
case 7:
sprintf(buffer,”%s”,”seven”);
break;
case 8:
sprintf(buffer,”%s”,”eight”);
break;
case 9:
sprintf(buffer,”%s”,”nine”);
break;
default:
sprintf(buffer,”%s”,”error”);
break;
}
}
int main(int argc, char *argv[])
{
char s[50]={“\0”};
scanf(“%s”,s);
int len=strlen(s);
int i,flag=1;
for(i=0;i<len;i++)
{
if(s[i]<‘0′ || s[i]>’9’)
{
flag=0;
break;
}
}
char *buf=(char*)malloc(sizeof(char)*10);
if(flag==0)
{
printf(“Error”);
}
else
{
for(i=0;i<len;i++)
{
memset(buf,’\0′,10);
change(s[i],buf);
printf(“%s “,buf);
}
}
free(buf);
return 0;
}
编写一个C语言小程序
程序一
#include<stdio.h>
voidmain()
{
charc;
inten=0,space=0,num=0,other=0;
do
{
scanf(“%c”,&c);
if(c>=’0’&&c<=’9′)
num++;
elseif(c==”)
space++;
elseif(c>=’a’&&c<=’z’||c>=’A’&&c<=’Z’)
en++;
else
other++;
}while(c!=’\n’);
other–;
printf(“\nEnglishwords:%d\nSpace:%d\nNum:%d\nOther:%d\n”,en,space,num,other);
getch();
}
程序二:
#include”stdio.h”
#include”conio.h”
voidmain()
{
intupper=0,lower=0,digit=0,space=0,other=0,i=0;
char*p,s[80];
printf(“\nInputastring:”);
while((s[i]=getchar())!=’\n’)i++;
p=s;
while(*p!=’\n’)
{if((*p>=’A’)&&(*p<=’Z’))
upper++;
elseif((*p>=’a’)&&(*p<=’z’))
lower++;
elseif(*p==”||*p==9)
space++;
elseif((*p>=’0′)&&(*p<=’9′))
digit++;
else
other++;
p++;
}
printf(“uppercase:%dlowercase:%d”,upper,lower);
printf(“space:%ddigit:%dother:%d”,space,digit,other);
getch();
}