50道C/C++编程练习题 复习必备(41-50)


使用C/C++两种语言完成50道题目,体会两种语言之间的不同。编译环境都是在VS2019,完成时间2020.06.21-2020.06.22 。由于C++ 对C的兼容性,部分main()函数没有写return 0 也可以执行成功,但是不提倡,最好加上,否则其他编译器可能报错。C语言不需要严格对齐,因此不对CSDN 的格式做过多的纠正。

41. 写一个字符串拷贝函数

C

#include<iostream>
using namespace std;
void strcpy1(char*, const char*);
void strcpy1(char* p, const char* q)
{
 while (*p++ = *q++);
}
int main()
{
 char p[20] = {};
 char q[20] = { "as456iiiiiihjkd" };
 strcpy1(p, q);
 int i = 0;
 while (p[i]!='\0')
 {
  printf("%c", p[i]);
  i++;
 }
 return 0;
}

C++

#include<iostream>
using namespace std;
void strcpy1(char*, const char*);
void strcpy1(char* p, const char* q)
{
 while (*p++ = *q++);
}
int main()
{
 char p[20] = {};
 char q[20] = { "as456iiiiiihjkd" };
 strcpy1(p, q);
 cout << p;
}

42. 写一个字符串比较函数

C

#include<stdio.h>
int strcmp1(char*, const char*);
int strcmp1(char* str1, const char* str2)
{
 while (*str1 && *str2 && *str1 == *str2)
 {
  str1++; str2++;
 }
 return *str1 - *str2;
}
int main()
{
 char str1[] = "aerttydfgs";
 char str2[] = "wertyyyyyg";
 printf( "%d",strcmp1(str1, str2));
}

C++

#include<iostream>
using namespace std;
int strcmp1(char*, const char*);
int strcmp1(char* str1, const char* str2)
{
 while (*str1 && *str2 && *str1 == *str2)
 {
  str1++; str2++;
 }
 return *str1 - *str2;
}
int main()
{
 char str1[] = "werttydfgs";
 char str2[] = "wertyyyyyg";
 cout << strcmp1(str1, str2);
}

43. 写一个字符串连接函数

C

#include<stdio.h>
int com(char*, char*);
int main()
{
 char str1[50] = "werttydfgs";
 char str2[20] = "wertyyyyyg\0";
 com(str1, str2);
 int i = 0;
 printf("%s\n", str1);
}
int com(char* str1, char* str2)
{
 char* p = str1;
 while (*str1 != '\0') str1++;
 while (*str1++ = *str2++);
 return 0;
}

C++

#include<iostream>
using namespace std;
char com(char*, char*);
int main()
{
 char str1[50] = "werttydfgs";
 char str2[20] = "wertyyyyyg";
    com(str1, str2);
 cout << str1<<endl;
 cout << str2;
}
 char com(char *str1,char *str2)
{
 char * p = str1;
 while (* str1 != '\0') str1++;
 while (*str1++ = *str2++);
 return *p;
}

44. 写一个求字符串长度函数

C

#include<stdio.h>
int getlenth(char*);
int main()
{
 char str1[50] = "wertt34555ydfgs";
 char str2[20] = "wertyyyyyg";
 int k = getlenth(str1);
 printf("%d",k);
}
int getlenth(char* str1)
{
 int i = 0;
 while (*str1 != '\0')
 {
  str1++;
  i++;
 }
 return i;
}

C++

#include<iostream>
using namespace std;
int getlenth(char*);
int main()
{
 char str1[50] = "werttydfgs";
 char str2[20] = "wertyyyyyg";
 int k =getlenth(str1);
 cout << k << endl;
}
int getlenth(char* str1)
{
 int i = 0;
 while(*str1!='\0')
 {
  str1++;
  i++;
 }
 return i;
}

45. 写一函数,在一数组里查找某个值

C

#include<stdio.h>
int findv(int*, int);
int main()
{
 int a[50] = { 12,23,45,78,9,54,85,45,7,78,45,63,45,78,78 };
 int need = 78;
 int k = findv(a, need);
 if (k)
  printf("%d",a[k]);
 else
  printf("false");
}
int findv(int* a, int b)
{
 int i = 0;
 while (*a != '\0')
 {
  if (*a != b)
  {
   a++;
   i++;
  }
  else
   return i;
 }
 return false;
}

C++

#include<iostream>
using namespace std;
int findv(int *,int);
int main()
{
 int a[50] = {12,23,45,78,9,54,85,45,7,78,45,63,45,78,78};
 int need = 89;
 int k = findv(a,need);
 if (k)
  cout << a[k] << endl;
 else
  cout << false;
}
int findv(int* a, int b)
{
 int i=0;
 while (*a!='\0')
 {
  if (*a != b)
  {
   a++;
   i++;
  }
  else
   return i;
 }
 return false;
}

46. 编一程序,求两个矩阵的乘积

pass 。主要是程序太长了,csdn老是卡住。

47. 计算某日是某年的第几天

C

#include<stdio.h>
bool isLeapYear(int y)   //判断某一年是否闰年
{
 return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
}
int main()
{
 int year, month, day, i, s = 0;
 int a[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
 };
 scanf_s("%d%d%d", &year, &month, &day);
 for (i = 1; i < month; i++)
  s = s + a[i];
 s = s + day;
 if (isLeapYear(year) && month > 2) s++;
 printf("%d",s);
}

C++

#include<iostream>
using namespace std;
bool isLeapYear(int y)   //判断某一年是否闰年
{
 return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
}
int main()
{
 int year, month, day, i, s = 0;
 int a[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
cin >> year >> month >> day;
for (i = 1; i < month; i++)
 s = s + a[i];
s = s + day;
if (isLeapYear(year) && month > 2) s++;
cout << s;
}

48. 编写一个帮助小学生学习加法的程序,随机产生2个数,让学生输入答案

C

#include<time.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
 int x, y, z;
 srand(time(0));
 x = rand() % 100;
 y = rand() % 100;
 printf("输入0结束\n");
 printf("%d+%d=", x, y);
 scanf_s("%d", &z);
 while (z != 0)
 {
  while (z != x + y)
  {
   printf( " × 错误!请重做");
   printf("%d+%d=", x, y);
   scanf_s("%d", &z);
  }
  printf( " √ 正确!\n");
  x = rand() % 100;
  y = rand() % 100;
  printf("%d+%d=", x, y);
  scanf_s("%d", &z);
 }
}

C++

#include<iostream>
#include<cstdlib>
using namespace std;
#include<time.h>
int main()
{
 int x, y, z;
 srand(time(0));
 x = rand() % 1000;
 y = rand() % 1000;
 cout << x << " + " << y << " = ";
 cin >> z;
 while (z != '\0')
 {
  while (z != x + y)
  {
   cout << " × 错误!请重做\n";
   cout << x << " + " << y << " = ";
   cin >> z;
  }
  cout << " √ 正确!\n";
  x = rand() % 1000;
  y = rand() % 1000;
  cout << x << " + " << y << " = ";
  cin >> z;
 }
}

49. 从52个数里选13个数

C

#include<stdio.h>
#include<cstdlib>
#include<time.h>
int main()
{
 int i, k, a[52], b[13];
 for (i = 0; i < 52; i++) a[i] = i + 1;
 srand(time(0));
 for (i = 0; i < 13; i++)
 {
  k = rand() % (52 - i);
  b[i] = a[k];
  swap(a[k], a[51 - i]);
 }
 for (i = 0; i < 13; i++) printf("%d ",b[i]);
}

C++

#include<iostream>
#include<cstdlib>
using namespace std;
#include<time.h>
int main()
{
 int i, k, a[52], b[13];
 for (i = 0; i < 52; i++) a[i] = i + 1;
 srand(time(0));
 for (i = 0; i < 13; i++)
 {
  k = rand() % (52 - i);
  b[i] = a[k];
  swap(a[k], a[51 - i]);
 }
 for (i = 0; i < 13; i++) cout << b[i] << " ";
}

50. 求100!

#include<iostream>
using namespace std;
int main() {
 double k=1;
 for (int i = 1; i <= 100; i++)
  k = k * i;
 cout << k;
}

猜你喜欢

转载自blog.csdn.net/qq_40575024/article/details/106908552
今日推荐