当前位置:网站首页>Basic exercises of C language for beginners
Basic exercises of C language for beginners
2022-07-24 01:00:00 【lhb2998658795】
1. Character inversion
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void swap(char *a,char *b)
{
if(a==NULL&&b==NULL){
printf("The ginseng error");
exit(EXIT_SUCCESS);// For beginners, use here return ; It's OK, too , Not affecting use ;
//exit To exit this process
}
int temp=0;
while(a<b){
temp=*a;
*a=*b;
*b=temp;
a++;
b--;
}
}
void change_word(char *a)
{
char *p,*q;
p=a;
q=a+strlen(a)-1;
swap(p,q);
q=p;
// This can still be used while Realization , however while It will increase the time complexity , Use as little as possible
while(*p!=0){
if(*p!=' '){
p++;
if(*p!=0){
continue;
}
}
swap(q,p-1);
if(*p==0){
break;
}
q=(++p);
}
}
int main(int argc, char const *argv[])
{
char buff[128]={0};
printf(" Please enter what you want to enter ");
fgets(buff,sizeof(buff),stdin);// For beginners, use here gets(buff) It's OK, too , Not affecting use ;
buff[strlen(buff)-1]='\0';
change_word(buff);
printf("%s\n",buff);
return 0;
}
2. Output all less than 100 The prime number of
#include <stdio.h>>
int main(int argc, char const *argv[])
{
int n=100;
int i=0;
int j=0;
int a=0;
for(i=1;i<n;i++){
a=0;
for(j=1;j<=i;j++){
if(i%j==0){
a++;
}
}
if(a==2){
printf("%d ",i);
}
}
puts("");
return 0;
}
3 There are three ways of exchange
3.1 The first three cups of water exchange
#include <stdio.h>
int main(int argc, char const *argv[])
{
int a=10;
int b=20;
int temp=0;
temp=a;
a=b;
b=temp;
printf("a=%d b=%d\n",a,b);
return 0;
}
3.2 The second XOR Exchange
#include <stdio.h>
int main(int argc, char const *argv[])
{
int a=10;
int b=20;
int temp=0;
a=a^b;
b=a^b;
a=a^b;
printf("a=%d b=%d\n",a,b);
return 0;
}
3.3 The third exchange // Note that the value beyond the limit cannot be too large
#include <stdio.h>
int main(int argc, char const *argv[])
{
int a=10;
int b=20;
int temp=0;
b=a+b;
a=b-a;
b=b-a;
printf("a=%d b=%d\n",a,b);
4. practice : 1. Design a program , Realization function : Enter a character , If it's capital , Just convert it to lowercase output If it's lowercase , It is converted to uppercase output If it's a number , Just * 10 Output If it's another character , All output # a --> A B --> b 2 --> 20 ( --> #
#include<stdio.h>
int main(int argc, const char *argv[])
{
char v = 0;
scanf("%c", &v);
getchar();
if (v >= 'A' && v <= 'Z'){
v += 32;
printf("%c\n", v);
}else if (v >= 'a' && v <= 'z'){
v -= 32;
printf("%c\n", v);
}else if (v >= '0' && v <= '9'){
v -= '0';
v *= 10;
printf("%d\n", v);
}else{
printf("%c\n", '#');
}
return 0;
}5ifelse Statements use
practice : Write a program to calculate height Every parent cares about the height of their children when they grow up , According to the knowledge of physiological health and mathematical statistics, it is shown that , There are genetic factors that affect the height of children as adults 、 Eating habits and physical exercise . The height of children as adults is closely related to their parents' height and their own gender . set up faHeight For his father's height ,moHeight For the height of his mother , The height prediction formula is : Male adult height = (faHeight + moHeight) * 0.54(cm) Female adult height = (faHeight * 0.923 + moHeight) / 2(cm) Besides , If you love physical exercise , Then you can increase your height on the original basis 2% If you have good healthy eating habits , Then you can increase your height on the original basis 1.5% Procedural requirements : Father's height and mother's height 、 The gender of the child 、 Do you like sports training Refining And whether there are good hygienic eating habits also input from the keyboard , The final output is predicted height . Tips : How to input the child's gender , Prompt can be given on the screen “ Please enter the gender of the child ( Boy input 1, Girl input 0):”, And then through if Statement to determine whether the character entered from the keyboard is 1 still 0. Whether you like physical exercise can also be achieved in a similar way .
#include <stdio.h>
int main(int argc, const char *argv[])
{
double faHeight = 0;
double moHeight = 0;
double childHeight = 0;
double pe_temp = 0,food_temp = 0;// It's used to record height
int sex = 0; // Gender 1 male 0 Woman
int PE = 0; // athletic sports 1 like 0 Don't like
int FOOD = 0;// eating habits 1 Good habit 0 I'm not used to it
printf(" Please enter your father's height :");
scanf("%lf", &faHeight);
printf(" Please enter your mother's height :");
scanf("%lf", &moHeight);
printf(" Please enter the child's gender (1 schoolboy 0 girl student ):");
scanf("%d", &sex);
if(1 == sex){
childHeight = (faHeight + moHeight) * 0.54;
}else{
childHeight = (faHeight * 0.923 + moHeight) / 2;
}
printf(" Please enter whether you like sports (1 like 0 Don't like ):");
scanf("%d", &PE);
printf(" Please enter whether you have good habits (1 Yes 0 No, ):");
scanf("%d", &FOOD);
if(1 == PE){
pe_temp = childHeight * 0.02;
}
if(1 == FOOD){
food_temp = childHeight * 0.015;
}
childHeight = childHeight + pe_temp + food_temp;
printf(" The predicted height of the child is :%.3lf\n",childHeight);
return 0;
}
6. Simple calculator implementation
. Write code , Realize the function of simple calculator ( Operands Is an integer + - * / %): scanf("%d %c %d",); Input :5 + 2 Output :7 Input :10 % 3 Output :1
#include <stdio.h>
int main(int argc, const char *argv[])
{
int num1 = 0, num2 = 0;
char operator = 0;
scanf("%d %c %d", &num1, &operator, &num2);
switch(operator){
case '+':
printf("%d + %d = %d\n", num1, num2, num1+num2);
break;
case '-':
printf("%d ‐ %d = %d\n", num1, num2, num1-num2);
break;
case '*':
printf("%d * %d = %d\n", num1, num2, num1*num2);
break;
case '%':
printf("%d %% %d = %d\n", num1, num2, num1%num2);
break;
case '/':
printf("%d + %d = %.3lf\n", num1, num2, (double)num1/(double)num2);
break;
default:
printf(" I only support + ‐ * / %%\n");
break;
}
return 0;
}
7 Student grade management
#include <stdio.h>
int main(int argc, const char *argv[])
{
int score = 0;
scanf("%d", &score);
getchar();
switch(score/10){
case 10:
case 9:
printf("A\n");
break;
case 8:
printf("B\n");
break;
case 7:
printf("C\n");
break;
default:
printf(" fail, \n");
break;
}
return 0;
}8. Use while loop , Realization 1~100 Sum up .
#include <stdio.h>
int main(){
int i = 1, sum = 0;
while(i<=100){
sum += i;
i++;
}
printf("sum = %d\n",sum);
return 0;
}9.. Monkeys eat peaches
#include <stdio.h>
int main(){
int sum = 1;
int i = 0;
while(i<9){
printf(" The first %d God , Yes %d Peach \n", 10-i, sum);
sum = (sum+1)*2;
i++;
}
printf(" The first %d God , Yes %d Peach \n", 10-i, sum);
return 0;
}10. daffodils
#include <stdio.h>
int main(int argc, const char *argv[])
{
int i = 0;
int ge = 0;
int shi = 0;
int bai = 0;
for(i = 100; i < 1000; i++){
ge = i % 10;
shi = i / 10 % 10;
bai = i / 100;
if(ge*ge*ge + shi*shi*shi + bai*bai*bai == i){
printf("%d\n", i);
}
}
return 0;
}
11 Typesetting shows the following results

#include <stdio.h>
int main(int argc, char const *argv[])
{
char ch='A';
int i=0;
int j=0;
int a=0;
for(i=0;i<8;i++){
for(j=0;j<i-1;j++){
printf("_");
}
for(a=0;a<i;a++){
printf("%c",ch+a);
}
puts("");
}
return 0;
}12. Enter a number , Output all the factors of this number .
#include <stdio.h>
int main(int argc, const char *argv[])
{
int num = 0;
scanf("%d", &num);
getchar();
for(int i= 1; i <= num; i++){
if(num%i == 0){
printf("%d\n", i);
}
}
return 0;
}
13 Perfect number
#include <stdio.h>
int main(int argc, const char *argv[])
{
int i = 0;
int j = 0;
int sum = 0;
for(i = 1; i < 1000; i++){
sum = 0;
for(j = 1; j < i; j++){
if(i%j == 0){
sum += j;
}
}
if(sum == i){
printf("%d\n", i);
}
}
return 0;
}
13 practice : 1. Define a 10 A length of int Array , And all initialized to 0, Loop to assign values to array members , The assigned value comes from keyboard input ; Output the largest element in the array and the subscript of the largest element .
#include <stdio.h>
int main(){
int arr[10] = {0};
int i = 0;
int max_index = 0;
for(i = 0; i < 10; i++){
scanf("%d", &arr[i]);
}
for(i = 1; i < 10; i++){
if(arr[i] > arr[max_index]){
max_index = i;
}
}
printf("max_value = %d , max_index = %d\n",arr[max_index],max_index);
return 0;
}14 Bubble sort
#include <stdio.h>
int main(int argc, const char *argv[])
{
int s[10] = {23,45,65,78,90,55,33,17,96,54};
int i = 0;
int j = 0;
int temp = 0;
int flags=0;
int len = sizeof(s)/sizeof(int);
for(i = 0; i < len; i++){
printf("%d ", s[i]);
}
printf("\n");
for(j = 0; j < len-1; j++){
flags=0;
for(i = 0; i < len-1-j; i++){
if(s[i] > s[i+1]){
temp = s[i];
s[i] = s[i+1];
s[i+1] = temp;
flags=1;
}
}
if(flags==0){
break;
}
}
for(i = 0; i < len; i++){
printf("%d ", s[i]);
}
printf("\n");
return 0;
}
15strlen function
#include <stdio.h>
int mystrlen(char *num)
{
int i=0;
while(*num){
i++;
num++;
}
return i;
}
int main(int argc, char const *argv[])
{
char num[20]="i love china";
int length=0;
length=mystrlen(num);
printf("%d\n",length);
return 0;
}16strcat
#include <stdio.h>
void mystrcat(char *dest, char *src)
{
//1. Determine whether the address is empty , If it is empty, exit
if(dest == NULL || src == NULL){
return ;
}
//2. find dest Of '\0'
while(*dest){
dest++;
}
//3. Copy the data
while(*src){
*dest++ = *src++;
}
//4. to dest Assign an end symbol
*dest = '\0';
}
int main(int argc, const char *argv[])
{
char arr[50] = {0};
char brr[100] = {0};
printf("input string (arr) > ");
gets(arr);
printf("input string (brr) > ");
gets(brr);
mystrcat(brr,arr);
printf("brr = %s\n",brr);
return 0;
}
16strcpy
#include <stdio.h>
void mystrcpy(char *dest, char *src)
{
//1. Determine whether the address is empty , If it is empty, exit
if(dest == NULL || src == NULL){
return ;
}
//2. Copy the data
while(*src){
*dest++ = *src++;
}
//3. to dest Assign an end symbol
*dest = '\0';
}
int main(int argc, const char *argv[])
{
char arr[100] = {0};
char brr[100] = {0};
printf("input string (arr) > ");
gets(arr);
mystrcpy(brr,arr);
printf("brr = %s\n",brr);
return 0;
}17strcmp
#include <stdio.h>
int mystrcmp(char *a,char *b)
{
while(*a&&*b&&*a==*b){
a++;
b++;
}
if((*a-*b)>0){
return 1;
}else if((*a-*b)<0){
return -1;
}else{
return 0;
}
}
int main(int argc, char const *argv[])
{
char a[20]={0};
char b[20]={0};
int flag=0;
printf("Please enter the value of a >");
gets(a);
printf("Please enter the value of b >");
gets(b);
flag=mystrcmp(a,b);
if(flag>0){
printf("a>b\n");
}else if(flag<0){
printf("a<b\n");
}else{
printf("a=b\n");
}
return 0;
}
18 To write atoi function
#include <stdio.h>
void my_gets(char *a,int n)
{
int i=0;
while(i<n){
if((a[i]=getchar())=='\n'){
i++;
break;
}
i++;
}
a[i]=0;
}
int my_atoi(char *s)
{
int num=0;
int sig=1;
if(*s=='-'){
sig=-1;
}
if(*s=='-'||*s=='+'){
s++;
}
while((*s)>='0'&&(*s)<='9'){
num=num*10+*s-'0';
s++;
}
return sig*num;
}
int main(int argc, const char *argv[]){
char array[50]={0};
my_gets(array,100);
printf("%d\n",my_atoi(array));
return 0;
}
19 Fibonacci sequence
#include <stdio.h>
int main(int argc, const char *argv[]){
int s[20]={1,1};
int i=0;
for(i=2;i<20;i++){
s[i]=s[i-1]+s[i-2];
}
for(i=0;i<20;i++){
printf("%d ",s[i]);
}
puts("");
return 0;
}
20 Find the maximum value in the array
#include <stdio.h>
int main(int argc, const char *argv[]){
int arr[10]={0};
int i=0;
for(i=0;i<10;i++){
scanf("%d",&arr[i]);
}
int max=0;
for(i=0;i<10;i++){
if(arr[i]>arr[max]){
max=i;
}
}
printf("%d %d\n",max,arr[max]);
return 0;
}
21 multiplication table
#include <stdio.h>
int main(int argc, char const *argv[])
{
int i=0;
int j=0;
for(i=1;i<10;i++){
for(j=1;j<=i;j++){
printf("%d*%d=%d ",j,i,i*j);
}
puts("");
}
return 0;
}
22 perpetual calendar
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main (){
int year = 0, month = 0, day = 0;
int hour = 0, min = 0, sec = 0;
scanf("%d%d%d%d%d%d", &year, &month, &day
, &hour, &min, &sec);// When the input Space off
while(1){
system("clear");// call clear Command clear screen , Realize single line display
printf("%4d-%02d-%02d %02d:%02d:%02d\n",
year, month, day, hour, min, sec);//%02d Is to format the output Later on
sec++;
if(60 == sec){
sec = 0;
min++;
if(60 == min){
min = 0;
hour++;
if(24 == hour){
hour = 0;
day++;
//31 Day month
if(1 == month || 3 == month || 5 == month || 7 == month || 8 == month
|| 10 == month || 12 == month){
if(32 == day){
day = 1;
month++;
}
//30 Day month
}else if(4 == month || 6 == month || 9 == month || 11 == month || 8 == month){
if(31 == day){
day = 1;
month++;
}
// Leap year's 2 month
}else if((year%4==0 && year%100!=0) || year%400==0){
if(30 == day){
day = 1;
month++;
}
// In ordinary years 2 month
}else{
if(29 == day){
day = 1;
month++;
}
}
if(13 == month){
month = 1;
year++;
}
}
}
}
sleep(1);// Sleep 1 second
}
return 0;
}边栏推荐
- Three usages of synchronized keywords in vernacular
- Modify node temporarily_ Modules package source code and compile reference to modify dependent packages
- 出于数据安全考虑 荷兰教育部要求学校暂停使用Chrome浏览器
- 暑假第四周总结
- Solve the problem that MySQL inserts Chinese garbled code into the table
- Create a self signed certificate to digitally sign exe files
- How to relieve the pillow quickly
- Sparksql design and introduction, 220722,
- What impact does the European "gas shortage" have on China?
- What the hell is ThreadLocal doing?
猜你喜欢

How to relieve the pillow quickly

Hcia-01 initial understanding of the Internet

網絡系統實驗:ping不通的問題解决

docker redis

docker mysql

Graphic pipeline (I) post-processing stage alpha test template test depth test mix

ACL——net

项目场景:nvidia-smi Unable to datemine the device handle for GPU 0000:01:00.0: Unknow Error

工作3年的测试员跳槽后工资是原来的2倍,秘诀原来是......

C language: deep analysis of const keyword
随机推荐
Bean Validation使用篇----05
[reply] about the fact that I chose the wrong technology at the wrong time
Image processing 1:rgb888_ YCbCr444
Chapter 1 water test --*offer
Starfish OS: create a new paradigm of the meta universe with reality as the link
Selection method of geometric objects in Creo 9.0
VLAN division, automatic allocation of IP to all hosts through DHCP, and communication accessible throughout the network
Tutorial on principles and applications of database system (051) -- MySQL query (XIII): using queries in DML statements
Source code installation and use of APIs IX
Client does not support authentication protocol requested by server; consider upgrading MySQL client
[flyway introduction]
freemarker
docker redis
Easy gene | target gene DNA methylation sequencing (target BS)
How to relieve the pillow quickly
[QNX Hypervisor 2.2用户手册]9 VM配置参考
Seektiger's okaleido has a big move. Will the STI of ecological pass break out?
Tutorial on the principle and application of database system (049) -- MySQL query (XI): sub query
对皮尔逊相关系数进行假设检验
Introduction to QT (2.1 the first procedure for the beginning of QT)