如何编写一个程序,这个程序可以读取怎么把文本变成程序运行编辑器中的某个怎么把文本变成程序运行内容?

一、分数统计(10分)课题内容:设计一个分数统计程序。包括学生信息的输入输出以及排序。通过该课题全面熟悉数组、字符串、文件的使用,掌握程序设计的基本方法及友好界面的设计。课题要求:(1)输入某班级学生的姓名、分数;(2)对(1)的分数进行降幂排列并输出;(3)具有输入输出界面。#include<stdio.h>
//结构体数组,用于存放学生信息
struct student
{
char name[20];
float score;
};
struct student stu[100];
//冒泡排序,根据学生分数从高到底排序
void Bubble_sort(struct student stu[],int n)
{
int i,j;
char temp1[20];
float temp2;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(stu[j].score<stu[j+1].score)
{
strcpy(temp1,stu[j].name);
temp2=stu[j].score;
strcpy(stu[j].name,stu[j+1].name);
stu[j].score=stu[j+1].score;
strcpy(stu[j+1].name,temp1);
stu[j+1].score=temp2;
}
}
}
}
int main()
{
int i=0,k;
char yn;
do
{
printf("请输入学生姓名:");
scanf("%s",stu[i].name);
printf("请输入学生成绩:");
scanf("%f",&stu[i].score);
getchar();
printf("继续输入吗? y/n: ");
yn=getchar();
i++;
}while(yn=='y'
yn=='Y');
Bubble_sort(stu,i);
for(k=0;k<i;k++)
printf("%s,%4.2f\n",stu[k].name,stu[k].score);
return 0;
}
二、打字程序(10分)课题内容:设计一个打字程序。包括随机产生字符串,以及字符串比较和统计。通过此课题,熟练掌握数组、格式输出、字符串处理等。课题要求:(1)随机产生一字符串,每次产生的字符串内容、长度都不同;(2)根据(1)的结果,输入字符串,判断输入是否正确,输出正确率;(3)具有输入输出界面。#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int length,i,j,count=0;//random随机数,长度
char a[10],b[10];
printf("请输入一个长度小于10的字符串: ");
scanf("%s",b);
srand(time(0));
length=1+rand()%11;//先产生随机长度
//再由字符串长度决定产生几个随机字符
for(i=0;i<length;i++)
{
a[i]=96+rand()%27;
}
printf("随机产生的字符串长度为%d\n",length);
printf("随机产生的字符串为:");
for(i=0;i<length;i++)
{
for(j=0;j<strlen(b);j++)
{
if(a[i]==b[j])
{
count++;
}
}
printf("%c",a[i]);
}
printf("\n正确率为:%d%c\n",(count*100/length),37);
return 0;
}
三、文本编辑器(10分)课题内容:设计一个简单的文本编辑器,该系统要求对一个文本文件中的内容进行各种常规操作,如:插入、删除、查找、替换等功能。通过此课题,熟练掌握文本文件的操作及用字符数组或字符指针实现字符串操作的功能。课题要求:(1)编辑文本;(2)保存、打开指定位置的文本文件;(3)具有输入输出界面。import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame implements ActionListener{
public
static void main(String[] args){
Test test=new Test();
}
JMenuBar jb=null;
JMenu jm=null;
JMenuItem jmi1=null;
JMenuItem jmi2=null;
JTextArea jta=null;
public Test()
{
jb=new JMenuBar();
this.setJMenuBar(jb);
jm=new JMenu("文件");
jb.add(jm);
jmi1=new JMenuItem("打开");
jmi1.addActionListener(this);
jm.add(jmi1);
jmi2=new JMenuItem("保存");
jmi2.addActionListener(this);
jm.add(jmi2);
jta=new JTextArea();
this.add(jta);
this.setSize(300,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jmi1)
{
JFileChooser jfc1=new JFileChooser();//创建一个文件选择对话框
jfc1.setDialogTitle("请选择文件...");//设置对话框标题
jfc1.showOpenDialog(null);
jfc1.setVisible(true);
String filename1=jfc1.getSelectedFile().getAbsolutePath();
FileReader fr=null;
BufferedReader br=null;
try{
fr=new FileReader(filename1);
br=new BufferedReader(fr);
String s="";
String allCon="";
while((s=br.readLine())!=null)
{
allCon+=s+"\r\n";
}
jta.setText(allCon);
}catch(Exception e1){
e1.printStackTrace();
}finally{
try{
br.close();
fr.close();
}catch(Exception e2){
e2.printStackTrace();
}
}
}else if(e.getSource()==jmi2){
JFileChooser jfc2=new JFileChooser();
jfc2.setDialogTitle("另存为...");
jfc2.showSaveDialog(null);
jfc2.setVisible(true);
String filename2=jfc2.getSelectedFile().getAbsolutePath();
FileWriter fw=null;
BufferedWriter bw=null;
try{
fw=new FileWriter(filename2);
bw=new BufferedWriter(fw);
bw.write(this.jta.getText());
}catch(Exception e3){
e3.printStackTrace();
}finally{
try{
bw.close();
fw.close();
}catch(Exception e4){
e4.printStackTrace();
}
}
}
}
}
四、加密(10分)课题内容:设计一个加密程序。包括明文与密钥的转换。通过此课题,熟练掌握数组、格式输出、字符串处理、类型转换等。课题要求:(1)输入任意一段明文M,以及密钥K;(2)根据以下公式将其转换为密文C。Ci = mi + K ,其中i = 0,1,……n-1 , K 为密钥;(3)具有输入输出界面。#include<stdio.h>
#include<string.h>
#define K 10
void encrypt(char a[100])
{
int i;
for(i=0;i<strlen(a);i++)
a[i]+=K;
}
int main()
{
char a[100];
printf("请输入明文:");
scanf("%s",a);
encrypt(a);
printf("%s\n",a);
return 0;
}
五、进制转换器(10分)课题内容:设计一个进制转换器程序。包括二进制、八进制、十进制、十六进制数互相转换。通过此课题,熟练掌握字符串、格式输出、进制换算的各种操作。课题要求:(1)可输入二进制、八进制、十进制、十六进制数;(2)将已输入的数转换成其余进制的数;(3)具有输入输出界面。package simplejavacalculator;
import java.util.Scanner;
public class Test{
public static void main(String[] args)
{
int n,option;
Menu();
System.out.println("请选择您要进行的操作!");
Scanner input=new Scanner(System.in);
option=input.nextInt();
switch(option)
{
case 1:
System.out.println("请输入一个二进制数:");
Scanner s1=new Scanner(System.in);
String n1;
n1=s1.next();
n=Integer.parseInt(n1, 2);
System.out.println(n1);
System.out.println(n1+"的十六进制是"+Integer.toHexString(n));
System.out.println(n1+"的八进制是"+Integer.toOctalString(n));
System.out.println(n1+"的十进制是"+Integer.parseInt(n1, 2));
break;
case 2:
System.out.println("请输入一个八进制数:");
Scanner s2=new Scanner(System.in);
String n2;
n2=s2.next();
n=Integer.parseInt(n2, 8);
System.out.println(n2+"的十六进制是"+Integer.toHexString(n));
System.out.println(n2+"的十进制是"+n);
System.out.println(n2+"的二进制是"+Integer.toBinaryString(n));
break;
case 3:
System.out.println("请输入一个十进制数:");
Scanner s3=new Scanner(System.in);
n=s3.nextInt();
System.out.println(n+"的十六进制是"+Integer.toHexString(n));
System.out.println(n+"的八进制是"+Integer.toOctalString(n));
System.out.println(n+"的二进制是"+Integer.toBinaryString(n));
break;
case 4:
System.out.println("请输入一个十六进制数:");
Scanner s4=new Scanner(System.in);
String n3;
n3=s4.next();
n=Integer.parseInt(n3, 16);
System.out.println(n3+"的十进制是"+n);
System.out.println(n3+"的八进制是"+Integer.toOctalString(n));
System.out.println(n3+"的二进制是"+Integer.toBinaryString(n));
break;
default:
break;
}
}
public static void Menu()
{
System.out.println("
进制转换器
");
System.out.println("1.二进制转换为其他进制");
System.out.println("2.八进制转换为其他进制");
System.out.println("3.十进制转换为其他进制");
System.out.println("4.十六进制转换为其他进制");
}
}
六、学生成绩核算系统的设计与实现(25分)课题内容:设计一个学生成绩核算系统。能实现从文件中读取学生成绩资料,并提供成绩查询统计服务。通过此课题,熟练掌握文件、数组、结构体的各种操作以及友好界面的设计。课题要求:(1)按班级按课程从文件中读入相应的平时成绩、期中考试成绩和期末考试成绩。(2)三个成绩对总评成绩的百分比被定义为常数,各占总成绩的30%、30%和40%。(3)计算每位学生的总评成绩。(4)计算该班级本课程的总平均成绩。(5)计算处于优、良、中、及格、不及格的学生人数以及占总人数的百分比。其中100-90为优,89-80为良,79-70为中,69-60为及格,60分以下为不及格。(6)按要求输出成绩在优、良、中、及格、不及格各区间的学生学号、成绩。#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<stdlib.h>
#include<conio.h>
#define CLASS_SIZE 32
struct student
{
char stuNum[10];
float regular_score;
float midterm_score;
float final_score;
float total_score;
};
struct student stu[CLASS_SIZE];
//初始化函数,用于从文件中读取数据,以及计算每位学生的总评成绩
void init()
{
int i;
FILE *fp;
fp=fopen("data.txt","r");
if(fp==NULL)
{
printf("data.txt not exist,now building it...\n");
fp=fopen("data.txt","w");
getch();
}
for(i=0;i<CLASS_SIZE;i++)
fscanf(fp,"%s %f %f %f\n",stu[i].stuNum,&stu[i].regular_score,&stu[i].midterm_score,&stu[i].final_score);
fclose(fp);
//初始化的时候顺便把总每个学生的总评成绩给算出来
for(i=0;i<CLASS_SIZE;i++)
stu[i].total_score=0.3*stu[i].regular_score+0.3*stu[i].midterm_score+0.4*stu[i].final_score;
}
//显示函数,用于显示该班级所有学生的成绩
void display()
{
int i,j;
system("cls");
for(j=0;(*stu[j].stuNum) && (j<CLASS_SIZE);j++);
printf("总共有%d个学生的成绩被记录了\n",j);
printf("学生成绩如下:\n");
printf("学号\t\t平时\t期中\t期末\t总评\n");
for(i=0;i<CLASS_SIZE;i++)
{
printf("%s \t%4.1f \t%4.1f \t%4.1f \t%4.1f \n",stu[i].stuNum,stu[i].regular_score,stu[i].midterm_score,stu[i].final_score,stu[i].total_score);
}
getch();
}
//查询函数,用于查询某个学生的成绩
void search(struct student *stu)
{
int i;
char stuNum[10];
printf("请输入学生学号:");
scanf("%s",stuNum);
for(i=0;i<CLASS_SIZE;i++)
{
if(strcmp(stu[i].stuNum,stuNum))
{
printf("%s的平时成绩%4.1f\n",stu[i].stuNum,stu[i].regular_score);
printf("%s的期中成绩%4.1f\n",stu[i].stuNum,stu[i].midterm_score);
printf("%s的期末成绩%4.1f\n",stu[i].stuNum,stu[i].final_score);
printf("%s的总评成绩为:%4.1f\n",stu[i].stuNum,stu[i].total_score);
getch();
return;
}
if(!*stu[i].stuNum)
{
printf("输入学号错误,没有这个学生!\n");
printf("\npress any key to continu!\n");
getch();
return;
}
}
}
//班级平均总评成绩计算函数
void classAverageTotalScore(struct student *stu)
{
int i;
float total=0,average;
for(i=0;i<CLASS_SIZE;i++)
{
total+=stu[i].total_score;
average=total/CLASS_SIZE;
}
printf("班级平均总评成绩为:%4.2f\n",average);
getch();
return;
}
//成绩划分函数,用于划分出优、良、中、及格、不及格的学生名单
void gradeDivide(struct student *stu)
{
int i,count=0;
printf("成绩为优的学生名单如下:\n");
for(i=0;i<CLASS_SIZE;i++)
{
if(stu[i].total_score>=90 && stu[i].total_score<=100)
{
printf("%s %4.1f\n",stu[i].stuNum,stu[i].total_score);
count++;
}
}
printf("占总人数的百分比为:%d%c\n",count*100/CLASS_SIZE,37);
count=0;
printf("\n成绩为良的学生名单如下:\n");
for(i=0;i<CLASS_SIZE;i++)
{
if(stu[i].total_score>=80 && stu[i].total_score<90)
{
printf("%s %4.1f\n",stu[i].stuNum,stu[i].total_score);
count++;
}
}
printf("占总人数的百分比为:%d%c\n",count*100/CLASS_SIZE,37);
count=0;
printf("\n成绩为中的学生名单如下:\n");
for(i=0;i<CLASS_SIZE;i++)
{
if(stu[i].total_score>=70 && stu[i].total_score<80)
{
printf("%s %4.1f\n",stu[i].stuNum,stu[i].total_score);
count++;
}
}
printf("占总人数的百分比为:%d%c\n",count*100/CLASS_SIZE,37);
count=0;
printf("\n成绩为及格的学生名单如下:\n");
for(i=0;i<CLASS_SIZE;i++)
{
if(stu[i].total_score>=60 && stu[i].total_score<70)
{
printf("%s %4.1f\n",stu[i].stuNum,stu[i].total_score);
count++;
}
}
printf("占总人数的百分比为:%d%c\n",count*100/CLASS_SIZE,37);
count=0;
printf("\n成绩不及格的学生名单如下:\n");
for(i=0;i<CLASS_SIZE;i++)
{
if(stu[i].total_score<60)
{
printf("%s %4.1f\n",stu[i].stuNum,stu[i].total_score);
count++;
}
}
printf("占总人数的百分比为:%d%c\n",count*100/CLASS_SIZE,37);
}
void menu()
{
printf("\n\n\t\t\t学生成绩管理系统\n");
printf("\t1.显示每位学生的成绩\n");
printf("\t2.查询某位学生的成绩\n");
printf("\t3.计算该班级本课程的总平均成绩\n");
printf("\t4.计算处于优、良、中、及格、不及格的学生人数以及占总人数的百分比\n");
printf("\t0.退出系统\n");
}
void main()
{
char x;
init();
while(1)
{
system("cls");
menu();
x=getchar();
switch(x)
{
case '0':exit(0);
case '1':display();break;
case '2':search(stu);break;
case '3':classAverageTotalScore(stu);break;
case '4':gradeDivide(stu);getch();break;
}
}
exit(0);
}
七、模拟电信计费系统的设计与实现(25分)课题内容:设计一个模拟电信计费系统。能实现从文件中读取通话以及费率资料,并提供计费、话费查询和话单查询等服务。通过此课题,熟练掌握文件读写、数组、结构体、格式输入输出的各种操作,以及友好界面的设计和一些算法思想的应用。课题要求:(1) 计费功能。根据存放在源数据文件中的通话记录和长途费率文件对每一条通话记录计算其通话费用,并将结果保存在费用文件中。其中:通话费的计算方法如下:通话费=长途电话费+本地电话费长途电话费=费率(元/分钟)×通话时长(分钟)(通话时长不满1分钟的按1分钟计算)本地电话费为:3分钟以内0.3元,以后每1分钟递增0.2元。(2) 话费查询。输入一个电话号码,从费用文件中统计该电话号码的所有本地话费、长途话费,并从用户文件中查找其用户名,最后在屏幕上显示:用户名 电话号码 本地话费 长途话费 话费总计(3) 话单查询。输入一个电话号码,查询并在屏幕显示该用户的所有通话记录,格式为:用户名 主叫电话号码 被叫电话号码 通话时长#include<stdio.h>
#include<windows.h>
#include<conio.h>
#include<string.h>
struct HostDial
{
char hostZone[10];
char hostNumber[10];
char beCallZone[10];
char beCallNumber[10];
int com_time;
};
struct FeeRate
{
char zone[4];
float fee;
};
struct User
{
char userNumber[10];
char userName[10];
};
struct Fee
{
char HostCallNumber[10];
char CallType[10];
float fee;
};
struct HostDial host_dial[20];
struct FeeRate fee_rate[5];
struct User user[40];
struct Fee fee[20];
void printAllInfo()
{
int i;
printf("CALL RECORDS:\n");
printf("host-dial
thost-number
becallzone
becall-number
com-time\n");
for(i=0;i<20;i++)
{
printf("%s\t
%s\t %s\t
%s\t
%d\n",host_dial[i].hostZone,host_dial[i].hostNumber,host_dial[i].beCallZone,host_dial[i].beCallNumber,host_dial[i].com_time);
}
printf("\nCHARGE RATE TABLE:\n");
printf("zone\tfee\n");
for(i=0;i<5;i++)
{
printf("%s\t%4.2f\n",fee_rate[i].zone,fee_rate[i].fee);
}
printf("\nUSER TABLE\n");
printf("user-number\tuser-name\n");
for(i=0;i<40;i++)
{
printf("%s\t%s\n",user[i].userName,user[i].userNumber);
}
printf("\nCALL CHARGE TABLE\n:\N");
printf("telephone-number\tcall-type\tcall charge\n");
for(i=0;i<20;i++)
{
printf("%s\t\t%s\t\t%4.2f\n",fee[i].HostCallNumber,fee[i].CallType,fee[i].fee);
}
}
void calcFee()
{
int i,k;
for(i=0;i<20;i++)
{
strcpy(fee[i].HostCallNumber,host_dial[i].hostNumber);
//if it is native call
if(strcmp(host_dial[i].hostZone,host_dial[i].beCallZone))
{
strcpy(fee[i].CallType,"native");
}
else
{
strcpy(fee[i].CallType,"long");
}
//figure out call charge according to zone charge rate
for(k=0;k<5;k++)
{
if(strcmp(fee_rate[k].zone,host_dial[i].beCallZone))
{
fee[i].fee=host_dial[i].com_time*fee_rate[k].fee;
break;
}
}
}
}
void init()
{
int i;
FILE *fp;
//loading hd.txt
fp=fopen("hd.txt","r");
if(fp==NULL)
{
printf("there not exist hd.txt file,now system is building it\n");
fp=fopen("hd.txt","w");
getch();
}
for(i=0;i<20;i++)
{
fscanf(fp,"%s %s %s %s %d\n",host_dial[i].hostZone,host_dial[i].hostNumber,host_dial[i].beCallZone,host_dial[i].beCallNumber,&host_dial[i].com_time);
}
fclose(fp);
//loading fl.txt
fp=fopen("fl.txt","r");
if(fp==NULL)
{
printf("there not exist fl.txt,now system is building it\n");
fp=fopen("fl.txt","w");
getch();
}
for(i=0;i<5;i++)
{
fscanf(fp,"%s %f",fee_rate[i].zone,&fee_rate[i].fee);
}
fclose(fp);
//loading yh.txt
fp=fopen("yh.txt","r");
if(fp==NULL)
{
printf("there not exist yh.txt,now system is building it\n");
fp=fopen("yh.txt","w");
getch();
}
for(i=0;i<40;i++)
{
fscanf(fp,"%s %s\n",user[i].userName,user[i].userNumber);
}
fclose(fp);
calcFee();
}
void store()
{
FILE *fp;
int i;
fp=fopen("fy.txt","w");
for(i=0;i<20;i++)
{
fprintf(fp,"%s\t\t%s\t\t%4.2f\n",fee[i].HostCallNumber,fee[i].CallType,fee[i].fee);
}
fclose(fp);
}
void printCase1Info()
{
printf("All the users call charge has been calculated as follow:\n");
printAllInfo();
printf("Press any key to return the mani menu");
getch();
}
void CallChargeInquiry()
{
int i;
float charge=0;
char phoneNumber[8];
printf("please input your telephone numbere:");
scanf("%s",phoneNumber);
for(i=0;i<20;i++)
{
if(strcmp(fee[i].HostCallNumber,phoneNumber)==0)
{
charge+=fee[i].fee;
}
}
printf("your current call charge is %4.2f\n",charge);
printf("Press any key to return the mani menu");
getch();
}
void BillLook()
{
int i;
char phoneNumber[10];
printf("please input your telephone numbere:");
scanf("%s",phoneNumber);
for(i=0;i<20;i++)
{
if(strcmp(fee[i].HostCallNumber,phoneNumber)==0)
{
printf("%s %s %4.2f\n",fee[i].HostCallNumber,fee[i].CallType,fee[i].fee);
}
}
printf("Press any key to return the main menu");
getch();
}
void menu()
{
printf("\t\t模拟电信计费系统\n");
printf("\t\t1.计算通话费用\n");
printf("\t\t2.话费查询\n");
printf("\t\t3.账单查询\n");
printf("\t\t0.退出并保存费用文件\n");
}
int main()
{
char x;
init();
while(1)
{
system("cls");
menu();
x=getchar();
switch(x)
{
case '0':store(); exit(0);
case '1':printCase1Info();break;
case '2':CallChargeInquiry();break;
case '3':BillLook();break;
default:break;
}
}
exit(0);
return 0;
}

我要回帖

更多关于 怎么把文本变成程序运行 的文章

 

随机推荐