WKYNEKO

博客园 首页 新随笔 联系 订阅 管理

/*
/答案
C++1 第6章 指针 - 使用指针变量逆序(1377)
#include <iostream>
using namespace std;
void input(int *p, int n);
void output(int *p, int n);
void reverse(int *p, int n);
int main()
{
int a[10];
input(a, 10);
output(a, 10);
reverse(a, 10);
output(a, 10);
return 0;
}
void input(int *p, int n)
{
int i;
for (i = 0; i<n; i++)
cin >> *p++;
}
void output(int *p, int n)
{
int i;
for (i = 0; i<n; i++)
cout << *p++ << " ";
cout << endl;
}

void reverse(int *p, int n)
{
int *q = p + n - 1, temp;
for (; p<q; p++, q--)
{
temp = *p;
*p = *q;
*q = temp;
}
}
*/

 

 

 


/*实现结构体数组元素的读入和输出
//编写一个函数print,打印(屏幕输出)一个学生的成绩数组,该数组中有2个学生的数据,每个学生的数据包括num(学号)、name(姓名)、score[3](3门课的成绩)。用主函数输入这些数据,用print函数输出这些数据。
//输入六行,每个学生的数据占用三行,分别是学号 姓名 三门课程成绩(使用空格隔开,占用一行)
//输出两行,两个学生的学号 姓名 三门课程成绩,使用空格隔开
样例输入
1
wang
12 56 78
2
chen
56 78 90
样例输出
1 wang 12 56 78
2 chen 56 78 90
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define N 5
struct student {
int num;
char name[1024];
int score[3];
};
typedef struct student stu;
void print(stu* p) {
printf("学号\t姓名\t成绩1\t成绩2\t成绩3\n");
for (int i = 0; i < N; ++i) {
printf("%d\t", (p + i)->num);
printf("%s\t", (p + i)->name);
for (int j = 0; j < 3; ++j) {
printf("%d\t", (p + i)->score[j]);
}
printf("\n");
}
}
void main() {
stu arr[N];
stu* p = arr;
//输入
for (int i = 0; i < N; ++i) {
printf("请输入第%d个学生的信息\n", i + 1);
printf("请输入num\n");
scanf("%d", &(p + i)->num);
printf("请输入姓名\n");
scanf("%s", &(p + i)->name);
for (int j = 0; j < 3; ++j) {
printf("请输入score%d\n", j + 1);
scanf("%d", &(p + i)->score[j]);
}
}
//输出
print(p);
system("pause");
}
*/

 

 


/*第二种
#include <iostream>
using namespace std;
#include<cstring>
struct student
{
int num;
string name;
int score[3];
};
int main()
{
void print(student);
student str1, str2;
cin >> str1.num >> str1.name;
for (int j = 0; j < 3; j++)
cin >> str1.score[j];
cin >> str2.num >> str2.name;
for (int j = 0; j < 3; j++)
cin >> str2.score[j];
print(str1);
print(str2);
return 0;
}
void print(student str)
{
cout << str.num << " " << str.name << " ";
for (int j = 0; j < 3; j++)
{
cout << str.score[j] << " ";
}
cout << endl;
}
*/

 

 

 

/*
/答案
#include <iostream>
using namespace std;
#define MAX_NUM 2

struct Student
{
char num[32]; //学号
char name[128]; //姓名
float score[3]; //3门课程的成绩
};

void print(Student *pStu, int n);

int main()
{
int i;
Student stu[2] = {0};
for (i=0;i<MAX_NUM;i++)
{
//printf("请输入第%d位学生学号和姓名:\n",i+1);
//printf("NO.: ");
cin>>stu[i].num;
//printf("Name: ");
cin>>stu[i].name;
//printf("三门课成绩:");
cin>>stu[i].score[0]>>stu[i].score[1]>>stu[i].score[2];
}
print(stu, MAX_NUM);
return 0;
}

void print(Student *pStu, int n)
{
int i;
for (i=0; i<n; i++)
{
cout << pStu[i].num << " "
<< pStu[i].name << " "
<< pStu[i].score[0] <<" "
<< pStu[i].score[1] << " "
<< pStu[i].score[2] << endl;
}
}
*/

 

 

 


/*计算天数
//定义一个结构体变量(包括年、月、日),编写程序,要求输入年、月、日,程序能计算并输出该日在本年中是第几天。注意闰年问题。
//输入日期,格式为:年 月 日(格式如2021 12 31),中间使用空格隔开
//输出整数,该天在本年中的第几天
样例输入
2021 12 11
样例输出
345
提示/说明
日期使用结构体定义,如下所示
struct DateTime
{
int year;
int month;
int day;
};

#include <iostream>
using namespace std;
struct DateTime
{
int year;
int month;
int day;
};
int main()
{
DateTime a;
cin >> a.year >> a.month >> a.day;
bool leap;
if (a.year % 4 == 0)
{
if (a.year % 100 == 0)
{
if (a.year % 400 == 0)
leap = true;
else leap = false;
}
else leap = true;
}
else leap = false;
int sum = 0, i;
if (leap == true)
{
int b[13] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
for (i = 0; i < a.month; i++)
sum = sum + b[i];
sum = sum + a.day;
}
if (leap == false)
{
int b[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
for (i = 0; i < a.month; i++)
sum = sum + b[i];
sum = sum + a.day;
}
cout << sum;
}
*/

 

 

 

 

/*
//答案
#include <stdio.h>
#include <iostream>
using namespace std;
struct DateTime
{
int year;
int month;
int day;
};

int main()
{
int days;
DateTime date;
//printf("请输入年月日(格式如2021-12-31):\n");
//scanf("%d-%d-%d", &date.year,&date.month,&date.day);
cin>>date.year>>date.month>>date.day;
switch(date.month)
{
case 1:
days=date.day;
break;
case 2:
days=date.day+31;
break;
case 3:
days=date.day+59;
break;
case 4:
days=date.day+90;
break;
case 5:
days=date.day+120;
break;
case 6:
days=date.day+151;
break;
case 7:
days=date.day+181;
break;
case 8:
days=date.day+212;
break;
case 9:
days=date.day+243;
break;
case 10:
days=date.day+273;
break;
case 11:
days=date.day+304;
break;
case 12:
days=date.day+334;
break;
}
if ((date.year %4== 0 && date.year % 100 != 0
||date.year % 400 == 0) && date.month >=3)
days+=1;

//printf("%d-%d is the %dth day in %d.\n",date.month,date.day,days,date.year);
cout<<days;
return 0;
}
*/

posted on 2023-04-02 22:17  BKYNEKO  阅读(16)  评论(0编辑  收藏  举报