3. 输出带框文字(Cprog0101.c) 题目描述:编写程序,在屏幕上输出了行信息。 输入格式:无输入。 输出格式:按照下列格式输出带框文字。 Hello World! 输入输出样例: 输入 输比 ########## Hello World! ##########
时间: 2024-09-18 20:04:47 浏览: 66
这是一个简单的控制台文本输出练习,需要你在C语言中编写程序,实现给定的输出格式。下面是基于C语言的一个简单示例代码,它将打印出指定的带框"Hello World!":
```c
#include <stdio.h>
#include <string.h>
int main() {
char text[] = "Hello World!";
int box_length = strlen(text) + 4; // 计算文字长度加两边两个'#'
// 打印上边框
printf("#");
for (int i = 1; i < box_length - 2; i++) {
printf("-");
}
printf("#\n");
// 打印中间内容
printf("|%s|\n", text);
// 打印下边框
printf("#");
for (int i = 1; i < box_length - 2; i++) {
printf("-");
}
printf("#\n");
return 0;
}
```
当你运行这个程序时,它会在屏幕上显示如下的效果:
```
#######
Hello World!
#######
```
相关问题
5.素数个数(Cprog0505.c) 题目描述:输入两个正整数m和n,统计并输出m~n之间素数的个数。输入格式:在一行中给出两个整数m(2≤m≤1000)和n(10≤n≤1000),且满足m小于n。输出格式:在一行中输出一个整数,即m~n之间素数的个数。
"5.素数个数(Cprog0505.c)"这个题目是一个经典的编程练习,通常涉及判断一个数是否为素数以及计数特定范围内素数的数量。在C语言中,解决这个问题需要编写一个函数来检查一个数是否为素数,然后遍历给定范围内的所有数字,对每个数应用这个素数检测函数,并累加计数。
首先,你需要创建一个函数,比如`isPrime()`,它接受一个整数作为参数,通过检查除1和自身外是否有其他因子来确定该数是否是素数。接下来,在主程序中,读取用户输入的m和n,初始化一个计数器为0,然后遍历从m到n的所有整数,如果是素数就增加计数器。
以下是一个简化的C程序示例:
```c
#include <stdio.h>
// 判断是否为素数的函数
int isPrime(int num) {
if (num <= 1) return 0;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0)
return 0;
}
return 1;
}
int main() {
int m, n, count = 0;
scanf("%d%d", &m, &n);
// 遍历m到n,统计素数个数
for (int i = m; i <= n; i++) {
if (isPrime(i))
count++;
}
printf("素数的个数: %d\n", count);
return 0;
}
```
用c++设计一个简单的学生成绩管理系统。设有学生成绩文件student.txt,每位学生成绩信息包含学号(num)、姓名(name)、性别(sex)、出生日期(birthday,包括年、月、日,分别表示为year,month,day)、3门功课的成绩(高数、C语言、大学物理)、总分和平均分。设计程序,要求从文件student.txt中读出学生成绩表,之后参对成绩表进行如下操作: (1)建立学生成绩信息结构数组(从student.txt读入数据) (2)显示。即输出出所有成绩。 (3)添加。可添加一条或多条记录。 (4)排序。分为两个方面,按学号递增排序和按总分递减排序。 (5)查找。分为三方面:按学号查找、按姓名查找和按分数查找。 (6)计算。计算各门课的平均分数。 (7)保存。退出程序前将结果保存到studeng.txt。 2)要求 (1)设计一个简单的界面 (2)每一个功能设为一个模块
这是一个比较大的项目,需要涉及到读写文件、结构体、数组、排序、查找等多个知识点,下面是一个简单的实现:
```
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
struct Student {
string num;
string name;
string sex;
int year;
int month;
int day;
int math;
int cprog;
int phy;
int total;
float avg;
};
int count = 0;
Student students[100];
void readData() {
ifstream fin("student.txt");
while (!fin.eof()) {
fin >> students[count].num;
fin >> students[count].name;
fin >> students[count].sex;
fin >> students[count].year;
fin >> students[count].month;
fin >> students[count].day;
fin >> students[count].math;
fin >> students[count].cprog;
fin >> students[count].phy;
students[count].total = students[count].math + students[count].cprog + students[count].phy;
students[count].avg = students[count].total / 3.0;
count++;
}
fin.close();
}
void display() {
cout << "学号\t姓名\t性别\t出生日期\t高数\tC语言\t大学物理\t总分\t平均分" << endl;
for (int i = 0; i < count; i++) {
cout << students[i].num << "\t" << students[i].name << "\t" << students[i].sex << "\t" << students[i].year << "-" << students[i].month << "-" << students[i].day << "\t";
cout << students[i].math << "\t" << students[i].cprog << "\t" << students[i].phy << "\t" << students[i].total << "\t" << students[i].avg << endl;
}
}
void add() {
cout << "请输入要添加的学生个数:";
int num;
cin >> num;
for (int i = 0; i < num; i++) {
cout << "请输入第" << i + 1 << "个学生的信息:" << endl;
cout << "学号:";
cin >> students[count].num;
cout << "姓名:";
cin >> students[count].name;
cout << "性别:";
cin >> students[count].sex;
cout << "出生日期(格式为YYYY MM DD):";
cin >> students[count].year >> students[count].month >> students[count].day;
cout << "高数成绩:";
cin >> students[count].math;
cout << "C语言成绩:";
cin >> students[count].cprog;
cout << "大学物理成绩:";
cin >> students[count].phy;
students[count].total = students[count].math + students[count].cprog + students[count].phy;
students[count].avg = students[count].total / 3.0;
count++;
}
}
bool cmpNum(Student a, Student b) {
return a.num < b.num;
}
bool cmpTotal(Student a, Student b) {
return a.total > b.total;
}
void sort() {
int choice;
cout << "请选择排序方式(1.按学号递增排序 2.按总分递减排序):";
cin >> choice;
if (choice == 1) {
std::sort(students, students + count, cmpNum);
} else if (choice == 2) {
std::sort(students, students + count, cmpTotal);
} else {
cout << "输入有误,请重新选择!" << endl;
sort();
}
}
void searchNum() {
string num;
cout << "请输入要查找的学生学号:";
cin >> num;
for (int i = 0; i < count; i++) {
if (students[i].num == num) {
cout << "学号\t姓名\t性别\t出生日期\t高数\tC语言\t大学物理\t总分\t平均分" << endl;
cout << students[i].num << "\t" << students[i].name << "\t" << students[i].sex << "\t" << students[i].year << "-" << students[i].month << "-" << students[i].day << "\t";
cout << students[i].math << "\t" << students[i].cprog << "\t" << students[i].phy << "\t" << students[i].total << "\t" << students[i].avg << endl;
return;
}
}
cout << "未找到该学生!" << endl;
}
void searchName() {
string name;
cout << "请输入要查找的学生姓名:";
cin >> name;
for (int i = 0; i < count; i++) {
if (students[i].name == name) {
cout << "学号\t姓名\t性别\t出生日期\t高数\tC语言\t大学物理\t总分\t平均分" << endl;
cout << students[i].num << "\t" << students[i].name << "\t" << students[i].sex << "\t" << students[i].year << "-" << students[i].month << "-" << students[i].day << "\t";
cout << students[i].math << "\t" << students[i].cprog << "\t" << students[i].phy << "\t" << students[i].total << "\t" << students[i].avg << endl;
return;
}
}
cout << "未找到该学生!" << endl;
}
void searchScore() {
int score;
cout << "请输入要查找的成绩:";
cin >> score;
bool found = false;
cout << "学号\t姓名\t性别\t出生日期\t高数\tC语言\t大学物理\t总分\t平均分" << endl;
for (int i = 0; i < count; i++) {
if (students[i].math == score || students[i].cprog == score || students[i].phy == score) {
cout << students[i].num << "\t" << students[i].name << "\t" << students[i].sex << "\t" << students[i].year << "-" << students[i].month << "-" << students[i].day << "\t";
cout << students[i].math << "\t" << students[i].cprog << "\t" << students[i].phy << "\t" << students[i].total << "\t" << students[i].avg << endl;
found = true;
}
}
if (!found) {
cout << "未找到该成绩!" << endl;
}
}
void calcAvg() {
int mathSum = 0;
int cprogSum = 0;
int phySum = 0;
for (int i = 0; i < count; i++) {
mathSum += students[i].math;
cprogSum += students[i].cprog;
phySum += students[i].phy;
}
float mathAvg = mathSum / (float)count;
float cprogAvg = cprogSum / (float)count;
float phyAvg = phySum / (float)count;
cout << "高数平均分:" << mathAvg << endl;
cout << "C语言平均分:" << cprogAvg << endl;
cout << "大学物理平均分:" << phyAvg << endl;
}
void saveData() {
ofstream fout("student.txt");
for (int i = 0; i < count; i++) {
fout << students[i].num << " ";
fout << students[i].name << " ";
fout << students[i].sex << " ";
fout << students[i].year << " ";
fout << students[i].month << " ";
fout << students[i].day << " ";
fout << students[i].math << " ";
fout << students[i].cprog << " ";
fout << students[i].phy << endl;
}
fout.close();
}
void showMenu() {
cout << "1. 显示成绩" << endl;
cout << "2. 添加学生成绩" << endl;
cout << "3. 排序" << endl;
cout << "4. 查找" << endl;
cout << "5. 计算平均分" << endl;
cout << "6. 保存数据" << endl;
cout << "0. 退出程序" << endl;
}
int main() {
readData();
while (true) {
showMenu();
int choice;
cout << "请输入要执行的操作:";
cin >> choice;
if (choice == 0) {
break;
} else if (choice == 1) {
display();
} else if (choice == 2) {
add();
} else if (choice == 3) {
sort();
} else if (choice == 4) {
int searchChoice;
cout << "请选择查找方式(1.按学号查找 2.按姓名查找 3.按分数查找):";
cin >> searchChoice;
if (searchChoice == 1) {
searchNum();
} else if (searchChoice == 2) {
searchName();
} else if (searchChoice == 3) {
searchScore();
} else {
cout << "输入有误,请重新选择!" << endl;
}
} else if (choice == 5) {
calcAvg();
} else if (choice == 6) {
saveData();
} else {
cout << "输入有误,请重新选择!" << endl;
}
}
return 0;
}
```
这个程序中,我们首先定义了一个结构体 `Student`,表示学生的基本信息和成绩,然后用一个数组 `students` 存储所有学生的信息。`readData()` 函数从文件中读取数据,`display()` 函数显示所有成绩,`add()` 函数添加一条或多条记录,`sort()` 函数按照学号或总分进行排序,`searchNum()`、`searchName()` 和 `searchScore()` 函数分别按照学号、姓名和分数进行查找,`calcAvg()` 函数计算各门课程的平均分数,`saveData()` 函数将结果保存到文件中。
在主函数中,我们通过一个循环来不断读取用户的输入,根据用户输入的操作来调用相应的函数。
阅读全文