南邮-软件设计实验(C++版)

说在前面:所有代码实现均在C++环境下实现(与c基本一致,只是文件后缀名不一样)。建立文件(*.cpp),粘贴代码即可,运行~
若将代码直接复制进.c文件编译会出错哦!
如需C语言版移步软件设计-C语言版

本次设计实验主要是对C语言字符串、数组、结构体、文件读写等知识的实际运用。文章中的代码测试基本可以运行,界面还需美化。

A档

一、分数统计

课题内容:

  设计一个分数统计程序。包括学生信息的输入输出以及排序。通过该课题全面熟悉数组、字符串、文件的使用,掌握程序设计的基本方法及友好界面的设计。

课题要求:

(1)输入某班级学生的姓名、分数;
(2)对(1)的分数进行降幂排列并输出;
(3)具有输入输出界面。

运行示例

示例代码:

A1.cpp

#include<stdio.h>
#include<string.h>
#include<iostream>

using namespace std;

void swap(char *p,char *q)
{
   
    char a[20];
    strcpy(a,p);
    strcpy(p,q);
    strcpy(q,a);
}

int main(){
   
    int score[5],i,j,score_temp;
    char name[5][10];
    cout<<"********学生成绩排序系统**********"<<endl;
    cout<<"请输入学生姓名成绩:"<<endl;
    for(i=0;i<5;i++){
   
        cin>>name[i];
		cin>>score[i];
    }
    for(i=0;i<5;i++){
   
        for(j=i+1;j<5;j++){
   
            if(score[i]<score[j]){
   
                score_temp = score[i];
                score[i] = score[j];
                score[j] = score_temp;
                swap(name[i],name[j]);
            }
        }
    }
    printf("学生成绩排名如下:\n");
    for(i=0;i<5;i++){
   
        printf("%s %d\n",name[i],score[i]);
    }
    return 0;
}


/*
one 90
two 80
three 92
four 99
five 86
*/
//此处测试用例5个,可自行扩展

二、打字程序

课题内容:

  设计一个打字程序。包括随机产生字符串,以及字符串比较和统计。通过此课题,熟练掌握数组、格式输出、字符串处理等。

课题要求:

(1)随机产生一字符串,每次产生的字符串内容、长度都不同;
(2)根据(1)的结果,输入字符串,判断输入是否正确,输出正确率;
(3)具有输入输出界面。

运行示例

示例代码:

A2.cpp

#include<stdio.h>
#include<iostream>
#include<time.h>
#include<stdlib.h>
#include<string.h>


using namespace std;

char *randstr(char *str)
{
   
	srand(time(NULL));
	int i,len;
	len = rand()%10+2;
	for (i = 0; i < len; ++i)
	{
   
		switch ((rand() % 3))
		{
   
		case 1:
			str[i] = 'A' + rand() % 26;
			break;
		case 2:
			str[i] = 'a' + rand() % 26;
			break;
		default:
			str[i] = '0' + rand() % 10;
			break;
		}
	}
	str[i] = '\0';
	return str;
}

int main(){
   
    char name[20],inputstr[20];
    int i=0,error = 0,gailv,j=5;
    while(j!=0){
   
        cout << randstr(name)<< endl;
        cin>>inputstr;
        while(name[i]!='\0'){
   
            if(name[i]!= inputstr[i]){
   
                error++;
            }
            i++;
        }
        if(error == 0){
   
            cout<<"输入正确,正确率:100%\n"<<endl;
        }else{
   
            gailv = (1-error/(float)strlen(name))*100;
            printf("输入错误,正确率:");
            cout<<gailv;
            printf("%%\n");
        }
        j--;
        error=0;
        i=0;
    }

    return 0;
}

三、文本编辑器

课题内容:

  设计一个简单的文本编辑器,该系统要求对一个文本文件中的内容进行各种常规操作,如:插入、删除、查找、替换等功能。通过此课题,熟练掌握文本文件的操作及用字符数组或字符指针实现字符串操作的功能。

课题要求:

(1)编辑文本;
(2)保存、打开指定位置的文本文件;
(3)具有输入输出界面。

运行示例

示例代码

A3.cpp

#include<iostream>
#include<string>
#include<cstdlib>
#include<ctype.h>
#include<cstdio>
#include<fstream>
using namespace std;
int NumberCount=0;//数字个数
int CharCount=0;//字母个数
int PunctuationCount=0;//标点符号个数
int BlankCount=0;//空白符个数

class Node
{
   
public:
    string character;
    int cursor;
    int offset;
    Node* next;
    Node(){
   
        cursor=0;//每行的光标初始位置
        offset=0;//每行的初始偏移位置
        next=NULL;
    }
};

class TextEditor
{
   
private:
    Node* head;
    string name;
    int line;//可更改的行数
    int length;//行数
public:
    TextEditor();
    ~TextEditor();
    string GetName();
    void SetName(string name);
    int GetCursor();
    int MoveCursor(unsigned int offset);
    int SetCursor(int line,int offset);
    void AddText(const string s);
    void InsertText(int seat,string s);
    int FindText(string s);
    void DeleteText(string s);
    int GetLine();
    void Count();
    friend ostream& operator<<(ostream& out,TextEditor &text);
    Node* Gethead(){
   
        return head;
    }
    //int GetLength()
    //{
   
   //     return length;
   // }
  //  int FindText(string s);
  //  void DeleteText(int seat,string s);
};

TextEditor::TextEditor()
{
   
    head=NULL;
    name="test";//文件初始名
    //tail=NULL;
    line=1;
    length=0;
}

TextEditor::~TextEditor()
{
   
    Node* p=head;
    Node* q;
    while(p!=NULL){
   
        q=p->next;
        delete p;
        p=q;
    }

}

int TextEditor::GetLine()
{
   
    return line;
}

string TextEditor::GetName()
{
   
    return name;
}

void TextEditor::SetName(string name)
{
   
    this->name=name;
}

int TextEditor::GetCursor()
{
   
    Node *p=head;
    while(p->next!=NULL)
        p=p->next;
    return p->cursor;
}

int TextEditor::MoveCursor(unsigned int offset)
{
   
    Node *p=head;
    int i=1;
    if(length+1<line){
   
        cout<<"输入错误!"<<endl;
        exit(0);
    }
    else{
   
        while(p->next!=NULL&&i<line){
   
            p=p->next;
            i++;
        }
    }
    if(offset>p->character.length()){
   
        cout<<"移动位置太大!"<<endl;
        exit(0);
    }
    else
       p->cursor+=offset;
    //cout<<"p  ->cursor="<<p->cursor<<endl;
    return p->cursor;
}

int TextEditor::SetCursor(int line,int offset)
{
   
    this->line=line;
   //cout<<"line="<<this->line<<endl;
    return MoveCursor(offset);
}

void TextEditor::AddText(const string s)
{
   
    line=length+1;
    Node* p=new Node;
    Node* q=head;
    p->character=s;
    p->next=NULL;
    if(head==NULL)
        head=p;
    else{
   
        while(q->next!=NULL)
            q=q->next;
        q->next=p;
    }

        length++;
       // line++;
}

void TextEditor::InsertText(int seat,string s)
{
   
    Node *p=head;
    unsigned int i=1;
    if(length+1<line){
   
        cout<<"输入错误!"<<endl;
        exit(0);
    }
    else{
   
        while(p->next!=NULL&&i<line){
   
            p=p->next;
            i++;
        }
    }
    //MoveCursor(seat);
    //cout<<"p->cursor="<<p->cursor<<endl;
    string substr;
    for(i=seat;i<s.length()+seat&&i<=p->character.length();i++)
    substr+=p->character[i];

    p->character.insert(p->cursor,s);


    cout<<"substr="<<substr<<endl;
    
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值