读取csv文件数据内容进行图形绘制(vc++描述)
2012-11-06 19:36
459 查看
根据csv文件数据内容绘图。
csv文件内容包括:图形类型(横线,竖线,横半圆弧,竖半圆弧),长度(线长度,半圆半径)。根据csv文件中提供的数据,在dialog中绘制出相应的图形。如果要绘制的图形超出dialog的
x轴边界,则从x=0,y不变的坐标开始绘制该图形;如果超出了y轴边界,则从x不变,y=0坐标开始绘制。(编码要求:从csv文件读取的内容和操作使用链表类保持)。
例如:csv文件内容为:(假设:a:横线,b:竖线,c:横半圆,d:竖半圆)
style,length
a, 50
b,50
c, 25
d, 25
首先要定义头文件:
#include <string>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <vector>
#include <stdio.h>
using namespace std;
vector<string> str_iterator ; //to deposit the data
在初始化里也可以加上如下语句:
m_fileName="d:\\recordset.csv";
UpdateData(FALSE);
然后在dialog中添加一个读取文件的按钮,并添加如下代码:
UpdateWindow();
str_iterator.clear();
fstream istrm; //define a I/O stream
string s;
int pos,i;
char *store;
string name;
name=m_fileName;
UpdateData(TRUE);
store=(char *)name.c_str();
istrm.open(store,ios::in); //open the file by only-read
if(!istrm)
{
MessageBox("Can't open file!\n");
}
else
{
while(!istrm.eof())
{
istrm>>s;
i=0;
int log=1;
while(log)
{
pos = s.find(",",i);
if(pos == string::npos)
{ //if can't find
str_iterator .push_back(s.substr(i));
log=0;
}
else //if find the character of ","
str_iterator .push_back(s.substr(i,pos-i));
i = pos+1;
}
}
str_iterator.pop_back();
istrm.close();
UpdateWindow(); //引用"OnPaint()"
}
最后在"OnPaint()"里面添加如下的处理代码:
UpdateWindow();
CClientDC dc(this);
CPen pen(PS_SOLID,1,RGB(0,0,255));
CRect rect;
GetClientRect(&rect);
dc.MoveTo(5,46);
CPoint p;
int var;
char *ch;
CWnd *pWnd = GetDlgItem(IDC_STATIC);
CPen *pOldPen= dc.SelectObject(&pen);
int x=0;
while (x < str_iterator .size())
{
ch = (char*)str_iterator [x].c_str();
var = atoi((char*)str_iterator [x+1].c_str());
switch(*ch)
{
case 'a': // draw the horizontal line
{
p=dc.GetCurrentPosition();
if((p.x+var) > rect.right) // 判断是否越界
{
dc.MoveTo(5,p.y);
}
p=dc.GetCurrentPosition();
dc.LineTo(p.x+var,p.y);
break;
}
case 'b': // draw the vertical line
{
p=dc.GetCurrentPosition();
if((p.y+var) > rect.bottom) // 判断是否越界
{
dc.MoveTo(p.x,46);
}
p=dc.GetCurrentPosition();
dc.LineTo(p.x,p.y+var);
break;
}
case 'c': // draw the semicircle
{
p=dc.GetCurrentPosition();
if(((p.x+var*2) > rect.right)) // 判断是否越界
{
dc.MoveTo(5,p.y);
}
if (((p.y+var) > rect.bottom))
{
dc.MoveTo(p.x,46);
}
p=dc.GetCurrentPosition();
dc.ArcTo(p.x,p.y-var,(p.x+var*2),p.y+var,p.x,p.y,(p.x+var*2),p.y);
break;
}
case 'd': // draw the semicircle
{
p=dc.GetCurrentPosition();
if((p.x+var)> rect.right) // 判断是否越界
{
dc.MoveTo(5,p.y);
}
if((p.y+var*2)>rect.bottom)
{
dc.MoveTo(p.x,46);
}
p=dc.GetCurrentPosition();
dc.Arc(p.x-var,(p.y+var*2),p.x+var,p.y,p.x,(p.y+var*2),p.x,p.y);
dc.MoveTo(p.x,p.y+var*2);
break;
}
default:
break;
}
x = x+2;
}
dc.SelectObject(pOldPen);
csv文件内容包括:图形类型(横线,竖线,横半圆弧,竖半圆弧),长度(线长度,半圆半径)。根据csv文件中提供的数据,在dialog中绘制出相应的图形。如果要绘制的图形超出dialog的
x轴边界,则从x=0,y不变的坐标开始绘制该图形;如果超出了y轴边界,则从x不变,y=0坐标开始绘制。(编码要求:从csv文件读取的内容和操作使用链表类保持)。
例如:csv文件内容为:(假设:a:横线,b:竖线,c:横半圆,d:竖半圆)
style,length
a, 50
b,50
c, 25
d, 25
首先要定义头文件:
#include <string>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <vector>
#include <stdio.h>
using namespace std;
vector<string> str_iterator ; //to deposit the data
在初始化里也可以加上如下语句:
m_fileName="d:\\recordset.csv";
UpdateData(FALSE);
然后在dialog中添加一个读取文件的按钮,并添加如下代码:
UpdateWindow();
str_iterator.clear();
fstream istrm; //define a I/O stream
string s;
int pos,i;
char *store;
string name;
name=m_fileName;
UpdateData(TRUE);
store=(char *)name.c_str();
istrm.open(store,ios::in); //open the file by only-read
if(!istrm)
{
MessageBox("Can't open file!\n");
}
else
{
while(!istrm.eof())
{
istrm>>s;
i=0;
int log=1;
while(log)
{
pos = s.find(",",i);
if(pos == string::npos)
{ //if can't find
str_iterator .push_back(s.substr(i));
log=0;
}
else //if find the character of ","
str_iterator .push_back(s.substr(i,pos-i));
i = pos+1;
}
}
str_iterator.pop_back();
istrm.close();
UpdateWindow(); //引用"OnPaint()"
}
最后在"OnPaint()"里面添加如下的处理代码:
UpdateWindow();
CClientDC dc(this);
CPen pen(PS_SOLID,1,RGB(0,0,255));
CRect rect;
GetClientRect(&rect);
dc.MoveTo(5,46);
CPoint p;
int var;
char *ch;
CWnd *pWnd = GetDlgItem(IDC_STATIC);
CPen *pOldPen= dc.SelectObject(&pen);
int x=0;
while (x < str_iterator .size())
{
ch = (char*)str_iterator [x].c_str();
var = atoi((char*)str_iterator [x+1].c_str());
switch(*ch)
{
case 'a': // draw the horizontal line
{
p=dc.GetCurrentPosition();
if((p.x+var) > rect.right) // 判断是否越界
{
dc.MoveTo(5,p.y);
}
p=dc.GetCurrentPosition();
dc.LineTo(p.x+var,p.y);
break;
}
case 'b': // draw the vertical line
{
p=dc.GetCurrentPosition();
if((p.y+var) > rect.bottom) // 判断是否越界
{
dc.MoveTo(p.x,46);
}
p=dc.GetCurrentPosition();
dc.LineTo(p.x,p.y+var);
break;
}
case 'c': // draw the semicircle
{
p=dc.GetCurrentPosition();
if(((p.x+var*2) > rect.right)) // 判断是否越界
{
dc.MoveTo(5,p.y);
}
if (((p.y+var) > rect.bottom))
{
dc.MoveTo(p.x,46);
}
p=dc.GetCurrentPosition();
dc.ArcTo(p.x,p.y-var,(p.x+var*2),p.y+var,p.x,p.y,(p.x+var*2),p.y);
break;
}
case 'd': // draw the semicircle
{
p=dc.GetCurrentPosition();
if((p.x+var)> rect.right) // 判断是否越界
{
dc.MoveTo(5,p.y);
}
if((p.y+var*2)>rect.bottom)
{
dc.MoveTo(p.x,46);
}
p=dc.GetCurrentPosition();
dc.Arc(p.x-var,(p.y+var*2),p.x+var,p.y,p.x,(p.y+var*2),p.x,p.y);
dc.MoveTo(p.x,p.y+var*2);
break;
}
default:
break;
}
x = x+2;
}
dc.SelectObject(pOldPen);
相关文章推荐
- canvas绘制心电图(js读取csv心电数据文件)
- java使用CsvReader和CsvWriter对csv文件内容进行读取和写入操作
- 根据文件中读取的数据使用turtle绘制图形路径
- MFC环境下搭建OpenGL框架并进行数据文件读取以及模型绘制的注意事项
- 使用文件进行数据存储及读取文件内容
- python实现从文件中读取数据并绘制成 x y 轴图形
- excel、csv、txt文件数据读取
- VC对csv文件进行读写操作
- python 读取.csv文件数据到数组(矩阵)的实例讲解
- java读取文件数据进行一些简单处理的例子
- SQL 读取csv 文件批量插入数据
- PHP读取CSV文件内容 并且插入到数据库中
- Python绘制直方图(Histograms)—从文件中读取数据画图update@2017-05-17
- C#根据指定url下载文件并读取.csv数据转为json格式
- 读取csv文件生成分析图形
- C++读取csv文件并以map进行存储
- Java读取本地文件,然后把数据存到mysql.(以csv文件为例)
- java读取目录下所有csv文件数据,存入三维数组并返回
- 从本地目录读取指定.txt文件,并且对数据进行解析。
- 字符流读取多行文件内容进行校验