Thank to Gary's solution giving me idea.
I have tested it. If there is any bug, just tell me.
This is Gary's link: http://garydeng.tumblr.com/
void Console::display(const char* str, int row, int col, int fieldLen){
for(setPos(row, col),row=0;fieldLen==0?(str[row] != 0):(row<fieldLen);row<strlen(str)?(putChar(str[row])):(putChar(' ')),row++);
}
Friday, 1 February 2013
Xmover solution
#include "console.h"
using namespace cio;
int main(){
bool done = false;
int row = console.getRows()/2;
int col = console.getCols()/2;
int key ;
while(!done){
console.setPos(row, col);
console.putChar('X');
console.setPos(row, col);
key = console.getKey();
console.setPos(row, col);
console.putChar('.');
switch(key){
case UP:
if(row > 0 ){
row--;
}
else{
console.alarm();
}
break;
case DOWN:
if((row < console.getRows()-2)||((col != console.getCols()-1)&&(row = console.getRows()-2))){
row++;
}
else{
console.alarm();
}
break;
case LEFT:
if(col > 0){
col--;
}
else{
console.alarm();
}
break;
case RIGHT:
if((col < console.getCols()-2)||((row != console.getRows()-1)&&(col = console.getCols()-2))){
col++;
}
else{
console.alarm();
}
break;
case ESCAPE:
done = true;
break;
}
}
return 0;
}
using namespace cio;
int main(){
bool done = false;
int row = console.getRows()/2;
int col = console.getCols()/2;
int key ;
while(!done){
console.setPos(row, col);
console.putChar('X');
console.setPos(row, col);
key = console.getKey();
console.setPos(row, col);
console.putChar('.');
switch(key){
case UP:
if(row > 0 ){
row--;
}
else{
console.alarm();
}
break;
case DOWN:
if((row < console.getRows()-2)||((col != console.getCols()-1)&&(row = console.getRows()-2))){
row++;
}
else{
console.alarm();
}
break;
case LEFT:
if(col > 0){
col--;
}
else{
console.alarm();
}
break;
case RIGHT:
if((col < console.getCols()-2)||((row != console.getRows()-1)&&(col = console.getCols()-2))){
col++;
}
else{
console.alarm();
}
break;
case ESCAPE:
done = true;
break;
}
}
return 0;
}
Monday, 21 January 2013
A easy copy cmd I code
//copy.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char * argv[]){
FILE *in, *out;
int c;
if(argc!=3){
printf("U forgot to enter a filename!");
exit(0);
}
if((in=fopen(argv[1],"r"))==NULL){
printf("cannot open infile\n");
exit(0);
}
if((out=fopen(argv[2],"w"))==NULL){
printf("cannot open outfile\n");
exit(0);
}
while( ( c = fgetc(in) ) != EOF )
{
fputc(c, out);
}
fclose(in);
fclose(out);
}
-------------------------------------------------------------------------------------------------------------------------
cc copy.c -o copy
copy fileA fileB
and you get a new fileB same as fileA.
PS: In linux, if you copy a executable file using this program,to run the new file u need to give the new file execute permission(chmod)......
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char * argv[]){
FILE *in, *out;
int c;
if(argc!=3){
printf("U forgot to enter a filename!");
exit(0);
}
if((in=fopen(argv[1],"r"))==NULL){
printf("cannot open infile\n");
exit(0);
}
if((out=fopen(argv[2],"w"))==NULL){
printf("cannot open outfile\n");
exit(0);
}
while( ( c = fgetc(in) ) != EOF )
{
fputc(c, out);
}
fclose(in);
fclose(out);
}
-------------------------------------------------------------------------------------------------------------------------
cc copy.c -o copy
copy fileA fileB
and you get a new fileB same as fileA.
PS: In linux, if you copy a executable file using this program,to run the new file u need to give the new file execute permission(chmod)......
CMD line calculation PGM
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int valid(char s[]){
int valid = 0;
int cnt = 0;
int i;
for(i = 0;i < strlen(s);i++){
if(s[i] >= '0' && s[i] <= '9'){
valid = 1;
}
else if(s[i] == '.')
cnt++;
else{
valid = 0;
break;
}
}
if(cnt > 1)
valid = 0;
if(valid == 0){
printf("Plz enter valid numbers!\n");
exit(1);
}
return valid;
}
int main(int argc,char * argv[]){
double num1;
double num2;
if(argc!=3){
printf("Plz enter two numbers!!\n");
exit(0);
}
if((valid(argv[1])) && (valid(argv[2]))){
sscanf(argv[1],"%lf",&num1);
sscanf(argv[2],"%lf",&num2);
}
printf("answer: %lf \n",num1+num2);
/* num1-num2
num1*num2
To get num1/num2 need to validate that the num2 cant not be zero.
*/
return 2;
}
#include <string.h>
#include <stdlib.h>
int valid(char s[]){
int valid = 0;
int cnt = 0;
int i;
for(i = 0;i < strlen(s);i++){
if(s[i] >= '0' && s[i] <= '9'){
valid = 1;
}
else if(s[i] == '.')
cnt++;
else{
valid = 0;
break;
}
}
if(cnt > 1)
valid = 0;
if(valid == 0){
printf("Plz enter valid numbers!\n");
exit(1);
}
return valid;
}
int main(int argc,char * argv[]){
double num1;
double num2;
if(argc!=3){
printf("Plz enter two numbers!!\n");
exit(0);
}
if((valid(argv[1])) && (valid(argv[2]))){
sscanf(argv[1],"%lf",&num1);
sscanf(argv[2],"%lf",&num2);
}
printf("answer: %lf \n",num1+num2);
/* num1-num2
num1*num2
To get num1/num2 need to validate that the num2 cant not be zero.
*/
return 2;
}
Monday, 14 January 2013
Subscribe to:
Posts (Atom)