LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > General
User Name
Password
General This forum is for non-technical general discussion which can include both Linux and non-Linux topics. Have fun!

Notices

Reply
 
LinkBack Search this Thread
Old 05-26-2010, 06:56 AM   #16
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713

Quote:
Originally Posted by MrCode View Post
strcmp(*argv,questions[i])
You're comparing the question to the name of the program, not the first argument!

argv[0] (equivalent to *argv) contains the command used to execute the program.

argv[1] contains the first argument.
 
Old 05-26-2010, 11:27 AM   #17
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-19-live-usb/ aix
Posts: 3,553

Rep: Reputation: 514Reputation: 514Reputation: 514Reputation: 514Reputation: 514Reputation: 514
http://www.linuxquestions.org/questi...center-719104/
 
Old 05-26-2010, 12:16 PM   #18
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,010

Rep: Reputation: 352Reputation: 352Reputation: 352Reputation: 352
i've probably posted this somewhere else in this forum at some point in time but.. a simple image randomization script for message board signatures (yes i probably could have randomized based on a directory list but i was too lazy and didn't know how at the time when i wrote it)
Code:
<?php
$me = rand(1,8);
$pic=imagecreatefromjpeg($me.".jpg");
list($width, $height, $type, $attr) = getimagesize($me.".jpg");
$layer = imagecreatetruecolor  ( 150  , 150  );
imagecopyresized  ( $layer  , $pic  , 0  , 0  , 0  , 0  , 150  , 150  , $width  , $height  );
header("Content-type: image/png");
imagejpeg($layer);
imagedestroy($layer);
imagedestroy($pic);
?>
 
Old 05-26-2010, 02:31 PM   #19
MrCode
Member
 
Registered: Aug 2009
Location: Oregon, USA
Distribution: Arch
Posts: 864
Blog Entries: 31

Rep: Reputation: 144Reputation: 144
Quote:
Originally Posted by MTK358
argv[0] (equivalent to *argv) contains the command used to execute the program.

argv[1] contains the first argument.
Duh! I knew it was probably a problem with the for loop!

Well, I changed the *argv to *(argv+1), and that makes the first question work, but I'm still trying to figure out why the others won't...

EDIT: Removing the else break; clause did it.

The revised code:

Code:
#include <stdio.h>

char* questions[3] = {"What are you doing now?",
		      "Who are you?",
		      "How are you feeling?"};

char* answers[3] = {"None of your beeswax...\n",
		    "Who are YOU?\n",
		    "Why do you care?\n"};

int main(int argc,char** argv)
{
	if(argc < 2)
	{
		printf("Usage: ./askme <phrase>\n");
		printf("(where <phrase> is a question)\n");
		
		return 1;
	}
	else
	{
		int i;
		for(i = 0; i <= 2; i++)
		{
			if(strcmp(*(argv+1),questions[i]) == 0)
				printf("%s",answers[i]);
		}
	}
	
	return 0;
}

Last edited by MrCode; 05-26-2010 at 02:34 PM.
 
Old 05-26-2010, 02:41 PM   #20
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
That's good.

I still don't understand why you wrote *(argv+1) instead of the equivalent, but shorter and easier to understand argv[1].
 
Old 05-26-2010, 02:47 PM   #21
MrCode
Member
 
Registered: Aug 2009
Location: Oregon, USA
Distribution: Arch
Posts: 864
Blog Entries: 31

Rep: Reputation: 144Reputation: 144
Quote:
I still don't understand why you wrote *(argv+1) instead of the equivalent, but shorter and easier to understand argv[1].
It helps to reinforce the concept of how pointers really work, in my mind. It reminds me that the "pointer" is really just a counter that keeps track of a memory address, and that the "array indices" are simply relative address points to the initial address that the "pointer" counter keeps track of, incrementing/decrementing by one or more units of the variable data type size (1 byte for char, 4 bytes for int, etc.).

Wow, even reading just part of that asm tutorial really solidified these concepts for me quite well!

Last edited by MrCode; 05-26-2010 at 02:50 PM.
 
Old 05-26-2010, 02:51 PM   #22
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
But the [] operator is designed as a shortcut for pointers treated as arrays.
 
Old 05-26-2010, 02:55 PM   #23
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,008

Rep: Reputation: 110Reputation: 110
I could show you the code I've written lately, but then I'd have to kill you.

I mean, kill you *sooner*
 
Old 06-19-2010, 09:52 AM   #24
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
I made a really cool PyQt4 animation test:

Code:
#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class DrawingTest(QFrame):
	def __init__(self, parent=None):
		QFrame.__init__(self, parent)

		self.paint_count = 0 # count frames
		self.angle = 0       # current angle

		self.ballx = self.width() / 2  # ball properties
		self.bally = self.height() / 2
		self.balldx = 1
		self.balldy = -2.5
		self.balld = 16
		self.gravity = 0.05

		# animation timer
		self.timer = QTimer()
		QObject.connect(self.timer, SIGNAL('timeout()'), self.animate)
		self.timer.start(10)

	def animate(self):
		self.angle += 16 # increment angle

		self.ballx += self.balldx # apply horizontal velocity
		if self.ballx < 0:        # bounce off left and right walls
			self.ballx = 0
			self.balldx = -self.balldx
		elif (self.ballx + self.balld) >= self.width():
			self.ballx = self.width() - self.balld - 1
			self.balldx = -self.balldx

		self.bally += self.balldy # apply vertical velocity
		if self.bally < 0:        # bounce off top and bottom walls
			self.bally = 0
			self.balldy = -self.balldy
		elif (self.bally + self.balld) >= self.height():
			self.bally = self.height() - self.balld - 1
			self.balldy = -self.balldy
		self.balldy += self.gravity # apply gravity
		
		self.repaint() # force repaint

	def paintEvent(self, event):
		self.paint_count += 1            # count frames
		print 'Paint:', self.paint_count
		
		p = QPainter(self) # draw background rectangle
		p.setPen(Qt.blue)
		p.setBrush(Qt.red)
		p.drawRect(10, 10, self.width() - 20, self.height() - 20)

		width = self.width() - 40 # calculate pie slice dimensions
		height = self.height() - 40
		x = (self.width() / 2) - (width / 2)
		y = (self.height() / 2) - (height / 2)

		p.setPen(Qt.NoPen) # draw pie slice
		p.setBrush(Qt.green)
		p.drawPie(x, y, width, height, self.angle, 45 * 16)

		p.setPen(Qt.NoPen) # draw ball
		p.setBrush(Qt.blue)
		p.drawEllipse(self.ballx, self.bally, self.balld, self.balld)
		p.setBrush(Qt.cyan) # draw ball highlight
		p.drawEllipse(self.ballx+self.balld/4, self.bally+self.balld/4, self.balld/3, self.balld/3)

class MainWindow(QMainWindow):
	def __init__(self):
		QMainWindow.__init__(self)

		drawing = DrawingTest()
		self.setCentralWidget(drawing)

app = QApplication(sys.argv)
w = MainWindow();
w.show()
sys.exit(app.exec_())

# EOF
Basically it shows a rotating pie slice shape on a red rectangle with a blue ball bouncing in the foreground.
 
Old 06-19-2010, 03:41 PM   #25
rsciw
Member
 
Registered: Jan 2009
Location: Essex (UK)
Distribution: Home: Debian/Ubuntu, Work: Ubuntu
Posts: 206

Rep: Reputation: 44
Quote:
Originally Posted by frieza View Post
i've probably posted this somewhere else in this forum at some point in time but.. a simple image randomization script for message board signatures (yes i probably could have randomized based on a directory list but i was too lazy and didn't know how at the time when i wrote it)
Code:
<?php
$me = rand(1,8);
$pic=imagecreatefromjpeg($me.".jpg");
list($width, $height, $type, $attr) = getimagesize($me.".jpg");
$layer = imagecreatetruecolor  ( 150  , 150  );
imagecopyresized  ( $layer  , $pic  , 0  , 0  , 0  , 0  , 150  , 150  , $width  , $height  );
header("Content-type: image/png");
imagejpeg($layer);
imagedestroy($layer);
imagedestroy($pic);
?>
neat, I've got something like that too
in one I display a few characters randomly of an MMO I play in the respective forum, in the other I just have a mockup of Schroedinger's cat, randomly one of the two given possibilities ^^
 
Old 09-02-2010, 06:35 PM   #26
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,010

Rep: Reputation: 352Reputation: 352Reputation: 352Reputation: 352
a bit of nostalgia
Code:
CLS
INPUT "number of columns"; cols
CLS
FOR x = 1 TO cols
        FOR z = 1 TO (cols - x)
                st$ = st$ + " "
        NEXT
        FOR y = 1 TO x
                st$ = st$ + "* "
        NEXT
        PRINT st$
        st$ = ""
NEXT
any guess as to the programming language?

same program in c++
Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main (int argc, char* argv[])
{
	if (argv[1] == NULL) {
		cout << "usage: pyramid number of rows\n";
		return 1;
	}
	int cols=atoi(argv[1]);
	for (int n=0; n<=cols; n++) {
		for (int z=0;z<=(cols-n);z++) {
		    cout << " ";
		}
		for (int y=0;y<=n;y++) {

			cout << "* ";
		}
		cout << "\n";
	}
	return 1;
}
and in c++ as a cgi program
accessable when compiled and placed in the cgi-bin folder by
Code:
http://{host}/cgi-bin/pyramid.cgi?{number of colums}
Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main (int argc, char* argv[])
{
      	cout<<"Content-type: text/html"<<endl<<endl;
	if (argv[1] == NULL) {
		cout << "usage: pyramid number of rows\n";
		return 1;
	}
	int cols=atoi(argv[1]);
	for (int n=0; n<=cols; n++) {
		for (int z=0;z<=(cols-n);z++) {
		    cout << "&nbsp;";
		}
		for (int y=0;y<=n;y++) {

			cout << "*&nbsp;";
		}
		cout << "<br />";
	}
	return 1;
}

Last edited by frieza; 09-02-2010 at 06:42 PM.
 
Old 09-10-2010, 06:38 PM   #27
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,010

Rep: Reputation: 352Reputation: 352Reputation: 352Reputation: 352
Code:
<?php
include("Archive/Tar.php");
$tar = new Archive_Tar("stuff.tar.bz2");
$filec=$tar->extractInString($_GET['file'].'.php');
$sanitized=ereg_replace("<\?php",'',ereg_replace("\?>",'',$filec));
eval($sanitized);
?>
 
Old 09-10-2010, 07:28 PM   #28
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
What does it do?
 
Old 09-11-2010, 11:42 AM   #29
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,010

Rep: Reputation: 352Reputation: 352Reputation: 352Reputation: 352
its extracts php code from a file ($_GET['file']) into memory from a tarball (on the fly) and executes it with eval()
(requires PEAR and the PEAR archive module)
would be invoked by
Code:
http://address/{whatevernameyougivethescript}.php?file={fileinarchive} (.php extension assumed)
 
Old 11-03-2012, 09:25 AM   #30
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-19-live-usb/ aix
Posts: 3,553

Rep: Reputation: 514Reputation: 514Reputation: 514Reputation: 514Reputation: 514Reputation: 514
xbmc addons

is this thread still alive ?; here is a link to a few of my xbmc addons written in python:

http://hyper.homeftp.net/xbmc/
 
  


Reply

Tags
code, source


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] make use of gcc source code to parse c++ source file famsinyi Programming 14 10-19-2009 06:47 PM
[SOLVED] why I couldn't find the source code of printf function in glibc source? famsinyi Programming 5 09-21-2009 09:06 AM
LXer: Voip Encryption, source code open source LXer Syndicated Linux News 0 07-09-2006 03:21 PM
How to convert Assembly code to "C" source code ssg14j Programming 2 08-01-2005 12:48 PM


All times are GMT -5. The time now is 08:45 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration